【MySQL】数据库闪回工具之binlo
短信预约 -IT技能 免费直播动态提醒
[root@wallet01 ~]# tar zxvf Python-3.6.1.tgz
[root@wallet01 ~]# cd Python-3.6.1
[root@wallet01 Python-3.6.1]# ./configure --prefix=/usr/local/python3
[root@wallet01 Python-3.6.1]# make && make install
[root@wallet01 ~]# rm -rf /usr/bin/python
[root@wallet01 ~]# rm -rf /usr/bin/pip
[root@wallet01 ~]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
[root@wallet01 ~]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
[root@wallet01 ~]# vi /etc/profile
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
[root@wallet01 ~]# source /etc/profile
[root@wallet01 ~]# python -V
Python 3.6.1
[root@wallet01 ~]# pip -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
[root@wallet01 ~]# pip install --upgrade pip
[root@wallet01 ~]# pip -V
pip 18.0 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
[root@wallet01 ~]# git clone https://github.com/danfengcao/binlog2sql.git
Initialized empty Git repository in /root/binlog2sql/.git/
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects: 100% (307/307), 147.79 KiB | 68 KiB/s, done.
remote: Total 307 (delta 0), reused 1 (delta 0), pack-reused 303
Resolving deltas: 100% (158/158), done.
[root@wallet01 ~]# cd binlog2sql
[root@wallet01 binlog2sql]# pip install -r requirements.txt
[root@wallet01 binlog2sql]# cd binlog2sql/
[root@wallet01 binlog2sql]# python binlog2sql.py --help
usage: binlog2sql.py [-h HOST] [-u USER] [-p [PASSWORD [PASSWORD ...]]]
[-P PORT] [--start-file START_FILE]
[--start-position START_POS] [--stop-file END_FILE]
[--stop-position END_POS] [--start-datetime START_TIME]
[--stop-datetime STOP_TIME] [--stop-never] [--help]
[-d [DATABASES [DATABASES ...]]]
[-t [TABLES [TABLES ...]]] [--only-dml]
[--sql-type [SQL_TYPE [SQL_TYPE ...]]] [-K] [-B]
[--back-interval BACK_INTERVAL]
--start-file --起始解析文件
--stop-file --终止解析文件
--start-position --起始解析位置
--stop-position --终止解析位置
--start-datetime --起始解析时间,格式'%Y-%m-%d %H:%M:%S'。
--stop-datetime --终止解析时间,格式'%Y-%m-%d %H:%M:%S'。
-d --仅解析目标db的sql
-t --仅解析目标table的sql
--only-dml --仅解析dml,忽略ddl。
--sql-type --仅解析指定类型,支持insert,update,delete。
-B --生成回滚SQL
[root@wallet01 ~]# mysql -uroot -pabcd.1234
mysql> grant select,replication client,replication slave on *.* to 'flashback'@'%' identified by 'flashback';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@wallet01 mysql]# mysql -utpcc -ptpcc
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2019-01-04 17:01:07 |
+---------------------+
1 row in set (0.00 sec)
mysql> use tpcc
mysql> select count(*) from warehouse;
+----------+
| count(*) |
+----------+
| 10 |
+----------+
1 row in set (0.00 sec)
mysql> delete from warehouse where w_id = 1;
Query OK, 1 row affected (0.07 sec)
mysql> select count(*) from warehouse;
+----------+
| count(*) |
+----------+
| 9 |
+----------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2019-01-04 17:14:09 |
+---------------------+
1 row in set (0.00 sec)
[root@wallet01 binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uflashback -p'flashback' -dtpcc100 --start-file='mysql-bin.000008' \
> --start-datetime='2019-01-04 17:01:07' \
> --stop-datetime='2019-01-04 17:14:09'
DELETE FROM `tpcc`.`warehouse` WHERE `w_id`=1 AND `w_name`='ltjfn6aUnj' AND `w_street_1`='eWRUrBRPS9nukJ' AND
`w_street_2`='GXyRahKIL6Mva9cl' AND `w_city`='9CGyLhLlp9fXQhmxdD' AND `w_state`='JV' AND `w_zip`='815964094' AND
`w_tax`=0.15 AND `w_ytd`=3680226.00 LIMIT 1; #start 430 end 712 time 2019-01-04 17:04:25
[root@wallet01 binlog2sql]# python binlog2sql.py -h127.0.0.1 -P3306 -uflashback -p'flashback' -dtpcc100 --start-file='mysql-bin.000008' \
> --start-position=430 --stop-position=712 -B > rollback.sql
[root@wallet01 binlog2sql]# cat rollback.sql
INSERT INTO `tpcc`.`warehouse`(`w_id`, `w_name`, `w_street_1`, `w_street_2`, `w_city`, `w_state`, `w_zip`, `w_tax`, `w_ytd`)
VALUES (1, 'ltjfn6aUnj', 'eWRUrBRPS9nukJ', 'GXyRahKIL6Mva9cl', '9CGyLhLlp9fXQhmxdD', 'JV', '815964094', 0.15, 3680226.00);
#start 430 end 712 time 2019-01-04 17:04:25
[root@wallet01 binlog2sql]# mysql -utpcc -ptpcc < rollback.sql
[root@wallet01 ~]# mysql -utpcc -ptpcc
mysql> use tpcc
Database changed
mysql> select count(*) from warehouse;
+----------+
| count(*) |
+----------+
| 10 |
+----------+
1 row in set (0.00 sec)
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341