Two events spurred me to write this blog.
First, my 2 year old “Subversion + Dreamhost + Post-Commit” blog still gets quite a number of hits. Second, after the latest Dreamhost
outage move, I’m beginning to feel a little more vigilant about backing up my data.
There are a few prerequisites to being able to back up your SVN repository.
- First and foremost, you must have already installed a SVN repository into your Dreamhost account via the control panel.
- Second, you must know how to SSH into your Dreamhost account. As a FYI, you sorta-kinda-need to know what that means in order to follow this tutorial.
Grabbing the backup script
Wait, you didn’t think I was writing my own right? In any case, if you actually installed/compiled Subversion on your own, it would’ve contained this file, hotbackup.py. Fortunately for us, Dreamhost has this file conveniently available at: /usr/bin/svn-hot-backup, but it’s an older version of the backup script. There are some subtle differences like being unable to pass in the number of backups you want the script to manage. Personally, I like to be on the edge, so let’s get the latest version. Execute the following commands from your home directory.
$ cd ~ $ mkdir scripts $ cd scripts $ wget http://svn.collab.net/repos/svn/trunk/tools/backup/hot-backup.py.in $ mv hot-backup.py.in svn-hot-backup.py
The commands issued above created a directory called scripts in your home directory, switched into the directory, downloaded the latest hot-backup.py file from CollabNet, and renamed it to svn-hot-backup.py. Now that you have the file, you’ll need to make a few edits. Personally, I’m accustomed to vi, but pick your poison (pico, nano, text editor of your choice) and find these two values (they should be close to the top of the file in consecutive lines).
# Path to svnlook utility svnlook = r"@SVN_BINDIR@/svnlook" # Path to svnadmin utility svnadmin = r"@SVN_BINDIR@/svnadmin"
and change them to the following:
# Path to svnlook utility svnlook = r"/usr/bin/svnlook" # Path to svnadmin utility svnadmin = r"/usr/bin/svnadmin"
(If you’re wondering, if and when you compile/install Subversion yourself, these two variables would have been automagically filled in for you.)
The python script we downloaded not only performs a hotcopy of your svn directory, but also can archive it and manage a set number of copies. Pretty neat right?
Preparing for the backups
Before you can actually back up your SVN repository, you’ll want to create a directory structure to manage your backups. Execute the following commands from your home directory.
$ cd ~ $ mkdir backup $ cd backup $ mkdir svn $ cd ~/scripts
The commands issued above created a directory called backup in your home directory, switched into the directory, and created another directory called svn within the backup directory. We’ll be using this directory to store all your backups. Finally, we switched back into the scripts directory created in the previous steps. Now that we have the backup script and directory structure to manage the back ups, let’s test it out!
Before you can back up your repository, you’ll have to know the name of the Subversion repository you’re trying to back up. To find the name of your repository, you can either look in the svn directory in your home directory, or you can check out the ID value in your Subversion Goodies control panel. In any case, remember the name of your SVN repository and issue the following commands.
$ cd ~/scripts/ $ python2.4 svn-hot-backup.py --archive-type=zip --num-backups=10 ~/svn/REPOSITORY_NAME_HERE/ ~/backup/svn/
You should see the following in the console:
Beginning hot backup of '/home/USERNAME/svn/lkg/'. Youngest revision is REVISION_NUMBER Backing up repository to '/home/USERNAME/backup/svn/REPOSITORY_NAME_HERE-701'... Done. Archiving backup to '/home/USERNAME/backup/svn/REPOSITORY_NAME_HERE-701.zip'... Archive created, removing backup '/home/USERNAME/backup/svn/REPOSITORY_NAME_HERE-701'...
Voila! (But there’s more)
Automating the backups
Now that you actually have the script backing up your SVN repository, let’s automate them! To do so, we’ll use the handy cron daemon. Cron has similarities to the Windows task scheduler in that it provides a service that enables a user to execute commands at a specified date/time or set intervals. To tell cron the tasks you want to execute, you’ll need to load a configuration file called a crontab. You can read more about it here and here. In any case, here’s what my crontab configuration file looks like.
MAILTO=ryankanno@CHANGE_TO_YOUR_EMAIL.com # minute (0-59), # | hour (0-23), # | | day of the month (1-31), # | | | month of the year (1-12), # | | | | day of the week (0-6 with 0=Sunday). # | | | | | commands 0 0 * * * /usr/bin/python2.4 /home/USERNAME/scripts/svn-hot-backup.py --archive-type=zip --num-backups=10 /home/USERNAME/svn/REPOSITORY_NAME/ /home/USERNAME/backup/svn/
Create a file in your scripts directory called svn_backup_once_a_day.cron and copy the contents above into your file. I’ve setup my crontab to backup my svn repository once a day.
Once you have this file called svn_backup_once_a_day.cron in your scripts directory, load the file into your crontab by issuing the following command:
$ crontab svn_backup_once_a_day.cron
As a FYI, this will replace your old crontab. If you have other items already running on cron, it’s a good idea to list them via the crontab -l command first. If you want to make sure that your cron will run, you can test it out by setting the values in the crontab to the time you want it to run. I’ll leave this as an exercise to the reader.
Storing your backups
Though out of scope of this blog, you’ll still have to store your backups somewhere. Please just don’t leave them in your Dreamhost account. Your best bet is probably to get an Amazon S3 account and store your backups there. Personally, I like to run another script immediately after the hotcopy finishes that pushes the backup to my S3 account. Other options include scp/sftp’ing the backups to your home machine. Here’s a link to read more about that option.