Bash script for mysql backup - error handling

Posted by Jure1873 on Server Fault See other posts from Server Fault or by Jure1873
Published on 2012-10-04T07:21:43Z Indexed on 2012/10/04 9:40 UTC
Read the original article Hit count: 315

Filed under:
|
|
|

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

© Server Fault or respective owner

Related posts about mysql

Related posts about backup