#!/bin/bash

####################################################
# Backup script to backup home, etc, usr and var to hotpluggable disk
# Copyright (C) 2007, 2008, 2010 Pete Boyd
# Version: 0.3.1
# Email: inkwire-at-thegoldenear-dot-org
# Web: http://thegoldenear.org/toolbox/unices/
####################################################

# Ref: http://stackoverflow.com/questions/59838/how-to-check-if-a-directory-exists-in-a-bash-shell-script



##################################################################
backup()
##################################################################
{
  # Copy those files that have not changed since last backup;
  # standard output goes to a log file;
  # standard error is sent to same place as standard output.

  #---------------------------------------------------------------
  # /home
  # if the name we want for a directory exists already, but as a symlink directory, then delete it
  if [[ -d /media/backup/home && -L /media/backup/home ]] ; then
      rm /media/backup/home
  fi

  # if the name we want for a directory exists already, but as a symlink, then delete it
  if [[ -L /media/backup/home ]] ; then
      rm /media/backup/home
  fi

  # If the directory doesn't exist then create it
  if [ ! -d /media/backup/home ]; then
      mkdir /media/backup/home
  fi
  # If we just delete it without first checking then we get an error which is emailed to 'root'
  # saying 'error: mkdir /media/backup/home'

  # Copy files
  cp /home/* /media/backup/home/ --archive --update > /var/log/hotswap-backup/backup-home-`date +%F`-`date +%H`-`date +%M`-`date +%S` 2>&1
  #---------------------------------------------------------------

  #---------------------------------------------------------------
  # /etc
  if [[ -d /media/backup/etc && -L /media/backup/etc ]] ; then
      rm /media/backup/etc
  fi

  if [[ -L /media/backup/etc ]] ; then
      rm /media/backup/etc
  fi

  if [ ! -d /media/backup/etc ]; then
      mkdir /media/backup/etc
  fi

  cp /etc/* /media/backup/etc/ --archive --update > /var/log/hotswap-backup/backup-etc-`date +%F`-`date +%H`-`date +%M`-`date +%S` 2>&1
  #---------------------------------------------------------------

  #---------------------------------------------------------------
  # /usr
  if [[ -d /media/backup/usr && -L /media/backup/usr ]] ; then
      rm /media/backup/usr
  fi

  if [[ -L /media/backup/usr ]] ; then
      rm /media/backup/usr
  fi

  if [ ! -d /media/backup/usr ]; then
      mkdir /media/backup/usr
  fi

  cp /usr/* /media/backup/usr/ --archive --update > /var/log/hotswap-backup/backup-usr-`date +%F`-`date +%H`-`date +%M`-`date +%S` 2>&1
  #---------------------------------------------------------------

  #---------------------------------------------------------------
  # /var
  if [[ -d /media/backup/var && -L /media/backup/var ]] ; then
      rm /media/backup/var
  fi

  if [[ -L /media/backup/var ]] ; then
      rm /media/backup/var
  fi

  if [ ! -d /media/backup/var ]; then
      mkdir /media/backup/var
  fi

  cp /var/* /media/backup/var/ --archive --update > /var/log/hotswap-backup/backup-var-`date +%F`-`date +%H`-`date +%M`-`date +%S` 2>&1
  #---------------------------------------------------------------

  #---------------------------------------------------------------
  # Unmount disk so that it can be unplugged and taken away
  umount /media/backup
  #---------------------------------------------------------------
}
##################################################################



# Create directory to save log files in if it doesn't already exist
if ! [ -d /var/log/hotswap-backup ]
then
 mkdir /var/log/hotswap-backup
fi


# If disk isn't mounted then try to mount it.
# If it mounts OK then do the backup, otherwise write to a log file.
# If disk is already mounted then do the backup.

# (if try to backup when device not mounted it will write to /media/backup, which, 
# when unmounted, probably lives in / and so could fill up the disk!)

if ! mount | grep "on /media/backup type" > /dev/null

then

 # If disk check above showed disk _wasn't_ mounted then try to mount it
 # If the disk doesn't mount OK the reason is written to the log file
 if mount /media/backup > /var/log/hotswap-backup/backup-general-`date +%F`-`date +%H`-`date +%M`-`date +%S` 2>&1

 then

  # If disk mounted OK above then do the backup
  backup

 fi

else

 # If disk check above showed disk _was_ mounted then don't try to mount it, just do the backup
 backup

fi


exit 0

