Using SVN To Create A Mirror

Subversion (SVN) is a very robust version control system offered by Tigris.org. Subversion offers hooks that can be triggered at various points of the commit process. These hooks can be used to trigger external scripts to perform various tasks. Examples include sending an email about commits and using SVN Notify Mirror to mirror changes to a remote server or local directory.

SVN::Notify::Mirror is a CPAN package created by John Peacock. It uses SVN::Notify as a base and extends this perl Module by adding the ability to push changes to a local or remote mirror. We’ll use this script and a post-commit hook to automatically sync a development server upon commit.


To use SVN::Notify::Mirror, you first need to install the perl module. If you don’t have SVN::Notify installed, CPAN will install this as a prerequisite. This will install the needed perl modules as well as a script called svnnotify.

In the ‘hooks’ directory of each repository created in SVN, there are templates for each type of hook. To create a post-commit hook, copy the post-commit.tmpl file to post-commit, removing the .tmpl extension and make this script executable. You can do this by using the command chmod 775 post-commit.

If you’re using web_dav the user running apache needs to own the script, otherwise the user running svnserve needs to own it. Check your local configurations to see which is needed.

Check which svnnotify to see where svnnotify was installed. In this example, we’ll assume /usr/local/bin/svnnotify.

Local Mirroring

To mirror locally, check out the project locally, configure the document root of apache (if needed) and add the following to your post-commit script in the SVN repository hooks directory.

/usr/local/bin/svnnotify --repos-path "$REPOS" --revision "$REV" --to "/path/to/trunk" --handler Mirror

Remote Mirroring

Mirroring remotely requires a bit more setup. I recently used the SSH method to sync to a remote development server. Here are the steps I followed:

1. Create a user on the subversion server. The user needs to have access permissions to the SVN repository. For this example, create a user ’svnuser’ and give this user access to the repository and add this user to the svn group.
2. Create the same user on the development server.
3. Setup Passwordless SSH between the two servers. If you need help on this, review a previous post on Passwordless SSH using an empty password.
4. Create a post-commit hook and use the following within it.

/usr/local/bin/svnnotify --repos-path "$REPOS" --revision "$REV" --to "/path/to/remote/htdocs" --handler Mirror::SSH --ssh-host dev.yoursite.com --ssh-user svnuser --ssh-identity /home/svnuser/.ssh/id_rsa

5. Test the passwordless ssh between the servers. Make sure this works before proceeding.
6. On the development server, check out the project from SVN as the user ’svnuser’. This ensures permissions are correct for updating the mirror copy.
7. On the svn server, test the post commit hook by becoming the user ’svnuser’ and using the following command from the svn repository hook directory. The path to the repository is the local file system path to the repository and does not include /branch; Rev is the current revision number. This will populate $REPOS and $REV in the post-commit hook script.
env -i post-commit /path/to/repository Rev
8. Add a test file to svn and commit it. Then check your dev site for the new file. If it’s there, you’re all set to go. Otherwise, you’ll need to do some debugging.

Debugging

The easiest way I have found to debug is to become the user executing apache or svnserve and try executing the svnnotify, then the post-commit hook. If permission errors occur, you may need to check group permissions or the user that checked out the project. If you can’t become the apache or svnserve user, this user may not have a login shell in /etc/passwd. You can temporarily enable this to debug.

Also, the prerequisites mentioned by John Peacock were especially helpful.

2 Comment(s)

  1. Hey! Good article :)

    The Perl group has certainly been low-key for the last few months, how are things in Diona-land?

    Mike | Dec 4, 2007 | Reply

  2. Hey Mike! Glad you liked it. This info was really useful for a project I’m working on. Automation is a beautiful thing. ;)

    I’ll email you privately to catch up.

    Diona Kidd | Dec 7, 2007 | Reply

Post a Comment