#!/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 restore check script
#	---------------------------
#
#	last edit-date: [Wed Jun 16 16:23:57 1999] by hm@hcs.de
#
#---------------------------------------------------------------------------

#===========================================================================
# configure section
#===========================================================================
ADMIN=operator	 			# who gets mail
TAPEDEV=/dev/nrsa0			# tape device to use
MAILPROG=/usr/bin/mail			# prog used to send mail
USRMTPROG=/usr/bin/mt			# prog used to control tape
RESTOREPROG=/sbin/restore		# prog used to read backup
RESTORELOG=/var/tmp/restore.log		# logfile for this script
USRRESTCHKOPTS="-tv"			# opts for prog used to read backup

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

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

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

#---------------------------------------------------------------------------
# restore failure - exit
#---------------------------------------------------------------------------
restorefail ()
{
	$MAILPROG -s "$HOST: restore failure" $ADMIN <<- EOF
		restore failure on $HOST: sysrestorechk script failed!
		could not read the tape.
		EOF
	exit 1
}

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

#---------------------------------------------------------------------------
# usage
#---------------------------------------------------------------------------
usage ()
{
	echo "sysrestorechk - perform dump tape checking using restore program"
	echo "usage: sysrestorechk -d [device] -f [filesys] -h [host]"
	echo "	     -d [device]  use tape device for restore"
	echo "	     -h [host]    restore from tape device on machine <host>"
	exit 1
}

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

UFILESYS=""
NL="
"

#---------------------------------------------------------------------------
# get commandline options
#---------------------------------------------------------------------------
args=`getopt d: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;;
		-h)
			REMOTE=$2
			shift; break;;
		--)
			shift; break;;
	esac
done

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

echo "" >$RESTORELOG
echo "-------- restore started at `date` ------------" >>$RESTORELOG

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

echo "     using tape device: $TAPEDEV" >>$RESTORELOG

if [ $REMOTE ]
then
	echo "        on remote host: $REMOTE" >>$RESTORELOG
else
	echo "         on local host: $HOST" >>$RESTORELOG
fi

#---------------------------------------------------------------------------
# prepare for reading restore
#---------------------------------------------------------------------------
$MTPROG -f $TAPEDEV rewind
if [ $? -ne 0 ]
then
	echo "inital tape rewind error for command: $MTPROG -f $TAPEDEV rewind" >>$RESTORELOG
	echo "-------- restore finished at `date` ------------" >>$RESTORELOG
	restoretapefailure
fi


#---------------------------------------------------------------------------
# check all filesystems dumped with restore
#---------------------------------------------------------------------------
while true
do
	$RESTOREPROG $RESTCHKOPTS >>$RESTORELOG 2>&1 
	if [ $? -ne 0 ]
	then
		echo "restore error for filesystem $filesys" >>$RESTORELOG
		echo "-------- restore finished at `date` ------------" >>$RESTORELOG
		restorefail
	fi

	$MTPROG -f $TAPEDEV fsf
	if [ $? -ne 0 ]
	then
		echo "tape error for command: $MTPROG -f $TAPEDEV fsf" >>$RESTORELOG
		echo "-------- restore finished at `date` ------------" >>$RESTORELOG
		restoretapefailure
	fi
done

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

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

success

exit 0

# EOF
