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.gz FILE_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.cn elif [ `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.cn else echo "MySQL back up $FILE have exist!" echo "MySQL back up successful!" fi done # 删除天之前的备份数据 find $basepath -mtime +30 -name "*.sql.gz" -exec rm -rf {} \;