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 wwwbackup.sh
#!/bin/bash # Backup WordPress # Author: Alfred Tong # websites="wordpress1 wordpress2 wordpress3 wordpress4 wordpress5" for A in $websites do tar czvf ~/backups/$A-`date "+%Y%m%d"`.tar.gz ~/www/$A >/dev/null 2>&1 echo "Backup of $A complete." done
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.
Then add it to the database and grant it all permissions.
Here’s the script I used to backup the databases.
#!/bin/bash # Backup WordPress Database # Author: Alfred Tong # databases="wordpressdb1 wordpressdb2 wordpressdb3" user="backup" password="backup_password" for A in $databases do /usr/bin/mysqldump -u $user -p$password $A | gzip -9 > ~/backups/$A-`date "+%Y%m%d"`.sql.gz EXITCODE=$? if [ $EXITCODE -ne 0 ] then echo "Backup failed with exit code $EXITCODE" else echo "Database $A backup complete" fi done
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.
Then I selected to execute the scripts I created weekly to perform a weekly backup.