您的位置:首页 > 其它

rm -rf 数据恢复(ext3grep)

2013-01-06 10:05 531 查看
人无完人,工作环境中,我们难免会存在误操作(如:rm -rf)。接下来,我们来模拟一个误删除操作(rm -rf),然后利用ext3grep取回被误删的数据。

ext3grep官方网站:http://code.google.com/p/ext3grep/

这里下载的是ext3grep-0.10.2.tar.gz

所需要的系统相关包:

[root@localhost ~]# rpm -qa | grep e2fsprogs

e2fsprogs-1.39-33.el5

e2fsprogs-libs-1.39-33.el5

e2fsprogs-libs-1.39-33.el5

e2fsprogs-devel-1.39-33.el5

系统必须安装e2fsprogs-libs,不然后面ext3grep的安装会出现问题。

下面进入编译安装阶段,过程如下:

[root@localhost software]# tar xvf ext3grep-0.10.2.tar.gz

[root@localhost software]# cd ext3grep-0.10.2

[root@localhost ext3grep-0.10.2]# ./configure

[root@localhost ext3grep-0.10.2]# make && make install

[root@localhost ext3grep-0.10.2]# ext3grep -v

Running ext3grep version 0.10.2

ext3grep v0.10.2, 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.

这样,ext3grep就安装完成了,默认ext3grep命令放在/usr/local/bin目录下。ext3grep的使用非常简单,这里就不做介绍,可以通过”ext3grep --help“ 获取详细的使用帮助。

数据恢复准则:

当发现某个分区的数据被删除以后,要做的第一件事就是立刻卸载被误删文件所在的分区,或者重新以只读的方式挂载此分区。

这么做的原因其实很简单:删除一个文件,就是将文件的Inode节点中的扇区指针清除,同时,释放这些数据对应的数据块,而真实的文件还存在磁盘分区中。但是这些被删除的文件不会一直存留在磁盘中,当这些释放的数据块被操作系统重新分配时,那些被删除的数据就会被覆盖。因此,在数据被误删除后,马上卸载文件所在分区可以降低数据块中数据被覆盖的风险,进而提高成功恢复数据的几率。

1,模拟数据误删除环境:

[root@localhost /]# mkdir mydata

[root@localhost /]# mkdir disk #建立一个挂载点

[root@localhost mydata]# dd if=/dev/zero of=/mydata/disk1 count=102400 #模拟磁盘分区,创建一个空设备

102400+0 records in

102400+0 records out

52428800 bytes (52 MB) copied, 0.334582 seconds, 157 MB/s

[root@localhost mydata]# mkfs.ext3 /mydata/disk1 #讲空设备格式化为ext3格式

[root@localhost ~]# mount -o loop /mydata/disk1 /disk/ #挂载设备到 /disk 目录下

[root@localhost ~]# cd /disk/

[root@localhost disk]# cp /etc/profile /disk/

[root@localhost disk]# echo "ext3grep test" > ext3grep.txt

[root@localhost disk]# mkdir ext3grep

[root@localhost disk]# cp /etc/hosts ext3grep/

[root@localhost disk]# ls -al

总计 25

drwxr-xr-x 4 root root 1024 08-29 17:02 .

drwxr-xr-x 27 root root 4096 08-29 16:58 ..

drwxr-xr-x 2 root root 1024 08-29 17:02 ext3grep

-rw-r--r-- 1 root root 14 08-29 17:02 ext3grep.txt

drwx------ 2 root root 12288 08-29 17:00 lost+found

-rw-r--r-- 1 root root 1345 08-29 17:01 profile

[root@localhost disk]# md5sum profile > /tmp/md5.txt #获取文件校验码(和恢复后的文件检验码做对比,看数据是否改变)

[root@localhost disk]# md5sum ext3grep.txt >> /tmp/md5.txt

[root@localhost ~]# rm -rf /disk/* #模拟误删数据操作

2,卸载磁盘分区

[root@localhost ~]# umount /disk/

3,查询恢复数据信息

[root@localhost ~]# ext3grep /mydata/disk1 --ls --inode 2

执行该命令后,ext3grep就开始搜索可以恢复的数据文件信息,输出如下

..........

Inode 2 is directory "".

Directory block 433:

.-- 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 end d 2 drwxr-xr-x ..

2 3 d 11 D 1346231173 Wed Aug 29 17:06:13 2012 drwx------ lost+found

3 end r 12 D 1346231173 Wed Aug 29 17:06:13 2012 rrw-r--r-- profile

4 end r 13 D 1346231173 Wed Aug 29 17:06:13 2012 rrw-r--r-- ext3grep.txt

5 end d 9161 D 1346231173 Wed Aug 29 17:06:13 2012 drwxr-xr-x ext3grep

"ext3grep /mydata/disk1 --ls --inode 2"主要用于扫描当前文件系统下所有文件的信息,包括存在的和已经删除的文件,其中含有 D 标识的就是已经被删除的文件,

如果不记得被删除的文件的名称,可以通过这种方式来获取要恢复的数据文件名称。通过下面的方式可以获取文件要恢复的路径信息。

[root@localhost ~]# ext3grep /mydata/disk1 --dump-names

Running ext3grep version 0.10.2

Number of groups: 7

Minimum / maximum journal block: 447 / 4561

Loading journal descriptors... sorting... done

The oldest inode block that is still in the journal, appears to be from 1346230883 = Wed Aug 29 17:01:23 2012

Number of descriptors in journal: 65; min / max sequence numbers: 2 / 18

Loading disk1.ext3grep.stage2... done

ext3grep

ext3grep.txt

ext3grep/hosts

lost+found

profile

4,恢复单个文件

[root@localhost ~]# ext3grep /mydata/disk1 --restore-file ext3grep.txt

Running ext3grep version 0.10.2

Number of groups: 7

Minimum / maximum journal block: 447 / 4561

Loading journal descriptors... sorting... done

The oldest inode block that is still in the journal, appears to be from 1346230883 = Wed Aug 29 17:01:23 2012

Number of descriptors in journal: 65; min / max sequence numbers: 2 / 18

Loading disk1.ext3grep.stage2... done

Restoring ext3grep.txt

由上面的信息可知,被删除的文件ext3grep.txt已经恢复成功。(在哪个目录下执行ext3grep 命令,恢复的数据文件就在哪个目录的RESTORED_FILES目录下)

"--restore-file" 后面指定的恢复文件路径,这个路径应该是文件的相对路径,这里的相对路径指的是相对指定设备的路径,比如,设备 /mydata/disk1的挂载点是/disk,而ext3grep.txt文件就是在/disk目录下,因此直接指定文件名就可以了,如果要恢复/disk/ext3grep/hosts 文件,那么指定的参数应该是"ext3grep/hosts",如:

[root@localhost ~]# ext3grep /mydata/disk1 --restore-file ext3grep/hosts

通过 "--restore-inode" 参数,只需要指定文件对应的Inode值即可恢复文件。操作如下(其中inode值为12的是profile文件):

[root@localhost ~]# ext3grep /mydata/disk1 --restore-inode 12

接下来可进入当前目录的RESTORED_FILES目录,验证文件是否成功恢复。

恢复所有已经删除的数据文件可用 "--restore-all" 参数,如下:

[root@localhost ~]# ext3grep /mydata/disk1 --restore-all

Running ext3grep version 0.10.2

Number of groups: 7

Minimum / maximum journal block: 447 / 4561

Loading journal descriptors... sorting... done

The oldest inode block that is still in the journal, appears to be from 1346230883 = Wed Aug 29 17:01:23 2012

Number of descriptors in journal: 65; min / max sequence numbers: 2 / 18

Loading disk1.ext3grep.stage2... done

Restoring ext3grep.txt

Restoring ext3grep/hosts

Restoring profile

"--restore-all" 参数将指定存储设备中可以恢复的文件都恢复出来并保存到了RESTORED_FILES目录中。"--restore-all" 参数对恢复大量数据文件是非常有用的。

文章来自 : http://bbs.bknks.net/thread-11619-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息