Linux下误删除后的恢复操作(ext3/ext4)

 

Linux是作为一个多用户、多任务的操作系统,文件一旦被删除是难以恢复的。尽管删除命令只是在文件节点中作删除标记,并不真正清除文件内容,但是其他用户和一些有写盘动作的进程会很快覆盖这些数据。在日常工程中,谁也说不准永远不犯错误,万一哪天不小心误操作删除了一些重要文件,该怎么办呢??
莫慌!这里介绍一款神器extundelete,这是针对ext4文件格式下文件删除后的恢复工具,十分强大!!

废话不多说,下面开始介绍这款神器的使用:
1)下载并安装软件
extundelete主页:http://extundelete.sourceforge.net/
下载地址:http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.0/extundelete-0.2.0.tar.bz2
百度云盘下载:https://pan.baidu.com/s/1c1XYHc0
获取地址:xsmg

下载到本机的/usr/local/src目录下
[root@slave-node ~]# cd /usr/local/src
[root@slave-node src]# tar -jvxf extundelete-0.2.0.tar.bz2 
[root@slave-node src]# cd extundelete-0.2.0
[root@slave-node extundelete-0.2.0]# yum -y install e2fsprogs*                      //不然会提示找不到ext2fs库
[root@slave-node extundelete-0.2.0]# ./configure
[root@slave-node extundelete-0.2.0]# make && make install

2)执行删除操作(要是/根分区之外的分区,并且只能是文件删除后的恢复,目录删除不能恢复)
[root@slave-node ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda3 ext4 151189708 2370020 141139688 2% /
tmpfs tmpfs 32960412 0 32960412 0% /dev/shm
/dev/sda1 ext4 198337 26798 161299 15% /boot
/dev/sda2 ext4 806346400 201304 765185096 1% /home

[root@slave-node ~]# cd /home/
[root@slave-node home]# echo "123456"> test1
[root@slave-node home]# echo "hahahahha" > wangshibo
[root@slave-node home]# ls
lost+found test1 wangshibo zabbix

删除文件
[root@slave-node home]# rm -rf test1 wangshibo 
[root@slave-node home]# ls
lost+found zabbix

3)数据删除后的恢复操作
首先恢复前,卸载需要恢复文件的分区
[root@slave-node ~]# umount /home/               //如果卸载失败,就用命令"fuser -k /home"结束使用此分区的进程树
[root@slave-node ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda3 ext4 151189708 2370024 141139684 2% /
tmpfs tmpfs 32960412 0 32960412 0% /dev/shm
/dev/sda1 ext4 198337 26798 161299 15% /boot

使用extundelete查看分区上存在的文件,如下:
--inode 为查找某i节点中的内容,使用2则说明为搜索,如果需要进入目录搜索,只须要指定目录I节点即可
[root@slave-node ~]# extundelete --inode 2 /dev/sda2
WARNING: Extended attributes are not restored.
Loading filesystem metadata ... 6250 groups loaded.
Contents of inode 2:
0000 | ed 41 00 00 00 10 00 00 84 10 49 58 82 10 49 58 | .A........IX..IX
0010 | 82 10 49 58 00 00 00 00 00 00 04 00 08 00 00 00 | ..IX............
0020 | 00 00 00 00 07 00 00 00 21 24 00 00 00 00 00 00 | ........!$......
0030 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0040 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0050 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0060 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0070 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0080 | 00 00 00 00 00 00 00 00 11 10 00 00 00 00 00 00 | ................
0090 | 00 00 00 00 00 00 00 00 62 0e 49 58 62 0e 49 58 | ........b.IXb.IX
00a0 | 62 0e 49 58 00 00 00 00 00 00 00 00 00 00 00 00 | b.IX............
00b0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00c0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00d0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00e0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00f0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................

Inode is Allocated
Group: 0
File mode: 16877
Low 16 bits of Owner Uid: 0
Size in bytes: 4096
Access time: 1481183364
Creation time: 1481183362
Modification time: 1481183362
Deletion Time: 0
Low 16 bits of Group Id: 0
Links count: 4
Blocks count: 8
File flags: 0
File version (for NFS): 0
File ACL: 0
Directory ACL: 0
Fragment address: 0
Direct blocks: 9249, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Indirect block: 0
Double indirect block: 0
Triple indirect block: 0

File name | Inode number | Deleted status
Directory block 9249:
. 2
.. 2
lost+found 11
haha 12 Deleted
zabbix 17432577
test1 12 Deleted
wangshibo 13 Deleted

如上可以看出,被删除的两个文件test1和wangshibo已经被找到,状态为已经删除。
接下来就将它们恢复出来:
[root@slave-node ~]# extundelete --restore-inode 12 /dev/sda2
WARNING: Extended attributes are not restored.
Loading filesystem metadata ... 6250 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Restored inode 12 to file RECOVERED_FILES/file.12
[root@slave-node ~]# extundelete --restore-inode 13 /dev/sda2
WARNING: Extended attributes are not restored.
Loading filesystem metadata ... 6250 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Restored inode 13 to file RECOVERED_FILES/file.13

[root@slave-node ~]# ls RECOVERED_FILES/
file.12 file.13
[root@slave-node ~]# mount /dev/sda2 /home/               //重新挂载home分区
[root@slave-node ~]# mv RECOVERED_FILES/file.12 /home/test1
[root@slave-node ~]# mv RECOVERED_FILES/file.13 /home/wangshibo

再次查看home分区,发现删除的文件已经恢复回来了,很强大!!!
[root@slave-node ~]# cd /home/
[root@slave-node home]# ls
lost+found test1 wangshibo zabbix
[root@slave-node home]# cat test1 
123456
[root@slave-node home]# cat wangshibo 
hahahahha

------------------------------------------------------------------------------------------------------
上面介绍的是在ext4文件格式下的文件删除后的恢复,那如果是ext3文件格式下的文件删除后想恢复,怎么办呢?
可以使用debugfs工具,这是linux系统自带工具,debugfs恢复Ext3的文件系统中被rm、rm -f 掉的文件。

实例说明:
[root@slave-node ~]# df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda3 ext3 151189708 2370036 141139672 2% /
tmpfs tmpfs 32960412 0 32960412 0% /dev/shm
/dev/sda1 ext3 198337 26798 161299 15% /boot

创建一个文件
[root@slave-node ~]# mkdir test
[root@slave-node ~]# echo "123456" > /root/test/test.file

删除文件
[root@slave-node ~]# rm -rf /root/test/test.file

接着运用系统自带工具debugfs来恢复已删除的文件
首先打开,刚刚被删除文件所在的分区
注意上面显示的有<>尖括号内的数字就是我们要找的文件Inode号,执行logdump –i <8654024>
[root@slave-node ~]# debugfs 
debugfs 1.41.12 (17-May-2010)
debugfs: open /dev/sda3
debugfs: ls -d /root/test
8654023 (12) . 8519681 (4084) .. <8654024> (4072) test.file 
debugfs: logdump -i 8654024
FS block 1006 logged at sequence 404351, journal block 7241
(inode block for inode 15):
Inode: 15 Type: regular Mode: 0664 Flags: 0×0 Generation: 0
User: 0 Group: 0 Size: 20
File ACL: 0 Directory ACL: 0
Links: 1 Blockcount: 8
Fragment: Address: 0 Number: 0 Size: 0
ctime: 0×48159f2d — Mon Apr 28 15:25:57 2008
atime: 0×48159f27 — Mon Apr 28 15:25:51 2008
mtime: 0×4806f070 — Thu Apr 17 12:08:40 2008
Blocks: (0+1): 102348
No magic number at block 7247: end of journal.

执行完命令后,显示了一屏信息,需要注意的是下面Blocks这一行后面的值(如上信息,需要记住Blocks这一行后面的数字102348)

输入quit,退出debugfs
debugfs: quit
[root@slave-node ~]#

执行如下命令进行恢复
[root@slave-node ~]# dd if=/dev/sda3 of=/tmp/test.file.bk bs=4096 count=1 skip=102348
1+0 records in
1+0 records out
4096 bytes (4.1 kB) copied, 0.0110028 s, 372 kB/s

最后查看tmp目录下,发现已经恢复了我们之前删除的文件内容
[root@slave-node ~]# cd /tmp/
[root@slave-node tmp]# cat test.file.bk
[root@slave-node tmp]# mv test.file.bk /root/test.file
[root@slave-node tmp]# cat /root/test.file
123456

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/6145308.html,如需转载请自行联系原作者
上一篇:如何保证业务变更的可回滚性


下一篇:小博无线技术团队使用的编程语言