#! /bin/sh
##########################################################################
# Shellscript :	dvd_backup.sh - Backup important file to DVD
# Version     :	1.3
# Author      :	jacques duplessis (jacques.duplessis@videotron.ca)
# Date        :	2005-12-09
# Requires    :	bash shell 
# Category    :	Backup
# SCCS-Id.    :	@(#) dvd_backup.sh   1.3 09.12.2008
# Description : Create a backup onto DVD of directories of your choice.
##########################################################################

# Programe Name and version 
PN=${0##*/}                                     ; export PN
VER='1.3'                                       ; export VER

# LINternUX Base directory
BASEDIR="/linternux" 				            ; export BASEDIR
LOGDIR="$BASEDIR/log"                           ; export LOGDIR
if [ ! -d $LOGDIR ] ; then mkdir $LOGDIR ; fi   # Create log dir. if not exist

# DVD Writer Device
DVD_DEV="/dev/dvd"                    		    ; export DVD_DEV

# Email to advise of success and failure of backup
SYSADMIN="jacques.duplessis@linternux.com"      ; export SYSADMIN

# Script Log file
LOGFILE="$LOGDIR/dvd_backup.log" 	            ; export LOGFILE

# ---------- Define Here the list of directory to backup on DVD 
BACKDIR="/linternux=/linternux /spool/cron=/var/spool/cron "
BACKDIR="${BACKDIR}/etc=/etc /home=/home /root=/root /sysadmin=/sysadmin"
export BACKDIR

# ---------- Write Starting info in the log file.
echo -e "\n\n---------------------------------" | tee    $LOGFILE
echo -e "Program $PN $VER - Starting `date`"    | tee -a $LOGFILE

# Make sure the DVD filesystem is unmount before formatting begin
umount $DVD_DEV > /dev/null 2>&1        

# i---------- Format DVD 
echo -e "Formatting the DVD ... dvd+rw-format -f $DVD_DEV" | tee -a $LOGFILE
dvd+rw-format -f $DVD_DEV >> /dev/null 2>&1 
RC1=$?
echo -e "DVD Format Error Code = $RC1\n" | tee -a $LOGFILE
if [ "$RC1" -ne 0 ]
   then echo -e "DVD Format Failed !"    | tee -a $LOGFILE
	    cat $LOGFILE | mail -s "DVD Format failed" $SYSADMIN
        exit 1
   else sleep 5
fi

# ---------- Backup to DVD
echo -e "Executing growisofs -Z /dev/dvd -R -J -allow-multidot -allow-leading-dots -l -pad -graft-points $BACKDIR ..."  | tee -a $LOGFILE
growisofs -Z /dev/dvd -R -J -allow-multidot -allow-leading-dots -l -pad -graft-points $BACKDIR >>$LOGFILE 2>&1
RC2=$?
echo "growisofs Error Code = $RC2" | tee -a $LOGFILE

# ---------- End of process
RC=`expr $RC1 + $RC2`
echo -e "End of process - Return Code = $RC - `date`"| tee -a $LOGFILE
echo -e "----------------------------------------\n" | tee -a $LOGFILE
if [ "$RC" -ne 0 ]
   then cat $LOGFILE | mail -s "DVD Backup failed"    $SYSADMIN
   else cat $LOGFILE | mail -s "DVD Backup succeeded" $SYSADMIN
fi
exit $RC

