首页 运维 linux运维 linux删除的文件如何恢复?

linux删除的文件如何恢复?

Apr 20, 2020 pm 04:12 PM
linux

linux删除的文件如何恢复?下面本篇文章给大家介绍一下恢复Linux删除文件的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

linux删除的文件如何恢复?

linux不像windows有个回收站,使用rm -rf *基本上文件是找不回来的。

那么问题来了:

对于linux下误删的文件,我们是否真的无法通过软件进行恢复呢?

答案当然是否定的,对于误删的文件,我们还是能通过软件恢复过来的。对于误删文件还原可以分为两种情况:

  • 一种是删除以后在进程存在删除信息

  • 一种是删除以后进程都找不到,只有借助于工具还原。

接下来以例子分别解说下两种不同的误删还原方式:

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

这种一般是有活动的进程存在持续标准输入或输出,到时文件被删除后,进程PID依旧存在。这也是有些服务器删除一些文件但是磁盘不释放的原因。

打开一个终端对一个测试文件做cat追加操作:

[root@docking ~]# echo "This is DeleteFile test." > deletefile.txt
[root@docking ~]# ls
deletefile.txt
[root@docking ~]# cat >> deletefile.txt 
Add SomeLine into deletefile for fun.
登录后复制

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

[root@docking ~]# ls
deletefile.txt
[root@docking ~]# cat deletefile.txt 
This is DeleteFile test.
Add SomeLine into deletefile for fun.
登录后复制

此时,删除文件rm -f deletefile.txt

[root@docking ~]# rm -f deletefile.txt 
[root@docking ~]# ls
#命令查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。
登录后复制
  • lsof查看删除的文件进程是否还存在。

  • 如没有安装请自行yum install lsof或者apt-get install lsof

1、类似这种情况,我们可以先lsof查看删除的文件 是否还在

[root@docking ~]# lsof | grep deletefile
cat       21796          root    1w      REG              253,1        63     138860 /root/deletefile.txt (deleted)
登录后复制

2、恢复cp /proc/pid/fd/1 /指定目录/文件名

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

[root@docking ~]# cd /proc/21796/fd
[root@docking fd]# ll
总用量 0
lrwx------ 1 root root 64 1月  18 22:21 0 -> /dev/pts/0
l-wx------ 1 root root 64 1月  18 22:21 1 -> /root/deletefile.txt (deleted)
lrwx------ 1 root root 64 1月  18 22:21 2 -> /dev/pts/0
登录后复制

恢复操作:

[root@docking fd]# cp 1 ~/deletefile.txt.backup
[root@docking fd]# cat ~/deletefile.txt.backup 
This is DeleteFile test.
Add SomeLine into deletefile for fun.
登录后复制

3、恢复完成。

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

准备一些文件目录

#准备一份挂载的盘
mkdir backuptest
cd backuptest
mkdir deletetest
mkdir deletetest/innerfolder
echo "Delete a folder test." > deletetest/innerfolder/deletefile.txt 

echo "tcpdump:x:172:72::/:/sbin/nologin" > tmppasswd
登录后复制

最后准备的目录结构如下:

taroballs@taroballs-PC:/media/taroballs/taroballs/backuptest$ cd ..
taroballs@taroballs-PC:/media/taroballs/taroballs$ tree backuptest/
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd

2 directories, 2 files
登录后复制

现在开始删除该目录rm -rf backuptest/

taroballs@taroballs-PC:/media/taroballs/taroballs$ rm -rf backuptest/
taroballs@taroballs-PC:/media/taroballs/taroballs$  ls  -l
总用量 0
登录后复制

这种情况一般是没有守护进行或者后台进程对其持续输入,所以删除就真的删除了。lsof也看不到,故需要采用工具进行恢复。

现在开始进行误删除文件的恢复。

我们采用的工具是extundelete第三方工具。恢复步骤以及注意事项如下:

  • 停止对当前分区做任何操作,防止inode被覆盖。inode被覆盖基本就告别恢复了。

  • 夸张一点讲,比如停止所在分区的服务,卸载目录所在的设备,有必要的情况下都可以断网。

  • 通过dd命令对 当前分区进行备份,防止第三方软件恢复失败导致数据丢失。

  • 适合数据非常重要的情况,这里是例子,所以就没有备份,如备份可以考虑如下方式:dd if=/path/filename of=/dev/vdc1

  • 通过umount命令,对当前设备分区卸载。或者fuser 命令umount /dev/vdb1

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

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

extundelete工具安装

extundelete下载地址:http://extundelete.sourceforge.net/

wget https://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
登录后复制

解压该文件tar jxvf extundelete-0.2.4.tar.bz2

若报这种错误

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
tar (child): bzip2:无法 exec: 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
登录后复制

则使用yum -y install bzip2进行解决

[root@docking ~]# tar jxvf extundelete-0.2.4.tar.bz2 
extundelete-0.2.4/
extundelete-0.2.4/acinclude.m4
extundelete-0.2.4/missing
extundelete-0.2.4/autogen.sh
extundelete-0.2.4/aclocal.m4
extundelete-0.2.4/configure
extundelete-0.2.4/LICENSE
extundelete-0.2.4/README
...................................................
登录后复制
cd  extundelete-0.2.4
./configure
登录后复制

若这步骤报错

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: in `/root/extundelete-0.2.4':
configure: error: C++ compiler cannot create executables
See `config.log' for more details
登录后复制

则使用yum -y install gcc-c++解决.

若执行上一步仍然报错,

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
configure: error: Can't find ext2fs library
登录后复制

则使用yum -y install e2fsprogs e2fsprogs-devel来解决。
#Ubuntu的解决办法为sudo apt-get install e2fslibs-dev e2fslibs-dev

不出意外的话到这里应该configure能够顺利完成.

[root@docking extundelete-0.2.4]# ./configure 
Configuring extundelete 0.2.4
Writing generated files to disk
[root@docking extundelete-0.2.4]#
登录后复制

最后make然后 make install

[root@docking extundelete-0.2.4]# make
make -s all-recursive
Making all in src
extundelete.cc: 在函数‘ext2_ino_t find_inode(ext2_filsys, ext2_filsys, ext2_inode*, std::string, int)’中:
extundelete.cc:1272:29: 警告:在 {} 内将‘search_flags’从‘int’转换为较窄的类型‘ext2_ino_t {aka unsigned int}’ [-Wnarrowing]
    buf, match_name2, priv, 0};
                             ^
[root@docking extundelete-0.2.4]# make install
Making install in src
  /usr/bin/install -c extundelete '/usr/local/bin'
登录后复制

extundelete安装完成.

扫描误删除的文件:

使用df -lh查看挂载:

taroballs@taroballs-PC:~$ df -lh
文件系统        容量  已用  可用 已用% 挂载点
udev            1.9G     0  1.9G    0% /dev
tmpfs           387M  1.8M  385M    1% /run
/dev/sda2        92G   61G   26G   71% /
tmpfs           1.9G   49M  1.9G    3% /dev/shm
tmpfs           5.0M  4.0K  5.0M    1% /run/lock
tmpfs           1.9G     0  1.9G    0% /sys/fs/cgroup
/dev/sda3       104G   56G   44G   57% /home
tmpfs           387M   40K  387M    1% /run/user/1000
/dev/sda4        70G   20G   47G   30% /media/taroballs/d8423f8c-d687-4c03-a7c8-06a7fb57f96d
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs
/dev/sr0        4.0G  4.0G     0  100% /media/taroballs/2018-01-16-12-36-00-00
taroballs@taroballs-PC:~$ cd /media/taroballs/taroballs/
taroballs@taroballs-PC:/media/taroballs/taroballs$
登录后复制

可以看到,我们的目录/media/taroballs/taroballs

挂载到/dev/sdb1 这个文件系统中.

umount我们的挂载盘

比如:

taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
/dev/sdb1       6.8G  4.1G  2.8G   60% /media/taroballs/taroballs
登录后复制

umount这个目录

taroballs@taroballs-PC:~$ umount /media/taroballs/taroballs
taroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1
taroballs@taroballs-PC:~$ 
#记得删除一定要后umount哦,不然二次写入谁也帮不了你呢。
登录后复制

通过inode节点恢复

taroballs@taroballs-PC:~$ mkdir recovertest
taroballs@taroballs-PC:~$ cd recovertest/
taroballs@taroballs-PC:~/recovertest$
登录后复制

执行恢复extundelete /dev/sdb1 --inode 2

taroballs@taroballs-PC:/media/taroballs/taroballs$ sudo extundelete /dev/sdb1 --inode 2
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
deletetest                                        12             Deleted
tmppasswd                                            14             Deleted
登录后复制

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

(1)恢复单一文件tmppasswd

taroballs@taroballs-PC:~/recovertest$  extundelete /dev/sdb1 --restore-file passwd   
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Successfully restored file tmppasswd
登录后复制

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

查看恢复的文件:

taroballs@taroballs-PC:~/recovertest$ cat tmppasswd 
tcpdump:x:172:72::/:/sbin/nologin
登录后复制

(2)恢复目录deletetest

extundelete /dev/sdb1 --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 ...
登录后复制

(3)恢复所有

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --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. 
taroballs@taroballs-PC:~/recovertest$ tree 
backuptest/
├── deletetest
│   └── innerfolder
│       └── deletefile.txt
└── tmppasswd
2 directories, 2 files
登录后复制

(4)恢复指定inode

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
taroballs@taroballs-PC:~/recovertest$ cat file.14 
tcpdump:x:172:72::/:/sbin/nologin
#注意恢复inode的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。
登录后复制

最后附上extundelete的用法:

$ extundelete --help
Usage: extundelete [options] [--] device-file
Options:
  --version, -[vV]       Print version and exit successfully.
  --help,                Print this help and exit successfully.
  --superblock           Print contents of superblock in addition to the rest.
                         If no action is specified then this option is implied.
  --journal              Show content of journal.
  --after dtime          Only process entries deleted on or after 'dtime'.
  --before dtime         Only process entries deleted before 'dtime'.Actions:
  --inode ino            Show info on inode 'ino'.
  --block blk            Show info on block 'blk'.
  --restore-inode ino[,ino,...]
                         Restore the file(s) with known inode number 'ino'.
                         The restored files are created in ./RECOVERED_FILES                         with their inode number as extension (ie, file.12345).
  --restore-file 'path'  Will restore file 'path'. 'path' is relative to root
                         of the partition and does not start with a '/'
                         The restored file is created in the current
                         directory as 'RECOVERED_FILES/path'.
  --restore-files 'path' Will restore files which are listed in the file 'path'.
                         Each filename should be in the same format as an option
                         to --restore-file, and there should be one per line.
  --restore-directory 'path'
                         Will restore directory 'path'. 'path' is relative to the
                         root directory of the file system.  The restored
                         directory is created in the output directory as 'path'.
  --restore-all          Attempts to restore everything.
  -j journal             Reads an external journal from the named file.
  -b blocknumber         Uses the backup superblock at blocknumber when opening
                         the file system.
  -B blocksize           Uses blocksize as the block size when opening the file
                         system.  The number should be the number of bytes.
  --log 0                Make the program silent.
  --log filename         Logs all messages to filename.--log D1=0,D2=filename   Custom control of log messages with comma-separated
   Examples below:       list of options.  Dn must be one of info, warn, or   --log info,error      error.  Omission of the '=name' results in messages   --log warn=0          with the specified level to be logged to the console.
   --log error=filename  If the parameter is '=0', logging for the specified
                         level will be turned off.  If the parameter is
                         '=filename', messages with that level will be written
                         to filename.
   -o directory          Save the recovered files to the named directory.
                         The restored files are created in a directory
                         named 'RECOVERED_FILES/' by default.
登录后复制

推荐:《linux教程

以上是linux删除的文件如何恢复?的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

无法以 root 身份登录 mysql 无法以 root 身份登录 mysql Apr 08, 2025 pm 04:54 PM

无法以 root 身份登录 MySQL 的原因主要在于权限问题、配置文件错误、密码不符、socket 文件问题或防火墙拦截。解决方法包括:检查配置文件中 bind-address 参数是否正确配置。查看 root 用户权限是否被修改或删除,并进行重置。验证密码是否准确无误,包括大小写和特殊字符。检查 socket 文件权限设置和路径。检查防火墙是否阻止了 MySQL 服务器的连接。

C语言条件编译:新手入门到实战应用的详尽指南 C语言条件编译:新手入门到实战应用的详尽指南 Apr 04, 2025 am 10:48 AM

C语言条件编译是一种根据编译时条件选择性编译代码块的机制,入门方法有:使用#if和#else指令根据条件选择代码块。常用条件表达式包括STDC、_WIN32和linux。实战案例:根据操作系统打印不同消息。根据系统位数使用不同的数据类型。根据编译器支持不同的头文件。条件编译增强了代码的可移植性和灵活性,使其适应编译器、操作系统和CPU架构变化。

Linux的5个基本组件是什么? Linux的5个基本组件是什么? Apr 06, 2025 am 12:05 AM

Linux的五个基本组件是:1.内核,管理硬件资源;2.系统库,提供函数和服务;3.Shell,用户与系统交互的接口;4.文件系统,存储和组织数据;5.应用程序,利用系统资源实现功能。

mysql 无法启动怎么解决 mysql 无法启动怎么解决 Apr 08, 2025 pm 02:21 PM

MySQL启动失败的原因有多种,可以通过检查错误日志进行诊断。常见原因包括端口冲突(检查端口占用情况并修改配置)、权限问题(检查服务运行用户权限)、配置文件错误(检查参数设置)、数据目录损坏(恢复数据或重建表空间)、InnoDB表空间问题(检查ibdata1文件)、插件加载失败(检查错误日志)。解决问题时应根据错误日志进行分析,找到问题的根源,并养成定期备份数据的习惯,以预防和解决问题。

mysql 可以在 android 上运行吗 mysql 可以在 android 上运行吗 Apr 08, 2025 pm 05:03 PM

MySQL无法直接在Android上运行,但可以通过以下方法间接实现:使用轻量级数据库SQLite,由Android系统自带,无需单独服务器,资源占用小,非常适合移动设备应用。远程连接MySQL服务器,通过网络连接到远程服务器上的MySQL数据库进行数据读写,但存在网络依赖性强、安全性问题和服务器成本等缺点。

MySQL安装在特定系统版本上报错的解决途径 MySQL安装在特定系统版本上报错的解决途径 Apr 08, 2025 am 11:54 AM

MySQL安装报错的解决方法是:1.仔细检查系统环境,确保满足MySQL的依赖库要求,不同操作系统和版本需求不同;2.认真阅读报错信息,根据提示(例如缺少库文件或权限不足)采取对应措施,例如安装依赖或使用sudo命令;3.必要时,可尝试源码安装并仔细检查编译日志,但这需要一定的Linux知识和经验。最终解决问题的关键在于仔细检查系统环境和报错信息,并参考官方文档。

mySQL下载完安装不了 mySQL下载完安装不了 Apr 08, 2025 am 11:24 AM

MySQL安装失败的原因主要有:1.权限问题,需以管理员身份运行或使用sudo命令;2.依赖项缺失,需安装相关开发包;3.端口冲突,需关闭占用3306端口的程序或修改配置文件;4.安装包损坏,需重新下载并验证完整性;5.环境变量配置错误,需根据操作系统正确配置环境变量。解决这些问题,仔细检查每个步骤,就能顺利安装MySQL。

MySQL安装时提示缺少依赖项如何解决 MySQL安装时提示缺少依赖项如何解决 Apr 08, 2025 pm 12:00 PM

MySQL安装失败通常因缺少依赖项导致。解决方法:1.使用系统包管理器(如Linux的apt、yum或dnf,Windows的VisualC Redistributable)安装缺失的依赖库,例如sudoaptinstalllibmysqlclient-dev;2.仔细检查错误信息,逐一解决复杂的依赖关系;3.确保包管理器源配置正确,能访问网络;4.对于Windows,下载安装必要的运行库。养成阅读官方文档和善用搜索引擎的习惯,能有效解决问题。

See all articles