#!/bin/sh
#< Backup $HOME/bin and optionally send to remote host
# bintar - backup script
#   Kevin Waldron 22/12/2003

_FILE=/files/backup/bin_`date +%d%b%y`_$$.tar

if [ "$#" -gt "2" ]; then
  echo "USAGE: `basename $0` [-z] [-r]"
  echo "       -z   gzip backup file"
  echo "       -r   copy to remote server"
  exit 1
fi

_GZIP=""
_REMOTE=""

while :
do
  case $1 in
    "-z" ) _GZIP="y"
           shift
           ;;
    "-r" ) _REMOTE="y"
           if [ `id -u` != 0 ]; then
              echo "Remote option must be issued as root!" && exit 1
           fi
           ping -w 2 192.168.0.1 >/dev/null 2>&1
           if [ "$?" -ne 0 ]; then
              echo "Cannot contact backup server!" && exit 1
           fi
           shift
           ;;
     * )   break ;;
  esac
done

tar cvf $_FILE /home/kevin/bin

echo "tar to $_FILE complete!"
echo "*********************************"

if [ "$_GZIP" = "y" ]; then
   gzip $_FILE
   echo "gzip to $_FILE.gz complete!"
   echo "*********************************"
fi
  
if [ "$_REMOTE" = "y" ]; then
   if [ "$_GZIP" = "y" ]; then
      _FILE_TO_COPY=${_FILE}.gz
   else
      _FILE_TO_COPY=${_FILE}
   fi
   smbmount //xpserver/i$ /bk_mnt
   cd /bk_mnt/_backup  
   cp $_FILE_TO_COPY .
   echo "Remote file transfer complete!"
   echo "*********************************"
   cd /home/kevin/bin
   ls -lt /bk_mnt/_backup | head -2 | tail -1
   smbumount /bk_mnt
fi

echo "BACKUP COMPLETE!"

exit 0