How to backup WordPress and Mysql Database on Bluehost using Cron

Recently I migrated my blog over to Bluehost as Godaddy was going to rip me off for $250+ per year on renewal. The migration was simple as I have a Linux background. Their C-Panel was quite standard as well.

Anyways once I had my several wordpress sites migrated, I quickly gained SSH access to my site and started whipping up some scripts to perform wordpress backups.

Once logged into the home directory of my server I started creating two folders namely “backups” & “cron

The backups directory will store my backups and the cron folder will store the scripts that cron will run.

Backup WordPress files

I started with a script called

# Backup WordPress
# Author: Alfred Tong
websites="wordpress1 wordpress2 wordpress3 wordpress4 wordpress5"
for A in $websites
tar czvf ~/backups/$A-`date "+%Y%m%d"`.tar.gz ~/www/$A >/dev/null 2>&1
echo "Backup of $A complete."

This script is pretty simple. Once executed it takes the the list of wordpress directories that are located in www and archives them into a tar.gz with a date into the backup directory that was created earlier.

Backup WordPress Database

Next I created another script that backups the wordpress databases. Before doing that, I created a backup user using MySQL Databases tool under the Databases Tools section of CPanel.
Screen Shot 2014-05-06 at 4.59.12 PM
Then add it to the database and grant it all permissions.
Screen Shot 2014-05-06 at 5.05.02 PM
Here’s the script I used to backup the databases.

# Backup WordPress Database
# Author: Alfred Tong
databases="wordpressdb1 wordpressdb2 wordpressdb3"

for A in $databases
/usr/bin/mysqldump -u $user -p$password $A | gzip -9 > ~/backups/$A-`date "+%Y%m%d"`.sql.gz
if [ $EXITCODE -ne 0 ]
        echo "Backup failed with exit code $EXITCODE"
	echo "Database $A backup complete"

This script once executed will perform a dump of the database defined in the databases list and archive it into a gzip file with a date into the backup directory.

Setup a cron job in Cpanel

The next thing to do is now setup a scheduled cron job to execute these scripts. In the Cpanel of Bluehost under advanced there is a “Cron Job” tool.
Screen Shot 2014-05-06 at 5.33.57 PM
Then I selected to execute the scripts I created weekly to perform a weekly backup.
Screen Shot 2014-05-06 at 5.34.32 PM


Related Posts with Thumbnails

About the Author

Alfred Tong
Author and owner of this blog. A Networking enthusiast, full time networking and systems Engineer. Generally curious about all things IT. Certifications: GIAC GSEC, CCNP-S, CCNP, CCSP, CCDP, CCNA, RHCE, JNCIA - FWV