Linux误删文件恢复概述
前言
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命令删除该文件。
使用ls命令查看目录,确认文件已不存在,接下来尝试将其恢复。
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
│ └── mail
│ └── 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
│ └── 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恢复误删除的文件或目录内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!