:
#	incbru    bru incremental backup script  
#
#  RCS CHANGE LOG 
#
#	incbru,v
# Revision 1.3  1994/05/07  23:09:57  jeff
# Added RCS keywords.
#	
#
#  DESCRIPTION
#
# 	This script will perform different levels of incremental
#	backups using bru.
#
#	It will backup all files that have changed since doing a
#	backup with a lower level.
#
#	If no argument is specified, it will do a level 1 backup
#	and will backup all files since the last full (level 0)
#	backup.  If the level 0 backup does not exist, it will
#	do that instead.
#
#  NOTES
#
#	Feel free to customize this script to handle your
#	specific backup requirements.  This script is designed
#	to do basic backups of the entire system from the root
#	directory. 
#	
#

BRUDEV=""			# name of backup device, use default if 
				# none is specified

BRUOPTS="-vvvv"			# bru options

DIRS="/"			# directories or filesystems to back up

LOGFILE=""			# log file

MAILTO=root			# where to mail the results

case "$#" in
    0)
	LVL=1		# no argument, so do a level 1 backup
	;;
    1)
	LVL=$1
	;;
    *)
	echo "usage: incbru [level]"
	exit 1
	;;
esac



#
# check for legal backup level
#
case $LVL in
	0|1|2|3|4|5|6|7|8|9)
		;;
	*)
		echo "incbru: $LVL is an illegal backup level"
		exit 1
	;;
esac

#
# if level 0, do a full backup
#
DATEFILE=""
if [ "$LVL" -eq 0 ]
then
	DATEFILE=""
else
	PREVLVL=`expr $LVL - 1`		# previous backup level	
	echo "PREVLVL = $PREVLVL"
	#
	#  find date marker file for previous level backup
	#
	i=0
	while [ "$i" -le "$PREVLVL" ]	
	do
		FILE=/etc/BRULEVEL$i
		if [ -f "$FILE" ]
		then
			DATEFILE=$FILE
			if [ "/etc/BRULEVEL${PREVLVL}" = "$FILE" ]
			then
				break	# found previous date marker file
			fi
		fi

		i=`expr $i + 1`
	done 
fi

if [ "$DATEFILE" = "" ]
then
	if [ "$LVL" -ne 0 ]
	then
		echo "incbru: No previous backup less than Level $LVL"
		LVL=0
	fi
	echo "Performing Full Backup (Level 0)"
else
	echo "Performing Level $LVL Backup"
	BRUOPTS="$BRUOPTS -n $DATEFILE"
fi

#
#  set up bru command that will do the backup, use default device
#  from brutab if none is specified
#
if [ "$BRUDEV" != "" ]
then
	BRUCMD="bru -c $BRUOPTS -f $BRUDEV $DIRS"
else
	BRUCMD="bru -c $BRUOPTS $DIRS"		# use default device 
fi

#
#  create a temporary date marker file.  if the backup is
#  successful, this will be renamed
#
MARKER=/etc/BRULEVEL${LVL}
TMP=$MARKER.err
TS=`date`
echo "$TS - Started BRU Level $LVL Backup\n\tcommand = '$BRUCMD'" > $TMP 


#
#  change to the root directory and execute the bru backup.  write
#  output to log file if one is specified
#
BRUEXIT=2
cd /	
if [ "$LOGFILE" != "" ]
then
	$BRUCMD > $LOGFILE 2>&1
	BRUEXIT=$?
else
	$BRUCMD 2>&1
	BRUEXIT=$?
fi


#
#  check the exit code.  if greater than or equal to 2, we've
#  got a problem
#
TS=`date`
if [ "$BRUEXIT" -ge 2 ]
then
	echo "incbru: ERROR in BRU Backup"
	echo "$TS - ERROR IN BRU BACKUP, check log file: '$LOGFILE'" >> $TMP 
	mail $MAILTO < $TMP 
	exit 2
else
	if [ "$BRUEXIT" -eq 0 ]
	then
		echo "BRU Level $LVL Backup successful"
	else
		# exit code = 1
		echo "BRU Level $LVL Backup completed (with warnings)"
	fi
	mv $TMP $MARKER 
	echo "$TS - BRU Level $LVL Backup completed, results in log file: '$LOGFILE'" | mail $MAILTO
	exit 0
fi
