Email script when automatic backup is finsihed

Hello all,

i'm still new to this site and thought i might find some help here :).
lately i performed a script to make an automatic backup of some files in certain directories. the script looks something like this:

#! /bin/bash

##############VARIABLES

path=/export/home/cassi/Backup
timestamp=`date +%Y%m%d`
host=`hostname`
file_in=$path/files2tar
tarfile=$path/$host-$timestamp-bak.tar

################TAR AND GZIP FILES

data=`cat $file_in`
tar cf $tarfile $data
/usr/bin/gzip $tarfile

With this script the backup of files works fine. This script is also set as a cronjob to be run weekly.

My question is this. I need to find a way to make an automatic email if all files are successfully transfered. is there a way to perform this?

Thanks for reading,

Matthew

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

You can check $? which will have the exit code of each former command (maybe your tar and gzip) with an if/fi instruction for example and then decide to send a mail or not. If $? is > 0 then it might have had some troubles.

Hi Wizard,

Try the following -

data=`cat $file_in`
tar cf $tarfile $data > /dev/null
res1 = $?
/usr/bin/gzip $tarfile > /dev/null
res2 = $?

if [ $res1 -eq 0 ]  && [ res2 = 0 ]; then
   echo "BackUP & Zip operation successful." > /tmp/mailtext
elif [ $res1 -eq 0 ]; then
   echo "BackUP is successful.  Some issue in Zip operation." > /tmp/mailtext
elif [ $res2 -eq 0]; then
   echo "Backup Failed" > /tmp/mailtext
fi

mailx -s "Backup Result"  <your email id>  < /tmp/mailtext

rm /tmp/mailtext

#Done

Thanks for your reply. Forgive my ignorance, i am still very new to UNIX and scripting. Can you show me such example?

thanks.

---------- Post updated at 08:01 AM ---------- Previous update was at 08:00 AM ----------

Thanks ggs i will try that!

---------- Post updated at 08:22 AM ---------- Previous update was at 08:01 AM ----------

Hi ggs, is there a way to cinfigure mailx? the reason is that the files are stored in this machine which i then need to receive an email on a windows machine.

Check for posts with "uuencode" using the search function of the forum. They will show you how to send mails with files as attachment.