WordPress Backup on CentOS

Now that I’ve moved my website over to CentOS, I’ll be showing you how I do backups.

My Setup

  • VirtualBox – CentOS web server running WordPress
  • iMac – connected Drobo

Goal

  • Run daily mysql backups
  • Run weekly website backups
  • Only keep the latest two backups
  • Run daily rsync’s to my Drobo from my iMac

CentOS — Create a backup directory within your home folder

mkdir -p /home/tommy/backup/itsmetommy.com

CentOS — Backup MySQL Database

How to run it manually.

/usr/bin/mysqldump -u tommy -psupersecretpassword itsmetommy > /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+%Y%m%d-%H%M%S').sql

CentOS — Backup Website Directory

How to run it manually.

/bin/tar -czvf /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+%Y%m%d-%H%M%S').tar.gz -C /var/www/ itsmetommy.com

You can see that both files are located in /home/tommy/backup/itsmetommy.

ls -l /home/tommy/backup/itsmetommy.com
total 6731528
-rw-r--r-- 1 root root 9927065 Dec 25 21:55 itsmetommy_20151225-215541.sql
-rw-r--r-- 1 root root 6883148434 Dec 25 22:04 itsmetommy_20151225-215602.tar.gz

CentOS — Setup cron

No one wants to run backups manually, so lets add these commands as a cronjob.

crontab -e
@daily /usr/bin/mysqldump -u tommy -psupersecretpassword itsmetommy > /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+\%Y\%m\%d-\%H\%M\%S').sql
@weekly /bin/tar -czvf /home/tommy/backup/itsmetommy.com/itsmetommy_$(date '+\%Y\%m\%d-\%H\%M\%S').tar.gz -C /var/www/ itsmetommy.com

If you’re having problems, check your cron logs.

tail -f /var/log/cron

MAC — Setup rsync on my MAC

I first created a backup directory on my Drobo.

mkdir -p /Volumes/Drobo5D02/Backup/itsmetommy.com

How to run it manually.

rsync -hva -e ssh --delete --progress tommy@itsmetommy.com:/home/tommy/backup/itsmetommy.com/ /Volumes/Drobo5D02/Backup/itsmetommy.com

MAC — Setup cron

Note: My iMac is always on, so this isn’t a problem for me. Obviously you may need to revisit the timing or method if your computer is not always on and connected to the internet.

Run everyday at 1am.

crontab -e
0 1 * * * /usr/bin/rsync -hva -e ssh --delete  tommy@itsmetommy.com:/home/tommy/backup/itsmetommy.com/ /Volumes/Drobo5D02/Backup/itsmetommy.com > /dev/null

The above setup works, but I really only want to keep the last two backups of both the .sql and .tar.gz files, otherwise I’d run out of space.

CentOS — only keep the last two backups

How to run it manually.

cd /home/tommy/backup/itsmetommy.com; ls -t | grep sql | tail -n +3 | xargs rm; ls -t | grep tar | tail -n +3 | xargs rm

CentOS — Setup cron

crontab -e
@weekly cd /home/tommy/backup/itsmetommy.com && /bin/ls -t | /bin/grep sql | /usr/bin/tail -n +3 | /usr/bin/xargs /bin/rm
@weekly cd /home/tommy/backup/itsmetommy.com && /bin/ls -t | /bin/grep tar | /usr/bin/tail -n +3 | /usr/bin/xargs /bin/rm