Operating System - Linux
1828838 Members
2772 Online
109985 Solutions
New Discussion

E-Mail Notification After Tar is Complete

 
SOLVED
Go to solution
Andrew Kaplan
Super Advisor

E-Mail Notification After Tar is Complete

Hi there --

I am going to be setting up a script that will run a tar job at a particular time every night. I would like to have an e-mail notification set up to let me know whether or not the job completed successfully. How would I go about doing that? Thanks.
A Journey In The Quest Of Knowledge
9 REPLIES 9
Jeff_Traigle
Honored Contributor
Solution

Re: E-Mail Notification After Tar is Complete

Just check the return code for the tar command:

tar c /some/path
RC=$?

if [ ${RC} -eq 0 ]
then
echo "tar was successful"
else
echo "tar was unsuccessful. Return code was ${RC}."
fi
--
Jeff Traigle
Ivan Krastev
Honored Contributor

Re: E-Mail Notification After Tar is Complete

You can use last exit code - $?
For example :


tar .......

if [ $? ];
then
mail "tar is OK" ...
fi


regards,
ivan
Jeff_Traigle
Honored Contributor

Re: E-Mail Notification After Tar is Complete

And if you want the email to go to a user other than the one running the script via cron, you can pipe the echo commands above to the mail program:

echo "My message" | mailx -s "tar job status" yourname@company.com
--
Jeff Traigle
Andrew Kaplan
Super Advisor

Re: E-Mail Notification After Tar is Complete

Hi there --

I created a script using the syntax that was mentioned in a previous reply. Here is what it looks like so far:

# This script is designed to run selective nightly backups
# on the Hadron server to the locally attached tape drive
# via the tar utility. Once the tape backup is complete,
# an e-mail notification is sent out to nofify the system
# administrator on the success or failure of the backup.

# Prior to running this script, ensure there is a backup_list.txt
# file located in the /root directory. This file is necessary in
# order for there to be a selective backup job.

# cd /root

# Run the following command in order to complete the selective
# backup of the server.

sh -c 'tar -cf /dev/st0 $(cat /root/backup_list.txt)'

# The following commands determine if the above command was successful,
# and send the appropriate notification via e-mail.

RC=$?

if [${RC} -eq 0 ]
then
echo "Hadron nightly backup was completed successfully" | mailx -s "Hadron Nightly Backups" ahkaplan@partners.org
else
echo "Hadron nightly backup was not successfully completed. The return code was ${RC}." | mailx -s "Hadron Nightly Backups" ahkaplan@partners.org
fi

I have two questions:

1. I am encountering an error with the line if [${RC} -eq 0 ]. The error I get is [0: command not found. I am guessing the syntax is wrong, but I don't know what the correction should be.

2. Earlier in the script I want to check for the presence of a backup_list.txt file in the /root directory. What syntax would I use to check for the file, and if it is not there exit with and error 1 status, and continue if it is there? Thanks.
A Journey In The Quest Of Knowledge
Ivan Krastev
Honored Contributor

Re: E-Mail Notification After Tar is Complete

For file checking see this article - http://linuxreviews.org/beginner/bash_GNU_Bourne-Again_SHell_Reference/#toc2


Best way is to use -s (Check if file has size greater than 0)


regards,
ivan
Jeff_Traigle
Honored Contributor

Re: E-Mail Notification After Tar is Complete

Make sure there's a space between [ and ${RC}.
--
Jeff Traigle
Jeff_Traigle
Honored Contributor

Re: E-Mail Notification After Tar is Complete

And to check for the existence of the file:

if [ ! -f /root/backup_list.txt ]
then
echo "No backup list file. Exiting"
exit
fi
--
Jeff Traigle
Peter Nikitka
Honored Contributor

Re: E-Mail Notification After Tar is Complete

Hi,

besides the missing space in the test operator: there is no need to wrap the tar command into a sh call:

Change
sh -c 'tar -cf /dev/st0 $(cat /root/backup_list.txt)'
to
tar cf /dev/st0 $(RC=$?

if [ $RC -eq 0 ]
then echo OK-message
else echo FAIL-$RC-message
fi | mailx -s Backup-Msg to_user@addr


The $(<..) can be used as a substitute in ksh/bash/zsh .

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Andrew Kaplan
Super Advisor

Re: E-Mail Notification After Tar is Complete

Thanks to all for your help. I have the script completed, and it appears to be working quite well.
A Journey In The Quest Of Knowledge