当前位置: 首页 > 科技观察

如何在Linux上恢复不小心删除的文件或目录

时间:2023-03-21 21:04:11 科技观察

Linux没有windows那么显眼的回收站,不是简单的恢复。Linux删除文件恢复分为两种情况,一种是删除后进程中有删除信息,另一种是删除后找不到进程,只能借助工具恢复。以下是检查和介绍。1、误删文件的过程还在进行中。这种进程在活动进程中一般都有连续的标准输入或输出。文件删除后,进程PID依然存在。这就是为什么有些服务器删除了一些文件,但磁盘没有释放的原因。例如,当前示例说明:cat通过shell终端对测试文件进行追加操作:[root@21yunwei_backup~]#echo"hellopy">testdelete.py[root@21yunwei_backup~]#cat>>testdelete.pyhellodelete另一个终端查看可以清楚的看到这个文件的内容:[root@21yunwei_backup~]#cattestdelete.pyhellopyhellodelete此时用rm-f./testdelete.py命令删除当前服务器上的文件,查看这个目录,文件已经不存在了,所以现在我们将其删除恢复出来。1、lsof检查被删除的文件进程是否还存在。这里用到一个命令lsof,如果没有安装,请自行yum或apt-get。类似这种情况,我们可以先lsof看看删除的文件是否还在:[root@21yunwei_backup~]#lsof|grepdeletedmysqld1512mysql5uREG252,306312397/tmp/ibzW3Lot(deleted)cat20464root1wREG252,3231310722/root/testdelete.py(deleted)幸运的是,如果是这样,进程仍然存在,然后开始恢复操作。2.恢复。恢复命令:cp/proc/pid/fd/1/指定目录/文件名进入进程目录,一般输入/proc/pid/fd/,对于当前情况:[root@21yunwei_backup~]#cd/proc/20464/fd[root@21yunwei_backupfd]#lltotal0lrwx-----1rootroot64Nov1518:120>/dev/pts/1l-wx-----1rootroot64Nov1518:121>/root/testdelete.py(删除)lrwx------1rootroot64Nov1518:122>/dev/pts/1恢复操作:cp1/tmp/testdelete.py查看文件:[root@21yunwei_backupfd]#cat/tmp/testdelete.pyhellopyhellodelete恢复完成。2、误删文件的进程已经不存在了,可以借助工具恢复。创建一个要删除的目录,回显一个文件,内容为:[root@21yunwei_backup21yunwei]#tree.├──deletetest│└──mail│└──test.py├──lost+found└──passwd3directories,2files[root@21yunwei_backup21yunwei]#cat/21yunwei/deletetest/mail/test.pyhelloDj[root@21yunwei_backup21yunwei]#tail-2passwdhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin执行删除:[root@21yunwei_backup21yunwei]#rm-rf./*[root@21yunwei_backup21yunwei]#lltotal0现在开始恢复误删文件。这种情况一般是没有daemon进程或者后台进程不断输入的,删就删,lsof也看不到。有必要使用工具。我们这里使用的工具是extundelete第三方工具。恢复步骤如下:1.停止对当前分区进行任何操作,防止inode被覆盖。inode被覆盖,基本告别recovery。例如停止所在分区的服务,卸载目录所在的设备,必要时断开网络。2、使用dd命令备份当前分区,防止第三方软件恢复失败导致数据丢失。适用于数据非常重要的情况。这里没有测试的备份。例如备份可以考虑采用如下方式:ddif=/path/filenameof=/dev/vdc13。使用umount命令卸载当前设备分区。或者fuser命令。如果umount/dev/vdb1或umount/21yunwei提示设备忙,可以使用fuser命令强制卸载:fuser-m-v-i-k/21yunwei4。下载第三方工具extundelete并安装,搜索误删文件进行恢复。wgethttp://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2tarjxvfextundelete-0.2.4.tar.bz2cdextundelete-0.2.4./configuremakemakeinstall扫描是否误删文件:[root@21yunwei_backupextundelete-0.2.4]#extundelete--inode2/dev/vdb1注意:扩展属性未恢复。正在加载文件系统元数据...8groupsloaded.Group:0Contentsofinode2:..OmitNlinesFilename|Inodenumber|Deletedstatus.2..2lost+found11Deleteddeletetedtest1我们删除的文件夹是扫描发现的,现在我们进行恢复操作。1.恢复单个文件passwd[root@21yunwei_backup/]#extundelete/dev/vdb1--restore-filepasswd注意:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.Successfullyrestoredfilepasswd恢复的文件放在当前目录FILECOVERED_。查看修复的文件:[root@21yunwei_backup/]#tail-5RECOVERED_FILES/passwdmysql:x:497:500::/home/mysql:/bin/falsenginx: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/nologin2.恢复目录deletetest[root@21yunwei_backup/]#extundelete/dev/vdb1--restore-directorydeletetestNOTICE:Extendedattributesarenotrestored.Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.Searchingforrecoverableinodesindirectorydeletetest...5recoverableinodesfound.Lookingthroughthedirectorystructurefordeletedfiles...[root@21yunwei_backup/]#catRECOVERED_FILES/deletetest/mail/test.pyhelloDj3.恢复所有[root@21yunwei_backup/]#extundelete/dev/vdb1--restore-all注意:扩展属性未恢复。正在加载文件系统元数据...8组已加载。加载日志描述符可重定向。46描述符重定向/...5可恢复inodesfound.Lookingthroughthedirectorystructurefordeletedfiles...0recoverableinodesstilllost.[root@21yunwei_backup/]#cdRECOVERED_FILES/[root@21yunwei_backupRECOVERED_FILES]#tree.├──deletetest│└──mail│└──test.py└──4directories.recoveryspecified,2filesinode[root@21yunwei_backup/]#extundelete/dev/vdb1--restore-inode14注意:扩展属性未恢复。Loadingfilesystemmetadata...8groupsloaded.Loadingjournaldescriptors...46descriptorsloaded.[root@21yunwei_backup/]#tail-5/RECOVERED_FILES/file.14mysqlx:497:500::/home/mysql:/bin/falsenginx: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选项参数说明,当前恢复的所有操作都已完成。