#!/bin/sh
#---------- ---------- ---------- ---------- ---------- ----------
#  24.Aug.2001 -- Art Mulder

#---------- ---------- ---------- ---------- ---------- ----------
## VARIABLES

PATH=/bin:/usr/bin:/etc:/bin:/usr/ucb:/usr/sbin:/sbin:/usr/bsd:/usr/local/bin:/irus/bin:/irus/sbin
export PATH

ADMINS="root cnorley"           ##id's to be mailed upon success or failure
MYNAME="`basename $0`"  ## Name of this Script
MYHOST="`hostname`"     ## Hostname

TMP=/usr/tmp
LOG=/var/log/rsync.log

#---------- ---------- ---------- ---------- ---------- ----------
## PRELIMINARIES

##- rotate the old log
cyclelog -n 15 -Z $LOG

## Welcome Message
echo "---------- ---------- ---------- ----------" |tee -a $LOG
date                                               |tee -a $LOG
echo "$MYNAME : $MYHOST"                           |tee -a $LOG
echo "Begining rsync of client systems" |tee -a $LOG
echo "---------- ---------- ---------- ----------" |tee -a $LOG

#---------- ---------- ---------- ---------- ---------- ----------
## This function does the actual rsync commands.
rsync_it()
{
  SRCHOST=$1 ; SRCDIR=$2

  OPTS="--force --delete-excluded  --one-file-system
      --delete --backup --backup-dir=/sync/$SRCDIR/$BACKUPDIR -av"

  # first delete the 7-day old stuff
  rsync --delete -a $EMPTY/ /sync/$SRCDIR/$BACKUPDIR/ 2>&1 |tee -a $LOG

  
  echo "rsync'ing $SRCHOST:/$SRCDIR/"   |tee -a $LOG
  echo "..." | tee -a $LOG

  # Now do the transfer
##  rsync $OPTS $SRCHOST:/$SRCDIR/ /sync/$SRCDIR/current 2>&1 |tee -a $LOG
## 30.Jan.01 -- Art Mulder - prepend "##" on the output, so that
## we can easily find it in the logs.
  rsync $OPTS $SRCHOST:/$SRCDIR/ /sync/$SRCDIR/current 2>&1 | sed -e "s/^/## /"
| tee -a $LOG
}

#---------- ---------- ---------- ---------- ---------- ----------
## MAIN BODY
#---------- ---------- ---------- ---------- ---------- ----------
# This script does backups to a rsync backup server. 
# You will end up with a 7 day rotating incremental backup.
# The incrementals will go into subdirectories named after
# the day of the week, and the current full backup goes into
# a directory called "current"

# Set up empty directory
EMPTY=/tmp/empty$MYNAME
[ -d $EMPTY ] || mkdir $EMPTY

BACKUPDIR=`date +%A`

#---------- ---------- ---------- ---------- ---------- ----------
# Host #1 - sabre
DIRS="sabre/data sabre/data2"

for DIR in $DIRS
do
  rsync_it sabre $DIR
done

#---------- ---------- ---------- ---------- ---------- ----------
# Host #2 - titan
DIRS="titan titan/data2 titan/data3"
for DIR in $DIRS
do
  rsync_it titan $DIR
done

#---------- ---------- ---------- ---------- ---------- ----------
# Host #3 - corvus
DIRS="corvus/mod corvus/corvus_cd corvus/cra corvus/data corvus/data2"
for DIR in $DIRS
do
  rsync_it corvus $DIR
done

#---------- ---------- ---------- ---------- ---------- ----------
# Host #4 - Mingus
DIRS="mingus/data mingus/data2"
for DIR in $DIRS
do
  rsync_it mingus $DIR
done

#---------- ---------- ---------- ---------- ---------- ----------
## FINISH UP
echo "\nFinishing Up At: \c" |tee -a $LOG
date | tee -a $LOG

# email results
##cat $LOG | mail -s "$MYHOST rsync backup log" $ADMINS
## 30.Jan.01 -- Art Mulder - strip the log file of all the filenames
cat $LOG | sed -e "/^## /d" |  mail -s "$MYHOST rsync backup log" $ADMINS

# Clean up
rmdir $EMPTY 

#---------- ---------- ---------- ---------- ---------- ----------
## END