#!/bin/bash
#
# Simple script to back up data to a mounted read/write file system.
#
finalLoc="/media/LACIE"
bkloc="$HOME/backup"
snapfile="$bkloc/snapshot.txt"
bkfiles="$bkloc/BackupFiles.txt"
exfiles="$bkloc/ExcludeFiles.txt"

# If it's Monday, erase the snapshot file, which forces a full backup
dayofweek=`date +%a` # Gets the day of the week in 3-letter format
if [ $dayofweek = Mon ]
then
  # It's Monday; erase the snapshot file so we do a full backup
  rm -f $snapfile
fi

# Try to mount the backup device if not already mounted.
mount | grep "$finalLoc" >/dev/null
if [ $? != 0 ]
then # grep didn't find $finalLoc, so drive isn't already mounted
  # Try to mount the backup drive ($finalLoc); if unsuccessful, print an
  # error message, compress the log, and exit the script.
  mount "$finalLoc" -o rw
  rc=$?
  if [ $rc != 0 ]; then
    echo "Error $rc mounting $finalLoc"
    exit 1
  fi
fi

cd /
# Back up files listed in $bkfiles (but not in $exfiles) to $finalLoc.  Create a log file to
# record the tar command's results; the log file is placed in the $bkloc directory.
tar -czvf $finalLoc/backupfile.$dayofweek.tar.gz -T $bkfiles -X $exfiles -g $snapfile >$bkloc/bkup.$dayofweek.log 2>&1