mysql 备份脚本
#!/bin/bash# 要备份的数据库名'多个数据库用空格分开databases=( basedata os-data os-ues policy_manage datadb uee enn_fnt etsp fannengdb filedb fnw-boss riet rules busi uac wit-operation mysql pvsdb pvs-datadb )# 备份文件要保存的目录basepath='/data/backup/'if [ ! -d "$basepath" ]; then mkdir -p "$basepath"fi# 循环databases数组for db in ${databases[*]} do # 备份数据库生成SQL文件/usr/local/mysql/bin/mysqldump -umha -p'admin+1234' -h10.39.41.111 --default-character-set=utf8 -R --triggers --events --opt --hex-blob --single-transaction --master-data=1 $db 2>/dev/null | gzip >$basepath$db-$(date +%Y%m%d-%H).sql.gz#/usr/local/mysql/bin/mysqldump -umha -p'admin+1234' --default-character-set=utf8 -R --triggers --events --opt --set-gtid-purged=on --hex-blob --single-transaction --dump-slave=2 $db 2>/dev/null | gzip >$basepath$db-$(date +%Y%m%d-%H%M%S).sql.gzFILE_NAME="$db-$(date +%Y%m%d-%H).sql.gz" #最终保存的数据库备份文件名FILE=${basepath}${FILE_NAME}#echo "$FILE"# 判断当天备份文件是否生成if [ ! -f "$FILE" ];then echo "####### $DATA #########">> /data/backup/mysql_bak.err echo "MySQL back up $IP hava not created!" >> /data/backup/mysql_bak.err echo "MySQL back up $IP hava not created!" | mail -s "WARN! MySQL back up $IP $FILE hava not created!" DbaAdmin@enn.cnelif [ `ls -s $FILE|awk '{print $1}'` -lt 15 ];then echo "####### $DATA #########">> /data/backup/mysql_bak.err echo "MySQL back up $IP hava created,but file is not greater than 15kB!" >> /data/backup/mysql_bak.err echo "MySQL back up $IP hava created,but file is not greater than 15kB!" | mail -s "WARN! MySQL back up $IP $FILE hava failed!" DbaAdmin@enn.cnelse echo "MySQL back up $FILE have exist!" echo "MySQL back up successful!"fi done# 删除天之前的备份数据find $basepath -mtime +30 -name "*.sql.gz" -exec rm -rf {} \;
赞 (0)