Linux数据恢复工具之extundelete

举报
小麦苗DB宝 发表于 2022/04/06 18:05:10 2022/04/06
【摘要】 简介在Linux系统下,通过命令“rm -rf”可以将任何数据直接从硬盘删除,并且没有任何提示,同时Linux下也没有与Windows下回收站类似的功能,也就意味着,数据在删除后通过常规的手段是无法恢复的,因此使用这个命令要非常慎重。在使用rm命令的时候,比较稳妥的方法是把命令参数放到后面,这样有一个提醒的作用。其实还有一个方法,那就是将要删除的东西通过mv命令移动到系统下的/tmp目录下...

简介

在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_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.

其中,参数(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
└── passwd
 
3 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
└── passwd
 
2 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的时候,恢复 出来的文件名和之前不一样,需要单独进行改名。内容是没问题的。

参考

http://extundelete.sourceforge.net/

https://blog.51cto.com/ixdba/1566856

http://blog.itpub.net/26736162/viewspace-1623938/

http://blog.itpub.net/26736162/viewspace-2655565/

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200