#!/bin/bash
#Full Backup of rkirkpat.net
#$Header: /home/Data/Scripts/RCS/backup.full.script.sh,v 1.13 1997/07/29 13:24:20 rkirkpat Exp $

# Copyright (c) 1997 by Ryan Kirkpatrick (rkirkpat@nag.cs.colorado.edu). 
#   All rights reserved.
#
# 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; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# 

# Turn debugging on.
# set -x

# Make pwd known.
cd /root

# Initialize tape drive for backup.
if [ "`mt rewind 2>&1`" = "/dev/tape: I/O error" ]; then
    echo "There is no tape in the drive!!!"
    exit
fi
mt erase
mt rewind

# Clean out the catalog directory
rm -rf /home/backup/catalogs/*

# Define some useful variables
ts=`date`
tss=`date +%Y%m%d%H%M%S`
hn=`hostname`

# Annouce our start.
echo "Full Backing Beginning at $ts ...."

# Put a header file on tape.
cat <<EOF > tape_header
Rkirkpat.net Backup Tape
Created at $ts
Created by $hn
EOF
echo "tape_header" | afio -oz /dev/nst0
currentpos=`mt tell | sed 's/At block //' | sed 's/\.//'`

# Cataloging
mv tape_header /home/backup/catalogs/tape_header.$tss
echo "0 : Tape Header, $ts" | cat >> /home/backup/catalogs/_tape_catalog

# Do a full backup of the local system.

  # Load information for backup
  fs=`cat ./backup_info/$hn`

  # Create backup volume header
  cat <<EOF  > zzzz_volume_header
Rkirkpat.net Full Backup Volume
Conents: $hn, $fs
Created at $ts
Created by $hn
EOF

  # Perform the backup
  echo -n "    $hn : $fs : $ts : "
  (echo "zzzz_volume_header"; find $fs -xdev -print) | afio -oz /dev/nst0

  # Catalog / Verify
  echo "$currentpos : Full Volume for $hn, $fs, $ts" | cat >> /home/backup/catalogs/_tape_catalog
  mv zzzz_volume_header /home/backup/catalogs/$hn.header.full.$tss

  newpos=`mt tell | sed 's/At block //' | sed 's/\.//'`
  mt seek $currentpos
  afio -t /dev/nst0 > /home/backup/catalogs/$hn.catalog.full.$tss
  mt seek $newpos
  currentpos=$newpos

# Prepare for network backup
cp -f /etc/pam.conf.backup /etc/pam.conf
mv -f ./.rhosts.backup ./.rhosts

# Now backup of the remainder of the systems.
for machine in `ls ./backup_info/`; do
  if [ $hn != $machine ]; then
    # Shorten host name
    machshort=`echo $machine | sed 's/\..*//'`

    # Double check that the machine is mounted.
    status=`mount | grep $machshort | sed 's/:\/.*//'`
    if [ "$status" = $machshort ]; then

	# Reset ts so we have a current time.
	ts=`date`
	tss=`date +%Y%m%d%H%M%S`
    
	# Prepare other machine for rsh.
	cp -f /mnt/$machshort/etc/pam.conf.backup /mnt/$machshort/etc/pam.conf
	mv -f /mnt/$machshort/root/.rhosts.backup /mnt/$machshort/root/.rhosts

	# Load information for backup.
	fs=`cat ./backup_info/$machine`

	# Create backup up volume header.
	cat <<EOF  > /mnt/$machshort/root/zzzz_volume_header
Rkirkpat.net Full Backup Volume
Conents: $machine, $fs
Created at $ts
Created by $hn
EOF

	# Preform the backup
	echo -n "    $machine : $fs : $ts : "
	rsh $machshort "(" echo "zzzz_volume_header" ";" find $fs -xdev -print ")"  "|" afio -oz $hn:/dev/nst0

	# Catalog / Verify
        echo "$currentpos : Full Volume for $machine, $fs, $ts" | cat >> /home/backup/catalogs/_tape_catalog
	mv -f /mnt/$machshort/root/zzzz_volume_header /home/backup/catalogs/$machine.header.full.$tss

	newpos=`mt tell | sed 's/At block //' | sed 's/\.//'`
	mt seek $currentpos
	afio -t /dev/nst0 > /home/backup/catalogs/$machine.catalog.full.$tss
	mt seek $newpos
	currentpos=$newpos

	# Cleanup
	cp -f /mnt/$machshort/etc/pam.conf.real /mnt/$machshort/etc/pam.conf
	mv -f /mnt/$machshort/root/.rhosts /mnt/$machshort/root/.rhosts.backup
    fi

  fi
done

# Write out where we stopped using the tape.
echo $currentpos > /home/backup/catalogs/_last_block

# Clean up
cp -f /etc/pam.conf.real /etc/pam.conf
mv -f ./.rhosts ./.rhosts.backup

mt -f /dev/nst0 rewind

#End of run...
ts=`date`
echo "Full Backup Complete at $ts ...."

#EOF








