# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.
#ident	"$Header: backup 1.3 89/10/10 $@(#)sadmin:admin/menu/filemgmt/backup	2.15"
#menu# backup files from built-in disk to removable media
#help# 
#help#	Backup saves copies of files from the built-in disk file systems to
#help#	removable media.  There are two kinds of backups: COMPLETE
#help#	(copies all files; useful in case of serious file system damage)
#help#	and  INCREMENTAL (copies files changed since the last backup).
#help#	The normal usage is to do a complete backup of each file system
#help#	and then periodically do incremental backups.  We recommend keeping
#help#	2 cycles (one set of complete backups and several incrementals to
#help#	each cycle).
#help#	Files backed up with "backup" are restored using "restore".

SAVEDIR=/etc/save.d
if [ ! -d ${SAVEDIR}/timestamp ]
then
	mkdir ${SAVEDIR} ${SAVEDIR}/timestamp 2>/dev/null
fi

incrtime()
{
	INCRFILE=`echo ${fname} | sed 's%/\([^/]*\)%/\1.d%g; s%.d$%%'`
	cat ${SAVEDIR}/timestamp/${INCRFILE}
}

dir=${0}.d
if [ -d ${dir} ]
then
	patterns=`ls ${dir} 2>/dev/null`
else
	patterns=
fi
cpiocmd=cpio
cpioargs=

ncpiocmd=${cpiocmd}	# for backward compatibility
ncpioargs=${cpioargs}

flags="-qq -k$$"
trap 'exit 2' 1 2  15

fslist=`/etc/mount  |  sed 's/ .*//' | sort | sed '$a\\
ALL
' | pr -t -6 `
fs=`checklist ${flags} -fe -l ' ' -H '
	You may list more than one file system, or the special answer "ALL"
	which will backup all the file systems listed.' \
	"
Available file systems:
${fslist}
Enter file system(s) you want to backup [?, q]:" ${fslist}`
case "${fs}" in
ALL )
	fs=`echo ${fslist} | sed 's/ALL//'`
	;;
?*ALL | ALL*? )
	echo >&2 '	ALL must be used by itself.  Please try again.'
	exec sh $0
esac

mode=`checklist ${flags} -h\? \
		-H'
	A complete backup copies everything in case of a complete  loss  of
	information and an incremental backup copies files recently changed
	in case a file or a few files are lost.' \
	-pfe 'Select complete or incremental backup [c, i, ?, q]:' \
	complete incremental`
case "${mode}" in
complete )
	mode=c
	;;	
incremental )
	mode=i
	KILROY=0
	ERR=0
	for f in ${fs}
	{	
		NESTED=
		if [ ${f} = / ]
		then
			fname=:root:
		else
			fname=${f}
		fi
		if test "`dirname ${f}`" != "/"
		then
			NESTED=`echo ${f} | sed -e 's%/\([^/]*\)%/\1.d%g'`
			NESTED=`dirname ${NESTED}`/
		fi
		if [ ! -f ${SAVEDIR}/timestamp/${NESTED}`basename ${fname}`  -o  ! -s ${SAVEDIR}/timestamp/${NESTED}`basename ${fname}` ]
		then
			if [ $KILROY -eq 0 ]
			then echo >&2 "	The following file systems need to run a complete backup
	before an incremental backup can be run:\n"
			     KILROY=1
			fi
			echo >&2 "\t\t\t${f}"
			ERR=1
		fi
	}
	if [ $ERR -ne 0 ]
	then
		echo "\nPlease try again."
		exit 1
	fi
	;;
* )
	admerr $0 "Bad answer '${mode}'"
	exit 1
esac

verbose=
if  checkyn ${flags} -f -h\? -H'
	If you answer "yes" to this question, each file will be listed as it
	is being copied to the removable media.  This could be useful
	should you need to find a particular file later.
	If you answer "no", then only a dot, ".", will be printed for each file
	copied.' \
	'Print each file name as it is copied?'
then
	verbose=-v
fi

ddrive=`selectdevice -c $$ /dev/rSA ${patterns}`
ndrive=`drivename ${ddrive}`

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${ddrive}`
fi

case "${mode}" in
i )
	btype=Incremental
	today=`date +"%a. %D, %r"`
	msg=`echo "
Before inserting the next medium into the ${ndrive}, mark it:
"
	for f in ${fs}
	{
		if [ ${f} = / ]
		then
			fname=:root:
		else
			fname=${f}
		fi
		echo "	Incremental Backup of ${f},
	\`incrtime\` to
	${today}"
		}
	echo '		part %d\n'`
	;;
c )
	btype=Complete
	msg=`date +"
Before inserting the next medium into the ${ndrive}, mark it:

	Complete Backup of ${fs},
	%a. %D, %r
		part %%d
 "`
esac

echo "${msg}"  |  sed '	s/the next /the first /
			s/%d/1/'
l=`disklabel -n "${ndrive}" $$ ${ddrive}`
eval `labelfsname "${l}"`
if [ -n "${fsname}" -a -d "/${fsname}" ]
then
	echo "
This is file system '${fsname}', labeled '${label}'."
	if  checkyn ${flags} -f 'Do you wish to overwrite it? [q]'
	then
		:
	else
		echo "\\tGet another and start over."
		exit 1
	fi
fi

if  /etc/savecpio -d "${ddrive}" -n "${ndrive}" -${mode} ${verbose} \
	-C "${cpiocmd} ${cpioargs}" -M "${msg}" ${fs}
then
	echo "\\n${btype} backup of " ${fs} " finished."
	echo 'You may now remove the medium.'
	exit 0
else
	echo "\\t${btype} backup of " ${fs} " did not work."
	echo 'You may now remove the medium.'
	exit 1
fi
