Bash script for mysql backup - error handling
- by Jure1873
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