Essential Geek Skills: Automating incremental snapshot backups on Windows with rsync

I just finished setting up a fully automated incremental snapshot backup regime on my Windows 7 machine. Every night, my script creates an exact replica of my data and retains 3 days of snapshots. This means that I can go “back in time” for three days in case I need to recover something quickly. More importantly, the data isn’t hidden away in some obscure, proprietary file format – it’s sitting there just like a bunch of normal files. This makes it really easy to recover from a catastrophic failure of my main drive.

Note: The process I’m about to describe is for geeks only. If you’re not a geek, maybe you should just buy a decent backup application and save yourself the trouble of doing this manually. If, on the other hand, you’ve got an hour to spare and you’re OK with dealing with things like Cygwin, ntrights and the Task Scheduler, read on!

The core idea is to use rsync to make a backup of your data and use hardlinks to create the snapshots, as described by Mike Rubel.

What you’ll need:

  1. Cygwin (we’ll be using rsync to make the backups)
  2. Windows 2003 Resource Kit (We need a copy of ntrights.exe to do this on Windows 7 Home Premium)
  3. A spare hard disk that’s at least the same size as the drive you want to backup

 

Step 1: Install Cygwin

Get the installer from the Cygwin site. Be sure to install the rsync package by selecting it from the installer:

image

 

Step 2: Prepare your backup disk

Format your disk and create the following files:

1) backup.sh

This file creates the actual backup and manages moving around the snapshots. Just be sure to replace /cygdrive/d/* with your source (the location with the data you want to backup) and /cygdrive/e in the following script to point to your backup drive.

rm -rf /cygdrive/e/backup.3
mv /cygdrive/e/backup.2 /cygdrive/e/backup.3
mv /cygdrive/e/backup.1 /cygdrive/e/backup.2
cp -al /cygdrive/e/backup.0 /cygdrive/e/backup.1
rsync -a /cygdrive/d/*  /cygdrive/e/backup.0/

 

2) cygrun.bat

This is the file that will startup a bash shell and run our backup script. We’ll be calling this from a scheduled task. The credit goes to Zenovations.com for this script:

@echo off
rem set HOME=c:\
if "%DEF_PATH%"=="" set DEF_PATH=%PATH%
set PATH=c:\cygwin\bin;%DEF_PATH%
set myargs=%*
if "%myargs%" == "" goto noarg
rem echo %myargs%
bash --rcfile "%HOME%/.bashrc" -i -c "%myargs%"
c:
rem pause
sleep 1
goto exit
:noarg

rxvt -e /usr/bin/bash --login -i

:exit
exit

Step 3: Create a scheduled task

Nothing special about this, just set up a standard scheduled task. I’ve set mine to trigger at 4:00 AM every day. The action should look like this:

image

Step 4: Making it work on Windows 7 Home Premium

If you choose to run this task as a non-administrator user, you will need to ensure that your user account has the “Logon as a batch job” privilege. Unfortunately, Windows 7 Home Premium doesn’t give you an easy way to do this. So get yourself the Windows 2003 Resource Kit and use the ntrights.exe tool that comes with it to assign the privilege from the command line. Credits to Daniel for this tip.

ntrights -u COMPUTER\User +r SeBatchLogonRight

 

And that, my dear readers, should be it. You should (hopefully) have a fully automated, scheduled, incremental snapshot backup running of your important data.

No Comments