一、开启binlog。
首先查看binlog是否开启
mysql> show variables like "log_bin";+---------------+-------+|Variable_name | Value +---------------+-------+| log_bin OFF +---------------+-------+1 row in set (0.00 sec) |
值为OFF,需开启,开启binlog方式如下:
#vim /etc/my.cnf |
在[mysqld]中加入
log-bin = mysql-binlog-bin = /usr/local/mysql/log/mysql-bin.log |
重启mysql服务
#service mysqld stop#service mysqld start |
二、模拟数据写入
建库
create database backup; |
建表
CREATE TABLE `number` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号', `updatetime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
写入数据
程序2-1
#coding:utf8#python2.7import MySQLdbimport timedef connect_mysql(db_host="192.168.11.169",user="martin",passwd="martin",db="backup",charset="utf8"): conn = MySQLdb.connect(host=db_host,user=user,passwd=passwd,db=db,charset=charset) conn.autocommit(True)return conn.cursor()#数据插入for i in range(0,10):#time=time.strftime("%Y-%m-%d %H:%M:%S")sql = 'insert into number(updatetime) values(%s)'values = [(time.strftime("%Y-%m-%d %H:%M:%S"))] db1 = connect_mysql()print db1.executemany(sql,values) |
查询数据
mysql> select * from number;+-------+------------------------+| id | updatetime +--------------------------------+| 1 | 2016-06-29 23:27:15 || 2 | 2016-06-29 23:27:15 || 3 | 2016-06-29 23:27:15 || 4 | 2016-06-29 23:27:15 || 5 | 2016-06-29 23:27:15 || 6 | 2016-06-29 23:27:15 || 7 | 2016-06-29 23:27:15 || 8 | 2016-06-29 23:27:15 || 9 | 2016-06-29 23:27:15 || 10 | 2016-06-29 23:27:15 |+-------+------------------------+10 rows in set (0.00 sec) |
三、全量备份
mysqldump -uroot -p -F --master-data=2 backup |gzip> /martin/data/backup_$(date +%F).sql.gz |
注:加-F能刷新binlog,方便恢复时操作。
四、模拟写入增量数据
继续执行程序2-1。
查询数据
mysql> select * from number;+----+---------------------------+| id | updatetime |+----+---------------------------+| 1 | 2016-06-29 23:27:15 || 2 | 2016-06-29 23:27:15 || 3 | 2016-06-29 23:27:15 || 4 | 2016-06-29 23:27:15 || 5 | 2016-06-29 23:27:15 || 6 | 2016-06-29 23:27:15 || 7 | 2016-06-29 23:27:15 || 8 | 2016-06-29 23:27:15 || 9 | 2016-06-29 23:27:15 || 10 | 2016-06-29 23:27:15 || 11 | 2016-06-29 23:31:03 || 12 | 2016-06-29 23:31:03 || 13 | 2016-06-29 23:31:03 || 14 | 2016-06-29 23:31:03 || 15 | 2016-06-29 23:31:03 || 16 | 2016-06-29 23:31:03 || 17 | 2016-06-29 23:31:03 || 18 | 2016-06-29 23:31:03 || 19 | 2016-06-29 23:31:03 || 20 | 2016-06-29 23:31:03 |+-------+---------------------+20 rows in set (0.00 sec) |