LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   output of a tar command not sending to my E-Mail (http://www.linuxquestions.org/questions/linux-newbie-8/output-of-a-tar-command-not-sending-to-my-e-mail-878956/)

amario26 05-05-2011 04:45 AM

output of a tar command not sending to my E-Mail
 
Hi,

Basically I have a script that runs every day (via crontab) and it works perfectly! Only problem is that the job will only send the output of most of the stuff and not the tar bit..

This is the main part of my script:

Code:

if (( $days1 <= 5 ))
          then
                echo
                echo "*******" "Begin Removing All Previous Log Files" "*******"
                        rm ./*cron.log
                echo
                echo "*******" "Begin Removing Files" "*******"
                        ssh username@servername rm ./../test/$TODAY/*.gz
                echo
                echo "*******" "Checking the following directories: $BACKUP" "*******"
                echo
                set -- $BACKUP
                        DIRS=$@
                        for i in "$@"
                                do
                                if [ -d $i ]; then
                                echo "Directory $i exists"
                                else
                                echo "!!!!!!! Directory $i doesnt exist - This directory will not be backed up. Your backup will continue as normal, skipping this directory. !!!!!!!"
                        fi
                done
                echo
                echo "*******" "Begin compressing and transferring files" "*******"
                echo
                        tar -czp $BACKUP | ssh -c blowfish username@servername "dd of=/home/test/$TODAY/"$DATETIME"__TestBackup.tar.gz"
                echo
          else
                echo "Your backup has failed"
    fi
done

It will only email me up to the bit where it says echo "*******" "Begin compressing and transferring files" "*******" after that it wont email me the output of the tar command just below it...

Here is my crontab line that does the mailing:

Code:

00 22 * * 1-5 bash /home/user/RoutineBackupTest.sh | mail -s "Backup Log" firstname.surname@company.com
Do any of you experts know why that is? I've been trying for ages to figure it out! I cant even get mutt -a to work.. nor tee..

Many thanks for your help in advance, its appreciated.

neonsignal 05-05-2011 05:20 AM

The standard output stream of tar is the tarfile itself (which is put by the script into the backup file). I'm guessing that what you want to see is the standard error stream, where diagnostics are more often sent.

Since your crontab script only pipes the stdout to the email, you are losing the stderr output. In bash you can use something like this to pass both stderr and stdout through the pipe to the email:

Code:

00 22 * * 1-5 bash /home/user/RoutineBackupTest.sh 2>&1 | mail -s "Backup Log" firstname.surname@company.com
(where the '2>&1' says to redirect stderr output to stdout)

Alternatively you can just use:

Code:

00 22 * * 1-5 bash /home/user/RoutineBackupTest.sh |& mail -s "Backup Log" firstname.surname@company.com
(which might be more portable, in cases where you aren't using bash)

amario26 05-05-2011 06:03 AM

Thanks neonsignal!

That was pretty much what I wanted but the only thing is now that when the E-Mail arrives it tells me its failed.. It was working before I added '2>&1' to the crontab as you suggested.. any ideas?

Many thanks

amario26 05-05-2011 06:09 AM

Actually.. ignore me.. I was typing some stuff and my fat fingers made a typo!

Thanks dude!


All times are GMT -5. The time now is 10:13 AM.