Jonas Skovmand,

Web developer from Sweden, Denmark and Norway.

Synchronize your WP installation with SVN

A very awesome and time-saving feature I stumbled across a couple of months ago was Subversions pre-commit hook and how it allows you to do whatever you want with the submitted changes when committing – including uploading changes through FTP to another server.

With a little help from the open source (PHP) utility svn2web, synchronizing your subversion with your website is a snap (after first setup). This is especially useful if you are on a shared host and don’t have access to svn.

By following this guide, you enable yourself to always have a working copy of your website, complete installation Wordpress, just your WP themes or plugins, or whatever you can think of — near at hand, always up-to-date — all you need is an SVN client and an Internet connection client side and a Subversion provider or your own server to sync to server side.

I use this method on most my projects (private and freelance), e.g. I’m using it to synchronize my theme right here on satf. This is awesome because I tend to break stuff and it’s comforting to know that salvation is only one revert away.

Here’s how to do it

1. Installing and configuring Subversion

There exists plenty of guides on how to install Subversion for just about any platform, and to guide you through it is beyond the scope of this tutorial. Go to the Subversion website and download a binary or compile it from source if you’re tech savvy and install it on your server. They have guides on how to configure there, too, but a google search for “install svn <OS>” on Google will surely bring up many useful results.

You will need to install an SVN client on your work station as well. While there exist good solid SVN GUIs I will in this guide only use SVN through the command line, and therefore I recommend downloading the CLI binaries from CollabNet: WindowsOS X.

Note: this guide is directed towards Subversion on a Linux/UNIX server, it probably won’t work on a Windows server. It doesn’t matter what work station OS you use, just that you have a fairly recent CLI Subversion client installed on it!

2. Installing svn2web

This is where we get a bit down and dirty with the technical stuff. I assume you by now have installed Subversion on your server and configured it correctly for remote access. And by that I also assume you have a shell active on your server, ready to go.

I’m borrowing most of this part from the svn2web install instructions, but they can be a bit confusing, so a break-down is probably best at hand.

As root, type the following in your terminal:

# cd /usr/share
# svn co https://svn2web.svn.sourceforge.net/svnroot/svn2web/trunk svn2web

This will download the svn2web files needed to synchronize with your web server to the directory /usr/share/svn2web. You can of course choose whatever path you want for the svn2web files, just make sure you reference it correctly later in the pre-commit file.

The svn2web files need to have excecutable permissions, give them that by issuing the following:

# cd svn2web
# chmod +x put_ftp
# chmod +x put_sftp
# chmod +x remove_ftp
# chmod +x remove_sftp
# chmod +x svn2web

3. Setting up your repository and pre-commit hook

Now is the time to set up your repository. CD to your SVN repo root (like cd /var/svn-repos) and, as root, issue the command:

# svnadmin create <repo>

This creates all the necessary files and directories for your repository to function.

Next up is configuring your repository and setting up your user.

# cd <repo>
# nano -w conf/svnserve.conf

(substitute nano with whatever file editor you want to edit the file)

You’ll find 4 settings to change in svnserve.conf, and they’re all pretty straight forward.

I use something along the following settings:

[general]
anon-access = none
auth-access = write
password-db = passwd
realm = My Wordpress Theme

After saving the file you have to edit the passwd file to set up your username and password.

nano -w conf/passwd

(again, use whatever file editor you want)

Below [users], just add a username = password string like:

jonas = mittenssmellgood

Save. Now you can access the repository, but there’s still one thing remaining server-side: setting up the pre-commit hook.

# cd hooks
# touch pre-commit
# chmod +x pre-commit

And then use your favourite editor to edit the pre-commit file. The file should contain:

#!/bin/bash
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/share/svn2web
svn2web $1 $2 >> /tmp/svn2web.log || exit 1
exit 0

Note: You will have to change the PATH variable if you installed svn2web at another location than described in this guide!

We’re now done messing with the server — just the client side left!

4. The propset way of committing

Switch to your work station.

Create a new directory (empty) somewhere where you want your files to be, and check out your new, empty, repository (logged in as root not necessary):

svn co svn://<user>@<svn-server>/<repo> <local directory>

Note: Depending on if you have started your svnserve with the -r option or not, you will either have to supply full path to your repository (/var/svn-repos/<repo>) or only the repo name (/<repo>).

If you want to have the “standard” repository structure (with tag, trunk and branches folders), now is the time to create those in the folder you just checked out. Copy your web files (if any) to the trunk or root of the repo depending on what you chose. It’s important that the web files are more or less the same on the web server since only changes will be uploaded to the web server.

Now, make an initial commit (issue this from the root of your repo folder):

svn import .

This will commit all updated files and directories. Issue svn update so that the new files can be versioned and after that it’s time to set up the propset action that gives SVN extra essential data to use when it hits the pre-commit hook. It’s time for the FTP credentials.

CD to your trunk (if you created one, otherwise just cd to the root of the repository dir) and issue the following command:

svn propset svn2web "ftp:<username>:<password>@<web host>:/<web server path>" .

Confused? To clarify, here’s an example (using sftp)

svn propset svn2web "sftp:jonas:mittensareawesome@satf.se:/home/www/wp-path/themes/mytheme" .

Remember to include the dot (.) in the end.

If your SVN server is the same as your web server you can use file:/<web path> instead.

All but one thing is done now: committing.

svn commit -m "Setting propset!"

…and Bob’s your uncle! After this just go ahead and commit like you never committed before. Each time you commit, it will upload new or changed files and delete deleted files (deleted through the svn delete command) on your web server automatically. Note that it is only a one-way synchronization, it does not synchronize if you change files on your web server only.

From now on, for each new computer you want to code on, all you have to do is download the Subversion CLI client (or GUI if you prefer), check out the code from the Subversion repo, work on it, and then commit when you are done:

# check out
svn co svn://<user>@<svn server>/<repo>
# edit stuff here
nano -w file
# and commit to server
svn commit -m "Changed X and Y"

When committing, you will notice a delay depending on how many or big files you committed, this is when the changed files are being uploaded and svn2web is at work. When it’s finished uploading, you’ll “regain control”.

For new projects (= new repositories) you only need to do steps 3 and 4, granted that you are using the same server with svn2web installed.

Learn more about Subversion

Version Control with Subversion – The Holy Grail

How to revert (roll back) to a previous revision with Subversion

Commonly used SVN commands

Questions? Love? Hate? Go ahead and comment below!

(6) (1)

Leave a Reply