It’s day three in our week-long exposè on automated backups, and today we’re taking a look at rsync in Windows. If you’ve been following along, you’ll notice setting up rsync in Windows is nearly identical to setting it up under Linux or Solaris (or MacOS, as we’ll look at tomorrow). The trick is to use the great Linux command-line emulator Cygwin, which creates a Linux “environment” on top of Windows. This article will take you through the installation process for Cygwin, and setting up rsync to automatically back up your important documents.
In the first installment of this series, we discussed how rsync communicates – there’s a server and a client, though which is which often doesn’t particularly matter. For our purposes, I’m using a Linux box as the rsync server and transferring all my documents there (and, as we saw yesterday, you can both send and receive documents regardless of which computer is the server).
The first step to get rsync working in Windows is to setup Cygwin and get rsync installed. First, download the installer for Cygwin (click the Install Cygwin Now link at the top right of the Cygwin page). This tiny executable gives you an installer and downloads only the packages necessary for installation. Let’s look at the installation process.
Click through the installer a few times and be sure to select the options that make most sense for you. Once you get the package selection screen, we have a few options we need to select from.
The rsync utility is listed under the Net group, so make sure this is marked for installation. In addition, you may want to install the openssh package as well, especially if you’re interested in encrypting your rsync transfers by tunneling communications through SSH (which I’ll discuss on Friday).
Feel free to add additional packages as well, but be careful not to deselect too much (unless you know what you’re doing), as many packages are necessary for Cygwin installation. Once you’ve made your selections, hit the Next button. Cygwin will now download the packages you’ve selected and attempt to install everything. It could take a few minutes (especially if you have a slow network connection), so go get a cup of coffee.
Once installed, you should be able to launch a Cygwin Bash Shell (see Start -> Programs -> Cygwin). You should get a command prompt. If you’re new to Cygwin, there’s some interesting things to note here. First, type
pwd (which prints your current working directory): you’ll notice you’re in /home/[user name]. But wait – those are forward-slashes! And /home? Well, here’s where things get even more interesting: type
df (which prints the mounted volumes): you’ll notice C:\cygwin is mounted as / – interesting. And what’s further, you can still access your Windows files directly: simply go to /cygwin/c. This is important to note, as we’ll need to access those Windows files from Cygwin in order to get our backups working properly.
Now that Cygwin is working properly, be sure to test that rsync was installed. Try running:
If you get the rsync version that was installed, you’re in business; otherwise go back to the Cygwin installer and make sure to choose the rsync package (in the Net directory) – it’s easy to miss.
With rsync working, the next step is to configure it.
As we saw yesterday, setting up rsync to backup files to a remote computer is very easy: one simple command is all that’s needed to run it. But there’s a bit of configuration to do on the server end first: namely, setting up a profile for this computer.
On the Linux computer (where we send files to), we need to add a profile for this computer. This is done by editing the /etc/rsyncd.conf file and adding a block similar to the following:
uid = ryan
gid = ryan
read only = true
auth users = ryan
secrets file = /etc/rsyncd.secrets
This configures the rsync server to accept incoming connections using the “ryan” user account (whose password is listed in /etc/rsyncd.secrets) and store all files from this computer in /backups/sigma (remember: I have a separate hard drive just for backups, mounted at /backups). Additionally, be sure the user for this profile exists in /etc/rsyncd.secrets (remember, this file is made up of key/value pairs: [username]:[password]). If you’re running the rsyncd as a daemon, be sure to reload its configuration file.
Next, we need to setup the Windows side. This is even easier. First, we need to create a password file that rsync can access, so we don’t get prompted to enter a password every time we attempt to run a backup (it wouldn’t be very automated otherwise). This is done by creating a text file and saving your password (that corresponds to the user you used in the rsync profile). I stored mine in C:\cygwin, but you can put it pretty much anywhere you like. Just create a file, enter your password, and save it.
Now that our password is stored so rsync can access it, we need to write a simple batch script to backup our files. Why use a batch script? Because Windows uses something called Scheduled Tasks to run programs at specified times, and we need to run multiple commands. This is similar to cron in Linux & Solaris, but differs in a few respects (not the least of which is the graphical interface).
Creating this batch script is very simple. I decided that I would only backup several directories on my Windows computer, rather than the entire hard drive, for practicality reasons. You could try to run it on your C drive and attempt to transmit everything (though I don’t think rsync is very good at sending files that are currently open on Windows, such as system files), but this just doesn’t make a lot of sense. So, instead I decided to backup only the following two directories:
- My Documents
- Local Settings\Application Data\Microsoft Outlook
This contains all of the important items I store in My Documents (I’m fairly meticulous at organizing my documents), and all my email from Microsoft Outlook. The batch script, therefore, looks as follows:
@ echo off
C:\Cygwin\bin\rsync -vrt --password-file=c:\cygwin\rsync.secret --delete "/cygdrive/c/Documents and Settings/Ryan/My Documents" email@example.com::sigma
C:\Cygwin\bin\rsync -vrt --password-file=c:\cygwin\rsync.secret --delete "/cygdrive/c/Documents and Settings/Ryan/Local Settings/Application Data/Microsoft/Outlook" firstname.lastname@example.org::sigma
Let’s take a look at what this script does. First, we run the rsync program as if it were any other DOS executable, though we include the full path (which is in C:\Cygwin\bin). Be sure to pay particular attention to the direction of the slashes.
v command sets rsync to run in verbose. This will print out what it’s doing as it happens, and lets us see as our files get sent to the backup server. Doing so also brings up a shell window to see the text scroll past. The
r command sets recursive mode (so we get all files and subdirectories in each directory we ask rsync to backup, and the
t command has rsync preserve the times associated with each file (such as when it was created, modified, etc). There are numerous other options to choose from; see
rsync --help for the complete list.
password-file argument. This points to the password file we created earlier, and instructs rsync to use the contents of that file as the password for the ryan user account (and thus not prompting us to enter a password whenever these commands are executed). Finally, the
delete argument instructs the remote server to stay in sync with the current files, deleting any stored files if necessary (if you delete it in Windows, it will be deleted on the backup end as well).
The final two arguments are the where and to arguments: specify the directory where you want to pull files from, and the location to send those files. In this case, we send the files to the Linux backup server and use the profile sigma (which we created earlier). Note that I use quotes around my where directory path: this is necessary if there are spaces in your path.
Be sure to save the batch script someplace you’ll remember (I put mine in C:\backup.bat). Try double-clicking this file – it should bring up a console and, after a moment or two, say “Building file list”). If not, try to troubleshoot the error (if the window closes too quickly for your to see the output, try running it from a DOS console (type
cmd in the Start -> Run window). If lots of files scroll past – great! That means things are being sent to the Linux rsync server.
Setting up Scheduled Tasks
The only remaining task to do now is to setup Windows to automatically run our batch script at regular intervals. Of course, we can always run a backup by hand just by double-clicking that batch script, but I’d feel better if the system was automatically doing it every day. So, we need to use Scheduled Tasks.
The first thing to do is to make sure you have permissions to run Scheduled Tasks – an item in the System Tools folder in Start -> Programs -> Accessories. If it doesn’t appear, be sure you have the Display Administrative Tools item checked (right click the Start menu, select Customize… next to your Start Menu theme, and choose the item Display Administrative tools from the Advanced Start menu options list). Open the Scheduled Tasks program.
In Scheduled Tasks, double-click the Add Scheduled Task item and follow the wizard through to setup the backup script. You’ll need to click the Browse… button to choose the batch script we created earlier. I have the script run daily at 3am (a time when I’m usually away from my computer). Remember that having the backup script run more often doesn’t hurt – it will check the files on disk against those remotely, and then transfer only the changes. You’ll probably want to run the script when you’re not likely to be using the computer, as it can slow things down (and it won’t get files that are currently being modified). Choose a time that works best for you, and finish the wizard. You’re backups will now run automatically at the time you chose.
And that’s it! You’ve now setup your Windows computer to regularly backup important documents to a separate Linux server, and it will continue to run regularly without any intervention. Of course, you’ll want to double check things are getting over to the Linux server properly before you trust it completely.
Tomorrow we’ll take a look at setting up rsync under MacOS and getting backups running regularly there (it’s easy).