Backup your Wordpress installation with rsync
for wordpress by gillesklein
What this article is about
- Daily Incremental backups of your Wordpress blog files
- Daily report by e-mail on the status of the backup
What it does not cover
- This script does not make a backup of your Wordpress MYSQL server. In the near future I will write another script that does this automatically every night as well.
This blog is slowly starting to feel a little like home and after much polishing the number of hours invested in it is increasing. I have uploaded pictures, plug ins and upgraded my installation.
This is always where my paranoia sets making me wonder ‘what if’.. what if is of course any kind of disaster that would wipe out my work. Mad hackers, my hosting provider going belly up, or some country taking exception to my writing or most likely: a hard disk crash at my provider.
So today is backup day. And as every day should be backup day I am going to automate the whole thing.
I have an Ubuntu Linux workstation at home to which I would like to sync a copy of my Wordpress blog and Rsync is the tool I am going to use. It is a great piece of backup software designed to transfer only the changes made, not your whole site. So if you modified only one file, it will only resend that one file. And if you only changed a small thing in that one file, it will only send the actual changes.
For this to work you will need to have the following:
- SSH access to your hosting provider and of course they should run some kind of Unix/Linux
- A Linux computer to backup to. MacOX should work as well, but lacking a Mac I cannot vouch for this.
- rsync , under Ubuntu
apt-get install rsyncshould do the trick. - mailutils, under Ubuntu
apt-get install mailutils, optional but its nice to receive an e-mailed report of your backup. (Probably not required if you have configured sendmail/postfix etc)
Part 1 - Setting up shared keys
I am going to take a little detour here. Because I would like rsync to run at night it is best to make sure that it can login automatically at your hosting provider. The most secure way of doing this is by setting up shared encrypted keys on your computer and you hosting provider.
If you skip this part you can still run the script below — you just have to enter your password every time it runs.
If you have previously created an SSH keyset, you can skip Step #1. If you are not sure check if the following file exists under your home directory (~/.ssh/id_dsa).
Step #1
Open a terminal and enter the following: ssh-keygen -t dsa
Just press enter several times there is no need for a pass phrase.
martin@edinburgh:~$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/nis/martin/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nis/martin/.ssh/id_dsa.
Your public key has been saved in /home/nis/martin/.ssh/id_dsa.pub.
The key fingerprint is:
4e:9f:46:f9:d5:dc:53:40:c8:6a:25:3e:70:27:3e:03 martin@edinburgh
Step #2 - Copy the public key to your web host
Here it will ask you for your username & password.
scp ~/.ssh/id_dsa.pub youraccount@yourdomain:./id_dsa.pub
Step #3 - Login to the host server using SSH
Here it will ask you for your username & password.
ssh -v youraccount@yourdomain
Step #4 - Add the new public key to the keyring
mkdir .ssh
chmod 0700 .ssh
cd .ssh
touch authorized_keys2
chmod 600 authorized_keys2
cat ../id_dsa.pub >> authorized_keys2
rm ../id_dsa.pub
Step #5 - Log out & try out
If everything went smoothly you can now login to your hosting account without having to provide a password. Your computer and your hosting provider can now do this by exchanging cryptographic keys instead.
exit
ssh -v youraccount@yourdomain
Part 2 - Setting up the backup script
In this example I going to make a backup directory under my users home directory on my local Ubuntu machine. Of course this backup directory can reside anywhere else on your computer.
Step #1 - The script
mkdir ~/backup
mkdir ~/backup/wordpress
chmod 0700 ~/backup
chmod 0700 ~/backup/wordpress
Now use your favorite editor to create & modify the following script:
gedit ~/backup/run_wordpress_backup.sh
#!/bin/bash
# Your login details for your hosting account
USERNAME="yourusername"
DOMAIN="yourdomain.com"
# Your e-mail address for reporting errors
SYSOP=youremail@gmail.com
# The directory on your hosting account that contains wordpress, note the ""
REMOTEHOME="~/public_html/html"
# Directory on you local machine that stores the backup
# Note: no "" around the following
BACKUPTO=~/backup/wordpress
MailSuccess()
{
MSG="`hostname` reports wordpress backup complete!"
SUBJ="Backup Complete"
echo $MSG | mail $SYSOP -s"$SUBJ"
}
MailError()
{
MSG="`hostname` reports wordpress backup failed!"
SUBJ="Backup Failed"
echo $MSG | mail $SYSOP -s"$SUBJ"
}
# The actual sync command
rsync -e 'ssh -p 22' -v -r $USERNAME@$DOMAIN:$REMOTEHOME/* $BACKUPTO
if [ $? -gt 0 ]; then
MailError
exit 1
fi
MailSuccess
exit 0
Step #2 - Make the script executable
chmod +x ~/backup/run_wordpress_backup.sh
Step #3 - Test the script
~/backup/run_wordpress_backup.sh
On success the script will send you a brief e-mail.
If things don’t work out here you can add “-v -v” to the rsync command line to obtain more debugging information.
Part 3 - Finishing it up - and running this at night
If your home PC doesn’t sleep at night (and mine is always busy) you can set it up to run the backup automatically every night. For this you need to add a little crontab entry to your Ubuntu installation.
crontab -e
This will open an editor, in which you can add the following line:
01 04 * * * /home/yourhomedirectoy/backup/run_wordpress_backup.sh
Note that I didn’t use the “~/run_wordpress_backup.sh” notation here — its better to be specific when setting up a crontab entry.
Every night at 4:01am your computer will run the backup script and fresh in your Inbox every morning should be an e-mail telling you that things went swimmingly. If its missing your backup scheme just fell apart.
And of course don’t forget to regularly backup the backup directory to a saver medium!

One Response to “Backup your Wordpress installation with rsync”
Trackbacks/Pingbacks
Leave a Reply