侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

linux删除的文件如何恢复?

linux  /  管理员 发布于 7年前   254

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

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

那么问题来了:

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

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

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

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

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

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

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

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

[root@docking ~]# echo "This is DeleteFile test." > deletefile.txt[root@docking ~]# lsdeletefile.txt[root@docking ~]# cat >> deletefile.txt Add SomeLine into deletefile for fun.

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

[root@docking ~]# lsdeletefile.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 deletefilecat       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总用量 0lrwx------ 1 root root 64 1月  18 22:21 0 -> /dev/pts/0l-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 backuptestcd backuptestmkdir deletetestmkdir deletetest/innerfolderecho "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└── tmppasswd2 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 nowtar: Child returned status 2tar: 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.m4extundelete-0.2.4/missingextundelete-0.2.4/autogen.shextundelete-0.2.4/aclocal.m4extundelete-0.2.4/configureextundelete-0.2.4/LICENSEextundelete-0.2.4/README...................................................
cd  extundelete-0.2.4./configure

若这步骤报错

[root@docking extundelete-0.2.4]# ./configure Configuring extundelete 0.2.4configure: error: in `/root/extundelete-0.2.4':configure: error: C++ compiler cannot create executablesSee `config.log' for more details

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

若执行上一步仍然报错,

[root@docking extundelete-0.2.4]# ./configure Configuring extundelete 0.2.4configure: 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.4Writing generated files to disk[root@docking extundelete-0.2.4]#

最后make然后 make install

[root@docking extundelete-0.2.4]# makemake -s all-recursiveMaking all in srcextundelete.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 installMaking install in src  /usr/bin/install -c extundelete '/usr/local/bin'

extundelete安装完成.

扫描误删除的文件:

使用df -lh查看挂载:

taroballs@taroballs-PC:~$ df -lh文件系统        容量  已用  可用 已用% 挂载点udev1.9G     0  1.9G    0% /devtmpfs           387M  1.8M  385M    1% /run/dev/sda2        92G   61G   26G   71% /tmpfs           1.9G   49M  1.9G    3% /dev/shmtmpfs           5.0M  4.0K  5.0M    1% /run/locktmpfs           1.9G     0  1.9G    0% /sys/fs/cgroup/dev/sda3       104G   56G   44G   57% /hometmpfs           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-00taroballs@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/taroballstaroballs@taroballs-PC:~$ df -lh | grep /dev/sdb1taroballs@taroballs-PC:~$ #记得删除一定要后umount哦,不然二次写入谁也帮不了你呢。

通过inode节点恢复

taroballs@taroballs-PC:~$ mkdir recovertesttaroballs@taroballs-PC:~$ cd recovertest/taroballs@taroballs-PC:~/recovertest$

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

taroballs@taroballs-PC:/media/taroballs/taroballs$ sudo extundelete /dev/sdb1 --inode 2NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Group: 0Contents of inode 2: ..省略N行 File name   | Inode number | Deleted status. 2..2deletetest    12 Deletedtmppasswd        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  deletetestNOTICE: 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-allNOTICE: 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└── tmppasswd2 directories, 2 files

(4)恢复指定inode

taroballs@taroballs-PC:~/recovertest$ extundelete /dev/sdb1 --restore-inode 14NOTICE: 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 --helpUsage: extundelete [options] [--] device-fileOptions:  --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 inoShow info on inode 'ino'.  --block blkShow 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删除的文件如何恢复?的详细内容,更多请关注其它相关文章!


  • 上一条:
    linux有哪些文件系统?
    下一条:
    linux软件是什么?
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客