Backup data: Windows 7 to Linux using Cygwin's rsync
I thought I would share the bash script I wrote (for Cygwin) to backup my files and photos from my main Windows 7 PC to both my HP Microserver (running Ubuntu using raid-z!) and a local external USB HD.
Here are some pre-requisites for running the script (I might have missed some, so if it doesn’t work please let me know and I can correct the post).
- Install Cygwin
- [Optional] Set up keys to authenticate between Cygwin and Linux
- Set up an rsync daemon on the Linux box
- Save my script below as
copy.sh
(to your cygwin user home dir e.g.C:\cygwin\home\username
) - Customise it for your PC and to fit your needs and run on Cygwin:
sh copy.sh -a
#!/bin/bash
# http://blog.ham1.co.uk/2013/08/12/backup-data-windows-7-to-linux-using-cygwin-rsync
usage()
{
cat >> EOF
This script backs up stuff to the server (default) or local disk 'e:'.
OPTIONS:
p - photos
f - files
a - all
l - local
t - test (rsync -n)
Usage examples:
sh copy.sh -al :: backup all to the external HD
sh copy.sh -p :: backup photos to the backup server.
EOF
}
while getopts "pfalt" OPTION
do
case $OPTION in
p)
PHOTOS=1
;;
f)
FILES=1
;;
a)
PHOTOS=1
FILES=1
;;
l)
LOCAL=1
;;
t)
TEST="-n"
;;
?)
usage
exit
;;
esac
done
# set some variables to tidy up the script.
NORMAL_OPTS="-rhutv --delete-before --progress --stats --fuzzy"
# Cygwin makes a mess of permissions so the chmod sorts it
REMOTE_OPTS="-p --chmod=Du=rwx,Fu=rw"
BACKUP_PATH="rsync://YOUR SERVER/backup"
if [ "$LOCAL" ]
then
BACKUP_PATH="/cygdrive/e"
REMOTE_OPTS=""
fi
if [ "$PHOTOS" ]
then
echo "Backing up photos to $BACKUP_PATH"
# excluding Lightroom preview files to save some space.
rsync $TEST $NORMAL_OPTS $REMOTE_OPTS --exclude 'Lightroom/*Previews.lrdata' /cygdrive/m/Pictures/ /cygdrive/c/Users/YOUR WINDOWS USERNAME/Pictures/ $BACKUP_PATH/Pictures/
fi
if [ "$FILES" ]
then
echo "Backing up files to $BACKUP_PATH"
# backs up the folders files/stuff and files/text docs to files/ on the server
rsync $TEST $NORMAL_OPTS $REMOTE_OPTS --exclude 'tmp' /cygdrive/m/files/{stuff,important\ docs} $BACKUP_PATH/files/
fi
# you could instead use SSH instead by adding the following options to REMOTE_OPTS
# -e "ssh -c blowfish-cbc"
# and change the BACKUP_PATH to:
# username@server:/backup