Saturday, September 16, 2006

Rsync on Windows

When several computers are networked together, you can take advantage from this - you can setup rsync to synchronize data between computers. This can be particularly useful when you want to sync laptop with desktop


Note: Maybe this tutorial is over-verbose, but i'm used to assume nothing from the _user_.


I'll be talking about Rsync usage in Windows... Although i am currently writing this in Vim editor under Linux.


After you have them installed, follow the following steps:


First, you have to install both cwRsync packages from here . After you have them installed, modify the batch source below to suit your needs. :)
Don't worry, the batch file example is reasonably commented and places that have to be changed are marked by "CHANGE HERE" lines


-------Start Batch example-------

:: This is first to suppress command themselves echoing to the screen
@ECHO OFF

:: Make environment variable changes local to this batch file
SETLOCAL

:: This command specifies directory cwRsync is installed to
:: Specify different path if different from default
SET CWRSYNCHOME=C:\PROGRAM FILES\CWRSYNC

:: Set CYGWIN variable to 'nontsec'. That makes sure that permissions
:: on your windows machine are not updated as a side effect of cygwin
:: operations.
SET CYGWIN=nontsec

:: Set HOME variable to your windows home directory. That makes sure
:: that ssh command creates known_hosts in a directory you have access.
SET HOME=%HOMEDRIVE%%HOMEPATH%

:: Make cwRsync home as a part of system PATH to find required DLLs
SET CWOLDPATH=%PATH%
SET PATH=%CWRSYNCHOME%\BIN;%PATH%

:: Example : C:\WORK\* --> /cygdrive/c/work/*
::
:: Example 1 - rsync recursively to a unix server with an openssh server :
::
:: rsync -r /cygdrive/c/work/ remotehost:/home/user/work/
::
:: Example 2 - Local rsync recursively
::
:: rsync -r /cygdrive/c/work/ /cygdrive/d/work/doc/


:: Simple menu for choosing synchronization direction

:: CHANGE HERE!
:: You are free to change the computer's name from Foo to something more sensible
:: Do one search and replace run on this menu.
echo 1. Retrieve Wiki from Foo
echo 2. Send Wiki to Foo
CHOICE /C:12 /N Make your choice:
IF ERRORLEVEL == 2 GOTO TO_FOO
IF ERRORLEVEL == 1 GOTO FROM_FOO

:: You must replace paths, IP adresses and usernames
:: An explanation for the clueless:
:: Replace user with user which was defined in secrets file
:: Replace IP with, well, Foo's IP adress
:: Replace path with your path
:: Note about cygwin's path: It consists from /cygdrive/x/Path
:: where x is your drive's letter, eg C drive
:: Another thing to note about UNIX paths in general
:: is that they use forward slashes, not backslashes
:: EG, C:\Windows\system32 in this format would be /cygdrive/WINDOWS/system32
:: Yet another difference is that path names are CASE SENSITIVE,
:: Meaning that Doc, doC and doc are three different filenames

:: CHANGE HERE!

:TO_FOO
rsync --del --progress -av "/cygdrive/c/Place/That/Needs/To/Be/in/sync/" user@192.168.1.1::Wiki/
GOTO END

:: CHANGE HERE!

:FROM_FOO
rsync --del --progress -av user@192.168.1.1::place/ "/cygdrive/c/Place/That/Needs/To/Be/in/sync/"
GOTO END

:: Here are the explanations of command line flags
:: used (taken from manual of course :):
:: -a, --archive archive mode; same as -rlptgoD (no -H)
:: --del an alias for --delete-during
:: --delete-during receiver deletes during xfer, not before
:: -v, --verbose increase verbosity
:: --progress show progress during transfer



:END
echo -----------------
echo I hope the sync was succesful! :-)

-------End Batch example-------


When you're done with that batch file, you should save it as C:\Program Files\cwRsync\sync.bat and you're done with the client part...


The daemon (server, service) part needs two files for configuration:

rsyncd.conf and rsync.scrt

The first file contains configuration, folders for sync et cetera.

Rsyncd.scrt contains plaintext pairs of usernames/passwords. You can name rsync.scrt whatever you want to, as long as you don't forget to update pointer in rsyncd.conf.



-------Start rsyncd.conf example-------

use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
pid file = rsyncd.pid
secrets file = rsync.scrt

[place]
path = /cygdrive/c/The Place/Docs
read only = false
auth users = user
transfer logging = yes

-------End rsyncd.conf example-------

-------Start rsyncd.scrt example-------

user:password

-------End rsyncd.scrt example-------


These configuration files must be unique for both hosts. I.e., 1 host must target 2nd host in batch file, et cetera.


When all of the configuration is done and config files are in place... start the daemon:

---

sc config RsyncServer start= auto
net start RsyncServer

---
(Enter this to cmd prompt...)
(no, the weird space in "start= auto" is NOT a typo. That's actually correct syntax... Duh)


Or, if you prefer the tedious rodent-GUI way:

Go to Start>Settings>Control Panel>Administrative tools>Services, find RsyncServer, go to properties, make startup type automatic, start it.


When finished, launch that "sync.bat" or whatever you have named it, and it should give you a proper mini-menu for choosing synchronising direction. Enter the choice number, and the synchronisation shall commence... Happy synching :-)

Edit (2006-09-20): Sorry, the "choice" command isn't included in Win2k/XP by default; It is part of Microsoft's NT Resource kit... Choice.exe can be downloaded from here. Put it to C:\(Windows|WinNT)\system32 or to directory where is that rsync script located (folder where CwRsync is installed)

4 Comments:

Anonymous Anonymous said...

Hi Karolis,

good post on your blog. I have one question to post-xfer exec. It's working on WinXP?

6/30/2009 12:22:00 PM  
Blogger Unknown said...

Thank's, this helped me getting it to work on XP. Eskimo

9/10/2009 10:05:00 PM  
Blogger Andrew Marlow said...

Thanks for this. At long last the mystery of corrupted permissions is solved using CYGWIN=NONTSEC.

2/12/2010 02:51:00 PM  
Anonymous altprog said...

Great post. Thanks!

3/26/2010 04:36:00 AM  

Post a Comment

<< Home