#!/bin/sh
# jason pepas's backup script - see http://jason.pepas.com
# shell script tutorials:
#  http://www.freeos.com/guides/lsst/ for shell script tutorial
#  http://www.linuxnewbie.org/nhf/intel/programming/introbashscript.html


# --------------------------------------------------
# set variables:
# --------------------------------------------------


directoryname=`date +%Y-%m-%d`"_"`hostname`"_backup"
fullbackuplabel="Full Backup of "`hostname`" on "`date '+%B %e, %Y'`
fullbackupname=`date +%Y-%m-%d`"_fullbackup.tar.gz"
fullbackuplogname=`date +%Y-%m-%d`"_fullbackup.log"
incrementalbackuplabel="Incremental Backup of "`hostname`" on "`date '+%B %e, %Y'`
incrementalbackupname=`date +%Y-%m-%d`"_incrementalbackup"`date +%H%M`".tar.gz"
incrementalbackuplogname=`date +%Y-%m-%d`"_incrementalbackup"`date +%H%M`".log"


# --------------------------------------------------
# functions:
# --------------------------------------------------


fullbackup()
{
    # create backup directory
    if test ! -e /backup/$directoryname; then
	echo "Creating /backup/$directoryname directory"
	mkdir /backup/$directoryname
    fi

    # create (or update) a shortcut called current to this directory
    echo "Updating /backup/current pointer"
    rm /backup/current
    ln -s /backup/$directoryname /backup/current

    # keep track of creation date of full backup (used with incremental backups)
    echo "Updating /backup/current/lastfullbackupdate"
    date>/backup/current/lastfullbackupdate
    
    # create backup
    echo "Running tar..."
    tar --create  --label "$fullbackuplabel" --files-from /root/scripts/whattobackup --exclude-from /root/scripts/whatnottobackup --ignore-failed-read --absolute-names --verbose --gzip --file /backup/current/$fullbackupname > /backup/current/$fullbackuplogname 2>&1
    gzip /backup/current/$fullbackuplogname
    echo "Done. Created /backup/current/$fullbackupname"
    echo "To view the log, type:"
    echo " zcat /backup/current/$fullbackuplogname"
}


incrementalbackup()
{
    # create variable with date of last full backup
    lastfullbackupdatevar=`cat /backup/current/lastfullbackupdate`
    
    # check for existence of incremental backup
    if test -e "/backup/current/$incrementalbackupname"; then 

        echo "Your last incremental backup was less than 60 seconds ago."
    	echo "Wait a minute and try again."

    else 

	# create incremental backup
    	echo "Running tar..."
	tar --create --label "$incrementalbackuplabel" --files-from /root/scripts/whattobackup --exclude-from /root/scripts/whatnottobackup --ignore-failed-read --after-date "$lastfullbackupdatevar" --absolute-names --verbose --gzip --file /backup/current/$incrementalbackupname > /backup/current/$incrementalbackuplogname 2>&1
	gzip /backup/current/$incrementalbackuplogname
	echo "Done. Created /backup/current/$incrementalbackupname"
        echo "To view the log, type:"
	echo " zcat /backup/current/$incrementalbackuplogname"

    fi
}


# --------------------------------------------------
# main routine:
# --------------------------------------------------

echo "---------- Backup Script Running... ----------"

if test `date +%A` = "Sunday" && ! -e "/backup/$directoryname"; then

    # if it is sunday and you havent yet done a full backup, do so
    echo "Performing Weekly Full Backup..."
    fullbackup;

elif test ! -e /backup/current/*fullbackup.tar.gz; then

    # if there is no current fullbackup, make one
    echo "No Current Full Backup - Performing Full Backup Now..."
    fullbackup;

else

    # otherwise, do an incremental backup
    echo "Performing Incremental Backup..."
    incrementalbackup;

fi # end if statement

echo "---------- Backup Script Done ----------"


