恢复rm删除的文件(ext3)

       df -T   # 首先查看磁盘分区格式,此软件只针对ext3
       umount /data/     # 卸载挂载,数据丢失请首先卸载挂载,或重新挂载只读
       ext3grep /dev/sdb1 --ls --inode 2         # 记录信息继续查找目录下文件inode信息
       ext3grep /dev/sdb1 --ls --inode 131081    # 此处是inode
       ext3grep /dev/sdb1 --restore-inode 49153  # 记录下inode信息开始恢复目录


# 1、安装
cd /tmp
wget http://ext3grep.googlecode.com/files/ext3grep-0.10.1.tar.gz
zypper install e2fsprogs e2fsprogs-devel
tar -zxf ext3grep-0.10.1.tar.gz
cd ext3grep-0.10.1
./configure
make
make install


# 2、新建文件并删除

bj-bi-recommend10:/opt/huawei # touch 1.txt 2.txt
bj-bi-recommend10:/opt/huawei # rm 1.txt 2.txt



# 3,扫描分区
bj-bi-recommend10:~ # ext3grep /dev/xvdb --ls --inode 2
Running ext3grep version 0.10.1
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 4000
Loading group metadata... done
Minimum / maximum journal block: 65503746 / 65537061
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1395811917 = Wed Mar 26 13:31:57 2014
Number of descriptors in journal: 169; min / max sequence numbers: 6 / 11
Inode is Allocated
Loading xvdb.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    4 d      11                                         drwx------  lost+found
  3    4 r      12  D 1395812112 Wed Mar 26 13:35:12 2014  rrw-------  1.txt
  4  end d21397505                                         drwx------  file
  5  end r      13  D 1395812112 Wed Mar 26 13:35:12 2014  rrw-------  2.txt


- - - - -


# 查询
ext3grep /dev/xvdb --ls --inode 2

# 恢复单个 仅目录 里面的文件恢复失败 ..
ext3grep /dev/xvdb --restore-file del --depth del

# 恢复目录里面的一个文件
ext3grep /dev/xvdb --restore-file del/1

# 恢复所有文件

bj-bi-recommend10:~ # ext3grep /dev/xvdb  --restore-all
Running ext3grep version 0.10.1
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 4000
Minimum / maximum journal block: 65503746 / 65537061
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1395811939 = Wed Mar 26 13:32:19 2014
Number of descriptors in journal: 45; min / max sequence numbers: 6 / 15
Loading xvdb.ext3grep.stage2... done
Restoring 1.txt
Restoring 2.txt

bj-bi-recommend10:~ # cd RESTORED_FILES/
bj-bi-recommend10:~/RESTORED_FILES # ls
1.txt  2.txt  lost+found



如果你有幸记得你误删除的文件名及其路径的话,就可以直接用下面的命令进行恢复了:

  ext3grep /dev/xvdb --restore-file 1.txt
时间段恢复文件  
date -d "2014-03-25 15:00:00" +%s  #把时间转化为数字
1395730800
date -d "2014-03-25 22:30:00" +%s  #把时间转化为数字
1395757800
 ext3grep /dev/xvdb  --restore-all  --after 1395730800 --before 1395757800
可以用这两个参数指定时间,从2014-03-25 15:00:00 到 2014-03-25 22:30:00

需要注意的是,上面的文件路径,是在该分区上文件路径。假设我们要恢复/dev/xvdb 分区上文件,这个分区原来的安装点是/opt/huawei,现在想恢复文件/home/easwy/vi/tips.xml,那么输入的命令应该是:

ext3grep /dev/xvdb --restore-file 1.txt

如果你忘记了文件名,或者你误删除的是一个目录而你无法记全该目录中的文件,你可以先用下面的命令查询一下文件名:

ext3grep /dev/xvdb  --dump-names | tee 1.txt

上面的命令把ext3grep命令的输出记录到文件filename.txt中,你可以慢慢查看,或者使用grep命令过滤出你需要的信息。

当你知道了目录/文件的信息后,就可以用上面说的命令进行恢复了。

在ext3grep命令中有一个–depth参数,应该是用来恢复目录以及其中的所有子目录和文件的,不过Easwy用这个参数无法恢复出任何文件,只好采用笨办法,写了个脚本,一个个文件的恢复。

经过几个小时的折磨(中间走了一些弯路),同事的文件终于恢复成功了!





通过下面的方式可以获取文件要恢复的路径信息。

[root@localhost opt]# ext3grep /data0/disk1 --dump-names
Running ext3grep version 0.10.2
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 7
Minimum / maximum journal block: 16616 / 20729
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1380011783 = Tue Sep 24 16:36:23 2013
Number of descriptors in journal: 67; min / max sequence numbers: 2 / 12
Loading disk1.ext3grep.stage2... done
ext3grep
ext3grep.txt
initrd-2.6.32-358.el6.i686kdump.img
lost+found
profile

4.恢复单个文件

如果要恢复被删除的某个文件,通过下面方式即可。

[root@localhost opt]# ext3grep /data0/disk1 --restore-file ext3grep.txt
Running ext3grep version 0.10.2
WARNING: I don't know what EXT3_FEATURE_COMPAT_EXT_ATTR is.
Number of groups: 7
Minimum / maximum journal block: 16616 / 20729
Loading journal descriptors... sorting... done
The oldest inode block that is still in the journal, appears to be from 1380011783 = Tue Sep 24 16:36:23 2013
Number of descriptors in journal: 67; min / max sequence numbers: 2 / 12
Writing output to directory RESTORED_FILES/
Loading disk1.ext3grep.stage2... done
Restoring ext3grep.txt