The importance of rsync

What is rsync

rsync is a tool commonly found in *nix systems for the synchronization of files across different machines. Put simply, it is a file transfer tool that has various uses. It is commonly used in backup solutions. It is fairly easy to get it working under windows with good results.

Why rsync

So why rsync? Why not just transfer files via FTP/Samba/NFS every x amount of days? The reason for using rsync is because I’ve been relying on a boring backup routine for years. When creating a backup I usually do it manually — This means opening up an SFTP connection, finding the files I want to backup, and with some of them I may want to compress them. This can take a considerable amount of time and the possibility of you missing a file that you perhaps meant to backup is too high. I decided it was time for a proper backup solution. I had been hearing about rsync and been meaning to take a look at it for quite a while, so off I went.

Using rsync allows you to create a nice scripted solution which just works — you can just run your script and it’ll perform your entire routine for you. You can run it transparently if you really want. The amount of software required is also minimal (no crappy shareware stuff, etc..)

How to use rsync

This isn’t really about how to use rsync but rather how _I_ use rsync. I have two drives connected to my NAS and that stores files which I wish to be backed up. This NAS has an SSH server running on it (openssh-server) in order to accept SSH/SFTP/RSYNC connections.

In order to allow my windows machines to use rsync I must use cygwin [cygwin.com]. When installing cygwin make sure you enable rsync.

In order to synchronize files you must enter cygwin and issue a command like the following:

rsync -e ssh -av −−delete “/cygdrive/e/Music/” 192.168.0.25:/media/disk1/Music/

In this instance, I am synchronizing my music collection located at E:Music (in cygwin this becomes /cygdrive/e/Music) into /media/disk1/Music/ on the other machine (located at 192.168.0.25).

The −−delete option is used to remove files on the destination server that no longer exist in the new file list. See the man pages (man rsync) for more details. Once you have gotten rsync working and can sync some files, you may realise that you need to enter a password each time.

Passwords

In order to create backups without having to enter a password every time, you will need to create a private/public keypair. To do this, run ‘ssh-keygen -P “”‘ within cygwin.

This will create a keypair that does not require a password. By default this is saved into ~/.ssh/id_rsa.pub (which becomes drive:cygwinhomekahrn.sshid_rsa.pub). Copy this file to the remote machine and append it to ~/.ssh/authorized_keys on the remote machine (cat id_rsa.pub >> ~/.ssh/authorized_keys).

In my case, I created a seperate user on the remote machine called ‘rsync’ and then appended the id_rsa.pub created in cygwin to /home/rsync/.ssh/authorized_keys. I could then prefix the rsync address with a username (e.g. rsync@192.168.0.25:/media/disk1/Music/) and it would use the rsync account rather than my main account.

You should now be able to login without having to enter a password every time rsync creates a connection. Just make sure nobody can obtain your key file.

Automating and shortcuts

The first thing you should do is create a shell script which contain the rsync commands you wish to execute. In my example,

# Critical data and other misc data
rsync -e ssh -av −−delete −−exclude “/cygdrive/e/data/global/” “/cygdrive/e/data” rsync@192.168.0.25:/media/disk0/
# Global data
rsync -e ssh -av −−delete “/cygdrive/e/data/global” rsync@192.168.0.25:/media/disk0/
# Music
rsync -e ssh -av −−delete “/cygdrive/e/Music/” rsync@192.168.0.25:/media/disk1/

Running ‘sh rsync_backup.sh’ will then execute the above shell script and the appropriate files will be backed up. In order to link this shell script with an ordinary windows shortcut you will have to copy rsync.exe (located in drive:cygwinbinrsync.exe) to the WINDOWS directory (or include rsync.exe into your path variables).

You can then create a batch file like the following:

@echo off

D:
chdir D:cygwinbin

bash ‘E:_desktoprsync_music.sh’
bash ‘E:_desktoprsync_other.sh’
bash ‘E:_desktoprsync_data.sh’

Then if you really wanted to you could merely create a desktop shortcut to this batch file. Awesome.

Further Information

Here are some further links to help you.
http://optics.ph.unimelb.edu.au/help/rsync/rsync_pc1.html
http://www.manpagez.com/man/1/rsync/
http://everythinglinux.org/rsync/
http://lifehacker.com/software/rsync/geek-to-live–mirror-files-across-systems-with-rsync-196122.php