Backing Up and Restoring Subversion Repositories

Although you might be tempted to copy or move a live Subversion repository using traditional Unix tools like cp, you really should use the proper Subversion tools instead.

If you try to copy the repository (e.g. to do a backup), you might do so while someone else is performing a commit. If that happened, you would end up with a corrupted repository.

The correct way to copy or backup a Subversion repository is to use the svnadmin dump and svnadmin load commands.

Manual Backups

Performing a manual backup is pretty simple:

$ svnadmin dump -q /path/to/repo/dir | bzip2 -c > ~/repo.bz2

This will dump all revisions from the repository and place them into a compressed file.

If your Subversion repository is on server that you have SSH access to, this might be a faster way to get a local copy of the repository dump:

$ ssh example.com 'svnadmin dump -q /path/to/repo/dir | bzip2 -c' > repo.bz2

Manual Restore

Once you have a repository dump, it's easy to load into a new and empty repository. You'll have to create and initialize a new Subversion repository, and then you can load your dump file into it.

$ mkdir repo
$ svnadmin create repo
$ bzcat repo.bz2 | svnadmin load -q repo

Automatic Backups

Of course, the best way to do backups is via an automated script that can be run by the cron daemon. I just so happen to have such a script.

More information about my backup Subversion script can be found here.


Tags: svn backup