Tuesday 7 December 2010

Syncing web servers with rsync

rsync is an amazing useful file tree synchronisation command-line tool. It's included by default on mac OS X.
Essentially you point it at a remote source directory and a local directory and it'll make the local directory reflect the remote one exactly in an amazingly fast manner. It is able to do this by only sending the changes to files across, and even then it compresses those changes so downloads are super-quick.

This means it's an excellent tool for updating the contents of remote web servers.

Now in a standard linux way, rsync is incredibly flexible and Google searches give you tons of seriously complicated stuff on it, most of which can be ignored if you just want to sync folders. For instance most tutorials include how to set up the rsync deamon, and how to make that secure.
In fact you don't need to do this at all (though I'm sure it's really useful in some circumstances), you can simply run rsync directly. It'll log in using ssh, so make sure you have a ssh user set up.

The best way to use rsync is to perform a PULL from your remote server..

eg

  • ssh to your remote server

  • run rsync pointing it at the machine holding the data to sync



Example Command



Log into the remote machine (e.g. the web server) and type:

rsync -avz --exclude '.svn/*' -v --delete user@example.com:path-on-example-server/ /path-on-this-server/


This command connects to example.com as the user 'user' and will sync the folder path-on-this-server with the path-on-example server on example.com. --delete means delete any files on this server that are not on the one we're syncing with. --exclude '.svn/*' will ensure that any svn folders are not copied. This is really handy.

No comments:

Post a Comment