linux误删数据,Linux中利用extundelete恢复误删除的数据

利用extundelete工具恢复磁盘误删除的数据

原理:

简单介绍下关于inode的知识。在Linux下可以通过“ls -id”命令来查看某个文件或者目录的inode值,例如查看根目录的inode值,可以输入:

[[email protected] Server-100 shell]# ls -id /

2 /

在利用extundelete恢复文件时并不依赖特定文件格式,首先extundelete会通过文件系统的inode信息(根目录的inode一般为2)

来获得当前文件系统下所有文件的信息,包括存在的和已经删除的文件,这些信息包括文件名和inode。

然后利用inode信息结合日志去查询该inode所在的block位置,包括直接块、间接块等信息。

最后利用dd命令将这些信息备份出来,从而恢复数据文件。

安装:

官网下载地址:https://sourceforge.net/projects/extundelete/files/latest/download?source=top3_dlp_t5

[[email protected] Server-100 src]# yum -y install e2fsprogs-libs e2fsprogs e2fsprogs-devel

[[email protected] Server-100 src]# rpm -q e2fsprogs-libs e2fsprogs e2fsprogs-devel

[[email protected] Server-100 src]# tar jxvf extundelete-0.2.4.tar.bz2

[[email protected] Server-100 src]# cd extundelete-0.2.4

[[email protected] Server-100 src]#extundelete-0.2.4]# ./configure && make && make install

安装完成之后生成一个可执行文件

使用:

[[email protected]Virtual Server-100 src]# extundelete --help

其中,参数(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,通过指定数据块大小来打开文件系统,一般用于查看已经知道大小的文件。

在数据删除之后,首先要卸载被删除数据所在的磁盘或是分区,如果是系统根分区遭到误删除,

就需要进入单用户模式下,将根分区以只读的方式挂载。

原因:因为文件删除之后,仅仅是将文件的inode节点中的扇区指针清零,实际上文件还存在磁盘上面

如果磁盘以读写方式挂载,这些删除的数据块可能会被系统从新分配出去,这些数据块被覆盖之后,这些

数据就真的丢失了,所以以只读的方式挂载,尽可能避免数据被覆盖。

实验:

下面的实验我是将磁盘格式化为ext4文件系统,当然在ext3文件系统下面也是同样的方法进行恢复

首先挂载一个新的分区

[[email protected] Server-100 src]# mkfs.ext4 /dev/sdb1

[[email protected] Server-100 src]# pwd

/usr/local/src/

[[email protected] Server-100 src]# mkdir test

[[email protected] Server-100 src]# mount /dev/sdb1 test/

[[email protected] Server-100 src]# cp /etc/passwd test/

[[email protected] Server-100 src]# cp -r shell/ test/

[[email protected] Server-100 src]# mkdir test/yhl

[[email protected] Server-100 src]# echo "Welcome to test" > test/yhl/1.txt

[[email protected] Server-100 src]# cd test/

[[email protected] Server-100 test]# md5sum passwd

b182c9886c816aa0b4fc77ca6585d42e passwd

[[email protected] Server-100 test]# md5sum yhl/1.txt

eb39646285ff90dd31f24bd9f0a34257 yhl/1.txt

[[email protected] Server-100 test]# ls shell/ yhl/

shell/:

6.sh check_system.sh hanyi.sh if2.sh if4.sh new.sh.bak p_s1.sh root.sh yanse.sh yuhulin.sh yunsuan.sh

case.sh chengji.sh if1.sh if3.sh new.sh ppp python.sh test youxi.sh yunsuan-1.sh

yhl/:

1.txt

[[email protected] Server-100 test]# rm -rf *

恢复:

1,卸载删除文件的分区

[[email protected] Server-100 test]# umount /usr/local/src/test/

umount: /usr/local/src/test: device is busy.

(In some cases useful info about processes that use

the device is found by lsof(8) or fuser(1))

提示设备忙,用下面方法卸载就可以卸载

[[email protected] Server-100 test]# fuser -m -v -i -k /usr/local/src/test/

[[email protected] Server-100 ~]# umount /usr/local/src/test/

查看能恢复的数据

[[email protected] Server-100 ~]# extundelete /dev/sdb1 --inode 2 (因为根分区的inode值是2)

File name | Inode number | Deleted status

. 2

.. 2

shell 130305 Deleted

passwd 12 Deleted

yhl 1042433 Deleted

首先测试恢复单个文件

[[email protected] Server-100 ~]# extundelete /dev/sdb1 --restore-file passwd //restore-file表示恢复文件

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 153 groups loaded.

Loading journal descriptors ... 79 descriptors loaded.

Successfully restored file passwd

[[email protected] Server-100 ~]# cd RECOVERED_FILES/ //恢复成功之后默认会创建这个文件,恢复的文件在这个目录下面

[[email protected] Server-100 RECOVERED_FILES]# ls

passwd

[[email protected] Server-100 RECOVERED_FILES]# md5sum passwd //进行MD5校验,和删除之前对比是一样的,说明恢复成功

b182c9886c816aa0b4fc77ca6585d42e passwd

测试恢复目录

[[email protected] Server-100 RECOVERED_FILES]# extundelete /dev/sdb1 --restore-directory /shell

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 153 groups loaded.

Loading journal descriptors ... 79 descriptors loaded.

Searching for recoverable inodes in directory /shell ...

26 recoverable inodes found.

Looking through the directory structure for deleted files ...

5 recoverable inodes still lost.

[[email protected] Server-100 RECOVERED_FILES]# ls

passwd RECOVERED_FILES

[[email protected] Server-100 RECOVERED_FILES]# cd RECOVERED_FILES/

[[email protected] Server-100 RECOVERED_FILES]# ls

shell

可以看见这个目录,但是有一个问题是恢复回来的文件权限和之前的发生了变化,需要你重新修改权限(我测试的时候是这样的)

恢复所有数据

[[email protected] Server-100 shell]# extundelete /dev/sdb1 --restore-all

NOTICE: Extended attributes are not restored.

Loading filesystem metadata ... 153 groups loaded.

Loading journal descriptors ... 79 descriptors loaded.

Searching for recoverable inodes in directory / ...

26 recoverable inodes found.

Looking through the directory structure for deleted files ...

1 recoverable inodes still lost.

[[email protected] Server-100 shell]# cd RECOVERED_FILES/

[[email protected] Server-100 RECOVERED_FILES]# ls

passwd shell yhl

[[email protected] Server-100 yhl]# ls

1.txt

[[email protected] Server-100 yhl]# cat 1.txt

Welcome to test

[[email protected] Server-100 yhl]# md5sum 1.txt

eb39646285ff90dd31f24bd9f0a34257 1.txt

[[email protected] Server-100 shell]# du -sh RECOVERED_FILES/*

4.0K RECOVERED_FILES/passwd

96K RECOVERED_FILES/shell

8.0K RECOVERED_FILES/yhl

可以看出,数据恢复成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值