#!/bin/bash

# updates a hardlinked backup
# Licensed under the GPL version 2
# Copyright Miek Gieben, 2006

prefix=/usr
exec_prefix=/usr
datadir=/usr/share/rdup
sysconfdir=/etc/rdup
lockdir=/var/lock
GNU_DEFS=rdup-snapshot.gnu

# common stuff
source $datadir/shared.sh

usage() {
        echo "$PROGNAME -b DIR [+N] [OPTIONS] DIR [DIR ...]"
        echo
        echo "This is a wrapper around rdup and snap.pl"
        echo
        echo DIR \ - directories to back up
        echo +N \ \ - Look N days back for previous backups, defaults to 8
        echo
        echo OPTIONS:
        echo " -b DIR     backup directory"
        echo " -c REMOTE  dump the backup on a remote machine with ssh"
        echo " -k KEYFILE encrypt all files, using crypt.pl"
        echo " -n PREFIX  optional directory prefix for the timestamp and filelist"
	echo " -l LOCKDIR optional directory for lock file (/var/lock)"
        echo " -E FILE    use FILE as an exclude list"
        echo " -f         force a full dump"
        echo " -z         compress all files, using gzip.pl"
        echo " -a         write extended attributes with uid/gid"
        echo " -e         filelist and timestamp are put in backup directory"
        echo " -v         echo the files processed to stderr"
        echo " -x         pass -x to rdup"
        echo " -h         this help"
        echo " -V         print version"
}

PROGNAME=$0
PREFIX=""
NOW=`date +%Y%m/%d`
c=""
l="-l"
etc=false
pipe=""
ssh=""
force=false
E=""

while getopts ":ab:E:k:c:n:vefzxhVl:" o; do
        case $o in
                b) BACKUPDIR="$OPTARG"
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-b needs an argument"
                        exit 1
                fi
                ;;
                e) etc=true ;;
                E)
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-E needs an argument"
                        exit 1
                fi
                E="-E $OPTARG"
		;;
		l)
		if [[ -z "$OPTARG" ]]; then
			_echo2 "-l needs an argument"
			exit 1
		fi
		lockdir=$OPTARG
                ;;
                c)
                if [[ -z $OPTARG ]]; then
                        _echo2 "-c needs an argument"
                        exit 1
                fi
                ssh=" ssh -x $OPTARG"
                c="-c"
                l=""
                ;;
                k)
                if [[ -z "$OPTARG" ]]; then
                        _echo2 "-k needs an argument"
                        exit 1
                fi
                if [[ ! -r "$OPTARG" ]]; then
                        _echo2 "Cannot read keyfile \`$OPTARG': failed"
                        exit 1
                fi
                pipe="$pipe | /usr/sbin/rdup-crypt $OPTARG"
                c="-c"
                ;;
                z) pipe="$pipe | /usr/sbin/rdup-gzip"
                c="-c"
                ;;
                f) force=true;;
                n) PREFIX=$OPTARG
                if [[ -z $OPTARG ]]; then
                        _echo2 "-n needs an argument"
                fi;;
                a) OPT="$OPT -a";;
                v) OPT="$OPT -v";;
                x) x="-x";;
                h) usage && exit;;
                V) version && exit;;
                \?) _echo2 "Invalid option: $OPTARG"; exit 1;;
        esac
done
shift $((OPTIND - 1))

if [[ ${1:0:1} == "+" ]]; then
        DAYS=${1:1}
        if [[ $DAYS -lt 1 || $DAYS -gt 99 ]]; then
                _echo2 "+N needs to be a number [1..99]"
                exit 1
        fi
        shift
else
        DAYS=8
fi

if [[ $# -eq 0 ]]; then
        _echo2 "No directories to backup"
        exit 1
fi
if [[ -z $BACKUPDIR ]]; then
        _echo2 "Missing the -b argument"
        exit 1
fi
# where to put the list and timestamp files.
if $etc; then
        if [[ ! -z $c ]]; then
                _echo2 "The -e option will not work with remote backups"
        fi
        ETC="$BACKUPDIR"
else
        ETC="/etc/rdup"
fi



# fix lonely root arg
IDENT=`ident_root $(basename $1)`

if [[ ! -z $PREFIX ]]; then
        STAMP="$PREFIX/$HOSTNAME.$IDENT.timestamp"
        LIST="$PREFIX/$HOSTNAME.$IDENT.list"
else
        STAMP="$ETC/$HOSTNAME.$IDENT.timestamp"
        LIST="$ETC/$HOSTNAME.$IDENT.list"
fi
# create our command line
if [[ -z $ssh ]]; then
        pipe="$pipe | /usr/sbin/rdup-snap $c $OPT -b $BACKUPDIR/$NOW"
else
        pipe="$pipe | $ssh rdup-snap $c $OPT -b $BACKUPDIR/$NOW"
fi
cmd="/usr/sbin/rdup $E $x $l $c -N $STAMP $LIST $@ $pipe"

if ! $force; then
        if [[ -z $ssh ]]; then
                /usr/sbin/rdup-snap-link $DAYS $BACKUPDIR $NOW
                purpose=$?
        else
                $ssh "rdup-snap-link $DAYS $BACKUPDIR $NOW"
                purpose=$?
        fi
else
        purpose=1
fi
case $purpose in
        0) _echo2 "INCREMENTAL DUMP" ;;
        1)
        _echo2 "FULL DUMP"
        rm -f $LIST
        rm -f $STAMP ;;
        2)
        _echo2 "Illegal return code from rdup-snap-link"
        exit 1 ;;
esac
# execute the backup command
#_echo2 "Executing: ${cmd}"
lock $lockdir/rdup
eval ${cmd}
unlock $lockdir/rdup
exit 0
