#!/bin/sh
#-------------------------------------------------------------------------------
#
#	Copyright (c) 1999 HCS Hanseatischer Computerservice GmbH
#
#	All rights reserved.
#
#	Redistribution and use in source and binary forms, with or without
#	modification, are permitted provided that the following conditions
#	are met:
#	1. Redistributions of source code must retain the above copyright
#	   notice, this list of conditions and the following disclaimer.
#	2. Redistributions in binary form must reproduce the above copyright
#	   notice, this list of conditions and the following disclaimer in the
#	   documentation and/or other materials provided with the distribution.
#
#	THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#	ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#	ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#	FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#	DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#	LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#	OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#	SUCH DAMAGE.
#
#---------------------------------------------------------------------------
#
#	system backup script
#	--------------------
#
#	last edit-date: [Wed Jun 16 16:23:30 1999] by hm@hcs.de
#
#---------------------------------------------------------------------------

#===========================================================================
# configure section
#===========================================================================
TAPEDEV=/dev/nrsa0			# tape device to use
ADMIN=operator	 			# who gets mail
MAILPROG=/usr/bin/mail			# prog used to send mail
DUMPLOG=/var/tmp/dump.log		# logfile for this script
USRMTPROG=/usr/bin/mt			# prog used to control tape
DUMPPROG=/sbin/dump			# prog used to write backup
USRDUMPOPTS="-0 -a -n -u"		# opts for prog used to write backup

#---------------------------------------------------------------------------
# nonconfigure section
#---------------------------------------------------------------------------
REMOTE=
HOST=`hostname` 		        

#===========================================================================
# subroutines
#===========================================================================

#---------------------------------------------------------------------------
# dump tape failure - exit
#---------------------------------------------------------------------------
dumptapefailure () 
{
	$MAILPROG -s "$HOST: dump tape failure" $ADMIN <<- EOF
		dump failure on $HOST: backup script failed! 
		apparently there was no tape in the device.
		EOF
	exit 1
}

#---------------------------------------------------------------------------
# dump failure - exit
#---------------------------------------------------------------------------
dumpfail ()
{
	$MAILPROG -s "$HOST: dump failure" $ADMIN <<- EOF
		dump failure on $HOST: backup script failed!
		could not write to the tape.
		EOF
	exit 1
}

#---------------------------------------------------------------------------
# Success
#---------------------------------------------------------------------------
success ()
{
	$MAILPROG -s "$HOST: backup completed successfully" $ADMIN <<- EOF
		successfully dumped filesystems on $HOST!
		the content of the /etc/dumpdates file is:
		`/bin/cat /etc/dumpdates`
		EOF
	echo "-------- backup finished at `date` ------------" >>$DUMPLOG
}

#---------------------------------------------------------------------------
# usage
#---------------------------------------------------------------------------
usage ()
{
	echo "sysbackup - perform system backup using dump program"
	echo "usage: sysbackup -d [device] -f [filesys] -h [host]"
	echo "	     -d [device]  use tape device for backup"
	echo "	     -f [filesys] backup filesystem matching <filesys> (repeatable)"
	echo "	     -h [host]	  backup to tape device on machine <host>"
	exit 1
}

#===========================================================================
# main script entry
#===========================================================================

UFILESYS=""
NL="
"

#---------------------------------------------------------------------------
# get commandline options
#---------------------------------------------------------------------------
args=`getopt d:f:h: $*`

#---------------------------------------------------------------------------
# check for error
#---------------------------------------------------------------------------
if [ $? != 0 ]
then
	usage
fi

#---------------------------------------------------------------------------
# process commandline options 
#---------------------------------------------------------------------------

set -- $args

for i
do
	case "$i"  
	in
		-d)
			TAPEDEV=$2
			shift; break;;
		-f)
			if [ "$UFILESYS" = "" ]
			then
				UFILESYS=$2
			else
				UFILESYS="$UFILESYS$NL$2"
			fi
			shift; shift;;
		-h)
			REMOTE=$2
			shift; break;;
		--)
			shift; break;;
	esac
done

#---------------------------------------------------------------------------
# prepare for a new logfile
#---------------------------------------------------------------------------
if [ -f $DUMPLOG ]
then
	mv -f $DUMPLOG $DUMPLOG.previous
fi

echo "" >$DUMPLOG
echo "-------- backup started at `date` ------------" >>$DUMPLOG

#---------------------------------------------------------------------------
# prepare list of filesystems to dump
#---------------------------------------------------------------------------
if [ "$UFILESYS" = "" ]
then
	FILESYS=`cat /etc/fstab | 
		 sed -e "/swap/d" | 
		 awk '/da[0-9]|wd[0-9]/ {print $1}' | 
		 sort -u`
else
	FILESYS=`cat /etc/fstab | 
		 sed -e "/swap/d" | grep -F "$UFILESYS" |
		 awk '/da[0-9]|wd[0-9]/ {print $1}' | 
		 sort -u`
fi

#---------------------------------------------------------------------------
# prepare commands in case we are dumping to a remote machine
#---------------------------------------------------------------------------
if [ $REMOTE ]
then
	DUMPOPTS="$USRDUMPOPTS -f $REMOTE:$TAPEDEV"
	MTPROG="rsh $REMOTE $USRMTPROG"
else
	DUMPOPTS="$USRDUMPOPTS -f $TAPEDEV"
	MTPROG="$USRMTPROG"
fi

count=0
echo -n "backing up filesystems:" >>$DUMPLOG
for filesys in $FILESYS
do
	echo -n "`echo -n $filesys | tr -d \n`" >>$DUMPLOG
	count=`expr $count + 1`
	if [ `expr $count % 4` = 0 ]
	then 
		echo "" >>$DUMPLOG
		echo -n "                       " >>$DUMPLOG
	fi
done
echo "" >>$DUMPLOG
echo "     using tape device: $TAPEDEV" >>$DUMPLOG
if [ $REMOTE ]
then
	echo "        on remote host: $REMOTE" >>$DUMPLOG
else
	echo "         on local host: $HOST" >>$DUMPLOG
fi

#---------------------------------------------------------------------------
# prepare for writing backup
#---------------------------------------------------------------------------
$MTPROG -f $TAPEDEV rewind
if [ $? -ne 0 ]
then
	echo "inital tape rewind error for command: $MTPROG -f $TAPEDEV rewind" >>$DUMPLOG
	echo "-------- backup finished at `date` ------------" >>$DUMPLOG
	dumptapefailure
fi

#---------------------------------------------------------------------------
# dump all filesystems to tape
#---------------------------------------------------------------------------
for filesys in $FILESYS
do
	$DUMPPROG $DUMPOPTS $filesys >>$DUMPLOG 2>&1 
	if [ $? -ne 0 ]
	then
		echo "dump error for filesystem $filesys" >>$DUMPLOG
		echo "-------- backup finished at `date` ------------" >>$DUMPLOG
		if [ $? -eq 1 ]
		then
			dumptapefailure
		else
			dumpfail
		fi
	fi
done

#---------------------------------------------------------------------------
# finish & cleanup
#---------------------------------------------------------------------------

$MTPROG -f $TAPEDEV rewind
if [ $? -ne 0 ]
then
	echo "final tape rewind error for command: $MTPROG -f $TAPEDEV rewind" >>$DUMPLOG
	echo "-------- backup finished at `date` ------------" >>$DUMPLOG
	dumptapefailure
fi

success

exit 0

# EOF
