My bosses needed literally desperately a way to backup some pc, or he would have to do it manually everyday... Here's what I came up with

Automated Incremental Remote Backups On Windows With Rsync

Quick! We need to backup several pc's everyday! But I don't want to do it everyday, besides, what about increments in the backups? I don't want to consume my servers' space!

I hate the way my boss always gets the most alarmed at the slightlest stimuli. I'll give him a chance, he is not THAT technology savvy... The first think that came to my mind was:

Hey, I think this can be done with Rsync! It's linux only and we have only windows pc's though...

But hey, nothing was lost: there's cygwin to save you. What's cygwin you may ask? Well, its website states that cygwin is:

  • a collection of tools which provide a Linux look and feel environment for Windows.
  • a DLL (cygwin1.dll) which acts as a Linux API layer providing substantial Linux API functionality.

In my own words: Cygwin is a compatibility layer for running linux only software on Windows. It also has a nice installer which lets you install several utilities.

I think that by now, it's obvious that I'm going to say that you can install both SSH and Rsync with cygwin. Let's get our hands dirty already:

How To Install SSH Client/Server and Rsync Client/Server On Windows

  1. Go get to cygwin's download page and download the installer. Run it and follow the steps until you reach the install packages screen. (this one)

    Cygwin's Install Packages Screeen

  2. Expand the "Net" branch and select for installing both ssh and rsync.

  3. Keep going with the installer, and by patient for cygwin must download a bunch of files to have it's minimal installation ready for use.

  4. Add C:\cygwin to your system path.

  5. Test client ssh or rsync. You're up to go!

How To Configure A Windows SSH And Rsync Server

Once SSH and Rsync are installed, you need to configure the services in order to get your server on. Remember, for remote backups, Rsync uses SSH, so it's vital to have it configured. In order to configure the SSH on your windows machine, follow the next steps:

  1. Open a cygwin console

  2. Run: $ssh-host-config

  3. You'll be asked several questions, here I list them and the corresponding answer:

    • Should privilege separation be used? (yes/no) yes
    • Should this script create a local user 'sshd' on this machine? (yes/no) yes
    • Do you want to install sshd as service? (Say "no" if it's already installed as service) (yes/no) yes
    • Should this script create a new local account 'sshd_server' which has the required privileges? (yes/no) yes
    • Which value should the environment variable CYGWIN have when sshd starts? It's recommended to set at least "ntsec" to be able to change user context without password. Default is "ntsec". ntsec
  4. The last step (although very important!) enabling port 22 for listening in the windows firewall. This step is really important, I myself lost unnecesary minutes (about 30! :/) catching this error of mine.

  5. Finally, run the service using net start sshd. You are ready to go.

How To Configure A Windows Rsync Server On Windows

Considering you already followed the two former how-to's, configuring the rsync server it's kindda easy. Take into account that cygwin needs to replicate a unix env inside windows, so if we want to find "/" well, we'll be looking at C:\cygwin. Having this in consideration, follow these steps:

  1. Create a /etc/rsyncd.conf file, it should look like this:

    use chroot = false
    strict modes = false
    hosts allow = *
    log file = c:/rsyncd.log
        path = /cygdrive/c/Users/Me/Backup
        comment = Rsync storage area
        read only = false

    Let's dissect the most interesting parts:

    • log file = rsyncd.log: Full Windows Path Rsync log. Here errors will be dumped. It helped me debugging several configuration mistakes, so I think you should have it too should you encounter a misconfiguration.
    • [modulename]: This is the name of a particular configuration for this server, when using rsync clietn you must specify one.
    • path = /cygdrive/c/Users/Me/Backup: Basicly, after /cygdrive/c/ you can place the path to the directory you want the bakup to be stored.
  2. Install the rsync service. It's a one-liner:

    cygrunsrv.exe -I "Rsync" -p /cygdrive/c/cygwin/bin/rsync.exe -a "--config=/cygdrive/c/cygwin/etc/rsyncd.conf --daemon --no-detach" -f "Rsync daemon service"
  3. You can start the service from command line using net start rsync or from the windows service utility services.msc as any other service.

  4. Again, remember opening port 873 for listening, as it is needed by rsync daemon/service.

Using The Rsync Client

Well, if you're too lazy to read some man pages (I am) I'll give you the cheat to backup your Windows Client to your Windows Server. From your client (configured accordingly) run this:

C:\Cygwin\bin\rsync -vrtz --password-file=c:\cygwin\secret --delete /cygdrive/c/Source

Let's quickly dissect it:

  • --password-file=c:\cygwin\secret: Points to a file that contains the server's SSH user password (you're asked for it when configuring ssh server). Let me rephrase it: this is a text file wich must contain only the server's SSH user password, no lne-breaks, no spaces, no more.

  • --delete: This option means that if a file gets deleted on the client's backed up directory, it will be deleted to to the destiny directory on the server. Not always useful so watch out.

  • /cygdrive/c/Source: This is the client's directory to bakup. Please do note that it is after /cygdrive/ you place the full path to your directory, in unix path style.

  • If you have used ssh, this one's easy. fabz is the server's SSH user, while is the server's address. modulename refers to the server's configuration at /etc/rsyncd.conf.


Posted by: fabzter
Last revised: 09 Jan, 2014 12:00 AM History


Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.