2.4.1.誤刪數據的單表恢復
對於誤刪除單表數據的情況下,我們仍然可以採用innobackupex 的方式,使用全部+增備+聯機binlog日誌進行恢復,恢復過程跟2.3.5單表的完全恢復的過程差不多。但是這樣顯的很笨重,因爲需要拷貝全備增備進行日誌應用,這個在庫大的時候還是比較耗時間的。對於這種單純因爲誤操作刪除了數據的情況,我們可以使用binlog2sql工具。
2.4.2.安裝
2.4.2.1.安裝pip
獲取阿里源:
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# yum clean all
# yum makecache
# yum repolist
如果生產環境沒有公網,可以自己弄一臺本地環境,把pip安裝一遍,然後把緩存的rpm保存下來,上傳到生產環境安裝即可。
2.4.2.2.安裝pip
# yum -y install epel-release
# yum install python-pip
# pip install --upgrade pip
2.4.2.3.修改pip源
# mkdir ~/.pip
# vi ~/.pip/pip.conf
[global] index-url = http://mirrors.aliyun.com/pypi/simple
[install] trusted-host=mirrors.aliyun.com |
2.4.2.4.下載binlog2sql
下載地址:
https://github.com/danfengcao/binlog2sql
# ll binlog2sql-master.zip
-rw-r--r-- 1 root root 31549 May 7 22:48 binlog2sql-master.zip
# unzip binlog2sql-master.zip
# cd binlog2sql-master
# pip install -r requirements.txt
Collecting PyMySQL==0.7.11 (from -r requirements.txt (line 1))
Downloading http://mirrors.aliyun.com/pypi/packages/c6/42/c54c280d8418039bd2f61284f99cb6d9e0eae80383fc72ceb6eac67855fe/PyMySQL-0.7.11-py2.py3-none-any.whl (78kB)
100% |████████████████████████████████| 81kB 3.7MB/s
Collecting wheel==0.29.0 (from -r requirements.txt (line 2))
Downloading http://mirrors.aliyun.com/pypi/packages/8a/e9/8468cd68b582b06ef554be0b96b59f59779627131aad48f8a5bce4b13450/wheel-0.29.0-py2.py3-none-any.whl (66kB)
100% |████████████████████████████████| 71kB 8.3MB/s
Collecting mysql-replication==0.13 (from -r requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/dd/23/384047702e694139e9fe75a8ba7ad007e8942fd119ebadabc32ce19f70f2/mysql-replication-0.13.tar.gz
Installing collected packages: PyMySQL, wheel, mysql-replication
Running setup.py install for mysql-replication ... done
Successfully installed PyMySQL-0.7.11 mysql-replication-0.13 wheel-0.29.0
2.4.3.使用前提
2.4.3.1.mysql server參數設置
Mysql server必須設置一下參數:
[mysqld]
server_id = 1
log_bin = /var/log/mysql/mysql-bin.log
max_binlog_size = 1G
binlog_format = row
binlog_row_image = full
2.4.3.2.user需要的最小權限
select, super/replication client, replication slave
建議授權
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO user;
權限說明:
select:
需要讀取server端information_schema.COLUMNS表,獲取表結構的元信息,拼接成可視化的sql語句
super/replication client:
兩個權限都可以,需要執行'SHOW MASTER STATUS', 獲取server端的binlog列表
replication slave:
通過BINLOG_DUMP協議獲取binlog內容的權限。
我們下面的例子使用root用戶,所以權限沒有問題。
2.4.4.優缺點
2.4.4.1.使用限制
mysql server必須開啓,離線模式下不能解析
參數 binlog_row_image 必須爲FULL,暫不支持MINIMAL
解析速度不如mysqlbinlog
2.4.4.2.優點(對比mysqlbinlog)
純Python開發,安裝與使用都很簡單
自帶flashback、no-primary-key解析模式,無需再裝補丁
flashback模式下,更適合閃回實戰
解析爲標準SQL,方便理解、篩選
代碼容易改造,可以支持更多個性化解析
2.4.5.使用恢復案例
2.4.5.1.測試表數據檢查
mysql> use test ;
mysql> select * from tmp_list_001 ;
+----+-------------+
| id | name |
+----+-------------+
| 1 | wufan |
| 2 | zhangsan |
| 3 | lisi |
| 4 | wangwu |
| 5 | liutao |
| 6 | liuyifei |
| 7 | luozhixiang |
| 8 | liudehua |
| 9 | guofucheng |
+----+-------------+
2.4.5.2.delete操作清空數據
mysql> delete from tmp_list_001 ;
mysql> select * from tmp_list_001 ;
Empty set (0.00 sec)
2.4.5.3.查看目前binlog
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mybinlog.000054 | 1370 | | | |
+-----------------+----------+--------------+------------------+-------------------+
2.4.5.4.定位誤操作位置
最新的binlog文件是mybinlog.000054,我們再定位誤操作SQL的binlog位置。誤操作人一般只能記得大致的誤操作時間,我們根據大致時間過濾數據。
# python /root/binlog2sql-master/binlog2sql/binlog2sql.py \
> -uroot \
> -pR00t_123 \
> -dtest \
> -t tmp_list_001 \
> --start-file='mybinlog.000054' \
> --start-datetime='2020-05-08 00:00:01' \
> --stop-datetime='2020-05-08 00:06:01'
DELETE FROM `test`.`tmp_list_001` WHERE `id`=1 AND `name`='wufan' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=2 AND `name`='zhangsan' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=3 AND `name`='lisi' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=4 AND `name`='wangwu' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=5 AND `name`='liutao' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=6 AND `name`='liuyifei' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=7 AND `name`='luozhixiang' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=8 AND `name`='liudehua' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
DELETE FROM `test`.`tmp_list_001` WHERE `id`=9 AND `name`='guofucheng' LIMIT 1; #start 989 end 1339 time 2020-05-08 00:02:35
注意每條sql的最後的註釋,明確的告訴我們了再binlog日誌中的start_pos和stop_pos位置。
2.4.5.5.生成回滾sql
# python /root/binlog2sql-master/binlog2sql/binlog2sql.py \
> -uroot \
> -pR00t_123 \
> -dtest \
> -t tmp_list_001 \
> --start-file='mybinlog.000054' \
> --start-position=989 \
> --stop-position=1339 \
> -B > /tmp/rollback.sql
2.4.5.6.查看回滾sql內容
# cat /tmp/rollback.sql
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (9, 'guofucheng'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (8, 'liudehua'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (7, 'luozhixiang'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (6, 'liuyifei'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (5, 'liutao'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (4, 'wangwu'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (3, 'lisi'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (2, 'zhangsan'); #start 989 end 1339 time 2020-05-08 00:02:35
INSERT INTO `test`.`tmp_list_001`(`id`, `name`) VALUES (1, 'wufan'); #start 989 end 1339 time 2020-05-08 00:02:35
2.4.5.7.執行回滾語句
確定回滾語句沒問題後,就可以執行回滾操作。
# mysql -uroot -pR00t_123 -D test < /tmp/rollback.sql
2.4.5.8.檢查數據恢復
mysql> select * from tmp_list_001 ;
+----+-------------+
| id | name |
+----+-------------+
| 1 | wufan |
| 2 | zhangsan |
| 3 | lisi |
| 4 | wangwu |
| 5 | liutao |
| 6 | liuyifei |
| 7 | luozhixiang |
| 8 | liudehua |
| 9 | guofucheng |
+----+-------------+
2.4.5.9.使用詳細說明
2.4.5.9.1.解析模式
--stop-never 持續解析binlog。可選。默認False,同步至執行命令時最新的binlog位置。
-K, --no-primary-key 對INSERT語句去除主鍵。可選。默認False
-B, --flashback 生成回滾SQL,可解析大文件,不受內存限制。可選。默認False。與stop-never或no-primary-key不能同時添加。
--back-interval -B模式下,每打印一千行回滾SQL,加一句SLEEP多少秒,如不想加SLEEP,請設爲0。可選。默認1.0。
2.4.5.9.2.解析範圍控制
--start-file 起始解析文件,只需文件名,無需全路徑 。必須。
--start-position/--start-pos 起始解析位置。可選。默認爲start-file的起始位置。
--stop-file/--end-file 終止解析文件。可選。默認爲start-file同一個文件。若解析模式爲stop-never,此選項失效。
--stop-position/--end-pos 終止解析位置。可選。默認爲stop-file的最末位置;若解析模式爲stop-never,此選項失效。
--start-datetime 起始解析時間,格式'%Y-%m-%d %H:%M:%S'。可選。默認不過濾。
--stop-datetime 終止解析時間,格式'%Y-%m-%d %H:%M:%S'。可選。默認不過濾。
2.4.5.9.3.對象過濾
-d, --databases 只解析目標db的sql,多個庫用空格隔開,如-d db1 db2。可選。默認爲空。
-t, --tables 只解析目標table的sql,多張表用空格隔開,如-t tbl1 tbl2。可選。默認爲空。
--only-dml 只解析dml,忽略ddl。可選。默認False。
--sql-type 只解析指定類型,支持INSERT, UPDATE, DELETE。多個類型用空格隔開,如--sql-type INSERT DELETE。可選。默認爲增刪改都解析。用了此參數但沒填任何類型,則三者都不解析。