Linux下使用命令和工具对误删的文件进行恢复 (linux小红帽系统)

教程大全 2025-07-09 13:36:30 浏览
linux小红帽系统

Linux不像windows有那么显眼的回收站,不是简单的还原就可以了;linux删除文件还原可以分为两种情况,一种是 删除以后在进程存在删除信息,一种是删除以后进程都找不到,只有借助于工具还原。

误删除文件进程还在的情况

这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID还是存在。这也就是有些 服务 删除一些文件但是磁盘不释放的原因。比如当前举例说明:

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

[root@21yunwei_backup~][root@21yunwei_backup~]hellodelete

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

[root@21yunwei_backup~]hellopyhellodelete

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

命令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。

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

[root@21yunwei_backup~]mysqld1512mysql5uREG252,306312397/tmp/ibzW3Lot(deleted)cat20464root1wREG252,3231310722/root/testdelete.py(deleted)

幸运的是这种情况进程还存在 ,那么开始进行恢复 操作。

2,恢复。

恢复命令:

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

进入 进程目录,一般是进入/proc/pid/fd/,针对当前情况:

[root@21yunwei_backup~][root@21yunwei_backupfd]total0lrwx------1rootroot64Nov1518:120>/dev/pts/1l-wx------1rootroot64Nov1518:121>/root/testdelete.py(deleted)lrwx------1rootroot64Nov1518:122>/dev/pts/1

恢复操作:

cp1/tmp/testdelete.py

查看文件:

[root@21yunwei_backupfd]hellopyhellodelete

恢复完成。

误删除的文件进程已经不存在,借助于工具还原

创建准备删除的目录并echo一个 带有内容的文件:

[root@21yunwei_backup21yunwei].├──deletetest│└──mail│└──test.py├──lost+found└──passwd3directories,2files[root@21yunwei_backup21yunwei]helloDj[root@21yunwei_backup21yunwei]haproxy:x:500:502::/home/haproxy:/bin/bashTCPdump:x:72:72::/:/sbin/nologin

执行删除操作:

[root@21yunwei_backup21yunwei][root@21yunwei_backup21yunwei]total0

现在开始进行误删除文件的恢复。这种情况一般是没有守护进行或者后台进程对其持续输入,所以删除就删除 了,lsof也看不到。就要借助于工具。这里我们采用的工具是extundelete第三方工具。恢复步骤如下:

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

2,通过dd命令对 当前分区进行备份,防止第三方软件恢复失败导致数据丢失。适合数据非常重要的情况,这里测试,就没有备份,如备份可以考虑如下方式:

dd=/path/filenameof=/dev/vdc1

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

umount/dev/vdb1或者umount/21yunwei

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

4,下载第三方工具extundelete安装,搜索误删除的文件进行还原。

wget

扫描误删除的文件:

[root@21yunwei_backupextundelete-0.2.4]NOTICE:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Group:0Contentsofinode2:..省略N行Filename|Inodenumber|Deletedstatus.2..2lost+found11Deleteddeletetest12Deletedpasswd14Deleted

通过扫描发现了我们删除的文件夹,现在执行恢复操作。

(1)恢复单一文件passwd

[root@21yunwei_backup/]NOTICE:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.Successfullyrestoredfilepasswd

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

查看恢复的文件:

[root@21yunwei_backup/]mysql:x:497:500::/home/mysql:/bin/nginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin

(2)恢复目录deletetest

[root@21yunwei_backup/]NOTICE:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.Searchingrecoverableinodesdirectorydeletetest...5recoverableinodesfound.Lookingthroughthedirectorystructuredeletedfiles...[root@21yunwei_backup/]helloDj

(3)恢复所有

[root@21yunwei_backup/]NOTICE:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.Searchingrecoverableinodesdirectory/...5recoverableinodesfound.Lookingthroughthedirectorystructuredeletedfiles...0recoverableinodesstilllost.[root@21yunwei_backup/][root@21yunwei_backupRECOVERED_FILES].├──deletetest│└──mail│└──test.py└──passwd2directories,2files

(4),恢复指定inode。

[root@21yunwei_backup/]NOTICE:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.[root@21yunwei_backup/]mysql:x:497:500::/home/mysql:/bin/nginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin

注意恢复inode的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。内容是没问题的。

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

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐