The server that this site runs on is a Bytemark Hosting virtual machine. Those of you who have been reading for a while will remember that we had a serious fuckup a couple of months ago and the site went away. And we didn’t have any backups. Fortunately, we were able to retrieve an old version of the sites hosted here, and reconstructed the data. But that’s not happening again, so I’ve set up some backups. Bytemark give you some free backup space to back up your VM. The following steps should give you full nightly backups of everything you want to back up.
First, mount your backup space over NFS. You need to get access to the backup space as a filesystem, although we’re not using NFS for the backups; we’ll be using rsync. Mount the backup space at /backup. All this stuff needs to be done as root.
mkdir /backup/.config mkdir /backup/rsync mkdir /backup/rsync/files mkdir /backup/rsync/db nano /backup/.config/rsync
Put the following line in that file:
This sets up rsync. VMNAME must be the name of your Bytemark VM (NB: not the domain name you’ve got pointed to it; it’ll also be called something like foobar.vm.bytemark.co.uk, and foobar is your VM’s name.) PASSWORD is your choice of rsync password. Pick something random. Wait for ages. Rsync stuff only gets activated a few times a day, so go away and come back the following day or eight hours later or something.
mkdir /usr/local/backups echo "RSYNC_PASSWORD_FROM_ABOVE" > /usr/local/backups/backup-password chmod og-rwx /usr/local/backups/backup-password
Now we’ll test that rsync is working.
rsync -a --password-file=/usr/local/backups/backup-password /etc/motd [email protected]::VMNAME-backup/files/test/
If that worked, there should now be a /backup/rsync/files/test directory with an “motd” file inside. That means that rsync is working! If it didn’t work then, er, rtfm, etc, etc.
Now to create two backup scripts, one to do your MySQL databases and one to do your files. If you don’t use MySQL, skip the DB one (or replace it with one that does your own choice of DB.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Run that file to test it. It should create a directory in /backup/rsync/db named after the date and time, and put a DB backup in it of all your databases. /usr/local/backup/backup-files.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
You’ll also need to create /usr/local/backups/files-to-include. The syntax of this file is a great big pain in the arse. Mine is below:
# List of directories to back up # If you want to back up directory /a/b, then you need the following lines: # /a (since otherwise we never get as far as b, because a is excluded) # /a/b (the directory we want to back up) # - /a/*/ (don't back up any other directories in /a) # - /a/* (don't back up any files in /a either) # Yes, this is completely shit. Rsync's fault. /var/ /var/www/ - /var/*/ - /var/* /home/ /etc/ /usr/ /usr/local/ - /usr/*/ - /usr/* /usr/local/backups/ - /usr/local/*/ - /usr/local/* # Directories to explicitly exclude. Note that you don't need all # that parent directory shit here; just put in dirs that you don't # want to back up for whatever reason. - /var/www/jonobacon.org/mysqlbackups/ - /var/www/jonobacon.org/mysqlbackups.old/ # Don't fuck about with the stuff here; it ensures that logs directories # aren't backed up, and neither is anything else in / (otherwise we'll # back up the whole machine or something equally dreadful). - **/*.mp3 - **/logs/ - /*/ - /*
Pay attention to the comments. I mean it.
This is actually a super clever backup method. All the magic is done by rsync’s—link-dest option. What that does is, every time you tell it to back up, it creates a tree of hardlinks in the backup space from the last backup. Then it rsyncs any changed files over the top. This means that every single backup is a full backup, but it only takes up the space of an incremental backup. This only works because hardlinks exist. I love Linux, I really do.
Anyway, those two files should do the job of backing up everything. Stick two lines in your crontab to kick them off.
# ########### Backups ############### # mysql 30 5 * * * root /usr/local/backups/backup-databases.sh # files (4.40am) 45 5 * * * root /usr/local/backups/backup-files.sh
and it should all work.
Note that these instructions probably won’t work for you right off, but they will at least give you a starting point.