#!/bin/bash # full and incremental backup script # created 07 February 2000 # Based on a script by Daniel O'Callaghan # and modified by Gerhard Mourani # and modified 19 December 2009 by Michael Burns PATH=/usr/bin:/bin ################################################# ################################################# # Change the follwing variables below to fit your computer/backup # name of this computer COMPUTER=`hostname` # or fixed to your liking # directoris to backup -- one per line DIRECTORIES="/home/me /sharedocs" ##### # The following directories are used to store the backups and # various logs. Make sure you create these directories by hand # before running this script!!! ##### # where to store the backups BACKUPROOT=/backup BACKUPDIR=${BACKUPROOT}/me/nightly # where to store time of full backup TIMEDIR=${BACKUPDIR}/last-full # where to store the log files LOGDIR=${BACKUPDIR}/logs # name and locaction of tar TAR=/bin/tar ################################################# ################################################# # You should not have to change anything below here DOW=`date +%a` # Day of the week e.g. Mon NDOW=`date +%u` # Day of the week number e.g. 1 = Mon DOM=`date +%d` # Date of the Month e.g. 27 MON=`date +%m` # Month e.g. 01 = January DM=`date +%d%b` # Date and Month e.g. 27Sep forcefull=0 # force a full backup forceincremental=0 # force an incremental backup # # On the 1st of the month and the last day of the year, 12/31, a # permanent full backup is made. # # Every Sunday a full backup is made. Last Sunday's backup will be # overwritten. # # 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 all files newer than the NEWER date. NEWER # gets it date from the file written every Sunday. (A full backup # does not update the "last-full" file.) # # Allow a "forced" monthly full backup (specify "full" on the command line) if [ ! -z $1 ] && [ $1 = "full" ]; then forcefull=1 echo Param = full -- setting forcefull to 1 fi # Force a full backup for 12/31 -- year end if [ $DOM = "31" ] && [ $MON = "12" ]; then forcefull=1 echo Date is 12/31 -- setting forcefull to 1 fi # Monthly full backup if [ $DOM = "01" -o $forcefull -eq 1 ]; then NEWER="" echo "Performing monthly full backup ..." > $LOGDIR/$COMPUTER-$DM.log echo "" >> $LOGDIR/$COMPUTER-$DM.log $TAR --gzip $NEWER -chvf $BACKUPDIR/$COMPUTER-$DM.tar.gz $DIRECTORIES >> $LOGDIR/$COMPUTER-$DM.log 2> $LOGDIR/$COMPUTER-$DM.error # force an incremental to prevent two full backups from occurring echo "Performed full backup -- setting forceincremental to 1" > $LOGDIR/$COMPUTER-$DM.log forceincremental=1 fi # Weekly full backup if [ $NDOW = "7" -a $forceincremental -eq 0 ]; then NEWER="" NOW=`date +%d-%b` echo "Performing weekly full backup ..." > $LOGDIR/$COMPUTER-$NDOW-$DOW.log echo "" >> $LOGDIR/$COMPUTER-$NDOW-$DOW.log # Update full backup date echo $NOW > $TIMEDIR/$COMPUTER-full-date $TAR --gzip $NEWER -chvf $BACKUPDIR/$COMPUTER-$NDOW-$DOW.tar.gz $DIRECTORIES >> $LOGDIR/$COMPUTER-$NDOW-$DOW.log 2> $LOGDIR/$COMPUTER-$NDOW-$DOW.error # Make incremental backup - overwrite last weeks else # Get date of last full backup NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`" echo "Performing daily incremental backup (newer = ${NEWER}) ..." > $LOGDIR/$COMPUTER-$NDOW-$DOW.log echo "" >> $LOGDIR/$COMPUTER-$NDOW-$DOW.log $TAR --gzip $NEWER -chvf $BACKUPDIR/$COMPUTER-$NDOW-$DOW.tar.gz $DIRECTORIES >> $LOGDIR/$COMPUTER-$NDOW-$DOW.log 2> $LOGDIR/$COMPUTER-$NDOW-$DOW.error fi