#!/bin/sh
#
# etc-saver version 0.5
# August 14 1998, Shawn Boyette (mdxi@collapsar.org)
#
# Simple script to create a tarball of /etc, gzip it and
# write it to a floppy disk (why not, what _else_ are you
# using it for?).

###################
# Set up variables
###################

# These first ones shouldn't need editing...
DATE=$(date)
DATE_STAMP=$(date +"%Y%j")
TARBALL=etc$DATE_STAMP.tar
HOSTNAME=$(hostname)
# ...but you should edit the following to suit your tastes...

# What are we writing to?  This mountpoint should be defined in 
# /etc/fstab or it's not gonna work...
DEVICE=/mnt/floppy

# How much space is on $DEVICE (in bytes)
# 1400000 (1400832 really) for floppy because 5% of filesystem is reserved 
BYTES_ON_DEVICE=1400000

# This is who to tell when device mount fails
MAINTAINER=root

# Run silently (1) or email info every run (0)?
VERBOSITY=0

# This sets the location of the etc-saver rc file which lists
# files to be excluded from the archive.  EDIT THIS FILE!!!
EXCLUDE_ITEMS='-X /usr/local/etc/etc-saver.conf'

################
# End Variables
################



#
# findspace - clear space on $DEVICE for new archive
#
function findspace { 
  let TARBALL_SIZE=$(ls -l $TARBALL.gz | awk '{print $5}')
  if [ $TARBALL_SIZE -gt $BYTES_ON_DEVICE ]
    then
      echo "etc-saver: Aborted, archive size exceeds capacity of $DEVICE." \
        | mail -s "etc-saver FAILURE" $MAINTAINER
      exit
    else
      du --bytes $DEVICE | cut -f1 > tmpspace
      SPACE_USED=$(tail -1 tmpspace)
      rm -f tmpspace
      let SPACE_AVAIL=$BYTES_ON_DEVICE-$SPACE_USED
      if [ $TARBALL_SIZE -lt $SPACE_AVAIL ]
	then
	  return 0
	else
	  return 1
      fi
  fi
}



#
# mail_report - email activity report to $MAINTAINER
#
function mail_report {
  echo "etc-saver successfully ran on $HOSTNAME" > message
  echo "at $DATE" >> message
  echo " " >> message
  echo "The archive file $TARBALL.gz was written to $DEVICE." >> message
  echo "$TARBALL.gz is $TARBALL_SIZE bytes in length." >> message
  if [ -z $FILES_REMOVED ]
    then
    echo " " >> message
    echo "No files were deleted." >> message
    echo " " >> message
    else
    echo "This necessitated the removal of these old archives:" >> message
    echo -e $FILES_REMOVED >> message
    echo " " >> message
  fi
  echo "End of Report." >> message
  cat message | mail -s "etc-saver Report" $MAINTAINER
  rm -f message
}
    
    
    
#
# main script routine
#
if mount $DEVICE >> /dev/null 2>&1 
  then
    cd /tmp
    /bin/tar $EXCLUDE_ITEMS -cf $TARBALL /etc/ >> /dev/null 2>&1
    /bin/gzip -9 $TARBALL
    until findspace
	do
	  ls $DEVICE/etc* > tmpfiles
	  rm -f $(sort tmpfiles | head -1)
	  FILES_REMOVED="$FILES_REMOVED\n$(sort tmpfiles | head -1)"
	  rm -f tmpfiles
	done
    cp /tmp/$TARBALL.gz $DEVICE 
    rm -f $TARBALL.gz
    umount $DEVICE
    if [ $VERBOSITY = "0" ]
      then
        mail_report
    fi
    else
	echo "etc-saver: Aborted, couldn't mount $DEVICE." | \
	  mail -s "etc-saver FAILURE" $MAINTAINER
fi
