MySQL利用ext3grep恢复Myisam表(一)

2014-11-24 17:42:40 · 作者: · 浏览: 0

我们知道,Mysql的MYISAM引擎表在当前Database目录下,有3个对应的文件,frm结构文件,MYI索引文件,MYD数据文件。当你在Mysql中,drop了MYISAM引擎表后,其实就是在文件系统里将其对应的3个文件rm了。所以当你执行drop后,如果能将上面3个文件恢复,那么表也将恢复。


如果你使用的是EXT3文件系统,那么可以使用ext3grep来完成上面的操作,举例说明:


1.安装ext3grep
首先需要安装e2fsprogs-libs,可以到http://e2fsprogs.sourceforge.net/下载源码包


tar -xzvf e2fsprogs-1.41.5.tar.gz
cd e2fsprogs-1.41.5
mkdir build; cd build
../configure
make
make install
make install-libs(e2fsprogs-libs)
在安装ext3grep,可以到http://code.google.com/p/ext3grep/downloads/list下载


tar xfvz ext3grep-0.10.1.tar.gz
cd ext3grep-0.10.1
./configure
make
make install


ext3grep -V
Running ext3grep version 0.10.1
ext3grep v0.10.1, Copyright (C) 2008 Carlo Wood.
ext3grep comes with ABSOLUTELY NO WARRANTY;
This program is free software; your freedom to use, change
and distribute this program is protected by the GPL.
2.删除测试表


mysql> use test
mysql> desc t;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> select * from t;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
3 rows in set (0.00 sec)

mysql>drop table t;

mysql> quit
3.停止mysql,并将数据文件所在分区umount。


# /etc/init.d/mysqld stop
Shutting down MySQL.. [ OK ]
cd
# umount /dev/sda6
4.利用ext3grep工具进行恢复


# ext3grep /dev/sda6 --ls --inode 2
Running ext3grep version 0.10.1
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 1778
Loading group metadata... done
Minimum / maximum journal block: 29065730 / 29099045
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1243924942 = Tue Jun 2 14:42:22 2009
Number of descriptors in journal: 418; min / max sequence numbers: 13 / 36
Inode is Allocated
Loading sda6.ext3grep.stage2... done
The first block of the directory is 1539.
Inode 2 is directory "".
Directory block 1539:
.-- File type in dir_entry (r=regular file, d=directory, l=symlink)
| .-- D: Deleted ; R: Reallocated
Indx Next | Inode | Deletion time Mode File name
==========+==========+----------------data-from-inode------+-----------+=========
0 1 d 2 drwxr-xr-x .
1 2 d 2 drwxr-xr-x ..
2 3 d 11 drwx------ lost+found
3 4 r 12 rrw-r--r-- 1
4 5 r 13 rrw-r--r-- 5
5 end d 237569 drwxr-xr-x data
6 end r 15 D 1243919862 Tue Jun 2 13:17:42 2009 rrw-r--r-- 3.txt
可以看到Mysql数据文件的目录data对应的Inode是237569,然后在查看改Inode对应的block。


# ext3grep /dev/sda6 --inode 237569
Running ext3grep version 0.10.1
No --ls used; implying --print.


Inode is Allocated
Group: 29
Generation Id: 3010341297
uid / gid: 500 / 500
mode: drwxr-xr-x
size: 4096
num of links: 4
sectors: 8 (--> 0 indirect blocks).


Inode Times:
Accessed: 1243930169 = Tue Jun 2 16:09:29 2009
File Modified: 1243930170 = Tue Jun 2 16:09:30 2009
Inode Modified: 1243930170 = Tue Jun 2 16:09:30 2009
Deletion time: 0


Direct Blocks: 968704
Loa