Linux借助lsof/extundelete工具恢复误删掉的文件或目录

更新时间:2020年08月23日16:42:08 作者:21运维

这篇文章主要给你们介绍了关于Linux借助lsof/extundelete工具恢复误删掉的文件或目录的相关资料,文中通过示例代码介绍的十分详尽,对你们的学习或则工作具有一定的参考学习价值,须要的同事们下边随着小编来一起学习学习吧

序言

Linux不像windows有这么醒目的回收站,不是简单的还原就可以了。

linux删掉文件还原可以分为两种情况,一种是删掉之后在进程存在删掉信息,一种是删掉之后进程都找不到,只有依靠于工具还原。这儿分别检测介绍下

一,误删掉文件进程还在的情况。

这些通常是有活动的进程存在持续标准输入或输出,到时文件被删掉后,进程PID还是存在。这也就是有些服务器删掉一些文件并且c盘不释放的诱因。例如当前举例说明:

通过一个shell终端对一个测试文件做cat追加操作:

[root@21yunwei_backup ~]# echo "hello py" > testdelete.py
[root@21yunwei_backup ~]# cat >> testdelete.py 
hello delete

另外一个终端查看这个文件可以清楚看见内容:

[root@21yunwei_backup ~]# cat testdelete.py 
hello py
hello delete

此时,在当前服务器删掉文件rm-f./testdelete.py

命令查看这个目录linux运维博客,文件早已不存在了,这么如今我们将其恢复下来。

1,lsof查看删掉的文件进程是否还存在。这儿用到一个命令lsof,如没有安装请自行yum或则apt-get。类似这些情况linux公社,我们可以先lsof查看删掉的文件是否还在:

[root@21yunwei_backup ~]# lsof | grep deleted
mysqld  1512 mysql 5u  REG    252,3   0 6312397 /tmp/ibzW3Lot (deleted)
cat  20464 root 1w  REG    252,3   23 1310722 /root/testdelete.py (deleted)

辛运的是这些情况进程还存在linux lost found 恢复,这么开始进行恢复操作。

2,恢复。

恢复命令:

cp /proc/pid/fd/1 /指定目录/文件名

步入进程目录,通常是步入/proc/pid/fd/,针对当前情况:

[root@21yunwei_backup ~]# cd /proc/20464/fd
[root@21yunwei_backup fd]# ll
total 0
lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1
l-wx------ 1 root root 64 Nov 15 18:12 1 > /root/testdelete.py (deleted)
lrwx------ 1 root root 64 Nov 15 18:12 2 > /dev/pts/1

恢复操作:

cp 1 /tmp/testdelete.py

查看文件:

[root@21yunwei_backup fd]# cat /tmp/testdelete.py
hello py
hello delete

恢复完成。

二,误删掉的文件进程早已不存在,依靠于工具还原。

创建打算删掉的目录并echo一个带有内容的文件:

[root@21yunwei_backup 21yunwei]# tree
.
├── deletetest
│ └── mail
│  └── test.py
├── lost+found
└── passwd
 

Linux误删除文件恢复_lsof工具使用教程_linux lost found 恢复_extundelete恢复误删文件方法

3 directories, 2 files [root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py hello Dj [root@21yunwei_backup 21yunwei]# tail -2 passwd haproxy:x:500:502::/home/haproxy:/bin/bash tcpdump:x:72:72::/:/sbin/nologin

执行删掉操作:

[root@21yunwei_backup 21yunwei]# rm -rf ./*
[root@21yunwei_backup 21yunwei]# ll
total 0

如今开始进行误删掉文件的恢复。这些情况通常是没有守护进行或则后台进程对其持续输入,所以删掉就删掉了,lsof也看不到。就要依靠于工具。这儿我们采用的工具是extundelete第三方工具。恢复步骤如下:

1,停止对当前分区做任何操作,避免inode被覆盖。inode被覆盖基本就挥别单车了。例如停止所在分区的服务,卸载目录所在的设备,有必要的情况下都可以断网。

2,通过dd命令对当前分区进行备份,避免第三方软件恢复失败造成数据遗失。适宜数据特别重要的情况,这儿测试,就没有备份,如备份可以考虑如下形式:

ddif=/path/filenameof=/dev/vdc1

3,通过umount命令,对当前设备分区卸载。或则fuser命令。

umount/dev/vdb1或则umount/21yunwei

假如提示设备busy,可以用fuser命令强制卸载:fuser-m-v-i-k/21yunwei

4,下载第三方工具extundelete安装linux lost found 恢复,搜索误删掉的文件进行还原。

wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
tar jxvf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4
./configure 
make
make install

扫描误删掉的文件:

Linux误删除文件恢复_lsof工具使用教程_extundelete恢复误删文件方法_linux lost found 恢复

[root@21yunwei_backup extundelete-0.2.4]# extundelete --inode 2 /dev/vdb1 NOTICE: Extended attributes are not restored. Loading filesystem metadata ... 8 groups loaded. Group: 0 Contents of inode 2: . .省略N行 File name | Inode number | Deleted status . 2 .. 2 lost+found 11 Deleted deletetest 12 Deleted passwd 14 Deleted

通过扫描发觉了我们删掉的文件夹,如今执行恢复操作。

(1)恢复单一文件passwd

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwd  
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Successfully restored file passwd
 

恢复文件是放在了当前目录RECOVERED_FILES。

查看恢复的文件:

linux lost found 恢复_extundelete恢复误删文件方法_Linux误删除文件恢复_lsof工具使用教程

[root@21yunwei_backup /]# tail -5 RECOVERED_FILES/passwd mysql:x:497:500::/home/mysql:/bin/false nginx:x:496:501::/home/nginx:/sbin/nologin zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin haproxy:x:500:502::/home/haproxy:/bin/bash tcpdump:x:72:72::/:/sbin/nologin

(2)恢复目录deletetest

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-directory deletetest 
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searching for recoverable inodes in directory deletetest ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ... 
[root@21yunwei_backup /]# cat RECOVERED_FILES/deletetest/mail/test.py 
hello Dj

(3)恢复所有

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searching for recoverable inodes in directory / ... 
5 recoverable inodes found.
Looking through the directory structure for deleted files ... 

Linux误删除文件恢复_lsof工具使用教程_linux lost found 恢复_extundelete恢复误删文件方法

0 recoverable inodes still lost. [root@21yunwei_backup /]# cd RECOVERED_FILES/ [root@21yunwei_backup RECOVERED_FILES]# tree . ├── deletetest │ └── mail │ └── test.py └── passwd 2 directories, 2 files

(4),恢复指定inode。

[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
[root@21yunwei_backup /]# tail -5  /RECOVERED_FILES/file.14 
mysql:x:497:500::/home/mysql:/bin/false
nginx:x:496:501::/home/nginx:/sbin/nologin
zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

注意恢复inode的时侯,恢复下来的文件名和之前不一样,须要单独进行更名。内容是没问题的。

更多的extundelete用法请参考extundelete–help选项参数说明,当前恢复所有的操作完成。

总结

到此这篇关于Linux借助lsof/extundelete工具恢复误删掉的文件或目录的文章就介绍到这了,更多相关Linux恢复误删掉的文件或目录内容请搜索脚本之家先前的文章或继续浏览下边的相关文章希望你们之后多多支持脚本之家!

Tagged:
Author

这篇优质的内容由TA贡献而来

刘遄

《Linux就该这么学》书籍作者,RHCA认证架构师,教育学(计算机专业硕士)。

发表回复