I'm trying to backup a bunch of MyISAM tables in a way that would allow me to rsync/rdiff the backup directory to a remote location. I've came up with a script that dumps only the recently changed tables and sets the date of the file so that rsync can pick up only the changed ones, but now I don't know how to do the error handling - I would like the script to exit with a non 0 value if there are errors. How could I do that?
#/bin/bash
BKPDIR="/var/backups/db-mysql"
mkdir -p $BKPDIR
ERRORS=0
FIELDS="TABLE_SCHEMA, TABLE_NAME, UPDATE_TIME"
W_COND="UPDATE_TIME >= DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND TABLE_SCHEMA<>'information_schema'"
mysql --skip-column-names -e "SELECT $FIELDS FROM information_schema.tables WHERE $W_COND;" | while read db table tstamp; do
echo "DB: $db: TABLE: $table: ($tstamp)"
mysqldump $db $table | gzip > $BKPDIR/$db-$table.sql.gz
touch -d "$tstamp" $BKPDIR/$db-$table.sql.gz
done
exit $ERRORS
The
mysqldump
command returns 0 for success and >0 for warning/error conditions. As you are looping you would need to protect $ERRORS from being overwritten by subsequent successful commands so a bit of logic is neededNow, when your script exits it will exit with 0 or the status from the first error encountered.
Ok, I had to reorder things a little. Iain's answer didn't work, as mysqldump is piped to gzip so I got the return value of gzip so I used PIPESTATUS and then I had to reorder the command for the while loop to get the Errors variable outside of the subshell.
I use
after mysqldump command.
$? returns status of the last executed command.
If you want you could check results after all commands and set ERRORS based on multiple results.