#!/bin/sh
#
# incremental backup script called from uback
# $Revision: 1.5 $


if [ "x$1" = "x--help" ]; then
    echo "incremental: does an incremental backup of the specified directories."
    echo "             it must run daily, on sunday a complete full backup will"
    echo "             be created but overwritten on every sunday. on all other"
    echo "             days only changes to the day before will be backed up."
    echo 
    echo "             configuration should be in /etc/uback.d/incremental."
    echo
    exit 1;
else
    . $1
fi

if [ "x$confdir" = "x" ]; then
    echo "Error: configfile \"$1\" missing or invalid!"
    exit 1;
else
    . $confdir/incremental
fi


etc=$base/etc
state=$base/state
log=$tarlog
incr=$backup/incremental
weekday=`$date +%w`
fulldate=`$date +%d-%b`
day=`$date +%A`
case $tarcompress in
    "-z")
	suffix="gz"
	;;
    "-I")
	suffix="bz2"
	;;
    "-Z")
	suffix="Z"
	;;
    "")
	suffix=""
	;;
    "*")
	suffix="gz"
	tarcompress="-z"
	;;
esac


trap '$rm -f $incr/$tardir/$weekday.tar.$suffix $log/incremental.$tardir.$weekday.log; echo "abort."; exit 127' 1 2 3 15

for dir in $backupdirs;
do
	tardir=`echo $dir | $sed s'/^\///' | $sed s'/\//_/g'`
	if [ $weekday = "0" ]; then
	    $install -d $incr/$tardir
	    # or use a tape drive: find $dir/ | afio -o /dev/rmt0
	    decho "   => $tar $taroptions $tarcompress -c -f $incr/$tardir/$day.tar.$suffix $dir"
	    $tar $taroptions $tarcompress -c -f $incr/$tardir/$day.tar.$suffix $dir > $log/incremental.$tardir.$day.log 2>&1
	    size=`$du -h $incr/$tardir/$day.tar.$suffix | $cut -f 1`
	    decho "      saved full $dir to $incr/$tardir/$day.tar.$suffix ($size)"
	    decho "      log is in $log/incremental.$tardir.$day.log"
	else
	    $install -d $incr/$tardir
	    # or use a tape drive: find $dir/ | afio -o /dev/rmt0
	    decho "   => $tar $taroptions $tarcompress -c --newer `$cat $state/incr.fulldate` -f $incr/$tardir/$day.tar.$suffix $dir"
	    $tar $taroptions $tarcompress -c --newer `$cat $state/incr.fulldate` \
		-f $incr/$tardir/$day.tar.$suffix $dir > $log/incremental.$tardir.$day.log 2>&1
	    size=`$du -h $incr/$tardir/$day.tar.$suffix | $cut -f 1`
	    decho "      saved $dir to $incr/$tardir/$day.tar.$suffix ($size)"
	    decho "      log is in $log/incremental.$tardir.$day.log"
	fi
	if [ "x$dbtool" != "x" ]; then
	    $rm -f $incr/$tardir/$day.catalog
	    $find $dir -newer $state/incr.fulldate -ls | $dbtool -d $incr/$tardir/$day.catalog -i -f -R -t "^(.+?) (\/.*)$"
	fi
done
echo $fulldate > $state/incr.fulldate
touch -d $fulldate $state/incr.fulldate