备份MYSQL脚本

#!/bin/sh
# this shell to backup mysql
#[email protected] (QQ:1413161683 DuChengJiu)

_dbDir=/var/lib/mysql/
_today=`date +%w`
_bakDir=/usr/backup/$_today
[ ! -d $_bakDir ] && mkdir -p $_bakDir
#Create db tar archive
#/usr/local/mysql/share/mysql/mysql.server stop
service mysqld stop
for db in `find $_dbDir -type d|tail -n +2`
do
        echo "Do $db ...."
        realDb=`echo $db|awk -F / '{print $NF}'`
        echo "Backup $realDb ......"
        tar cvf $_bakDir/${realDb}.tar $db || echo "$realDb   :  1" >>$_bakDir/backup-error
done

#/usr/local/mysql/share/mysql/mysql.server start
service mysqld start

#gzip backup
for zip in $_bakDir/*.tar
do
        echo "Compress $zip ........"
        gzip -f $zip
done
find $_bakDir -name "*tar" -exec rm -f {} \;

相关推荐