J. David Carr
  • Home
  • Contact
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu
Backup button on computer keyboard

cPanel Backup

April 17, 2017/in Technology/by David Carr

One of the services I perform for web clients is periodically making backups of their web site. This includes backing up the web directory (all of their HTML files including WordPress and any accompanying theme files and plugins) and taking a backup of their site’s MySQL database.

Rather than doing this manually, it’s easier (and more reliable) to just schedule the process in cPanel.  Unfortunately, if you’re using a reseller account like mine or if you have multiple sites, you can’t select a single web site in the native cPanel Backups module.  You’ll have to write a bash script and run it at your desired frequency through Cron.

It’s helps if you have shell access (I use PuTTY for this) and can do these from the command line in linux but you can also do it through an FTP client.  I give WordPress its own directory, so I create the bash script and a folder called ‘backup’ in the client’s web site directory and then write a ‘tar’ command in the bash script to compress the entire WordPress directory into the ‘backup’ directory I created.  There are step-by-step instructions here that walk you through the process, including steps to automate the script to run in Cron.

Then create a bash script to backup the MySQL database and put it in the same folder.  You can use the standard command line for this:

0 0 * * * /usr/bin/mysqldump --opt dbname -u dbuser -ppass > /home/username/dbname.sql

Or you can get all fancy and use a custom script to it:

<?php
$datestamp = date("Y-m-d");     // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR SETUP */
$dbuser = "database_username";     // Database username
$dbpwd = "database_password";     // Database password
$dbname = "database_name";     // Database name. Use --all-databases if you have more than one
$filename = "backup-$datestamp.sql.gz";     // The name (and optionally path) of the dump file

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);
?>

Either way, this will get you a full site back up and a copy of its database on whatever frequency you specify in Cron.  I typically take weekly SQL backups and monthly site backups for my own site; these frequencies vary for clients based on what the client wants.

Source for cPanel site backups: http://coolestguidesontheplanet.com/backup-website-cpanel-manual-automatically-script-cron/

Source for MySQL database backup command line: https://forums.cpanel.net/threads/using-mysqldump-in-a-cronjob-options-in-the-user-panel.197472/

Source for PHP script to backup SQL database: http://forums.hostgator.com/backup-database-cron-job-t62654.html

https://jdavidcarr.com/wp/wp-content/uploads/2017-04-17-backup-keys.jpg 600 1200 David Carr https://jdavidcarr.com/wp/wp-content/uploads/jdavidcarr-logo-2-300x126.png David Carr2017-04-17 20:33:002023-01-08 12:08:07cPanel Backup

Categories

  • Agile/Scrum (1)
  • Evernote (1)
  • GTD (1)
  • Mac/Apple (1)
  • Personal (3)
  • Productivity (1)
  • Software (1)
  • Technology (2)
  • Uncategorized (2)

Archives

  • February 2025 (1)
  • August 2022 (1)
  • April 2022 (1)
  • March 2022 (1)
  • February 2022 (1)
  • January 2022 (1)
  • February 2021 (1)
  • October 2020 (1)
  • April 2017 (1)

Tags

Featured (3)

RSS feed: DavidCarr.us DavidCarr.us

  • Exploring Backpacking March 25, 2025
  • New Scrum Master Role February 8, 2025
  • New Motorcycle (Again) December 7, 2024
  • Web Design Project September 24, 2024
  • New Motorcycle November 26, 2023

RSS feed: JustDoTheRun.com JustDoTheRun.com

© 2013-2026 J. David Carr. All rights reserved.
  • Link to X
  • Link to Facebook
  • Link to LinkedIn
  • Link to Rss this site
  • Link to Mail
Link to: Life Reorganized Link to: Life Reorganized David and kids, hiking at Moses Cone Park, October 2020Life Reorganized
Scroll to top Scroll to top Scroll to top