#!/bin/sh
exec 2>&1
# 
# This assumes all files have been copied over and a file exists called
# /root/filestobackup, make note of trailing slashes as they affect tar output.
#
# Features:
# logs to syslog and uses indentation for easy reading of logs
# checks to make sure each step worked correctly
# emails administrators with notifications of failure(s) or success and size of
# backup to monitor long term usage
# compiles all files first, then writes to tape (faster)
# trap statements to clean up properly if kill'd
#

# Variables and trap statements (just in case)
ADMINS="jason.hammerschmidt@maclaren.com, hammerschmidt@home.com"
COMPILEDIR="/tmp/backup$$"
TAPEDEV="/dev/st0"
trap 'rm -f $COMPILEDIR >/dev/null 2>&1' 0
trap "exit 2" 1 2 3 13 15

wall "Starting automated backup process, expect slow down of system..."
mkdir $COMPILEDIR
cd $COMPILEDIR
logger "Starting backup to tape script..."
echo "Starting backup to tape script..."
echo "We'll be backing up the following files and directories:"
cat /root/filestobackup

logger " Starting tarring files..."
tar cvf $COMPILEDIR/`date -I`.tar `cat /root/filestobackup`
        RESULT=$?
        if [ $RESULT -ne 0 ] ; then
                logger "Failure in backup script couldn't run tar" $RESULT
		echo "tar puked this " $RESULT | mail -s "Failure in tape backup script could not run tar" $ADMINS
                exit "Failure doing tar" $RESULT
	fi
logger " Finished tarring files..."

logger " Starting to bzip2 tar files..."
echo "Finished tarring files, now we will bzip2 them"
bzip2 $COMPILEDIR/`date -I`.tar
        RESULT=$?
        if [ $RESULT -ne 0 ] ; then
                logger "Failure in backup script couldn't bzip tar" $RESULT
                echo "bzip2 puked this " $RESULT | mail -s "Failure in tape backup script could not bzip tar file" $ADMINS
                exit "Bzip2 failied " $RESULT
        fi
logger " Finished zipping files..."
echo "Finished zipping files... Beginning tape functions"
logger " Begining tape functions..."

# uncomment this section if you have to erase your tapes first.  Some tapes
# need erasing first, most dont.
#logger "  Erasing Tape contents..."
#mt -f /dev/st0 erase
#        RESULT=$?
#        if [ $RESULT -ne 0 ] ; then
#                logger "Failure in backup script"
#                echo $RESULT | mail -s "Failure in tape backup script" $ADMINS
#                exit $RESULT
#        fi
#logger "  Done Erasing Tape contents..."

logger "  Copying files to Tape..."
tar cvf $TAPEDEV $COMPILEDIR/`date -I`.tar.bz2
        RESULT=$?
        if [ $RESULT -ne 0 ] ; then
                logger "Failure in backup script"
                echo $RESULT | mail -s "Failure in tape backup script" $ADMINS
                exit $RESULT
        fi
logger " Finished writing to tape successfully..."
echo "cleaning up..."

SIZE=`ls -la $COMPILEDIR/\`date -I\`.tar.bz2`
echo "Successfully completed backup script, size of backup was" $SIZE | mail -s "Successfully completed backup script" $ADMINS

rm -fr $COMPILEDIR/
logger "Finished back to tape script sucessfully."
echo "Done."
