Using svnadmin and rsync to Backup an SVN Repository to a Remote Server

In order to ensure your SVN repository is fully backed up you should schedule regular backups and store them on a remote machine. This document shows how to use svnadmin to backup your repository, rsync to copy it to a remote server, and cron to automate the process (so you don't have to remember to run the backup yourself).

Backing up your SVN Repository

First, use the svnadmin dump function to dump a copy of your repository to the local machine using a command like this:

svnadmin dump /path/to/repository/location >/path/to/backup/file

e.g.,

svnadmin dump ~/webapps/svn/ >~/svn.backup.bak

This would create a new file in your home directory called svn.backup.bak containing all the information needed to recreate the SVN repository stored in ~/webapps/svn/

Automating the Backing Up with cron

In order to automate this process you should use a scheduler like cron, which this tutorial does.

Before you Begin

First you will need to ensure that your machine is running cron, rsync, and ssh. You will also need to know the following:

  1. the location of your repository (this example uses ~/webapps/svn/)
  2. the path of the directory used to store the SVN backup (this example will use a called directory in your home directory called automated_backups to store this file, i.e., ~/automated_backups)
  3. the name or address of the remote server. This server must be network accessible and must run sshd (this example will use a machine called www.example.com)
  4. the path of the directory where backups will be stored on the server (in this example this path is /backups). This directory must be writable by the rsync process

Writing the Backup Script

You will need to create a new script containing the commands needed to make your backup. Create a new file in the backup directory called svn.backup.sh. Make sure it is executable:

cd ~/automated_backups
touch svn.backup.sh
chmod +x svn.backup.sh

Now edit the new script and enter the following:

LOGFILE=’rsync.backups.log’
SVN_BACKUP_FILENAME=~/automated_backups/svn.backup.`date +%Y.%m.%d`.bak
echo $’\n\n’ >> $LOGFILE
svnadmin dump ~/webapps/svn/ > $SVN_BACKUP_FILENAME
/usr/bin/rsync --delete --rsync-path='nice -n 19 rsync' -avz -e ssh $SVN_BACKUP_FILENAME example.com:/backups 2>&1 >> $LOGFILE

Now you should be able to execute this script which will dump a copy of your SVN repository and rsync it to your remote server. Unfortunately it will ask for a username/password every time this is run which is not acceptable for an automated script. To overcome this you will need to set up a new ssh key to allow you to log into the server without a password.

Running the backup script with cron

Edit your crontab now with the following command:

crontab -e

And add the following line:

49 1 * * * cd ~/automated_backups;./svn.backup.sh

This will execute the backup script automatically at 1:49am every day.

public/backupsvn.txt · Last modified: 2009/01/03 08:30 by admin
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki