#!/bin/sh
# snapback - create snapshotesque backups
# created  - Aug 04 2004 
#
# list servers to be backed up
SERVERS="server1 server2"
HOST=`hostname | tr a-z A-z`
echo Starting Data Refresh...
for SERVER in `echo $SERVERS`
do
	echo
	USERVER=`echo $SERVER | tr a-z A-Z`
	echo Update $USERVER
	[ ! -d /u/archive/$SERVER ] && mkdir /u/archive/$SERVER
	cd /u/archive/$SERVER
	echo " - Shift Directories"
	echo -n '   6'
	[ -d 6 ] && rm -r 6
	[ -d 5 ] && mv 5 6
	echo -n ' 5'
	[ -d 4 ] && mv 4 5
	echo -n ' 4'
	[ -d 3 ] && mv 3 4 
	echo -n ' 3'
	[ -d 2 ] && mv 2 3
	echo -n ' 2'
	[ -d 1 ] && mv 1 2
	echo -n ' 1'
	[ -d 0 ] && mv 0 1 
	echo -n ' 0'
	[ -d 1 ] && cp -al 1 0
	[ ! -d 0 ] && mkdir 0
        echo
	# list directories to back up
	[ "$SERVER" = "server1" ] && DIRECTORIES="/root /etc /var/www /home"
	[ "$SERVER" = "server2" ] && DIRECTORIES="/home"
	# sync direcories
	for DIRECTORY in `echo $DIRECTORIES`
        do
		echo " - rsync $SERVER:$DIRECTORY to 0"
		rsync -avRz -e ssh --delete $SERVER:$DIRECTORY 0 >/dev/null 2>&1
		if [ ! "$?" = "0" ]
		then
			echo "   Rsync Failed!!!"
			MESG="$HOST: Rsync of $SERVER:$DIRECTORY Failed"
			echo "Please Investigate and Fix Errors." | mail -s "$MESG" user@domain.com
			echo "`date`: $MESG" >>/var/log/snapback.log
		else
			echo "`date`: Rsync Archive of $SERVER:$DIRECTORY Completed Successfully" >>/var/log/snapback.log
		fi
	done
done
echo
# Start a Tape Backup 
echo "`date`: Start Tape Backup" >> /var/log/snapback.log
tar cf /dev/nst0 /u/archive/[a-z]*/0 -b125
if [ $? -eq 0 ]; then
	echo "`date`: Finished Tape Backup" >> /var/log/snapback.log
else
	echo "`date`: ERROR - Error Returned While Writing to Tape. Backup May Be Incomplete." >> /var/log/snapback.log
	exit 1
fi