Backup Your Web Site Automatically in cPanel

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:

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

$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:

Source for MySQL database backup command line:

Source for PHP script to backup SQL database: