An updated version of the script can be found here.
I recently bought a NAS, a Synology DiskStation DS211j and stuffed two 1TB disks in it. I configured the disks to be in RAID 1 (mirrored) in case one of them decides to die. I then brought the NAS to a family member’s house and installed it there. Now she uses it to back up her important files (and as a storage tank for music and videos).
The good thing for me is that I can now make off-site backups of my home directories. I configured the DS211j to accept SSH connections so that I can log into it (as user admin or root). I used the web interface to create a directory for my backups (which appeared to be /volume1/BackupLennart
after logging in with SSH).
After making a hole in her firewall that allowed me to connect to the DS211j, I created a backup script in /etc/cron.daily
with the following contents:
#!/bin/bash
#
# This script makes a backup of my home dirs to a Synology DiskStation at
# another location. I use LVM for my /home, so I make a snapshot first and
# backup from there.
#
# Time-stamp: <2011-02-06 21:30:14 (lennart)>
###############################
# Some settings
###############################
# LVM options
VG=raidvg01
LV=home
MNTDIR=/mnt/home_rsync_snapshot/
# rsync options
DEST=root@remote-machine.example.com:/volume1/BackupLennart/
SRC=${MNTDIR}/*
OPTIONS="-e ssh --delete --progress -azvhHS --numeric-ids --delete-excluded "
EXCLUSIONS="--exclude lost+found --exclude .thumbnails --exclude .gvfs --exclude .cache --exclude Cache"
###############################
# The real work
###############################
# Create the LVM snapshot
if [ -d $MNTDIR ]; then
# If the snapshot directory exists, another backup process may be
# running
echo "$MNTDIR already exists! Another backup still running?"
exit -1
else
# Let's make snapshots
mkdir -p $MNTDIR
lvcreate -L5G -s -n snap$LV /dev/$VG/$LV
mount /dev/$VG/snap$LV $MNTDIR
fi
# Do the actual backup
rsync $OPTIONS $EXCLUSIONS $SRC $DEST
# Remove the LVM snapshot
if [ -d $MNTDIR ]; then
umount /dev/$VG/snap$LV
lvremove -f /dev/$VG/snap$LV
rmdir $MNTDIR
else
echo "$MNTDIR does not exist!"
exit -1
fi |
#!/bin/bash
#
# This script makes a backup of my home dirs to a Synology DiskStation at
# another location. I use LVM for my /home, so I make a snapshot first and
# backup from there.
#
# Time-stamp: <2011-02-06 21:30:14 (lennart)>
###############################
# Some settings
###############################
# LVM options
VG=raidvg01
LV=home
MNTDIR=/mnt/home_rsync_snapshot/
# rsync options
DEST=root@remote-machine.example.com:/volume1/BackupLennart/
SRC=${MNTDIR}/*
OPTIONS="-e ssh --delete --progress -azvhHS --numeric-ids --delete-excluded "
EXCLUSIONS="--exclude lost+found --exclude .thumbnails --exclude .gvfs --exclude .cache --exclude Cache"
###############################
# The real work
###############################
# Create the LVM snapshot
if [ -d $MNTDIR ]; then
# If the snapshot directory exists, another backup process may be
# running
echo "$MNTDIR already exists! Another backup still running?"
exit -1
else
# Let's make snapshots
mkdir -p $MNTDIR
lvcreate -L5G -s -n snap$LV /dev/$VG/$LV
mount /dev/$VG/snap$LV $MNTDIR
fi
# Do the actual backup
rsync $OPTIONS $EXCLUSIONS $SRC $DEST
# Remove the LVM snapshot
if [ -d $MNTDIR ]; then
umount /dev/$VG/snap$LV
lvremove -f /dev/$VG/snap$LV
rmdir $MNTDIR
else
echo "$MNTDIR does not exist!"
exit -1
fi
Let’s walk through it: in the first section I configure several variables. Since I use LVM on my server, I can use it to make a snapshot of my /home partition. The LVM volume group I use is called ‘raidvg01’. Withing that VG my /home partition resides in a logical volume called ‘home’. The variable MNTDIR
is the place where I mount the LVM snapshot of ‘home’.
The rsync options are quite straight forward. Check the rsync man page to find out what they mean. Note that I used the --numeric-ids
option because the DS211j doesn’t have the same users as my server and this way all ownerships will still be correct if I ever need to restore from this backup.
In the section called “The real work” I first create the MNTDIR
directory. Subsequently I create the LVM snapshot and mount it. After this the rsync backup can be run and finally I unmount the snapshot and remove it, followed by the removal of the MNTDIR
.
Since the script is placed in /etc/cron.daily
it will be executed every day. Since we use SSH to connect to the remote DS211j I set up SSH key access without a password. This Debian howto will tell you how to set that up.
The only thing missing in this setup is that the backups are not stored in an encrypted form on the remote NAS, but for now this is good enough. I can’t wait until the network bandwidth on both sides of this backup connection get so fast (and affordable) that I can easily sync my music as well. Right now uploads are so slow that I hardly dare to include those. I know that I shouldn’t complain since the Netherlands has one of the highest broadband penetrations in the world, but, hey, don’t you just always want a little more, just like Oliver Twist?
Related Images: