【MySQL Database】資料庫閃回恢復--binlog2sql
阿新 • • 發佈:2020-12-08
[root@wallet01 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@wallet01 ~]# python -V Python 2.7.5 [root@wallet01 ~]# yum install -y python-pip [root@wallet01 ~]# pip -V pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7) [root@wallet01 ~]# git clone https://github.com/danfengcao/binlog2sql.git Initialized empty Git repository in /root/binlog2sql/.git/ remote: Enumerating objects: 323, done. remote: Total 323 (delta 0), reused 0 (delta 0), pack-reused 323 Receiving objects: 100% (323/323), 151.51 KiB | 245 KiB/s, done. Resolving deltas: 100% (170/170), 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 binlog2sql]# mysql -uroot -p Enter password: mysql> grant select,replication client,replication slave on *.* to 'fb'@'%' identified by 'fb@2020'; Query OK, 0 rows affected (0.03 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000048 | 1049673 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> select now(); +---------------------+ | now() | +---------------------+ | 2020-12-08 16:58:00 | +---------------------+ 1 row in set (0.00 sec) mysql> use soe Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select count(*) from customers; +----------+ | count(*) | +----------+ | 1000000 | +----------+ 1 row in set (0.22 sec) mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> delete from customers where nls_territory = 'Korea'; Query OK, 12140 rows affected (0.80 sec) mysql> commit; Query OK, 0 rows affected (0.02 sec) mysql> select count(*) from customers; +----------+ | count(*) | +----------+ | 987860 | +----------+ 1 row in set (0.21 sec) mysql> select now(); +---------------------+ | now() | +---------------------+ | 2020-12-08 17:13:52 | +---------------------+ 1 row in set (0.00 sec) [root@wallet01 binlog2sql]# python binlog2sql.py -hlocalhost -P3306 -ufb -p'fb@2020' -dsoe -tcustomers \ --start-file='mysql-bin.000048' --only-dml --sql-type=delete \ --start-datetime='2020-12-08 16:58:00' --stop-datetime='2020-12-08 17:13:52' >fb.log [root@wallet01 binlog2sql]# more fb.log DELETE FROM `soe`.`customers` WHERE `cust_last_name`='ayala' AND `customer_id`=459 AND `nls_language`='GV' AND `dob`='1961-12-02 00:00:00' AND `suggestions`='Health' AND `partner_mailshot`='N' AND `customer_class`='Occasional' AND `account_mgr_id`=724 AND `cust_email`='[email protected]' AND `cust_first_name`='terence' AND `customer_since`='2006-08-06 00:00:00' AND `preferred_address`=405839 AND `credit_limit`=10000.00 AND `mailshot`='Y' AND `preferred_card`=711872 AND `nls_territory`='Korea' LIMIT 1; #start 1734987 end 1743286 time 2020-12-08 17:10:30 [root@wallet01 binlog2sql]# tail -n 1 fb.log DELETE FROM `soe`.`customers` WHERE `cust_last_name`='welch' AND `customer_id`=999974 AND `nls_language`='DS' AND `dob`='1960-01-18 00:00:00' AND `suggestions`='Music' AND `partner_mailshot`='N' AND `customer_class`='Occasional' AND `account_mgr_id`=629 AND `cust_email`='[email protected]' AND `cust_first_name`='felix' AND `customer_since`='2004-06-30 00:00:00' AND `preferred_address`=1308796 AND `credit_limit`=6500.00 AND `mailshot`='N' AND `preferred_card`=1164781 AND `nls_territory`='Korea' LIMIT 1; #start 1734987 end 3165963 time 2020-12-08 17:10:30 [root@wallet01 binlog2sql]# python binlog2sql.py -hlocalhost -P3306 -ufb -p'fb@2020' -dsoe -tcustomers \ --start-file='mysql-bin.000048' --start-position=1734987 --stop-position=3165963 -B > rollback.sql [root@wallet01 binlog2sql]# more rollback.sql INSERT INTO `soe`.`customers`(`cust_last_name`, `customer_id`, `nls_language`, `dob`, `suggestions`, `partner_mailshot`, `customer_class`, `account_mgr_id`, `cust_email`, `cust_first_name`, `customer_since`, `preferred_address`, `credit_limit`, `mailshot`, `preferred_card`, `nls_territory`) VALUES ('welch', 999974, 'DS', '1960-01-18 00:00:00', 'Music', 'N', 'Occasional', 629, '[email protected]', 'felix', '2004-06-30 00:00:00', 1308796, 6500.00, 'N', 1164781, 'Korea'); #start 1734987 end 3165963 time 2020-12-08 17:10:30 [root@wallet01 binlog2sql]# mysql -uroot -p < rollback.sql Enter password: [root@wallet01 binlog2sql]# mysql -uroot -p Enter password: mysql> use soe Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select count(*) from customers; +----------+ | count(*) | +----------+ | 1000000 | +----------+ 1 row in set (0.24 sec)