LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-13-2009, 02:52 AM   #1
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Rep: Reputation: 15
Exclamation backup script


hello all,

Currently I have a backup script which every night will write to a tape, and store the tape output to a log file.

In the same script it will then email out the log file to email address i specify.

However, also have (If any errors are encountered) that it writes these errors to a 'error log' file.

However, the problem I have is that when the backup fails - i.e no tape, it just emails a blank email (Which would of been the log file), is there anyway I can email error file (in the same email) or even better would be that if the backup failed, instead of using the log file it emails the error file.

Please see below for my current syntax (minus the variables):

rm /utils/tar.all.log
rm /utils/tar.all.error.log

cd $STARTDIR

tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE

echo "Backup of X Server $STARTDIR finished" $NOW >> $DATESTAMP
tar tvf $TAPEDEVICE |mail -s "System backup of X Server $(date)" -c $C
USTOMEREMAIL $CUSTOMEREMAIL2 $MAIL
tar tvf $TAPEDEVICE >> $TAPELOGFILE
 
Old 01-13-2009, 03:33 AM   #2
fredheeks
LQ Newbie
 
Registered: Jan 2009
Location: Rossendale, UK
Distribution: Fedora
Posts: 5

Rep: Reputation: 0
Just some ideas

Have you just tried checking if anything has been written to the error file. If it has then email the error file, otherwise email the log file.

Something like:

if [-s $TAPEERRORLOGFILE ]; then
echo "Backup of X Server $STARTDIR failed" $NOW >> $DATESTAMP

#Do your error stuff here

else
echo "Backup of X Server $STARTDIR finished" $NOW >> $DATESTAMP

#Do your successful finish stuff here

fi


Note, in a bash script [-s <file>] checks if a file exists and is non-zero in length. I.e. it has some content.
 
Old 01-13-2009, 03:38 AM   #3
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by fredheeks View Post
Have you just tried checking if anything has been written to the error file. If it has then email the error file, otherwise email the log file.

Something like:

if [-s $TAPEERRORLOGFILE ]; then
echo "Backup of X Server $STARTDIR failed" $NOW >> $DATESTAMP

#Do your error stuff here

else
echo "Backup of X Server $STARTDIR finished" $NOW >> $DATESTAMP

#Do your successful finish stuff here

fi


Note, in a bash script [-s <file>] checks if a file exists and is non-zero in length. I.e. it has some content.
Thats a good idea which I can use temporarily, however the error file always has something in it - e.g: removing leading '/', so it means i would always get that email.
 
Old 01-13-2009, 04:43 AM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
You should check the program (process) status after each tar cmd eg
Code:
tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE
if [[ $? -ne 0 ]]
then
    # failed - send failure email and exit
fi
 
Old 01-13-2009, 04:03 PM   #5
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
You should check the program (process) status after each tar cmd eg
Code:
tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE
if [[ $? -ne 0 ]]
then
    # failed - send failure email and exit
fi
Thanks for your help, I wasn't aware that the Tar commands gave 'error codes'

Anyway, see below for the code as it is now (A lot tidier), I haven't tried to run it yet as I am working remotely at the moment.



rm /utils/tar.all.log
rm /utils/tar.all.error.log

cd $STARTDIR

tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE

if[$? =0]

mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $EMAIL < $TAPELOGFILE

else

mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $EMAIL < $TAPEERRORLOGFILE

fi
 
Old 01-13-2009, 08:15 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Note my code formatting; in bash you MUST have spaces between operators and values in if conditions. Also, you must have the keyword 'then' after the 'if' line.

See http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

Incidentally, all Unix cmds/progs deliver a status value ($?) after running. Convention is 0 = success, anything else is a (prog/cmd specific) error, so if $? -ne 0, log the error or email (log only if email fails).

Test every cmd's rtn status if this is for a production system.
http://tldp.org/LDP/abs/html/refcards.html#AEN20918

Last edited by chrism01; 01-13-2009 at 08:19 PM.
 
Old 01-14-2009, 04:17 AM   #7
stuaz
Member
 
Registered: Dec 2008
Posts: 59

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by chrism01 View Post
Note my code formatting; in bash you MUST have spaces between operators and values in if conditions. Also, you must have the keyword 'then' after the 'if' line.

See http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

Incidentally, all Unix cmds/progs deliver a status value ($?) after running. Convention is 0 = success, anything else is a (prog/cmd specific) error, so if $? -ne 0, log the error or email (log only if email fails).

Test every cmd's rtn status if this is for a production system.
http://tldp.org/LDP/abs/html/refcards.html#AEN20918

Thanks, I noticed that after uploading it onto the forums!

rm /utils/tar.all.log
rm /utils/tar.all.error.log

cd $STARTDIR

tar cvf $TAPEDEVICE $DATESTAMP * 2>$TAPEERRORLOGFILE > $TAPELOGFILE

if [ $? = 0 ]
then

mail -s "System backup of $CUSTOMER completed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL >$TAPELOGFILE

else

mail -s "System backup of $CUSTOMER Failed $(date)" -c $CUSTOMEREMAIL1 $CUSTOMEREMAIL2 $VALEEMAIL > $TAPEERRORLOGFILE

fi

This is how it looks now, but when I run it I get a problem with the mail command saying it is expecting:

mail: option requires argument --c
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
regarding backup script ramesh_manu Red Hat 2 02-16-2007 11:01 PM
Need backup script. gtrawoger Linux - Software 2 07-17-2006 07:31 AM
backup script nitaish Linux - General 1 04-22-2005 11:31 AM
Need a backup script enygma Linux - General 5 11-04-2004 03:49 PM


All times are GMT -5. The time now is 11:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration