backing up windows to unix with rsync

My IT staff at work doesn’t provide a standard way to back up my computer.  There are a ton of very cheap online backup systems that work extremely well in Windows, but I would violate my work policy on information.  So instead I just did it myself locally.  I actually have three work computers, and one of them is a very basic stock install of FreeBSD.  This works great, so I can rsync both the Windows XP laptop and desktop to the FreeBSD machine and use that as my backup storage.  Here is how I did it.

 

First, install cygwin from http://www.cygwin.com/.  While installing this be sure to select Net/rsync, Net/openssh, and Editors/vim.

Just a random cygwin sanity note (besides installing vim) when you have the command window open; if you right click on the menu bar, hit defaults, and click quick edit mode it makes dealing with copy/paste a bit more reasonable.

Now, the first thing is to create an ssh key so you can copy files without entering a password.  To do this, use the command ssh-keygen.  The defaults are fine, and be sure to just hit enter at the passphrase prompt.  Now, you’ll need to take the contents of ~/.ssh/id_rsa.pub and get them in to ~/.ssh/authorized_keys on the server where the backup will occur.  Be sure authorized_keys is only readable by your user id or sshd won’t use it.  chmod 600 ~/.ssh/authorized_keys is all you need for that.

Be sure to check that ssh is working with the key auth (no password/passphrase required) before moving on.

Now we’re ready to set up the backup.

First create a shell script to run in cygwin to do the actual backup.  Here is the one I wrote;

#!/usr/bin/bash

export PATH=$PATH:/usr/bin

/usr/bin/rsync -a –delete –numeric-ids "/cygdrive/c/Documents and Settings/<local username>/" <remote username>@<remote host>:<backup directory> 2>&1 | egrep -v ‘Device or resource busy|Outlook/.*\.(ost|pst).*(Permission denied|failed verification)|some files could not be transferred’ | while read out
do
  echo `date +%F-%H:%M:%S` $out
done > /cygdrive/c/Documents\ and\ Settings/<local username>/backup.log

You’ll note I’m only backing up the contents of my own Documents and Settings folder.  You may want to do more..  I’m also ignoring a few errors I expect to get semi-often so I can see actual failures.  The script then date/time stamps the output and puts a log in my Documents and Settings folder.

Now make the script executable (chmod 755) and run it to make sure it works.  It may take a few minutes since this is a full backup.

Next you need to install StartX from http://www.naughter.com/startx.html

This will make it so when we run the task no DOS windows pop up and it will be completely invisible.

Last but not least the task needs to be scheduled.  To do this, go to Scheduled Tasks in Control Panel.  In there, double click on Add Scheduled Task.  You’ll want to browse to StartX when it asks what program to run.  Tell it you want to run it daily.  Personally I told it to start at midnight every day, starting today.  After you fill in your auth info check that you want to open advanced properties and hit finish.  In the window that comes up add the arguments for what we are running.  First add /B to make the window not appear, then add "C:\cygwin\bin\bash -c <location of backup script>"".  Be sure to also hit that you only want it to run if you’re logged in.  This will help ensure that you don’t have to change your password in the scheduled task everytime you change your password for the machine.

 Next hit the Schedule tab and the Advanced button.  Click Repeat task and now you can make it run multiple times throughout the day.  This is a very non-intuitive spot in Windows, you’re basically running a daily task that repeats.  I’m not sure why you couldn’t just run it in minutes or hours in the first place.  Anyways, I told it to repeat every 2 hours and run for a duration of 23 hours and 59 minutes.

In the Settings tab I also told it to kill off the task if it runs for 2 hours since I’m on a LAN and a few GBytes don’t take but a few minutes.

And now.. you’re done.

There are few alternate ways to do this.  I could have not used the StartX/scheduled tasks but used cygrunsrv and had it execute crond in unix, but I didn’t want to install extra services non-native to windows.  You could also have the shell script actually email you the log results when there are some, but my IT stuff does a good job of blocking outbound SMTP and I don’t care that much about bouncing around it.  Just remember to install some of the mail packages in cygwin if you’re doing that.

I’m also going to setup an rsync to copy the data of both machines to each other so I can see docs on either machine, but it’ll be more of the same, except adding the –exclude command to my backup script to make sure I don’t backup the copy of the backup in my backup.

Say that ten times fast.

One thought on “backing up windows to unix with rsync

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