溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

由percona-data-recovery-tool恢復ibd數(shù)據(jù)的工具的一些測試

發(fā)布時間:2020-08-09 05:53:14 來源:ITPUB博客 閱讀:425 作者:darren__chan 欄目:MySQL數(shù)據(jù)庫

        無意間看到了percona-data-recovery-tool 這個工具,這個工具是用來恢復innodb數(shù)據(jù)文件中的數(shù)據(jù),貌似都建議row_format必須是REDUNDANT或者COMPACT。而在mysql5.7.8以上默認為Dynamic,但其實在此處我在Dynamic下測試是沒有問題的。

當我們誤刪除某個表數(shù)據(jù)時,實際上數(shù)還存留在數(shù)據(jù)文件中,因此可通過特殊手段從數(shù)據(jù)文件中抽取相應的數(shù)據(jù)。

數(shù)據(jù)被誤刪除后,需要盡快將保護現(xiàn)場,停止數(shù)據(jù)庫,把 idb 文件拷貝出來,防止 ibd 文件寫入數(shù)據(jù)被覆蓋。


安裝方法:

https://launchpadlibrarian.net/78359944/percona-data-recovery-tool-for-innodb-0.5.tar.gz

yum install glibc-static

yum -y install perl-DBD-MySQL.x86_64


tar -xf percona-data-recovery-tool-for-innodb-0.5.tar.gz

cd percona-data-recovery-tool-for-innodb-0.5

cd mysql-source/

./configure

cd ..

make


解析 ibd 文件:此處會將ibd文件以16k為單位分割成n個文件

./page_parser -5 -f /home/mysql/datal/cwdtest/card.ibd  

參數(shù)解釋:

-5:代表 row format 為 Compact

-f:代表要解析的文件,innodb的ibd文件

[root@xxxx-sql-auditing percona-data-recovery-tool-for-innodb-0.5]# ./page_parser -5 -f /home/mysql/datal/cwdtest/card.ibd
Opening file: /home/mysql/datal/cwdtest/card.ibd:
64769		ID of device containing file
189843585		inode number
33184		protection
1		number of hard links
1001		user ID of owner
1001		group ID of owner
0		device ID (if special file)
9437184		total size, in bytes
4096		blocksize for filesystem I/O
18432		number of blocks allocated
1543312312	time of last access
1543394915	time of last modification
1543394915	time of last status change
9437184	Size to process in bytes
104857600	Disk cache size in bytes
83.25% done. 2018-11-28 16:52:19 ETA(in 00:00 hours). Processing speed: 7856357 B/sec


生成表定義

./create_defs.pl -host 127.0.0.1 -port 3306 -user root -password mysql123 -db cwdtest -table card >include/table_defs.h

make



開始恢復 pages 中刪除的數(shù)據(jù):

./constraints_parser -5 -D -f ./pages-1543395138/FIL_PAGE_INDEX/0-602/22-00000025.page >/tmp/22-00000025.page.txt

參數(shù):

-5 -f 的參數(shù)和 page_parser 相同;

-D:該參數(shù)的含義為代表恢復刪除的數(shù)據(jù)頁;

[root@xxxx-sql-auditing percona-data-recovery-tool-for-innodb-0.5]# ./constraints_parser -5 -D -f ./pages-1543395138/FIL_PAGE_INDEX/0-602/40-00000071.page >/tmp/00000071.sql
LOAD DATA INFILE '/usr/local/src/percona-data-recovery-tool-for-innodb-0.5/dumps/default/SYS_TABLES' REPLACE INTO TABLE `SYS_TABLES` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'SYS_TABLES\t' (NAME, ID, N_COLS, TYPE, MIX_ID, MIX_LEN, CLUSTER_NAME, SPACE);
[root@xxxx-sql-auditing percona-data-recovery-tool-for-innodb-0.5]# 
'


然后生成的文件就用load data 方式插入,其實該方法介紹的文章很多,這里只給出關鍵步驟。而對于在誤刪數(shù)據(jù)后是否數(shù)據(jù)還真的存于ibd中,我這里做個小小實驗。

表中有三行數(shù)據(jù),刪除其中hhhhhhh一行。

mysql> select * from test;
+----------+----------+
| col1     | col2     |
+----------+----------+
|          | aaaaaaaa |
| ccccccc  | NULL     |
| hhhhhhhh | xxxxxxxx |
+----------+----------+
3 rows in set (0.00 sec)
mysql> delete from test where col1='hhhhhhhh';
Query OK, 1 row affected (0.01 sec)


用hexdump來查看ibd文件:

查看頁結構:
python py_innodb_page_info.py -v /data/mysql/cwdtest/test.ibd
page offset 00000000, page type <File Space Header>
page offset 00000001, page type <Insert Buffer Bitmap>
page offset 00000002, page type <File Segment inode>
page offset 00000003, page type <B-tree Node>, page level <0000>
page offset 00000000, page type <Freshly Allocated Page>
page offset 00000000, page type <Freshly Allocated Page>
Total number of page: 6:
Freshly Allocated Page: 2
Insert Buffer Bitmap: 1
File Space Header: 1
B-tree Node: 1
File Segment inode: 1


對該表數(shù)據(jù)文件做個hexdump:

hexdump -C -v  /data/mysql/cwdtest/test2.ibd >/tmp/udb.txt


page type <B-tree Node>, page level <0000> 從第四個頁開始,從hexdump中可找到相應的位置0x0000c000開始,16k*3=49152=0x0000c000

0000c000  8c 69 ae 6a 00 00 00 03  ff ff ff ff ff ff ff ff  |.i.j............|
0000c010  00 00 00 00 00 29 6a a5  45 bf 00 00 00 00 00 00  |.....)j.E.......|
0000c020  00 00 00 00 00 21 00 02  00 e7 80 05 00 00 00 00  |.....!..........|
0000c030  00 c4 00 02 00 00 00 03  00 00 00 00 00 00 00 00  |................|
0000c040  00 00 00 00 00 00 00 00  00 33 00 00 00 21 00 00  |.........3...!..|
0000c050  00 02 00 f2 00 00 00 21  00 00 00 02 00 32 01 00  |.......!.....2..|
0000c060  02 00 1d 69 6e 66 69 6d  75 6d 00 04 00 0b 00 00  |...infimum......|
0000c070  73 75 70 72 65 6d 75 6d  08 00 00 00 00 10 00 22  |supremum......."|
0000c080  00 00 00 00 02 0d 00 00  00 00 0b 36 ab 00 00 01  |...........6....|
0000c090  22 01 10 61 61 61 61 61  61 61 61 07 01 00 00 18  |"..aaaaaaaa.....|
0000c0a0  00 22 00 00 00 00 02 0e  00 00 00 00 0b 37 ac 00  |."...........7..|
0000c0b0  00 01 23 01 10 63 63 63  63 63 63 63 08 08 00 20  |..#..ccccccc... |
0000c0c0  00 20 ff ac 00 00 00 00  02 0f 00 00 00 00 0b a1  |. ..............|
0000c0d0  76 00 00 01 72 01 10 68  68 68 68 68 68 68 68 78  |v...r..hhhhhhhhx|<<<<<<<<<<
0000c0e0  78 78 78 78 78 78 78 00  00 00 00 00 00 00 00 00  |xxxxxxx.........|
0000c0f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000c100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000c110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|


發(fā)現(xiàn)hhhhhhhh一行是在的,于是做個 analyze,查看是否會被清除。

mysql> optimize table  test;
+--------------+----------+----------+-------------------------------------------------------------------+
| Table        | Op       | Msg_type | Msg_text                                                          |
+--------------+----------+----------+-------------------------------------------------------------------+
| cwdtest.test | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| cwdtest.test | optimize | status   | OK                                                                |
+--------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0.03 sec)
mysql> analyze table test;
+--------------+---------+----------+----------+
| Table        | Op      | Msg_type | Msg_text |
+--------------+---------+----------+----------+
| cwdtest.test | analyze | status   | OK       |
+--------------+---------+----------+----------+


再查hexdump

0000c000  f0 d3 26 ea 00 00 00 03  ff ff ff ff ff ff ff ff  |..&.............|
0000c010  00 00 00 00 00 29 7b ce  45 bf 00 00 00 00 00 00  |.....){.E.......|
0000c020  00 00 00 00 00 24 00 02  00 bc 80 04 00 00 00 00  |.....$..........|
0000c030  00 a2 00 02 00 00 00 02  00 00 00 00 00 00 00 00  |................|
0000c040  00 00 00 00 00 00 00 00  00 36 00 00 00 24 00 00  |.........6...$..|
0000c050  00 02 00 f2 00 00 00 24  00 00 00 02 00 32 01 00  |.......$.....2..|
0000c060  02 00 1d 69 6e 66 69 6d  75 6d 00 03 00 0b 00 00  |...infimum......|
0000c070  73 75 70 72 65 6d 75 6d  08 00 00 00 00 10 00 22  |supremum......."|
0000c080  00 00 00 00 02 0d 00 00  00 00 0b 36 ab 00 00 01  |...........6....|
0000c090  22 01 10 61 61 61 61 61  61 61 61 07 01 00 00 18  |"..aaaaaaaa.....|
0000c0a0  ff ce 00 00 00 00 02 0e  00 00 00 00 0b 37 ac 00  |.............7..|
0000c0b0  00 01 23 01 10 63 63 63  63 63 63 63 00 00 00 00  |..#..ccccccc....|
0000c0c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000c0d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000c0e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000c0f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
0000c100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|


此時已被清理掉了。所以,一旦發(fā)送誤刪操作且沒有備份,第一時間應該趕緊把ibd文件備份。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI