#!/bin/sh
# backup-tree version 0.90 released to the public domain by Daniel M. Webb 2005
SCRIPT_DIR=/root/scripts/backup
THIS=backup-tree
MIRROR_VERIFY=$SCRIPT_DIR/mirror-verify
RDIFF_BACKUP=rdiff-backup

if [ -z "$3" ]; then
    cat <<END_HELP
Backup a directory tree to a remote server

usage: backup-tree <directory> <remote account> <remote basedir> 
                   [--no-touch-timestamp] [--filelist-generator <generator program>]
                   [verify]

    The order of options is significant!
    Normally, the file <directory>/.backup_tree_timestamp is touch'ed before the
        backup.  --no-touch-timestamp prevents this.
    <generator program> is a program that will return the filelist to
        backup, using null delimiter.  If this option is not given,
        "find . -name '.' -o -print0" will be used.
    verify means backup then do strict verification

example: backup-tree /home/bob/ backups@remote.com backups/mirrors/bob_backup \\
            /home/bob/.backup_filelist verify

Note: The trailing '/' after bob in the example is significant, because without
      it the verification won't work right (to see why, look at the way the
      arguments are passed to rsync in the mirror-verify script).
      
Note: if <remote basedir> is a mounted filesystem, set the directory mount-point
      read-only so that you won't fill up the directory if the filesystem isn't
      mounted.
END_HELP
    exit 1
fi

function die() {
  echo "$0: $*"
  exit 1
}

[ -z "$USER" ] && USER=$(whoami)
DIR="$1"
[ -d "$DIR" ] || die "directory $DIR doesn't exist"
REMOTE_ACCOUNT=$2
REMOTE_BASEDIR=$3
shift; shift; shift

touch_timestamp=true
if [ "$1" = --no-touch-timestamp ]; then
    touch_timestamp=false
    shift
fi

FILELIST_GEN="find . -name '.' -o -print0"
if [ "$1" = --filelist-generator ]; then
    shift
    FILELIST_GEN=$1
    [ -x $FILELIST_GEN ] || die "can't run $FILELIST_GEN"
    shift
fi

verify=false
if [ "$1" = verify ]; then
    verify=true
    shift
fi

echo "---------------------------------------------------------------------"
echo "Starting $THIS run with options: $*"
date
echo "---------------------------------------------------------------------"

echo "Backing up $DIR"
cd $DIR >/dev/null 2>/dev/null || echo "error during cd $dirdir"
[ $touch_timestamp = true ] && touch .backup_tree_timestamp
tmpfile=$(mktemp)
eval $FILELIST_GEN > $tmpfile
[ "$(cat $tmpfile | wc -c)" -eq 0 ] && die "$FILELIST_GEN created an empty filelist"
$RDIFF_BACKUP --force --terminal-verbosity 1 \
    --null-separator --include-filelist $tmpfile --exclude '**' \
    . $REMOTE_ACCOUNT::$REMOTE_BASEDIR || echo "error during rdiff-backup of $DIR"

if [ $verify = true ]; then
    echo "Verifying $DIR" 
    $MIRROR_VERIFY $DIR $REMOTE_ACCOUNT:$REMOTE_BASEDIR --filelist $tmpfile
fi

rm -f $tmpfile
