Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backup script...Please help Urgent

Status
Not open for further replies.

copperslim

Technical User
Dec 13, 2002
101
GB
I have a backup script that runs every night. When backup finishes it emails me with the path of the backup log.

So the date of the file becomes the current backup log file.

thisprog=$(basename $0)
LOG="/tape/logs/$thisprog.$(date +%d%m%y).log"


I please need a script that will grep “failed” in the log file base on the current backup (+%d%m%y ) and mail me a message Backup failed when ever it reads failed in the log file? If there is no failed in the log file then it mails me with the message backup was successful.
 
It would be easier to initionally call the log backup.log do the grep then move backup.log to backup.date.log

a=`cat $LOG|grep fail|wc -l`

if [ $a != "0" ]
then
mail user
else
sleep 1
fi

This way you don't have any of the problems of first of month dates

Mike

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
thisprog=$(basename $0)
LOG1="/tape/logs/$thisprog.$(date +%d%m%y).log"
LOG="backup.log"

a=`cat $LOG|grep fail|wc -l`

if [ $a = "0" ]
then
mailx -s "Backup failed" you@you.com
else
mailx -s "Backup OK" you@you.com
fi

mv $LOG $LOG1


--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Thanx mrn
I will try and advise tomorrow.

Would it pick the latest backup log file as the previous files are still kept on the directory.

 
If you want to keep the current log use cp instead of mv but make sure you use a single redirect > to over-write the log each day you would use >> to append.

eg

cat /etc/passwd > newfile
cat /etc/group > newfile

would over-write passwd info with group info

but

cat /etc/passwd >> newfile
cat /etc/group >> newfile

would add group info onto the end of the passwd info

Regards

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
mrn I copied it to a new folder but it hungs when it is sending the mail. I have to press ctrl D before the mail gets send.

The script amended.

LOG1="/tsis/logs/tape_backup.$(date +%d%m%y).log"
LOG="/home/burlal/lovette_backuptest.log"

cp $LOG1 $LOG

a=`cat $LOG|grep fail|wc -l`

if [ $a = "0" ]

then
mailx -s "Backup OK`hostname` " you@you.co.uk
else
mailx -s "Backup failed hostname` " you@you.co.uk
fi

#mv $LOG $LOG1
 
change mailx lines to something like:
Code:
mailx -s &quot;Backup OK`hostname` &quot; you@you.co.uk < /dev/null
 
Maybe better

mailx -s &quot;Backup OK`hostname` &quot; you@you.co.uk > /dev/null

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
It worked when I used jad's.
This is the msg I get on the screen.

Null message body; hope that's ok.

Thanx Guys.

I will like to add a line in the message body which will reads

&quot;please check backup log '/tmp/logs/tape_backup.$(date +%d%m%y).log' for more details.&quot;

 
Why don't you do it like this...
Code:
cat ${LOG} | mailx -s &quot;Backup failed on $(hostname) $(date)&quot; you@you.com
...this way you not only get notified, but you have the log there in the email to look over.

Hope this helps.

 
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=&quot;/root/TAPE.TOC.`date +%Y%m%d`&quot;
TAR_LOGS= &quot;/root/tar.stderr /root/tar.stdout /root/tar_invocation&quot;
ERROR_FILE=$TOC&quot;.err&quot;
DEVICE=&quot;/dev/st0&quot;
MOUNTOPTS=&quot;rsize=8192,wsize=8192&quot;
BACKUP=&quot;tar&quot;

# For Each Remote Server you need add a NAME={mnt_point}
SERVER=&quot;/mnt/server&quot;
SDIRS=&quot;DIR1 DIR2&quot;
# SDIRS for Remote Drives needs $SERVER in front of each Dir to backup.
#SDIRS=&quot;$SERVER/DIR1 $SERVER/DIR2&quot;
ADMIN=&quot;user@isp.com&quot;


## FUNCTIONS ##

function MountDrives ()
{
# Mount Drives
#
echo &quot;Mounting Drives for Backup...&quot;
# 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 &quot;Removing $TAR_LOGS&quot;
if [ -e $TAR_LOGS ]; then
rm -f $TAR_LOGS
fi
}

function Archive ()
{
# Archive Table of Contents
echo &quot;Archive TOC.&quot;
mv /root/TAPE.TOC.* /root/archive
}


function BuildTOC ()
{
# Build table of contents first...
#
echo -n &quot;Building table of contents...&quot;
echo &quot;$SERVER mounted&quot;>>$TOC

echo -n .
ls -lR $SDIRS >> $TOC; echo -n .
echo &quot;done.&quot;
}

function TapeRetention ()
{
# Attempt to retention tape.
# Doesn't really seem to work on AIWA DDS-2 drive.
#
echo &quot;Retentioning tape...&quot;
mt -f $DEVICE reten
}


function Backup ()
{
# Back up the files...
#
echo &quot;Backing up files to tape...&quot;

# 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 [ &quot;$TAR_RESULTS&quot; = &quot;0&quot; ]; then
SUBJECT=&quot;backup cron for `date '+%m-%d-%Y'`&quot;
ERROR=/root/tar.stderr
BODY=&quot;Tar Successful&quot;
else
SUBJECT=&quot;Critical Error on backup for `date '+%m-%d-%Y'`&quot;
ERROR=/root/tar.stderr
BODY=&quot;Tar Failed, on error $TAR_RESULTS &quot;
fi
eval &quot; echo $BODY | /bin/mail -s \&quot;$SUBJECT\&quot; $TO < $ERROR &quot;
}

function UmountDrives ()
{
echo &quot;Umounting Drives ...&quot;
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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top