As piping in bash commands is common and very usefull, controling the exit status of each piped commands in bash scripting can be vital, especially for backups.
mysqldump -u $USERNAME -p $PASS $DBNAME | bzip2 -9c -c > Dump.sql.gz
Now, what if the backup failed and the bzip2 command succeeded? In fact, the exit status will be the one of the last command.
echo $? 0
And this will be always successfull.
echo ${PIPESTATUS[1]} will refer to the compression
So, one solution in our example could be:
mysqldump -u $USERNAME -p $PASS $DBNAME | bzip2 -9c -c > Dump.sql.gz
if [ ${PIPESTATUS[0]} -ne "0" ]
then
echo "the MySQL Database backup failed with Error: ${PIPESTATUS[0]}";
else
echo "The MySQL Database backup was successfull!";
fi
I hope it will help...