Want to SVN, but can’t? Rsync to the Rescue!

Today I had a little problem deploying a new update to a site for one of my clients.

Normally the way I deploy is by writing a build script that updates the code from an SVN repository. This is easy peasy and means that once setup, the site doesn’t have to be touched apart from running the build script when a new update has been released.

However, this client has just moved their site to a new server, and for reasons that are beyond me, it doesn’t have Subversion installed, nor is it going to. So I have a problem. My method of deployment is SVN, but I can’t deploy using SVN. Also, the location the website is to be deployed to contains lots of cached images and web pages that I don’t want to wipe.

So what do I do?

Fortunately Rsync is here to save the day!

The solution I settled for was a 2 step process.

1. Checkout a brand new copy of the latest from the SVN repository onto my workstation.

2. Copy the code from this checkout up to the server using rsync. I need to make sure I don’t delete any files on the server that don’t exist on the workstation, and also make sure I don’t overwrite any newer files on the server than in the repository.

So this is how I did it.

Step One – SVN Checkout.

To create a clean checkout I did the following

1. Create a new directory

$ mkdir LIVE

2. Checkout the repository

$ svn co http://svnserver/reportistory/trunk/ ./LIVE --username rob

It should then pump out a load of info.

Now I could go into the newly checked out repository and update database settings etc to reflect the live server, plus chmod any directories I needed to (e.g. for writing cache files to etc)

The directory is now all ready to copy to the server

Step Two – Rsync to the Server

To rsync to the server I was connecting via SSH, and the command is as follows:

$ rsync -avzue ssh ./LIVE/ user@liveserver:/path/to/live/httpdocs/ --progress

If we take a little time to analyse the above command because there is quite a lot going on there.
If we look at the switches first:

The -a switch ensures the files are transfered in “archive” mode, which essentially keeps the permissions etc.

The -v switch is for verbosity

-z compresses the transfers, reducing bandwidth

-u is very important, and it skips files that are newer on the live server. I needed this as the client can upload image and the system create cached files etc that should not be overwritten

And finally the -e switch allows you to specify a remote shell, effectively allows us to use SSH

After these switches we specify that we will be using SSH to conduct the transfer. If you don’t have the -e switch and the SSH command, you can use Rsync to copy from one file location to another, great for backing up onto an external hard drive.

After the SSH, you specify the source to transfer the files from, in this case the local directory ./LIVE. This is followed by the destination. Here it is a path on an server accessed by SSH. Note the colon (“:”) to separate the login details from the path details.

Finally I added the –progress switch just so it gives me a bit more information whilst uploading the files.

And that’s it. I still keep the site up-to-date with SVN, but I have another little step to deploy to the live server.

Hope someone finds this useful!

Useful Links

Rsync Man Page

Subversion Homepage

Notes: I’m running Ubuntu Intrepid, and the server I was trying to deploy to is some version of Redhat. But this should work for all flavours of Linux that have rsync installed and possibly even Mac OS X.