Friday, August 10, 2007

Rsync from Windows to Linux

With the mounting documentations that I'm having at work, there's always the insecurity that losing them, especially those "critical work-in-progress" documents. To most of us, having some sort of an easy backup solution is a must, but "making things easy" is one of the biggest hurdles that I ended up delaying to find the solution until recently.

And so I bumped into rsync, which is a fast and secure way to perform backups. And having it installed by default in our office's development server (a Redhat Enterprise Linux 4), I sort of gave it a go.

So the idea is to have a shortcut at windows so that it can mirror all important documents into Linux, which goes through rsync. Since Redhat have rsync daemon setup to run via xinetd, so the first thing is to setup rsync client for Windows, which is a piece of cake if you install Cygwin packages. Gaztronics.net provides a good tutorial for it (See 1. Cygwin and 3. Client section).

All is good but now the problem is that everytime when rsync is executed the server always asks for a password. Reason being the rsync that came with Redhat using SSH as the transport protocol, which means you can't simply do a "rsync --password-file=[file]". To automate the authentication step (i.e. not needing to key in the password every time), Troy Johnson had a tutorial which explains how to do it.

Just for your quick reference, this is the big picture on what I have done ([text in square brackets] denote fill in the blank fields :) ):

  1. Install rsync and ssh Cygwin packages in Windows
  2. Perform a rsync transfer to make sure that it works (basically just a "rsync [local file] [linux-username]@[linuxserver-address]:[remote directory]" would do)
  3. Perform a ssh-keygen at Windows to get a public-private key pair (*Note: You'd have to use "ssh-keygen -t dsa -b 1024 -f [key filename]" command)
  4. Transfer the public key (the file with the .pub extension) to Linux
  5. Copy the contents of the public key into ~/.ssh/authorized_keys and make the necessary modifications (See Troy Johnson's tutorial above)
  6. Perform a rsync transfer test again to make sure that it works and without requiring a password from you ("rsync -e "ssh -i [private key file]" [local file] [linux-username]@[linuxserver-address]:[remote directory])
  7. Write a batch script (refer to Gastronics' tutorial) which automates the whole backup process
  8. Add the shortcut on your desktop and your done :)

Note that you might face some problems if you have directory names having spaces (especially if you want to backup documents in your My Documents folder. Basically here's my working script for your reference:
@cls
@echo off

rem Rsync job control file

set DOCROOT=/cygdrive/c/Documents and Settings/shleong/My Documents

C:\Cygwin\bin\rsync -e 'ssh -i rsynckey' -vrtz --delete --include-from='%DOCROOT%/bin/filelist' '%DOCROOT%/' shleong@linuxserver:docstore

No comments: