【MySQL】Innodb 恢复工具介绍

来源:互联网 发布:dikies是什么档次知乎 编辑:程序博客网 时间:2024/05/10 03:53

在和数据打交道的过程中,总会遇到误删除数据的情况,在没有备份和binlog的情况下,如何恢复呢?本文介绍一个工具Percona Data Recovery Tool for InnoDB 
使用 该工具的 注意事项:
1 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;  
2 The tools work on a saved copy of your data files, not on the running MySQL server.
3 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.
如果数据被覆盖,则使用该工具无法恢复。必须使用系统或者物理方法来恢复
4 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.
  当发生误删除数据时必须立即备份表文件以便确保数据被覆盖或者损坏。
5 There is manual work to do. Not everything is automatic.
6 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 https://launchpad.net/percona-data-recovery-tool-for-innodb/trunk/release-0.5/+download/percona-data-recovery-tool-for-innodb-0.5.tar.gz
 cd percona-data-recovery-tool-for-innodb-0.5/mysql_source/
 ./configure
 cd ..
 make 
编译生成page_parser和constraints_parser工具 
注意create_defs.pl脚本需要依赖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:代表要解析的文件
输出信息:       
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/ 二级索引。可以安装开启innodb_table_monitor获取。
四 获取表结构的定义
 
 ./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_null: FALSE,
                                        uint_min_val: 0,
                                        uint_max_val: 4294967295ULL
                                },

                                can_be_null: FALSE
                        },
                        { /*  */
                                name: "DB_TRX_ID",
                                type: FT_INTERNAL,
                                fixed_length: 6,

                                can_be_null: FALSE
                        },
                        { /*  */
                                name: "DB_ROLL_PTR",
                                type: FT_INTERNAL,
                                fixed_length: 7,

                                can_be_null: FALSE
                        },
                        { /* varchar(50) */
                                name: "mac",
                                type: FT_CHAR,
                                min_length: 0,
                                max_length: 150,

                                has_limits: FALSE,
                                limits: {
                                        can_be_null: FALSE,
                                        char_min_len: 0,
                                        char_max_len: 150,
                                        char_ascii_only: TRUE
                                },

                                can_be_null: FALSE
                        },
                        { /* varchar(50) */
                                name: "name",
                                type: FT_CHAR,
                                min_length: 0,
                                max_length: 150,

                                has_limits: FALSE,
                                limits: {
                                        can_be_null: TRUE,
                                        char_min_len: 0,
                                        char_max_len: 150,
                                        char_ascii_only: TRUE
                                },

                                can_be_null: TRUE
                        },
                        { /* tinyint(4) */
                                name: "scope",
                                type: FT_INT,
                                fixed_length: 1,

                                has_limits: FALSE,
                                limits: {
                                        can_be_null: TRUE,
                                        int_min_val: -128,
                                        int_max_val: 127
                                },

                                can_be_null: TRUE
                        },
                        { /* datetime */
                                name: "gmt_create",
                                type: FT_DATETIME,
                                fixed_length: 8,

                                can_be_null: FALSE
                        },
                        { /* datetime */
                                name: "gmt_modify",
                                type: FT_DATETIME,
                                fixed_length: 8,

                                can_be_null: FALSE
                        },
                        { type: FT_NONE }
                }
        },
};

#endif
五 根据include/table_defs.h,重新编译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 -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);

执行 constraints_parser 命令会生成上午load data 的命令,在数据库中执行上述命令即可.
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)
root@127.0.0.1 : test 22:21:18> exit

总结
 1 整个恢复过程并不顺利,Percona 依赖于perl,在安装的时候遇到DBD安装不了的问题。
 2 可以恢复delete的数据,如果执行truncate table 是恢复失败的,drop的时候ibd文件丢同样没有文件来获取数据页而无法进行恢复。
  对于truncate的测试例子大家可以手动测试一下。

本文转载自http://blog.itpub.net/22664653/viewspace-771993/


0 0
原创粉丝点击