#!/bin/bash
#Inc Backup of rkirkpat.net
#$Header: /home/Data/Scripts/RCS/backup.inc.script.sh,v 1.12 1997/07/29 13:24:14 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 off an 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
currentpos=`cat /home/backup/catalogs/_last_block`
mt seek $currentpos

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

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

# Do a inc 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 Inc 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 -mtime -1 -print) | afio -oz /dev/nst0

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

  newpos=`mt tell | sed 's/At block //' | sed 's/\.//'`
  mt seek $currentpos
  afio -t /dev/nst0 > /home/backup/catalogs/$hn.catalog.inc.$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 Inc 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 -mtime -1 -print ")"  "|" afio -oz $hn:/dev/nst0

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

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

      # Clean up
      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 ended on 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 "Inc Backup Complete at $ts ...."

#EOF

