MySQL 备份脚本

Shell/Python  
#!/bin/bash
dstpath=/data/backup/db  
today=`date +%Y%m%d`  
backpath=$dstpath/$today                   # 备份目录格式 /data/backup/db/20150609  
dbhost=127.0.0.1                           # 数据库地址  
dbuser=bakuser                             # MySQL备份用户  
dbpasswd=123456                            # MySQL备份密码  
dbs=(game game2)                           # 需要备份的库空格隔开

mkdir -p $backpath  
cd $backpath  
for dbname in ${dbs[@]}  
do  
    mysqldump -h$dbhost -u$dbuser -p$dbpasswd --single-transaction --flush-logs -R $dbname|gzip > ${dbname}.`date +%H%M%S`.sql.gz 2>> $backpath/erros.log     # 备份文件格式 game.时分秒.sql.gz 
    [ $? == 0 ] && echo "backup $dbname success"
done  
# 删除超过30天的备份
find $dstpath -mtime +30 -exec rm -rf {} \;   

#备份失败发送邮件提醒
if [ `cat  $backpath/erros.log|wc -l` -gt 0 ];then  
        echo "dbbackup up failed" | mutt -s "Wran: dbbackup error" < $backpath/erros.log admin@imdst.com
fi