#!/bin/ksh
# filename:cpio.sh
# description: performs a level 0 cpio backup on either Sun or HP
#              platforms.
# author:  robin manapsal, 11-23-99
# version: 1.0, revised to cpio instead of ufsdump/vxdump command
# history: 11-23-99, date created
#          12-15-99, modified probe status tape to accomodate Solaris
#                    and HP-UX platform

UNAME=`/usr/bin/uname -s`
SUNDEV=/dev/rmt/0mn
HPDEV=/dev/rmt/0mn
HOSTNAME=`/usr/bin/hostname`
CONTACTS="youremail here"
FILE=/tmp/tape.status
DATE=`date '+%Y-%m-%d'`
LOG=/tmp/backups/backup.log_$DATE
TEE=/bin/tee
GRAPH=/tmp/graphfile
CPIO=/bin/cpio
GZIP=/usr/bin/gzip

##########################################################
#
#       Probe the status of the tape device
#
# Checks the size of the $FILE. If the $FILE=0 bytes
# the backup procedure is aborted.  If the $FILE>0 bytes
# the procedure is initiated until the process is
# completed.
#
##########################################################
echo "Scanning OS and tape status"
case "$UNAME" in
  HP-UX)
     mt -t /dev/rmt/0m rew > /dev/null 2> /tmp/tape.status
     if [[ ! -s $FILE ]]
     then
       echo "HP tape is mounted"
       echo "Proceeding with the backup"
     else
       echo "Hp tape is not mounted"
       mailx -s "Error aborting backup on $HOSTNAME - tape not ready" $CONTACTS < /dev/null > /dev/null 2>&1
       exit 1
      fi
   ;;
  SunOS)
     mt -t /dev/rmt/0m status > $FILE
     if [[ ! -s $FILE ]]
       then
        echo "Sun tape is not mounted"
        mailx -s "Error aborting backup on $HOSTNAME - tape not ready" $CONTACTS < /dev/null > /dev/null 2>&1
        exit 1
     else
        echo "Sun tape is mounted"
        echo "Proceeding with the backup"
     fi
   ;;
      *)
       echo "OS not suppported"
     ;;
esac
##########################################################
#
#       Collecting files for backup
#
##########################################################
echo "Collecting graphfile"
find / \( -name xfn -o -name proc \) \
-prune -o -print > $GRAPH

###########################################################
#
# Determine the OS Version and excute the cpio
# This part of the script uses the local cpio depending
# on the flavor of UNIX.  This part can be modified to
# suit the various flavors of UNIX such as BSD, Linux
# Solaris, HP, IBM-AIX, SCO (maybe)..
#
###########################################################
echo "Please wait: Identifying HOST and UNIX flavor"
case $UNAME in
   SunOS)
       if [ "$UNAME"="SunOS" ]; then
       (cat $GRAPH | $CPIO -ocBv  > $SUNDEV) 2>&1 | $TEE -a $LOG
       mailx -s "$HOSTNAME backup complete" $CONTACTS < /dev/null > /dev/null 2>&1
     fi
    ;;
   HP-UX)
       if [ "$UNAME"="HP-UX" ]; then
       (cat $GRAPH | $CPIO -ocBv > $HPDEV) 2>&1 | $TEE -a $LOG
       mailx -s "$HOSTNAME backup complete" $CONTACTS < /dev/null > /dev/null 2>&1
     fi
    ;;
  *)
       echo "$UNAME is not supported in this script"
       echo "Backup failed"
       mailx -s "Backup failed on $HOSTNAME" $CONTACTS < /dev/null > /dev/null 2>&1
    ;;
esac

##########################################################
#
#               Eject the tape(s)
#
##########################################################
echo "Ejecting Tape"
  if [ "$UNAME"="SunOS" ]; then
   mt -f $SUNDEV offline
  elif
   [ "$UNAME"="HP-UX" ]; then
   mt -f $HPDEV offl
  else
   echo "Tape on $UNAME is offline"
   mailx -s "$HOSTNAME No tape loaded or drive offline" $CONTACTS < /dev/null > /dev/null 2>&1
  fi
echo "Tape Ejected"

##########################################################
#
#               Compress the log files
#
##########################################################
echo "Compressing Log file"
$GZIP $LOG
echo "Compression done"
##########################################################
#
#               Clean up the backup logs
#
##########################################################
#echo "Removing log file"
#find $LOG -mtime +5 -exec rm -f {} \;