Sync files from Windows to Linux using SSH

Over the weekend I decided to figure out how to sync files between windows based computers and Linux based computers, specifically Ubuntu. On windows I investigated a number of technologies. Finally I settled on cwrsync. The reason for the choice is that I really like rsync. I have a number of scripts that work really well (and are fast) that I use on my Linux boxes on a regular basis. There is rsync available in cygwin but that is far too heavy for simple file synchronization. cwrsync is the best of both worlds. It packages the cygwin dll and rsync binaries in a form that is easy to use on windows.

You’ll need to download the cwrsync package and install it on windows. Also it is a good idea to install putty so that you can test your connectivity to your Linux box through ssh. That will eliminate some of the frustration. This article doesn’t go into setting up an ssh server. It is very easy and a quick Google will find detailed tutorials on the subject of setting up an ssh server.

The first script is a windows batch file that will pull changes from the server to the windows box. The second script is pushes the changes from the windows box to the Linux box. The are both virtually identical except for the order in which the paths are called in the rsync command (basically a source – destination order).

The pull script:

echo Pulling changes from server to local
@ECHO OFF
rem uuid = 54b06855-2937-4476-800b-1c6f5af37d18

SET CWRSYNCHOME=C:\PROGRAM FILES\CWRSYNC
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
SET HOME=C:\Program Files\cwRsync\bin
SET CWOLDPATH=%PATH%
SET CYGWIN=nontsec

rem Use the --dry-run option to show what would happen
rem sync from server to local

rsync -rtvze "ssh -p 3687 -i '/cygdrive/c/path/to/keys/rsa.key'" user@ssh.server.com:"'/home/user/files to sync/'" "/cygdrive/c/local files/to/sync"

pause

The push script:

echo Push changes from local to server
@ECHO OFF
rem uuid = f989134a-d48e-4ee7-9de2-2bf758764294

SET CWRSYNCHOME=C:\PROGRAM FILES\CWRSYNC
SET PATH=%CWRSYNCHOME%\BIN;%PATH%
SET HOME=C:\Program Files\cwRsync\bin
SET CWOLDPATH=%PATH%
SET CYGWIN=nontsec

rem Use the --dry-run option to show what would happen
rem sync from local to server

rsync -rtvze "ssh -p 3687 -i '/cygdrive/c/path/to/keys/rsa.key'" "/cygdrive/c/local files/to/sync"  user@ssh.server.com:"'/home/user/files to sync/'"
pause

First off, I use two scripts to make sure that I don’t accidentally push changes when I meant to pull them! If you are like me then your home server will have a non-standard ssh port. This is indicated in the rsync command by the ‘-p 3687′ bit. I also disallow password based logins. I only use key authentication. I specify the private key to use by the “-i ‘/cygdrive/c/path/to/keys/rsa.key'” bit of the rsync command. Note, the single quotes around the path, they are used to deal with spaces. The rest of the command is pretty self explanatory.

A nice use for these scripts is to create your own, secure, dropbox clone. I find it works very well.

About these ads

3 thoughts on “Sync files from Windows to Linux using SSH

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s