首页 > 代码库 > mysql自动备份策略
mysql自动备份策略
目标:每7天做一个完整备份,每天做一份binlog日志,第二周将之前的备份删除并产生新的完整备份和binlog日志,备份要求每天2:00自动完成
mysql 版本:mysql5.5
1.开启binlog
vim /etc/my.cnf
[mysqld]
log-bin=binlog 2.重启mysqld服务
/etc/init.d/mysqld restart 验证binlog开启成功
/usr/local/mysql/var/下会出现binlog.0000001,binlog.index文件 3.刷新日志产生新的binlog
mysql -u root -p flush-logs 4.删除前一时期的日志除了最后一个
rm -f `sed -n ‘$!p‘ binlog.index` 5.产生新的完整备份
mysqldum --all-databases -u root -p 6.删除最后一个日志
rm -f `sed -n ‘$p‘ binlog.index` 7.产生新的增量备份
mysqladmin -u root -p flush-logs 8.删除最后一份日志
rm -f `sed -n ‘$!p‘ binlog.index` 9.将新的日志信息重新写入binlog.index
sed -n ‘$p‘ binlog.index >tmp
cat tmp >binlog.index
操作步骤: 一.每天凌晨零点一分产生一份binlog,用crontab实现
crontab -e
01 00 * * * mysqladmin -u root -pdbpass flush-logs 二.用脚本实现每周产生一个新的完整备份,并将原来的增量备份全删除
1.vim mysql_bak.sh #!/bin/bash
#This script will backup mysql database MYSQL_DIR=/usr/local/mysql #delete old binlogs except the last one
rm -f `sed -n ‘$!p‘ $MYSQL_DIR/data/binlog.index` #backup db
mysqldump -u root -pdbpass dbname >/var/backup/dbname_`date +"%Y%m%d"`.sql #delete the last binlog
rm -f `sed -n ‘$p‘ $MYSQL_DIR/data/binlog.index` #create new binlog by flush-logs
mysqladmin -u root -pdbpass flush-logs #write new binlog into binlog.index
sed -n ‘$p‘ $MYSQL_DIR/data/binlog.index >/tmp/tmp.index
cat /tmp/tmp.index >$MYSQL_DIR/data/binlog.index2.mv mysql_bak.sh /usr/local/mysql/bin/3.crontab实现每周日备份
crontab -e
00 02 * * 7 /usr/local/mysql/bin/mysql_bak.sh
本文来自: 华盟黑客(中国最大黑客培训中心网站) 详细出处参考:http://www.hmhack.com/article/html/493.html
mysql自动备份策略