#!/bin/sh
# Create full DAR backup archive onto network drive
# Henrik Ingo, henrik.ingo@avoinelama.fi, Feb 2008
#
# License: GPLv2

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

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

# Filename (body) for archive files
# Chosen logic: Do full backup monthly
TIMESTAMP="`date +%Y-%m`"
FILENAME="`hostname`-$TIMESTAMP-backup-full"

#
#### 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 "$BACKUPDIRFINAL"
then
  echo "Backup directory $BACKUPDIRFINAL not available (not mounted, no network)."
  exit 1
fi

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

#create directory for this months backups
BACKUPDIRMONTH="$BACKUPDIR/$TIMESTAMP"
if test ! -d "$BACKUPDIRMONTH"
then
  mkdir "$BACKUPDIRMONTH"
elif test ! -w "$BACKUPDIRMONTH"
then
  echo "$BACKUPDIRMONTH exists but is not writable."
  exit 3
fi

#do dar backup
BACKUPPATHFILE="$BACKUPDIRMONTH/$FILENAME"
dar --create "$BACKUPPATHFILE" -B "$DARRC" -v

echo "Testing integrity of created archive."

dar -t "$BACKUPPATHFILE"


if test $BACKUPDIR eq $BACKUPDIRFINAL
then
  # Already wrote files to final dest, nothing left to do
  echo Backup complete.
  exit 0
fi

BACKUPDIRFINALMONTH="$BACKUPDIRFINAL/$TIMESTAMP"
#create directory for this months backups
if test ! -d "$BACKUPDIRFINALMONTH"
then
  mkdir "$BACKUPDIRFINALMONTH"
elif test ! -w "$BACKUPDIRFINALMONTH"
then
  echo "$BACKUPDIRFINALMONTH exists but is not writable."
  exit 3
fi

echo
echo "Now copying backups $BACKUPPATHFILE* to $BACKUPDIRFINALMONTH."
echo
echo
cp "$BACKUPPATHFILE"* "$BACKUPDIRFINALMONTH"


echo end of $0 reached






syntax highlighted by Code2HTML, v. 0.9.1