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.
1
|
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.
1
2
|
echo $? 0 |
And this will be always successfull.
So, one solution in our example could be:
1
2
3
4
5
6
7
8
|
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 |
Article Number: 330
Posted: Wed, Jul 25, 2018 2:00 PM
Last Updated: Wed, Jul 25, 2018 2:00 PM
Online URL: http://kb.ictbanking.net/article.php?id=330