Pedro Rodrigues

home github

rsync for webmasters

Updating code on remote servers can be a painful experience, specially if you are still iterating on that code.

There are lots of available solutions to update files remotely, [S]FTP, git/svn, etc.

I have personally used FTP, tried out git and even attempted to use macFuse to mount a local drive of an SFTP folder. FTP(sFTP or scp) is ok for one file at a time but becomes unusable for many files, git is acceptable for multiple file changes, keeps history but is a bit too slow for one file changes and macFuse is impossibly slow to mount.

I set out to find the silver bullet and went to a place long forgotten.

Enter the unix world, the world where programs do one thing and do it well.

rsync. It’s purpose is to keep directories in sync, the largest mirror sites use this to keep up to date with the changes.

In my setup I am using rsync over an ssh connection, which makes this a tad bit more complicated, still extremely useful while safe. Keeping local directories in sync with rsync is pretty simple.

> rsync origin/ destination/

That will keep the contents of the directory ‘destination’ in sync with ‘origin’. I added the option -r to make sure rsync recurses into directories.

> rsync -r origin/ destination/

rsync does not delete files from destination that are missing in the origin folder, for this I added —delete and —force to remove directories from destination even if they are not empty.

> rsync -r —delete —force origin/ destination/

But because I sometimes work on live code the updates should not mess with the running code. For this I added the option —delay-updates and changed —delete to —delete-after. —delay-updates will tell rsync to upload all changes and apply them after all uploads are sent, making the time it takes to apply changes much shorter. —delete-after will tell rsync to delete the files after uploading. Both these options make changes look more “atomic”.

> rsync -r —delete-after —delay-updates —force origin/ destination/

Because I get bored really fast and having some info on how things are going I also added —progress, this will show how much has been uploaded, transfer speed and ETA.

> rsync -r —delete-after —delay-updates —force —progress origin destination

Now because sync happens over the internet I decided to use ssh for the transfer. To tell rsync to transfer over ssh I used the -e option which allows the user to define a shell for rsync.

> rsync -r —delete-after —delay-updates —force —progress -e ‘ssh’ origin/

Note to separate the domain name from the path with ‘:’ Most of the work is done, but has you may notice, if you have large files, the upload speed is not very fast. To help speed up the syncing I use -z, this will help mostly with text based files.

> rsync -r —delete-after —delay-updates —force —progress -e ‘ssh’ -z origin/

or for a shorter version

> rsync -rze ‘ssh’ —force —delete-after —delay-updates —progress origin/

At this point rsync is pretty decent, except it request the password every time you want to sync. This is easily fixed with public/private key files. For further help take a look at this fine tutorial on setting up ssh keys.

Read or