I think it is best to have your backup script check the return code of the backup function.
Then email based on that.
Here is a script that I wrote a couple of years ago.
If you can not cut and paste it out, then email me at lonf@earthlink.net and I will email your directly.
Feel free to edit the script any way you see fit.
Make sure you edit the email address and server dir paths.
#! /bin/bash
############################################################################
############################################################################
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
## Variables ##
TOC="/root/TAPE.TOC.`date +%Y%m%d`"
TAR_LOGS= "/root/tar.stderr /root/tar.stdout /root/tar_invocation"
ERROR_FILE=$TOC".err"
DEVICE="/dev/st0"
MOUNTOPTS="rsize=8192,wsize=8192"
BACKUP="tar"
# For Each Remote Server you need add a NAME={mnt_point}
SERVER="/mnt/server"
SDIRS="DIR1 DIR2"
# SDIRS for Remote Drives needs $SERVER in front of each Dir to backup.
#SDIRS="$SERVER/DIR1 $SERVER/DIR2"
ADMIN="user@isp.com"
## FUNCTIONS ##
function MountDrives ()
{
# Mount Drives
#
echo "Mounting Drives for Backup..."
# Need to edit the mount to work for the type of mount you need.
#mount SERVER:/SHARE $SERVER
#mount -t smbfs //SERVER/SHARE $SERVER -o username=user%password
}
function TOC ()
{
# Security on the TOC
umask 066
#
# If todays's TOC exists, there was probably an error. Remove the
# file *before* the following mv command as today's TOC is most
# likely invalid.
#
if [ -e $TOC ]; then
rm $TOC
fi
#
}
function Tar_Logs ()
{
echo "Removing $TAR_LOGS"
if [ -e $TAR_LOGS ]; then
rm -f $TAR_LOGS
fi
}
function Archive ()
{
# Archive Table of Contents
echo "Archive TOC."
mv /root/TAPE.TOC.* /root/archive
}
function BuildTOC ()
{
# Build table of contents first...
#
echo -n "Building table of contents..."
echo "$SERVER mounted">>$TOC
echo -n .
ls -lR $SDIRS >> $TOC; echo -n .
echo "done."
}
function TapeRetention ()
{
# Attempt to retention tape.
# Doesn't really seem to work on AIWA DDS-2 drive.
#
echo "Retentioning tape..."
mt -f $DEVICE reten
}
function Backup ()
{
# Back up the files...
#
echo "Backing up files to tape..."
# Output ERROR to file
tar --totals -cvf $DEVICE $TOC $SDIRS > /root/tar.stdout 2> /root/tar.stderr
TAR_RESULTS=$?
return $TAR_RESULTS
}
function EjectTape ()
{
# Eject the tape when finished...
mt -f $DEVICE offline
}
function EmailAdmin ()
{
# Email Administrator
TO=$ADMIN
if [ "$TAR_RESULTS" = "0" ]; then
SUBJECT="backup cron for `date '+%m-%d-%Y'`"
ERROR=/root/tar.stderr
BODY="Tar Successful"
else
SUBJECT="Critical Error on backup for `date '+%m-%d-%Y'`"
ERROR=/root/tar.stderr
BODY="Tar Failed, on error $TAR_RESULTS "
fi
eval " echo $BODY | /bin/mail -s \"$SUBJECT\" $TO < $ERROR "
}
function UmountDrives ()
{
echo "Umounting Drives ..."
umount $SERVER
}
## CALL FUNCTIONS ##
# Comment out MountDrives and UmountDrives if not using Remote Drives.
#MountDrives
Tar_Logs
TOC
Archive
BuildTOC
TapeRetention
Backup
EjectTape
EmailAdmin
#UmountDrives
# vim: ts=4
# vim: sw=4
# vim: nonu
>---------------------------------------Lawrence Feldman
SR. QA. Engineer SNAP Appliance
lfeldman@snapappliance.com