一 前言
作为DBA 运维MySQL 数据库的过程中,肯定遇到过在没有备份和binlog的情况下,ibd文件损坏或者误删除数据的情况,如何恢复呢?本文介绍一个工具Percona Data Recovery Tool for InnoDB
使用 该工具的注意事项:
The tools work only for InnoDB/XtraDB tables, and will not work with MyISAM tables. Percona does have a preliminary set of tools for MyISAM data recovery; 仅仅支持 InnoDB 引擎的表
The tools work on a saved copy of your data files, not on the running MySQL server. 工具针对 ibd 文件要是目标表的备份的ibd,而不是针对正在运行中的 实例。
There is no guarantee. Even with these tools, data is sometimes unrecoverable. For example, data that is overwritten cannot be recovered with these tools. There may be file system specific or physical means to recover overwritten data. 不能保证数据总一定可被恢复。例如,被重写的数据不能被恢复,这种情况下可能需要针对系统或物理的方式来恢复,不属于本工具的范畴。
Time is of the essence. The best chance for recovery comes when you act immediately to save a copy of your raw data files as soon as you discover the loss or corruption. 当发生误删除数据时必须立即备份表文件以便确保数据被覆盖或者损坏。
There is manual work to do. Not everything is automatic.
Recovery depends on knowing your data. As part of the process you may have to choose between two versions of your data. The better you know your data, the better the chance you'll be able to recover it.
需要理解的是innodb-tools工具不是通过连接到在线的database进行数据恢复,而是通过离线拷贝数据的方式进行的。
注意:不要在MySQL运行的时候,直接拷贝InnoDB文件,这样是不安全的,会影响数据恢复过程。不过这点我做了测试,在数据库运行的时候是可以进行数据库恢复的。
一 安装
进入解压后根目录下的mysql-source目录,运行配置命令,不运行make命令
wget
cd percona-data-recovery-tool-for-innodb-0.5/mysql_source/
./configure
cd ..
make
编译生成 page_parser
和 constraints_parser
工具
注意 create_defs.pl 脚本需要依赖perl DBD,DBI,安装过程中可能会遇到错误。
二 模拟误删除数据
root@127.0.0.1 : test 22:12:22> delete from mac where id < 51398;
Query OK, 4999 rows affected (0.62 sec)
root@127.0.0.1 : test 22:12:29>
三 获取数据页
InnoDB页的默认大小是16K,innodb的page分为两大部分,一部分一级索引部分,另一部分为二级索引部分。page_parser
工具通过读取数据文件,根据页头中的index ID,拷贝每个页到一个单独的文件中。
如果 my.cnf 配置了innodb_file_per_table=1
,那么系统实现上述过程。所有需要的页都在单独的.ibd文件,而且通常你不需要再切分它。
如果 .ibd 文件中可能包含多个index,那么将页单独切分开还是有必要的。如果MySQL server没有配置innodb_file_per_table
,那么数据会被保存在一个全局的表命名空间,这时候就需要按页对文件进行切分。
[root@rac1 recovery-tool]# ./page_parser -5 -f /opt/mysql/data/test/mac.ibd
-5:代表 row format为Compact
-f:代表要解析的文件
运行后,page_parser工具会创建一个pages-
输出信息:
Opening file: /opt/mysql/data/test/mac.ibd:
2051 ID of device containing file
20283635 inode number
33200 protection
1 number of hard links
103 user ID of owner
106 group ID of owner
0 device ID (if special file)
11534336 total size, in bytes
4096 blocksize for filesystem I/O
22560 number of blocks allocated
1377958353 time of last access
1377958359 time of last modification
1377958359 time of last status change
11534336 Size to process in bytes
104857600 Disk cache size in bytes
[root@rac1 recovery-tool]# less pages-1377958391/FIL_PAGE_INDEX/0-205
0-2057/ 0-2058/ 0-2059/
以上三个为索引文件 0-2057/主键,0-2058/ 0-2059/ 二级索引。
四 获取表结构的定义
./create_defs.pl --host 127.0.0.1 --user root --port 3306 --db test --table mac > include/table_defs.h
[root@rac1 recovery-tool]# more include/table_defs.h
#ifndef table_defs_h
#define table_defs_h
// Table definitions
table_def_t table_definitions[] = {
{
name: "mac",
{
{ /* int(10) unsigned */
name: "id",
type: FT_UINT,
fixed_length: 4,
has_limits: FALSE,
limits: {
can_be_: FALSE,
uint_min_val: 0,
uint_max_val: 4294967295ULL
},
can_be_: FALSE
},
{ /* */
name: "DB_TRX_ID",
type: FT_INTERNAL,
fixed_length: 6,
can_be_: FALSE
},
{ /* */
name: "DB_ROLL_PTR",
type: FT_INTERNAL,
fixed_length: 7,
can_be_: FALSE
},
{ /* varchar(50) */
name: "mac",
type: FT_CHAR,
min_length: 0,
max_length: 150,
has_limits: FALSE,
limits: {
can_be_: FALSE,
char_min_len: 0,
char_max_len: 150,
char_ascii_only: TRUE
},
can_be_: FALSE
},
{ /* varchar(50) */
name: "name",
type: FT_CHAR,
min_length: 0,
max_length: 150,
has_limits: FALSE,
limits: {
can_be_: TRUE,
char_min_len: 0,
char_max_len: 150,
char_ascii_only: TRUE
},
can_be_: TRUE
},
{ /* tinyint(4) */
name: "scope",
type: FT_INT,
fixed_length: 1,
has_limits: FALSE,
limits: {
can_be_: TRUE,
int_min_val: -128,
int_max_val: 127
},
can_be_: TRUE
},
{ /* datetime */
name: "gmt_create",
type: FT_DATETIME,
fixed_length: 8,
can_be_: FALSE
},
{ /* datetime */
name: "gmt_modify",
type: FT_DATETIME,
fixed_length: 8,
can_be_: FALSE
},
{ type: FT_NONE }
}
},
};
#endif
五 重新编译constraints_parser工具:
[root@rac1 recovery-tool]# make
gcc -DHAVE_OFFSET64_T -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -Wall -O3 -g -I include -I mysql-source/include -I mysql-source/innobase/include -c tables_dict.c -o lib/tables_dict.o
gcc -DHAVE_OFFSET64_T -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -Wall -O3 -g -I include -I mysql-source/include -I mysql-source/innobase/include -c check_data.c -o lib/check_data.o
gcc -DHAVE_OFFSET64_T -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -Wall -O3 -g -I include -I mysql-source/include -I mysql-source/innobase/include -o constraints_parser constraints_parser.c lib/tables_dict.o lib/print_data.o lib/check_data.o lib/libut.a lib/libmystrings.a
gcc -DHAVE_OFFSET64_T -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -D_LARGEFILE_SOURCE=1 -Wall -O3 -g -I include -I mysql-source/include -I mysql-source/innobase/include -static -lrt -o page_parser page_parser.c lib/tables_dict.o lib/libut.a
恢复误删除的数据:
运行 constraints_parser
工具以提取行记录。和 page_parser
工具一样,需要通过 -5或 -4 参数指定InnoDB页格式(COMPACT/REDUNDANT),-f指定输入文件。
执行 constraints_parser 命令会生成 load data 的命令,在数据库中执行上述命令即可.
./constraints_parser -D -5 -f pages-1377958391/FIL_PAGE_INDEX/0-2057/ > /tmp/mac.rec
LOAD DATA INFILE '/root/recovery-tool/dumps/default/mac' REPLACE INTO TABLE `mac` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'mac\t' (id, mac, name, scope, gmt_create, gmt_modify);
root@127.0.0.1 : test 22:20:54> select count(1) from mac;
+----------+
| count(1) |
+----------+
| 9973 |
+----------+
1 row in set (0.00 sec)
root@127.0.0.1 : test 22:21:09> LOAD DATA INFILE '/tmp/mac.rec' REPLACE INTO TABLE `mac` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'mac\t' (id, mac, name, scope, gmt_create, gmt_modify);
Query OK, 4999 rows affected (0.22 sec)
Records: 4999 Deleted: 0 Skipped: 0 Warnings: 0
root@127.0.0.1 : test 22:21:13> select count(1) from mac;
+----------+
| count(1) |
+----------+
| 14972 |
+----------+
1 row in set (0.00 sec)
总结
该可以解析正常 ibd 文件的数据。本文没有针对坏块做特殊测试。 delete和truncate 时,恢复不一定百分百成功。所以一定要检查勤做备份以及检查备份的有效性。 感兴趣的朋友 ,可以自己测试看看。
参考文章
https://www.percona.com/blog/2012/02/20/how-to-recover-deleted-rows-from-an-innodb-tablespace/
恢复误操作的方法-介绍binlog2sql恢复dml 操作。