:
#	@(#) backup.sh 1.6 89/08/25 
#
#
#    Portions Copyright 1983-1989 The Santa Cruz Operation, Inc
#		      All Rights Reserved
#	Copyright (c) 1984, 1986, 1987, 1988 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.


#	Backup script 
#	Options:
#	c - complete backup
#	p - incremental backup
#	h - backup history
#	u "user1 [user2]" - backup users (use "all" to backup all users)
#	t - backup done to tape; only used if backing up to tape
#	d - special device name; defaults to /dev/rfd096ds15
#	f "<files>" - backup by file name (argument must be in quotes)

USAGE="$0 -d "device" [ -t ] [ -c | -p | -u [user] | -f <files> ]\n $0 -h"
MEDIA="floppy disk"
CPIO=cpio
CPIOFLGS="-ocB"
DEV=""
BTYPE=""
SIZE=""
trap "cd /; rm -f /tmp/FILE$$ /tmp/VFILE$$ /tmp/flp_index; echo \"\nYou have \
cancelled the Backup\n\"; exit 1 " 1 2 3 9 15

dofind() {
	find $1 $2 -print 2>/dev/null | while read file
	do
	if [ -f $file ]
		then du -a $file
	elif [ -d $file ]
		then echo "D\t$file"
	fi
done >> /tmp/FILE$$
}

while getopts cd:f:hptu: c
do
	case $c in
	c)	type=c
		BTYPE="Complete System"
		;;
	p)	type=p
		BTYPE="Incremental System"
		;;
	d)	DEV="$OPTARG"
		;;
	t)	MEDIA="cartridge tape"
		CPIOFLGS="-ocB -C 10240"
		;;
	h)	type=h;
		;;
	u)	type=u
		user="$OPTARG"
		if [ -n "$FILES" ]
		then
			echo "Error: -u and -f options can't be used together"
			exit 1
		fi
		BTYPE="User"
		;;
	f)	type=f
		FILES="$OPTARG"
		if [ -n "$user" ]
		then
			echo "Error: -u and -f options can't be used together"
			exit 1
		fi
		BTYPE="Selective"
		;;
	\?)	echo "$0: $USAGE"
		exit 0
		;;
	*)	echo "$0: $USAGE"
		exit 1
		;;
	esac
done

if [ "$MEDIA" = "cartridge tape" -a ! "$DEV" ] 
then
	echo "$0 : $USAGE"
	echo "Must use -d with -t option."
	exit 1
fi

case "$type" in

	"")
		echo "$0: $USAGE"
		exit 1
		;;
	h)
		
		if [ -s /etc/.lastbackup ]
		then
			echo "Last complete backup done on `cat /etc/.lastbackup`"
		else
			echo "No complete backup has been done."
		fi
		if [ -s /etc/.lastpartial ]
		then
			echo "Last incremental backup done on `cat /etc/.lastpartial`"
		else
			echo "No incremental backup has been done."
		fi
		exit 0
		;;
esac


shift `expr $OPTIND - 1`

if [ ! "$DEV" ]
then
	while true
	do
		echo "Press ENTER to use default : Floppy Drive 0 (/dev/rfd0)."
		echo "Enter valid floppy drive number if different."
		echo "> \c"
		
		read ans
		case $ans in
		""|0)
			FDRIVE=/dev/rfd0
			break
			;;
		1)
			FDRIVE=/dev/rfd1
			[ -w $FDRIVE ] && break
			;;
		*)
			FDRIVE=/dev/rfd${ans}
			[ -w $FDRIVE ] && break
			;;
			esac
		done

		DEV="$FDRIVE"96ds15
		SIZE=1200
fi

if [ ! "$SIZE" ]
then
	echo "Enter size of media in 1K-byte units > \c"
	read SIZE
fi

MESSAGE="You may remove the $MEDIA. To exit, press 'q' followed by ENTER.
To continue, insert $MEDIA number %d and press the ENTER key. "

UID=`id | cut -c5`
if [ "$UID" != "0" ]
then
	RNAME=`id | cut -d\( -f2 | cut -d\) -f1`
	if [ "$type" = "c" -o "$type" = "p" ]
	then
		echo "\"$RNAME\" may only select 'Backup History' \
or 'Private Backup'."
		exit 1
	fi
	if [ \( "$type" = u -a "$user" = all \) ]
	then
		echo "\"$RNAME\" may only select 'Backup History' \
or 'Private Backup'."
		exit 1
	fi
fi

case $type in
	c)
#	handle complete backup
		DIR=/
		TIR=/
		NEWER="-newer /etc/.installdate"
		SNEWER=
		if [ -s /etc/.installdate ]
		then 
			NEWER="-newer /etc/.installdate"
		else 
			NEWER="-newer /unix"
		fi
		if [ -s /etc/.installstart ]
		then 
			SNEWER="! -newer /etc/.installstart"
		fi
		file2=/etc/.lastbackup
		;;
	p)
#		set up for an incremental update
#		assumption: date IN the file is the same as date OF the file
		if [ ! -s /etc/.lastbackup ]
		then
			echo "A complete backup up must be done before the Incremental backup."
			exit 1
		fi
		if [ ! -s /etc/.lastpartial ]
		then 
			NEWER="-newer /etc/.lastbackup"
		else 
			NEWER="-newer /etc/.lastpartial"
		fi
		file2=/etc/.lastpartial
		TIR=/
		DIR=/
		;;
	f)
		DIR="$FILES"
		NEWER=
		TIR=
		;;
	u)
		DIR=
		if [ "$user" = all ]
		then
			user="`pwdmenu`"
			if [ "Name=N O N E" = "$user" ]
			then
			   echo "There are no users to backup"
			   exit 1
			fi
			for i in `pwdmenu`
			do
				T=`echo $i | cut -d= -f2`
				T=`grep "^$T:" /etc/passwd | cut -d: -f6`
				DIR="$DIR $T"
			done
			TIR=/usr
		else
			for i in $user
			do
				TDIR=`grep "^$i:" /etc/passwd | cut -d: -f6`
				if [ -z  "$TDIR" ]
				then
					echo "$i doesn't exist"
					continue
				else
					DIR="$DIR $TDIR"
				fi
			done
			TIR=/usr
		fi
		;;
	*)	# THIS CASE SHOULDN'T BE REACHED
		echo "Invalid type $type"
		echo "$0: $USAGE"
		exit 1
		;;
esac

message -d "Calculating approximate number of $MEDIA(s) required. Please wait."

#  Compute Blocks, number of floppies etc ..
>/tmp/FILE$$
#       dofind $DIR "$NEWER"
find $DIR $NEWER -print 2>/dev/null | while read file
do
	if [ -f $file ]
 		then du -a $file
 	elif [ -d $file ]
 		then echo "D\t$file"
 	fi
done >> /tmp/FILE$$
 
if [ -n "$SNEWER" ]
then
#	dofind $DIR "$SNEWER"
 	find $DIR $SNEWER -print 2>/dev/null | while read file
 	do
 		if [ -f $file ]
 			then du -a $file
 		elif [ -d $file ]
 			then echo "D\t$file"
 		fi
 	done >> /tmp/FILE$$
fi

grep -v "	/usr/spool/locks" /tmp/FILE$$ > /tmp/VFILE$$
mv /tmp/VFILE$$ /tmp/FILE$$
grep -v "	/tmp/FILE$$" /tmp/FILE$$ > /tmp/VFILE$$
mv /tmp/VFILE$$ /tmp/FILE$$
grep -v "	/etc/.installstart" /tmp/FILE$$ > /tmp/VFILE$$
mv /tmp/VFILE$$ /tmp/FILE$$
grep -v "^D" /tmp/FILE$$ > /tmp/VFILE$$
cp /tmp/FILE$$ /tmp/flp_index
if [ ! -s /tmp/VFILE$$ ]
then
	if [ "$type" != "u" ]
	then
		if [ ! -s /etc/.lastpartial ]
		then 
			set `cat -s /etc/.lastbackup`
		else 
			set `cat -s /etc/.lastpartial`
		fi
		message -d "There are no new files since \
the last Incremental Backup on $2 $3.  Therefore no Backup will be done."
	else
		echo  "There are no files to be backed up."
	fi
	rm -f /tmp/FILE$$ /tmp/VFILE$$ /tmp/flp_index
	exit 0
fi

BLOCKS=`cut -f1 < /tmp/VFILE$$ | sed -e '2,$s/$/+/' -e '$s/$/p/'| dc`

cut -f2 /tmp/FILE$$ > /tmp/VFILE$$

# Block maxs are:
# 702 for 360K, 1422 for 720K, 2370 for 1.2MB and 2844 for 1.44MB floppies
# 131072 for the 60MB cartridge tape
# 257000 for the 125MB cartridge tape. This also allows for cpio overhead.

if [ "$MEDIA" = "cartridge tape" ]
then
	CTAPEA=`expr \( $BLOCKS / 131072 \) + \( 1 \& \( $BLOCKS % 131072 \) \)`
	CTAPEB=`expr \( $BLOCKS / 257000 \) + \( 1 \& \( $BLOCKS % 257000 \) \)`
	TIME=`expr ${CTAPEA} \* 30`
	echo /tmp/flp_index > /tmp/FILE$$
	cat /tmp/VFILE$$ >> /tmp/FILE$$
	echo "BLOCKS=$BLOCKS" >> /tmp/flp_index
	rm -f /tmp/VFILE$$
	message "The backup will need approximately:\n\
$CTAPEA cartridge tape(s) for a 60 MB drive or \n\
$CTAPEB cartridge tape(s) for a 125 MB drive \n\
and will take no more than $TIME minute(s).\n\n\
Please insert the first cartridge tape.  Be sure to number the cartridge tape(s) \
consecutively in the order they will be inserted."
else
	FP5D9=`expr \( $BLOCKS / 690 \) + \( 1 \& \( $BLOCKS % 690 \) \)`
	FP5H=`expr \( $BLOCKS / 2360 \) + \( 1 \& \( $BLOCKS % 2360 \) \)`
	FP3H=`expr \( $BLOCKS / 2830 \) + \( 1 \& \( $BLOCKS % 2830 \) \)`
	FP3Q=`expr \( $BLOCKS / 1410 \) + \( 1 \& \( $BLOCKS % 1410 \) \)`
	TIME=`expr \( ${FP5D9}0 / 15 \) + \( 1 \& \( ${FP5D9}0 % 15 \) \)`
	echo /tmp/flp_index > /tmp/FILE$$
	cat /tmp/VFILE$$ >> /tmp/FILE$$
	echo "BLOCKS=$BLOCKS" >> /tmp/flp_index
	rm -f /tmp/VFILE$$
	message "The backup will need approximately:\n\
$FP5H formatted 1.2MB 5.25\" floppy disk(s) or\n\
$FP5D9 formatted 360KB 5.25\" floppy disk(s) or\n\
$FP3H formatted 1.44MB 3.5\" floppy disk(s) or\n\
$FP3Q formatted 720MB 3.5\" floppy disk(s).\n\
and will take no more than $TIME minute(s).\n\n\
Please insert the first floppy disk.  The floppy disk(s) you are using \
for the backup MUST be formatted.  Be sure to number the floppy disks \
consecutively in the order they will be inserted."
fi

echo "\n$BTYPE backup in progress\n\n\n"

cat /tmp/FILE$$ | ${CPIO} ${CPIOFLGS} -M"$MESSAGE" -O $DEV -K$SIZE
err=$?
if [ "$err" = "4" ] 
then
	message -d "You have cancelled the Backup to $MEDIA(s)."
elif [ "$err" != "0" ] 
then
	if [ "$MEDIA" = "cartridge tape" ]
	then
		message -d "An error was encountered while writing to the ${MEDIA}. \
Please be sure the $MEDIA is inserted properly, and wait for the notification \
before removing it."
	else
		message -d "An error was encountered while writing to the ${MEDIA}. \
Please be sure all your $MEDIA(s) are formatted, and wait for the notification \
before removing them."
	fi
else
	if [ $type = c ]
	then
		message -d "The Complete Backup is now finished."
	elif [ $type = p ]
	then
		set `cat -s /etc/.lastbackup`
		eval M=$2 D=$3
		if [ "$NEWER" = "-newer /etc/.lastbackup" ]
		then 
			set `cat -s  /etc/.lastbackup`
		else 
			set `cat -s  /etc/.lastpartial`
		fi
		message -d  "The incremental backup of the files that have changed \
since the last backup, on $2 $3, is now finished.\n\n\
In order to restore the system totally, first restore the last \
complete backup dated $M $D, and then restore the incremental backup(s) that \
you have performed since $M $D starting with earliest one and \
ending with the one you have just finished."
	else
		message -d "Backup is now completed."
	fi
	if [ "$TIR" = "/" ]
	then
		if [ "$file2" = "/etc/.lastbackup" ]
		then 
			rm -f /etc/.lastpartial
		fi
		date > $file2
	fi
fi
rm -f /tmp/FILE$$ /tmp/flp_index
