#!/bin/sh
# Create differential DAR backup archive onto network drive
# Depends on a full backup archive to exist, see fullbackup.sh
# Henrik Ingo, henrik.ingo@avoinelama.fi, Feb 2008
#
# License: GPLv2


#debugging...
echo "$0 being run by `whoami` at `date`" >> /var/log/diffbackup

#### SETTINGS ####
#
#
DARRC="/usr/local/etc/backup.darrc"
DISKSERVER=diskserver
HOSTNAME=`hostname`

# Directory for backups
BACKUPDIR="/media/$DISKSERVER/backup/$HOSTNAME"

# Script that creates the full backup if needed
FULLBACKUPSH="fullbackup.sh"

# Chosen logic: Do full backup monthly, maintain diff backup each day for a week
# (eg, next monday we'll overwrite this mondays backup)
TIMESTAMP="`date +%Y-%m`"
WDAY="`date +%A`"
DAY="`date +%d`"
FILENAMEFULL="`hostname`-$TIMESTAMP-backup-full"
FILENAMEDIFF="`hostname`-$TIMESTAMP-$DAY-backup-diff"

#
#### SETTINGS END ####


# Do not do anything unless the specified directory is available 
# (ie we are on the road and it's not mounted)
if test ! -d "$BACKUPDIR"
then
  echo "Backup directory not available (not mounted, no network)."
  exit 1
fi

if test ! -w "$BACKUPDIR"
then
  echo "Backup directory not writable."
  exit 2
fi

# directory for this months backups
BACKUPDIRMONTH="$BACKUPDIR/$TIMESTAMP"
# and for this weekday
BACKUPDIRWDAY="$BACKUPDIRMONTH/$WDAY"
# and the dar files (diff to create, full to read)
BACKUPPATHFILEDIFF="$BACKUPDIRWDAY/$FILENAMEDIFF"
BACKUPPATHFILEFULL="$BACKUPDIRMONTH/$FILENAMEFULL"

# full reference backup must exist. If not, create it.
if test ! -f "$BACKUPPATHFILEFULL.1.dar"
then
  echo "Full reference backup (for this month) doesn't exist. Creating it first (this will take time)."
  echo Running $FULLBACKUPSH
  echo
  $FULLBACKUPSH
  echo
  echo Now returning to $0
fi


if test ! -d "$BACKUPDIRWDAY"
then
  mkdir "$BACKUPDIRWDAY"
elif test ! -w "$BACKUPDIRWDAY"
then
  echo "$BACKUPDIRWDAY exists but is not writable."
  exit 3
else
  # WDAY directory exists, so remove old backups
#  echo removing old backups from "$BACKUPDIRWDAY"
#  rm $BACKUPDIRWDAY/*
  echo
fi


echo removing old backups from "$BACKUPDIRWDAY"
rm $BACKUPDIRWDAY/*


#do dar backup
dar --create "$BACKUPPATHFILEDIFF" --ref "$BACKUPPATHFILEFULL" -B "$DARRC" -v

echo
echo "Testing integrity of created archive."
echo 

dar -t "$BACKUPPATHFILEDIFF"


echo end of $0 reached
#debugging...
echo "end of $0 reached at `date`" >> /var/log/diffbackup





syntax highlighted by Code2HTML, v. 0.9.1