!/bin/sh
# Time Machine equivalent for Linux. This backs up the local root filesystem.
# Set variables
LABEL=HardDriveLabel
MOUNT=/media/${LABEL}
BKPDIR=${MOUNT}/backup/laptop
CURRENT=${BKPDIR}/current
DEVICE=/dev/disk/by-label/${LABEL}
EXCLUDES=${BKPDIR}/excludes.txt
LOG=${BKPDIR}/rsync.log
NEWDIR=${BKPDIR}/$(date "+%Y-%m-%dT%H:%M:%S")
OPTS="-aqx"
UMOUNT=
SOURCES=/
die () {
# If we mounted the disk, unmount it again
if [ ${UMOUNT} ] ; then
pumount ${DEVICE}
fi
exit $1
}
# Is the backup disk plugged in
if [ ! -e ${DEVICE} ]; then
exit 1
fi
# Is the backup disk mounted
if ! grep -q ${MOUNT} /proc/mounts ; then
pmount ${DEVICE} ${LABEL}
UMOUNT=1
# Give the disk time to sort itself out
sleep 3
fi
if ! grep -q ${MOUNT} /proc/mounts ; then
# Still not mounted, give up.
die 1
fi
# Make sure the backup directory is there
mkdir -p ${BKPDIR}
if [ ! -d ${BKPDIR} ] ; then
# mkdir failed, give up
die 1
fi
# Set extra options
if [ -f ${EXCLUDES} ] ; then
OPTS="${OPTS} --exclude-from=${EXCLUDES}"
fi
if [ -h ${CURRENT} ] ; then
OPTS="${OPTS} --link-dest=${CURRENT}"
fi
if [ ${LOG} ] ; then
OPTS="${OPTS} --log-file=${LOG}"
fi
# Perform the backup
rsync ${OPTS} ${SOURCES} ${NEWDIR}
# Did we create a new backup
if [ ! -d ${NEWDIR} ] ; then
die 1
fi
# Now update the current soft link
if [ -h ${CURRENT} ] ; then
rm -f ${CURRENT}
fi
ln -s ${NEWDIR} ${CURRENT}
die $?