- backup-mydb.sh
#!/bin/sh
# shell script to backup all mysql db to disk(/var/lib/mysql)
#
# last update: 2009-11-09
# edit to your needs
PROGNAME=$0
ROOTPW="0000"
DUMPFILE="/var/lib/mysql/alldb"
# check current state RO or RW, force RW if not
mount | egrep "ROOT_FS.+rw,"
if [ $? -ne 0 ] ; then
rw=0
/usr/local/sbin/remountrw
else
rw=1
fi
# protect dumpfile from error
set -e
mysqldump --add-drop-table --quote-names -u root -p$ROOTPW --all-databases | gzip -c > $DUMPFILE.tmp
r=$?
if [ "$r" != "0" ] ; then
logger "$PROGNAME - db dump failed!"
exit 1
fi
sync
# shift the current dump to backup dump
[ -f $DUMPFILE.current ] && mv -f $DUMPFILE.current $DUMPFILE.bak
mv -f $DUMPFILE.tmp $DUMPFILE.current
sync
# return to previous state
[ "$rw" = "0" ] && /usr/local/sbin/remountro
Back to top