Commenting out an echo killed my bash backup script
Archive - Originally posted on "The Horse's Mouth" - 2015-01-19 02:41:19 - Graham Ellis
"Commenting out an echo killed my bash script" - yes, really!
Here (with password amended!) is part of my bash script which takes a database snapshot from time to time - and it's been running well over a 24 hour cycle, giving me checkpoints to return to. But it was getting a bit verbose, so I added a simple # in front of the successful emailing ine.
/usr/local/mysql/bin/mysqldump -ufgw -psummathere -h127.0.0.1 fgw > /home/backups/$HOUR/fgw.sql
if [ "$?" -eq 0 ]; then
echo "Success $HOUR fgw" | mail -s "FGW good dump" graham@wellho.net
else
echo "Mysqldump encountered a problem $HOUR fgw" | mail -s "FGW BAD dump" graham@wellho.net
fi
My change got rid of the good backup message which had been "spamming" me during testing ... but it also got rid of the rest of my backup procedure. Oops. It turns out that you're not allowed an empty block in an if!
Advise is to refactor the script - or to add a null statement into the block if you want to retain the ability to uncomment the echo and start emailing again (or whatever your programmer-switchable action is!). Suitable null statements are :
(yes, a sinle colon) or echo nothing > /dev/null
but please note I have not yet tested these. I'm more concerned, as I write this, to be handcranking a backup and watching the backup pots refill with complete data sets!