#! /bin/sh
# backup.sh - create multi-archive tape backups and print index
# Copyright (C) 2000 Ed Cashin
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# 

# BUGS: doesn't handle spaces in files that are in $dirlist

host=`hostname | sed 's/\..*//'`

# PRODUCTIONCONFIG: the list of directories to archive
dirlist='/boot /etc /lib /root /www/foo /www/bar /www/baz /home /adm /bin /var /sbin /usr /dev'

# PRODUCTIONCONFIG: a file containing files *not* to archive (see tar manpage)
excludelist=/adm/tools/tar_exclude-$host.txt

# PRODUCTIONCONFIG: make it "yes" to include sfdisk output in archives
do_sfdisk_dump=yes

tape=/dev/nst0

#-----------it wasn't finding sfdisk, so I'm specifying a path
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/adm/tools:

date=`date`

mt -f $tape rewind || {
    echo Error: could not rewind tape $tape 1>&2
    exit $?
}
cd / || {
    echo Error: could not chdir to / 1>&2
    exit 1
}

#-------print a header for the tape index
printf "%-30s   $host\n" "$date"
# label the blockcounts column
printf "%25s\t%s\n" "" "blockcounts"

# the following block is for a feature that Darrell requested:
# the sfdisk tool dumps partition info, and that info is the first
# thing on the tape.
# 
if [ "$do_sfdisk_dump" = "yes" ]; then
    tmpdir=/tmp/backup.sh-$$
    mkdir -p $tmpdir/sfdisk || {
	echo "Error: could not create tmpdir ($tmpdir/sfdisk): $?" 1>&2
	exit 1
    }
    chmod 700 $tmpdir

    # dump the partition info to a file
    echo "# $0"					   >  $tmpdir/sfdisk/dump
    echo "# output of 'sfdisk -d' on $host, $date" >> $tmpdir/sfdisk/dump
    sfdisk -d					   >> $tmpdir/sfdisk/dump

    # add the dump directory to the list of files to backup.
    dirlist="$tmpdir/sfdisk $dirlist"
fi

for target in $dirlist; do
    # beginning block number
    begin=`mt -f $tape tell`

    if [ "$target" = "" -o ! -d "$target" ]; then
	echo "Warning: skipping bad directory ($target)" 1>&2
	continue
    fi

    # print the beginning block on the tape; sed extracts the block number
    printf "%25s\t%s - " "$target" \
	      `mt -f $tape tell | sed 's/.* \([0-9][0-9]*\)\.$/\1/'`

    # remove initial slash, since we're in the root dir
    target="`echo $target | sed 's!^/!!'`"

    # write the archive to tape
    tar cfX $tape $excludelist "$target" || {
	echo "Error: tarring directory $target (continuing)" 1>&2
	continue
    }

    # print out the ending block for this archive
    printf "%s\n" `mt -f $tape tell | sed 's/.* \([0-9][0-9]*\)\.$/\1/'`
done

mt -f $tape rewind || {
    echo Warning: could not rewind tape $tape 1>&2
}

# print some directions for posterity
printf "\ndirections:\n    %s \\\ \n    %s\n" \
    "mt -f $tape seek {blockcount} && "		\
    "tar xvf $tape www/COE_WEB/index.html"

mt -f $tape offline

#---------cleanup the sfdisk dump temporary directory
#         (with some paranoia checks)
if [ "$do_sfdisk_dump" = "yes" ]; then
    if [ "$tmpdir" != "" -a -d "$tmpdir" ]; then
	rm -rf $tmpdir 1>&2
    fi
fi


