linux 文件恢复

一、删除文件后(进程还存在)

[root@localhostdel]#echo 'hello  linux' > zhang.txt
[root@localhostdel]#echo 'delete'  >>zhang.txt 
[root@localhostdel]#cat zhang.txt 
hello  linux
delete
#模拟进程占用该文件
[root@localhostdel]#cat >> zhang.txt 


#删除文件
[root@localhostdel]#rm -f zhang.txt 
#查看删除的文件
[root@localhostdel]#lsof  |grep deleted
cat        25558          root    1w      REG                8,2        21   69197603 /del/zhang.txt (deleted)

#文件恢复
[root@localhostdel]#ll /proc/25558/fd/1 
l-wx------ 1 root root 64 12月  6 09:57 /proc/25558/fd/1 -> /del/zhang.txt (deleted)
#开始恢复(25558是对应的进程pid号)
[root@localhostdel]#cp /proc/25558/fd/1  /del/
[root@localhostdel]#cat /del/1 
hello  linux
delete

  

extundelete工具安装
yum install extundelete -y

  

二、删除文件(无进程占用)(ext3文件系统)(yum下载的版本不能恢复ext4)(xfs文件系统不支持)

#查看文件系统
[root@localhost~]#blkid 
/dev/sdb: UUID="91d7e77d-83d8-44ad-9406-bc5e2dbb4d76" TYPE="ext3" 
#挂载到/mnt/目录
[root@localhostmnt]mount /dev/sdb  /mnt/
[root@localhostmnt]#ll
总用量 0
#创建文件与目录
[root@localhostmnt]#for i in {1..10};do echo zhang___${i} >test${i}.txt;done 
[root@localhostmnt]#mkdir  haha
[root@localhostmnt]#echo 123 >haha/123.txt
[root@localhostmnt]#ll
总用量 44
drwxr-xr-x 2 root root 4096 12月  6 10:43 haha
-rw-r--r-- 1 root root   11 12月  6 10:42 test10.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test1.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test2.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test3.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test4.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test5.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test6.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test7.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test8.txt
-rw-r--r-- 1 root root   10 12月  6 10:42 test9.txt
#删除文件与目录
[root@localhostmnt]#rm -rf *
[root@localhostmnt]#ll
总用量 0
##恢复步骤以及注意事项如下:
1、停止对当前分区做任何操作,防止inode被覆盖。inode被覆盖基本就告别恢复了。
2、可以通过dd命令对 当前分区进行备份,防止第三方软件恢复失败导致数据丢失。
3、对当前设备分区卸载。

#扫描误删除的文件
[root@localhostmnt]#extundelete  --inode 2 /dev/sdb 
.省略N行

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

File name                                       | Inode number | Deleted status
.                                                 2
..                                                2
test1.txt                                         11             Deleted
test2.txt                                         12             Deleted
test3.txt                                         13             Deleted
test4.txt                                         14             Deleted
test5.txt                                         15             Deleted
test6.txt                                         16             Deleted
test7.txt                                         17             Deleted
test8.txt                                         18             Deleted
test9.txt                                         19             Deleted
test10.txt                                        20             Deleted
haha                                              393217         Deleted

#1、恢复单一文件
[root@localhostmnt]#extundelete   /dev/sdb  --restore-file test1.txt 
#2、查看恢复的文件(当前目录下的RECOVERED_FILES目录中)
[root@localhostqqq]#ll RECOVERED_FILES/
总用量 4
-rw-r--r-- 1 root root 10 12月  6 11:01 test1.txt
[root@localhostqqq]#cat RECOVERED_FILES/test1.txt 
zhang___1
#3、恢复目录
extundelete /dev/sdb  --restore-directory haha (目录里面有文件不能这么自接恢复)
#4、恢复所有
extundelete /dev/sdb --restore-all
#查看
[root@localhostqqq]#ll RECOVERED_FILES/总用量 48
-rw-r--r-- 1 root root  4 12月  6 11:06 file.131074  ###内容是123
-rw-r--r-- 1 root root 11 12月  6 11:06 test10.txt
-rw-r--r-- 1 root root 10 12月  6 11:01 test1.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test1.txt.v1
-rw-r--r-- 1 root root 10 12月  6 11:06 test2.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test3.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test4.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test5.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test6.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test7.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test8.txt
-rw-r--r-- 1 root root 10 12月  6 11:06 test9.txt
#恢复指定inode。
extundelete /dev/sdb --restore-inode [inode号]

  

原文地址:https://www.cnblogs.com/zhangb8042/p/10075094.html