#!/bin/sh
if [ `id -u` != "0" ]; then
echo "Not running as root!"
exit 2
fi
# Separate with a space. Exclude trailing slash!
SOURCES="/"
TARGET="/mnt/hda2"
TARGETDEV="/mnt/hda2"
# dirs and files to be excluded, one per line.
# /proc/
# *.bak
EXCLUDE_FILE="/usr/local/share/backup-hda2-exclude.txt"
TARGETMOUNTED=0
echo "Verifying target..."
if [ ! -d $TARGET/home ]; then
echo "Backup target is not mounted. Mounting..."
mount $TARGETDEV
if [ ! -d $TARGET/home ]; then
echo "Backup target does not exist or is not mountable!"
echo "Exiting..."
exit 2
else export TARGETMOUNTED=1
fi
else export TARGETMOUNTED=2
fi
echo "Target verified."
echo "Verifying sources..."
for source in $SOURCES; do
echo "Checking $source..."
if [ ! -x $source ]; then
echo "Backup source does not exist!"
echo "Exiting..."
exit 2
fi
done
if [ -f $EXCLUDE_FILE ]; then
EXCLUDE="--exclude-from=$EXCLUDE_FILE"
fi
echo "Sources verified."
for source in $SOURCES; do
if [ ! -d $TARGET/$source ]; then
echo "Mimicking sources directory hierarchy..."
mkdir -p $TARGET/$source
fi
echo "Running rsync..."
ionice -c3 nice -n19 rsync --progress --exclude=$TARGET/ $EXCLUDE -a --delete $source/ $TARGET/$source/ "$@"
done
if [ $TARGETMOUNTED = 1 ]; then
sleep 2
umount $TARGETDEV
if [ -d $TARGET/home ]; then
echo "Backup target still mounted!"
echo "Exiting..."
exit 1
fi
echo "Backup target unmounted."
fi
if [ $TARGETMOUNTED = 2 ]; then
echo "Backup target was mounted and will remain mounted."
fi
exit 0