#!/bin/sh
# full and incremental backup script
# created 07 February 2000, last mod 14 Oct 2006
# Based on a script by Daniel O'Callaghan  and Gerhard Mourani 
# and modified by Georgi Todorov 
# License: GPL

#Change the variables below to fit your computer/backup

COMPUTER=insomniac                           	# name of this computer
DIRECTORIES="/etc /usr/local/share/tomcat/webapps /var/lib/mysql /var/lib/pgsql"                     	# directoris to backup
BACKUPDIR=/imports/192.168.10.9/backup/arcib                      	# where to store the backups
TIMEDIR=/imports/192.168.10.9/backup/arcib/last-full              	# where to store time of full backup
LOGSDIR=$BACKUPDIR/logs							# where to output tar information
TAR=/bin/tar                            		# name and locaction of tar
MKDIR="/usr/bin/mkdir -m 750"
SECURE="chmod o-rwx"

#You should not have to change anything below here

PATH=/usr/local/bin:/usr/bin:/bin
DOW=`date +%a`         		# Day of the week e.g. Mon
DOM=`date +%d`              		# Date of the Month e.g. 27
DM=`date +%d%b`             	# Date and Month e.g. 27Sep

# On the 1st of the month a permanet full backup is made
# Every Sunday a full backup is made - overwriting last Sundays backup
# The rest of the time an incremental backup is made. Each incremental
# backup overwrites last weeks incremental backup of the same name.
#
# if NEWER = "", then tar backs up all files in the directories
# otherwise it backs up files newer than the NEWER date. NEWER
# gets it date from the file written every Sunday.
/usr/sbin/ntpdate time.mit.edu
mount $BACKUPDIR
sleep 10

# Monthly full backup
if [ $DOM = "01" ]; then
        NEWER=""
	rm -rf $BACKUPDIR/$COMPUTER-$DM
	$MKDIR $BACKUPDIR/$COMPUTER-$DM
        $TAR $NEWER -cf - $DIRECTORIES 2>/dev/null|$TAR -xvf - -C $BACKUPDIR/$COMPUTER-$DM |tee $BACKUPDIR/$COMPUTER-$DM/LOGFILE
	echo "$BACKUPDIR/$COMPUTER-$DM created on `date`"
fi

# Weekly full backup
if [ $DOW = "Sun" ]; then
        NEWER=""
        NOW=`date +%d-%b`

        # Update full backup date
        echo $NOW > $TIMEDIR/$COMPUTER-full-date
	rm -rf $BACKUPDIR/$COMPUTER-$DOW
	$MKDIR $BACKUPDIR/$COMPUTER-$DOW
        $TAR $NEWER -cf - $DIRECTORIES 2>/dev/null|$TAR -xvf - -C $BACKUPDIR/$COMPUTER-$DOW |tee $BACKUPDIR/$COMPUTER-$DOW/LOGFILE
	echo "$BACKUPDIR/$COMPUTER-$DM created on `date`"

# Make incremental backup - overwrite last weeks
else

        # Get date of last full backup
        NEWER="--newer-mtime `cat $TIMEDIR/$COMPUTER-full-date`"
	rm -rf $BACKUPDIR/$COMPUTER-$DOW
	$MKDIR $BACKUPDIR/$COMPUTER-$DOW
	find $DIRECTORIES -type f -mtime -7 -print |$TAR -cf - -T -|$TAR xvf - -C $BACKUPDIR/$COMPUTER-$DOW |tee $BACKUPDIR/$COMPUTER-$DOW/LOGFILE
	echo "$BACKUPDIR/$COMPUTER-$DM created on `date`"
fi
sleep 10
umount $BACKUPDIR