Automatically updating FTP sites from Subversion with svn2web

Suppose you want to be able to commit to your subversion repository and automatically FTP (or SFTP) your changes to a remote site. "svn2web" is just the tool for the job!

"svn2web" allows you to configure a particular path in your subversion repository with one or more FTP sites to update. Every time you commit changes to that path, or any path under there, your changes are automatically transferred to your FTP location. This is particularly handy for updating websites.

Setting up

Log in as root on your subversion server, and run the following commands to get svn2web:

root@host # cd /usr/share
root@host # svn co https://svn2web.svn.sourceforge.net/svnroot/svn2web/trunk svn2web
You will also need the following installed:

Now create a file called "pre-commit" in your subversion repository hooks directory. The file should look like this:


#!/bin/sh
# PRE-COMMIT HOOK
REPOS="$1"
TXN="$2"
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/share/svn2web
svn2web $REPOS $TXN 2>&1 >> /var/log/svn2web.log || exit 1
exit 0

The hook script should be executable by the user running the subversion server. In this example we’re going to make it executable by the whole of the www-data group:

root@host # chgrp www-data pre-commit
root@host # chmod g+x pre-commit

We’re writing to a log file called "/var/log/svn2web.log" so we need to make this file writable by the group running the subversion server. In this case it is "www-data":

root@host # touch /var/log/svn2web.log
root@host # chgrp www-data /var/log/svn2web.log
root@host # chmod g+rw /var/log/svn2web.log

That’s all there is to installing svn2web. The next stage is to configure your subversion repository with the properties that svn2web needs

Configuring

Different paths in your subversion repository can be configured with different FTP locations. Let's say for example that we have checked out our website from subversion to /home/dave/mysite and we want to automatically update our web server host "webhost". We need to set up a subversion property to tell svn2web where we want our site to be transferred to:

dave@host $ cd /home/dave/mysite
dave@host $ svn propset svn2web "ftp:username:password@webhost:/path/to/site/on/webhost/" .
dave@host $ svn commit -m "Upload commits to server using svn2web"

Before we commit anything, we should make sure "webhost" is already up to date as svn2web only copies out changes. Once this is done, any commits to the subversion repository will cause svn2web to make those same changes on "webhost".

The subversion property that we set will apply to any child of the path we set the property in. Any other location within our subversion repository will be unaffected. This means that we could configure different paths in subversion for different FTP (or SFTP) sites. It’s a really magical tool!

Other options are available, including local file copies and using SFTP. The svn2web project README file gives a complete description of these.