#!/bin/bash

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


# Mount the disk; if the disk doesn't mount then write so to a log file.
# (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 /media/backup
then
 echo "FATAL: Backup did not run because error mounting media" > /var/log/backup-`date +%F`-`date +%H`-`date +%M`-`date +%S`
 # should probably send this to syslog instead!

else

  # 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
  mkdir /media/backup/home
  cp /home/* /media/backup/home/ --archive --update > /var/log/backup-`date +%F`-`date +%H`-`date +%M`-`date +%S` 2>&1

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

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

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

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

fi
