转载:https://www.linuxidc.com/Linux/2017-05/143870.htm
首先呢,请各位注意Percona Data Recovery Tool for InnoDB工具的适用范围:
1)本次应用的恢复工具仅适用与innodb存储引擎,Myisam不支持
2)Truncate tabe 不能恢复
3)Drop table 想也别想了
恢复原理:
对于INNODB存储引擎而言,DELETE操作,不是真正删除物理文件上的行,而是给删除的行添加了一个删除的标记,我们利用此工具找到那些标注了删除标记的行,然后将其存放到一个文本中去,最后通过load data恢复数据;而truncate操作,是直接将数据行清空,并非添加删除标记(查看物理文件,执行truncate的表的ibd文件会缩小,而执行了DELETE的表,甚至比之前大)
正文开始:
下载:percona-data-recovery-tool-for-innodb-0.5.tar.gz
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
安装 yum -y install ncurses-devel.x86_64
安装 yum install glibc-static
安装 yum install -y mariadb-server
开启:systemctl start mariadb.service
开机启动:systemctl enable mariadb.service
安装 yum -y install perl-DBD-MySQL.x86_64 (如果没有mysql)
开启:
安装编译
tar xvf percona-data-recovery-tool-for-innodb-0.5.tar.gz
[root@zabbix percona]# cd percona-data-recovery-tool-for-innodb-0.5
[root@zabbix percona-data-recovery-tool-for-innodb-0.5]# cd mysql-source/
[root@zabbix mysql-source]# ./configure
[root@zabbix percona-data-recovery-tool-for-innodb-0.5]# make
右键属性查看 ibd所在路径 。我的是/root/mysql/xxxx.ibd
执行
[root@dns1 percona-data-recovery-tool-for-innodb-0.5]# ./page_parser -5 -f /var/lib/mysql/test/site_ksteam.ibd
[root@dns1 percona-data-recovery-tool-for-innodb-0.5]# ls
check_data.c create_defs.pl ibdconnect incrementalupdate.c INSTALL mysql-source pages-1494339302 split_dump.pl
constraints_parser docs ibdconnect.c innochecksum lib page_parser pages-1494339674 tables_dict.c
[root@dns1 percona-data-recovery-tool-for-innodb-0.5]# ls pages-1494339302/FIL_PAGE_INDEX/
0-2475 0-2476 0-2477
在FIL_PAGE_INDEX目录下会生成主键和索引,数据最小的是主键,后面的依次是每个列的索引,如0-2475是site_ksteam表的主键,0-2476是表的索引
2)生成site_ksteam表的表结构:
[root@dns1percona-data-recovery-tool-for-innodb-0.5]# ./create_defs.pl --host localhost --port 3306 --user root --password 123456 --db test --table site_ksteam > include/table_defs.h 正常情况下没有任何的报错
3)还得再次执行make命令
[root@dns1 percona-data-recovery-tool-for-innodb-0.5]# 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 -static -lrt -o page_parser page_parser.c lib/tables_dict.o lib/libut.a
最后是这条信息,就是正常的
4)将查找到的数据保存到/tmp/site_ksteam.txt文件
[root@dns1percona-data-recovery-tool-for-innodb-0.5]# ./constraints_parser -D -5 -f pages-1494339674/FIL_PAGE_INDEX/0-2351/ > /tmp/site_ksteam.txt -D 恢复删除的行;-5表的文件格式,默认Compact;
-f 指定site_ksteam表的主键目录
LOAD DATA INFILE '/com/percona-data-recovery-tool-for-innodb-0.5/dumps/default/site_ksteam' REPLACE INTO TABLE `site_ksteam` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'site_ksteam\t' (id, catid, typeid, title, style, thumb, keywords, description, posids, url, listorder, status, sysadd, islink, username, inputtime, updatetime, contact, titles, level, cimage, bimage, language, subject);
5)连接Mysql,将数据导入表中
mysql> LOAD DATA INFILE '/tmp/site_ksteam.txt' REPLACE INTO TABLE `site_ksteam` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'site_ksteam\t' (id, catid, typeid, title, style, thumb, keywords, description, posids, url, listorder, status, sysadd, islink, username, inputtime, updatetime, contact, titles, level, cimage, bimage, language, subject);
最后呢,还是想吐槽下,开始搞这个工具的时候,网上的资料,呵呵,人云亦云的,也不说中间可能遇到啥问题,怎么解决,最最重要的是, 此工具对在线运行的数据库进行恢复操作,会出现数据丢失现象!!!