简介

在Linux系统下,通过命令“rm -rf”可以将任何数据直接从硬盘删除,并且没有任何提示,同时Linux下也没有与Windows下回收站类似的功能,也就意味着,数据在删除后通过常规的手段是无法恢复的,因此使用这个命令要非常慎重。在使用rm命令的时候,比较稳妥的方法是把命令参数放到后面,这样有一个提醒的作用。其实还有一个方法,那就是将要删除的东西通过mv命令移动到系统下的/tmp目录下,然后写个脚本定期执行清除操作,这样做可以在一定程度上降低误删除数据的危险性。

其实保证数据安全最好的方法是做好备份,虽然备份不是万能的,但是没有备份是万万不行的。任何数据恢复工具都有一定局限性,都不能保证完整地恢复出所有数据,因此,把备份作为核心,把数据恢复工具作为辅助是运维人员必须坚持的一个准则。

在Linux下,基于开源的数据恢复工具有很多,常见的有debugfs、R-Linux、ext3grep、extundelete等,比较常用的有ext3grep和extundelete,这两个工具的恢复原理基本一样,只是extundelete功能更加强大。

extundelete的恢复原理

在Linux下可以通过“ls –id”命令来查看某个文件或者目录的inode值,例如查看根目录的inode值,可以输入:

[root@cloud1 ~]# ls -id  /
2 /

由此可知,根目录的inode值一般为2。

在利用extundelete恢复文件时并不依赖特定文件格式,首先extundelete会通过文件系统的inode信息(根目录的inode一般为2)来获得当前文件系统下所有文件的信息,包括存在的和已经删除的文件,这些信息包括文件名和inode。然后利用inode信息结合日志去查询该inode所在的block位置,包括直接块,间接块等信息。最后利用dd命令将这些信息备份出来,从而恢复数据文件。

安装

yum直接安装:

yum install -y extundelete

也可以编译安装:

wget  http://zy-res.oss-cn-hangzhou.aliyuncs.com/server/extundelete-0.2.4.tar.bz2
yum -y install  bzip2  e2fsprogs-devel  e2fsprogs  gcc-c++ make
tar -xvjf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4 ./configure
make && make install

使用方法extundelete --help

[root@oraclelhr reco]# 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_FILESwith their inode number as extension (ie, file.12345).--restore-file 'path'  Will restore file 'path'. 'path' is relative to rootof the partition and does not start with a '/'The restored file is created in the currentdirectory 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 optionto --restore-file, and there should be one per line.--restore-directory 'path'Will restore directory 'path'. 'path' is relative to theroot directory of the file system.  The restoreddirectory 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 openingthe file system.-B blocksize           Uses blocksize as the block size when opening the filesystem.  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-separatedExamples 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 specifiedlevel will be turned off.  If the parameter is'=filename', messages with that level will be writtento filename.-o directory          Save the recovered files to the named directory.The restored files are created in a directorynamed 'RECOVERED_FILES/' by default.

其中,参数(options)有:
–version, -[vV],显示软件版本号。
–help,显示软件帮助信息。
–superblock,显示超级块信息。
–journal,显示日志信息。
–after dtime,时间参数,表示在某段时间之后被删的文件或目录。
–before dtime,时间参数,表示在某段时间之前被删的文件或目录。

动作(action)有:
–inode ino,显示节点“ino”的信息。
–block blk,显示数据块“blk”的信息。
–restore-inode ino[,ino,…],恢复命令参数,表示恢复节点“ino”的文件,恢复的文件会自动放在当前目录下的RESTORED_FILES文件夹中,使用节点编号作为扩展名。
–restore-file ‘path’,恢复命令参数,表示将恢复指定路径的文件,并把恢复的文件放在当前目录下的RECOVERED_FILES目录中。
–restore-files ‘path’,恢复命令参数,表示将恢复在路径中已列出的所有文件。
–restore-all,恢复命令参数,表示将尝试恢复所有目录和文件。
-j journal,表示从已经命名的文件中读取扩展日志。
-b blocknumber,表示使用之前备份的超级块来打开文件系统,一般用于查看现有超级块是不是当前所要的文件。
-B blocksize,通过指定数据块大小来打开文件系统,一般用于查看已经知道大小的文件

常用命令

-- 查看sdb1 分区根目录下面可被恢复的文件及文件夹
extundelete /dev/sdb1 --inode 2
-- 恢复单个文件,恢复对应inode的文件,例如1.txt的inode为12,那么此命令即恢复1.txt
extundelete /dev/sdb1 --restore-inode 12
extundelete /dev/sdb1 --restore-file  filename-- 恢复目录,空目录不会被恢复
extundelete /dev/sdb1 --restore-directory-- 恢复所有文件
extundelete /dev/sdb1 --restore-all

注意

在数据删除之后,首先要卸载被删除数据所在的磁盘或分区,如果是系统根据分区遭到误删除,就需要进入单用户模式下,将根分区以只读的方式挂载。

原因:因为文件删除之后,仅仅是将文件的inode节点中的扇区指针清零,实际上文件还存在磁盘上面,如果磁盘以读写方式挂载,这些删除的数据块可能会被系统从新分配出去,这些数据块被覆盖之后,这些数据就真的丢失了,所以以只读的方式挂载,尽可能避免数据被覆盖。

文件句柄存在

linux删除文件还原可以分为两种情况,一种是删除以后进程存在删除信息,一种是删除以后进程都找不到,只有借助于工具还原,例如extundelete工具。

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

恢复过程可以参考:
http://blog.itpub.net/26736162/viewspace-1623938/

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

[root@backup ~]# echo  "hello  py" > testdelete.py
[root@backup ~]# cat  >> testdelete.py
hello delete

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

[root@backup ~]# cat testdelete.py
hello  py
hello delete

此时,在当前服务器删除文件rm -f ./testdelete.py

查看这个目录,文件已经不存在了,那么现在我们将其恢复出来。

1,lsof查看删除的文件进程是否还存在。这里用到一个 lsof,如没有安装请自行yum或者apt-get。类似这种情况,我们可以先lsof查看删除的文件 是否还在:

[root@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@backup ~]# cd   /proc/20464/fd
[root@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@backup fd]# cat  /tmp/testdelete.py
hello  py
hello delete

恢复完成。

extundelete恢复示例

创建准备删除的目录并echo一个 带有内容的文件:

[root@backup yunwei]# tree
.
├── deletetest
│   └── mail
│       └── test.py
├── lost+found
└── passwd3 directories, 2 files
[root@backup yunwei]# cat /yunwei/deletetest/mail/test.py
hello Dj
[root@backup yunwei]# tail  -2  passwd
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

执行删除操作:

[root@backup yunwei]# rm  -rf    ./*
[root@backup yunwei]# ll
total 0

现在开始进行删除文件的恢复。这种情况一般是没有守护者进行或者后台进程对其持续输入,所以删除就删除 了,lsof也看不到。就要借助于工具。这里我们采用的工具是extundelete第三方工具。恢复步骤如下:

1,停止对当前分区做任何操作,防止inode被覆盖。inode被覆盖基本就告别自行车了。比如停止所在分区的服务,卸载目录所在的设备,有必要的情况下都可以断网。

2,通过dd命令对 当前分区进行备份,防止第三方软件恢复失败导致数据丢失。适合数据非常重要的情况,这里测试,就没有备份,如备份可以考虑如下方式:

dd if=/path/filename of=/dev/vdc1

3,通过umount命令,对当前设备分区卸载。或者fuser 命令。

umount /dev/vdb1 或者 umount /yunwei

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

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

wget
tar jxvf extundelete-0.2.4.tar.bz2
cd  extundelete-0.2.4
./configure
make
make  install

扫描误删除的文件:

[root@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@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@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@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@backup /]# cat  RECOVERED_FILES/deletetest/mail/test.py
hello Dj

(3)恢复所有

[root@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@backup /]# cd RECOVERED_FILES/
[root@backup RECOVERED_FILES]# tree
.
├── deletetest
│   └── mail
│       └── test.py
└── passwd2 directories, 2 files

(4),恢复指定inode。

[root@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@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的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。内容是没问题的。

觉得写得好的小伙伴给个点赞+关注啦,谢谢~

如果有写得不正确的地方,麻烦指出,感激不尽。

同时非常期待小伙伴们能够关注,后面慢慢推出更好的干货~嘻嘻

Linux数据恢复工具之extundelete相关推荐

  1. linux文件恢复工具下载_十大最佳Linux数据恢复工具,用于恢复已删除/损坏的文件

    linux文件恢复工具下载 Today we'll be going over the best Linux data recovery tools to help you recover any d ...

  2. fat32 linux 打包工具_11款最棒的Linux数据恢复工具发布啦!

    导读 无论你使用的是台式电脑还是笔记本,需要关注的重点之一都是如何保护好你的宝贵数据.因为总会有各种突发情况使你的系统崩溃,然后你要做的就是恢复数据.不管你怎么想,要是我失去了所有的数据却无法恢复的话 ...

  3. 【收集】11款Linux数据恢复工具

    如果你使用的是Linux操作系统,那么你一定想知道一旦硬盘崩溃的话又该如何保存和恢复数据.其实,现在有很多Linux数据恢复工具可以让我们摆脱数据安全的困扰.小编已经为各位准备好了一些最好的Linux ...

  4. linux硬盘恢复软件哪个好用,这些Linux数据恢复工具,你用过哪几个

    不论你运用的是台式电脑仍是笔记本,需求重视的要点之一都是怎么保护好你的名贵数据.由于总会有各种突发状况使你的系统溃散,然后你要做的就是恢复数据.不论你怎么想,要是我失去了一切的数据却无法恢复的话,我会 ...

  5. linux系统硬盘数据恢复软件下载,11 款最棒的 Linux 数据恢复工具

    原标题:11 款最棒的 Linux 数据恢复工具 (点击上方公众号,可快速关注) 英文:The Geek Desire 译者:码农网 www.codeceo.com/article/11-linux- ...

  6. 11款最棒的Linux数据恢复工具

    无论你使用的是台式电脑还是笔记本,需要关注的重点之一都是如何保护好你的宝贵数据.因为总会有各种突发情况使你的系统崩溃,然后你要做的就是恢复数据.不管你怎么想,要是我失去了所有的数据却无法恢复的话,我会 ...

  7. 11款最棒的Linux数据恢复工具发布啦!

    导读 无论你使用的是台式电脑还是笔记本,需要关注的重点之一都是如何保护好你的宝贵数据.因为总会有各种突发情况使你的系统崩溃,然后你要做的就是恢复数据.不管你怎么想,要是我失去了所有的数据却无法恢复的话 ...

  8. linux有数据恢复工具吗,Linux运维人员必备的数据恢复工具有哪些?

    今天小编要跟大家分享的文章是关于Linux运维人员必备的数据恢复工具有哪些?相信对于很多正在从事Linux运维工作,或者是其他Linux运维爱好者都需要重点.无论你使用的是台式电脑还是笔记本,需要关注 ...

  9. linux重新分区丢失数据恢复,用于Linux上数据及被删除分区恢复的5个最佳数据恢复工具...

    ------ 什么是数据恢复(Data Recovery)? 数据恢复是当数据不能正常访问时,从损坏/报错/讹误或不可访问的磁盘介质上进行数据拯救的一个过程. 被拯救的数据通常存放在存储介质如内部或外 ...

最新文章

  1. UI设计师必备技能,看看你都学会了吗?
  2. input的readonly属性与TextBox的ReadOnly和Enabled属性区别
  3. html下拉列表插件,js+CSS实现模拟华丽的select控件下拉菜单效果
  4. 高级软件工程第四次作业:两只小熊队团队作业
  5. [流体力学] 推导柱坐标系下的连续性方程
  6. Android Studio 打开activity_main.xml不能正常显示
  7. python从文件初始化失败_iOS 6:libpython2.7.a初始化导入错误
  8. JAVA高并发工作笔记0002---高并发编程之使用ThreadFactory来创建新的线程
  9. ThinkPHP admin.php后台登录
  10. 添加多个button或其他的控件
  11. 熊猫concat()例子
  12. 中国移动吉比特GM220-S网关改桥接模式
  13. 22个最常见的Python3 包
  14. 电磁兼容测试技术简介
  15. 一支笔的测试点_软件测试面试:如何测试一支笔(铅笔,钢笔,中性笔)
  16. 困难时拉你一把的图片_在你遇到的困难时,总会有人拉你一把、你命中有这样的贵人吗?...
  17. 转载--[数据库] MySQL汉字字段按拼音排序
  18. babylonjs 分部加载模型_基于Babylonjs自制WebGL3D模型编辑器
  19. HDU 6578. Blank (DP)
  20. Vue使用Eslint报“Parsing error: x-invalid-end-tag“错误的解决方案

热门文章

  1. 去IT培训机构学习适合哪些人去学习
  2. 免费网站空间申请地址
  3. 苹果x屏幕多少钱_iphone12系列屏幕维修多少钱 苹果iphone12维修价格表一览[多图]-手机资讯...
  4. 涂鸦智能季报图解:营收5532万美元同比降3% 亏5500万美元
  5. Python中self用法详解
  6. 一文了解市面上的N种物理锁架构
  7. 如何解决Win10操作系统无法访问局域网共享文件夹的问题
  8. linux shell下求文件的交集、并集、差集
  9. 2022-12-03:部门工资最高的员工。以下数据Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。sql语句如何写? 输出结果如下: department emp
  10. 计算机考试屏蔽范围,哪些屏蔽设备可用于计算机机房的电磁屏蔽?