Create 2 types of SVN backups quickly and easily
Like many of you, I run my own SVN repository. I have several projects and several people that use them. So I wanted a quick and easy way to perform backups. Here are the two methods I use.
The first is an incremental backup where I use a post-commit hook. The other is a bash script that I setup in cron to run once per week (you can do daily, monthly, or when ever. It depends on how quickly you want the full backup script to run). The bash script is designed to create a full backup using svnadmin dump.
The idea of a full backup is nothing new, but the way my script works is a little different than others. Their script just creates a new backup when ever run. Mine creates a full backup of every revision number. Even if there have been several updates since the last backup.
Lets get started.
Incremental backup using a post-commit
Post-commit hooks are very powerful. You can do a lot with them. In this case we are going to do an incremental backup. I don’t do a full because my repository is very large and I don’t want to be slowed down every time I commit new files.
First you need to go to the directory where your repository is. Lets call it /srv/svn/myproject. In that directory there is a folder called hooks. Looks for post-commit (it maybe listed as post-commit.tmpl, just rename it without the .tmpl). Open the file in an editor (vi, nano, emacks) and add this line to the end.
svnadmin dump "$REPOS" --revision "$REV" --incremental >/srv/backups/myproject/incremental/commit-$REV 2>> /srv/backups/myproject/incremental/backup.log |
NOTE: If mailer.py is not commented out, put a hash in front of it “#”. You don’t need it.
Take note of the directories I’m using. Just change them to where your backups will go. Also, you need to give those directories the same ownership as your svn archive! Otherwise when the post-commit runs, it will error out because it cannot write to the new location.
And that is it! Now every commit you make will create a new incremental backup.
There is one down side to this method. If you already have several commits, you will not get your entire history. I did this a lazy way since at the time I was only up to around 20 commits.
./post-commit /srv/svn/myproject 0 |
This will create the backup starting at revision 0. Just keep running it and moving the number up. If you have hundreds or even thousands of commits… you might want to write something to do all that hard work for you. You might even get some ideas from the next section.
Full backup
This is where I’m different from everyone else… at least that I could find. While this is not the most elegant way of doing things, it really doesn’t take very long to run (unless there are many revisions to create new archives for.
#!/bin/bash svnLocale=/srv/svn/myproject/ backupLocale=/srv/backups/svn/myproject/full/ fileName=myproject.rev. extension=.svndump.bz2 latestRevision=`svnlook youngest $svnLocale` let stopCounting=latestRevision+1 COUNTER=0 while [ $COUNTER != $stopCounting ]; do rev=$COUNTER # check to see if file exists if [ ! -e $backupLocale$fileName$rev$extension ]; then svnadmin dump $svnLocale -r $rev -q > $backupLocale$fileName$rev.svndump bzip2 -z9q $backupLocale$fileName$rev.svndump fi let COUNTER=COUNTER+1 done |
Seems a bit much, but all you would need to worry about is changing svnLocale, backupLocale, and fileName. You can change extension if you wish, but I would leave it unless you plan on changing the svnadmin or bzip2.
Please note that the first time you run this, nothing is dumped on the screen. I ran this on an archive with 1215 revisions as a test. I went to lunch. You can modify the script and remote the -q from both svnadmin and bzip2 to see your progress. I silence them while running as a cron job.
The benefit to this script is it does check to see if an archive has been created for each revision. If not, it gets created. This script will even get backups you may have deleted and recreate them, then continue on. It doesn’t matter. It’s so simple that it just works.
So that’s pretty much it. Questions? Comments? Let me know what you think.