把mysql表删了怎么 mysql表数据删除了能还原么( 二 )


这里我们需要借助 undrop-for-innodb 工具恢复数据 , 它能读取表空间信息得到 page , 将数据从 page 中提取出来 。
# wgetyum install -y gcc flex bison# make# make sys_parser
# ./sys_parser 读取表结构信息
sys_parser [-h] [-u] [-p] [-d] databases/table
stream_parser 读取 InnoDB page 从 ibdata1 或 ibd 或分区表
# ./stream_parserYou must specify file with -f optionUsage: ./stream_parser -f innodb_datafile [-T N:M] [-s size] [-t size] [-V|-g]Where:-h- Print this help-V or -g- Print debug information-s size- Amount of memory used for disk cache (allowed examples 1G 10M). Default 100M-T- retrieves only pages with index id = NM (N - high word, M - low word of id)-t size- Size of InnoDB tablespace to scan. Use it only if the parser can't determine it by himself.
c_parser 从 innodb page 中读取记录保存到文件
# ./c_parserError: Usage: ./c_parser -4|-5|-6 [-dDV] -f InnoDB page or dir -t table.sql [-T N:M] [-b external pages directory]Where-f InnoDB page(s) -- InnoDB page or directory with pages(all pages should have same index_id)-t table.sql -- CREATE statement of a table-o file -- Save dump in this file. Otherwise print to stdout-l file -- Save SQL statements in this file. Otherwise print to stderr-h-- Print this help-d-- Process only those pages which potentially could have deleted records (default = NO)-D-- Recover deleted rows only (default = NO)-U-- Recover UNdeleted rows only (default = YES)-V-- Verbose mode (lots of debug information)-4-- innodb_datafile is in REDUNDANT format-5-- innodb_datafile is in COMPACT format-6-- innodb_datafile is in MySQL 5.6 format-T-- retrieves only pages with index id = NM (N - high word, M - low word of id)-b dir -- Directory where external pages can be found. Usually it is pages-XXX/FIL_PAGE_TYPE_BLOB/-i file -- Read external pages at their offsets from file.-p prefix -- Use prefix for a directory name in LOAD DATA INFILE command
接下来,我们演示场景的几种数据恢复场景 。
场景1:drop table
是否启用了 innodb_file_per_table 其恢复方法有所差异,当发生误删表时,应尽快停止MySQL服务,不要启动 。若 innodb_file_per_table=ON,最好只读方式重新挂载文件系统,防止其把mysql表删了怎么他进程写入数据覆盖之前块设备的数据 。
如果评估记录是否被覆盖,可以表中某些记录的作为关键字看是否能从 ibdata1 中筛选出 。
# grep WOODYHOFFMAN ibdata1
Binary file ibdata1 matches
也可以使用 bvi(适用于较小文件)或 hexdump -C(适用于较大文件)工具
以表 sakila.actor 为例CREATE TABLE `actor` (`actor_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,`first_name` varchar(45) NOT NULL,`last_name` varchar(45) NOT NULL,`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`actor_id`),KEY `idx_actor_last_name` (`last_name`)) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8
首先恢复表结构信息1. 解析系统表空间获取 page 信息
./stream_parser -f /var/lib/mysql/ibdata1
2. 新建一个 schema,把系统字典表的 DDL 导入
cat dictionary/SYS_* | mysql recovered
3. 创建恢复目录
mkdir -p dumps/default
4. 解析系统表空间包含的字典表信息,
./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/0000000000000001.page -t dictionary/SYS_TABLES.sqldumps/default/SYS_TABLES 2 dumps/default/SYS_TABLES.sql./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/0000000000000002.page -t dictionary/SYS_COLUMNS.sqldumps/default/SYS_COLUMNS 2 dumps/default/SYS_COLUMNS.sql./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/0000000000000003.page -t dictionary/SYS_INDEXES.sqldumps/default/SYS_INDEXES 2 dumps/default/SYS_INDEXES.sql./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/0000000000000004.page -t dictionary/SYS_FIELDS.sqldumps/default/SYS_FIELDS 2 dumps/default/SYS_FIELDS.sql
5. 导入恢复的数据字典

推荐阅读