Linux利用lsof/extundelete工具恢复误删除的文件或目录
作者:21运维 发布时间:2023-08-05 01:34:39
前言
Linux不像windows有那么显眼的回收站,不是简单的还原就可以了。
linux删除文件还原可以分为两种情况,一种是删除以后在进程存在删除信息,一种是删除以后进程都找不到,只有借助于工具还原。这里分别检查介绍下
一,误删除文件进程还在的情况。
这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID还是存在。这也就是有些服务器删除一些文件但是磁盘不释放的原因。比如当前举例说明:
通过一个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
命令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。
1,lsof查看删除的文件进程是否还存在。这里用到一个命令lsof,如没有安装请自行yum或者apt-get。类似这种情况,我们可以先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)
幸运的是这种情况进程还存在 ,那么开始进行恢复 操作。
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
│ └── test.py
├── lost+found
└── passwd
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命令对 当前分区进行备份,防止第三方软件恢复失败导致数据丢失。适合数据非常重要的情况,这里测试,就没有备份,如备份可以考虑如下方式:
dd if=/path/filename of=/dev/vdc1
3,通过umount命令,对当前设备分区卸载。或者fuser 命令。
umount /dev/vdb1 或者 umount /21yunwei
如果提示设备busy,可以用fuser命令强制卸载:fuser -m -v -i -k /21yunwei
4,下载第三方工具extundelete安装,搜索误删除的文件进行还原。
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
扫描误删除的文件:
[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。
查看恢复的文件:
[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 ...
0 recoverable inodes still lost.
[root@21yunwei_backup /]# cd RECOVERED_FILES/
[root@21yunwei_backup RECOVERED_FILES]# tree
.
├── deletetest
│ └── 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选项参数说明,当前恢复所有的操作完成。
来源:http://www.21yunwei.com/archives/6030


猜你喜欢
- 国内做SEO的人可能会经常听到这样一句话“你可以不懂如何改META,但你必须知道如何做外链。”可见外链作为SEO优化中的一个手段被许多SEO
- 如果你已经是google adsense内容发布商,登录帐户后台后可以看到每个月初由google adsense发送的优化建议(如“2007
- 互联网的普及早已深透到我们生活的方方面面,大家都在为互联网的飞速发展感到惊叹。今天,笔者已找不到任何一个可以离开互联网的理由。如果真的离开了
- 对于外部链接的质量判断,基本上可以根据以下几个因素来:1.站点相关 * 相关性是搜索引擎用来判断网站质量的一个重要因素,也是SEO的重点。所
- 用户需要输入解密密码才能访问已加密的虚拟机。没有解密密码,就无法访问加密虚拟机的VMDK文件。VMware Workstation加密位于物
- 网站简介:蓝色理想 www.blueidea.com 诞生于1999年的10月,站长本名曾沐阳。从成立之初,蓝色理想就以建设网站设计与开发人
- 中国人民银行县支行服务器(Windows NT)一般既作为单位内部局域网的域控制器,又担负着网上公文处理、共享资源管理和收发电子邮件等重要任
- 一、WSUS 安装要求1、硬件要求:对于多达 500 个客户端的服务器,建议使用以下硬件:* 1 GHz 的处理器* 1 GB 的 RAM2
- 1、下载镜像docker pull selenium/hubdocker pull selenium/node-firefoxdocker
- iis &nb
- Godaddy主机用户如果想使用ColdFusion连接到某个数据库的话,必须创建个ColdFusion DSN. To Create a
- 四、Sendmail的配置配置Sendmail的步骤如下。1、在/etc/mail目录下创建access文件,内容如下:127.0.0.1
- 最近,湖南SEO在子目录下面安装了一个discuz论坛程序。大家都知道zblog是asp,discuz是php,那么asp的zblog应该如
- 一、作业执行容错Flink 的错误恢复机制分为多个级别,即 Execution 级别的 Failover 策略和 ExecutionGrap
- .htaccess文件是非常有用的,下面一篇介绍:• Part 1 – Introduct
- IIS服务器出错的原因是复杂的。如服务启动失败、IIS进程中断或者站点不能启动这些错误都会在系统日志中记录一个错误事件。不论IIS出现何种错
- 我是一名专职的设计师,工作之余,经常会去一些个威客网承接的任务,来充实一下自己的,我去的目的也并不是单单只为钱,也为了去消磨一点时间,可是最
- 当网站没流量的时候,我们的站长该怎么做呢?我就拿我一个成功网站的(www.hngwyw.com)经验给大家说下,从以前的100到现在日IP5
- 要建立一个安全Linux服务器就首先要了解Linux环境下和网络服务相关的配置文件的含义及如何进行安全的配置。在Linux系统中,TCP/I
- 现在网络上的术语太多了,不记一下还真容易犯糊涂。雅虎统计中有个“UV”这个简写名称,一开始还真没弄明白什么意思,后来查了一下,原来就是独立访