Linux系統經過extundelete恢復已刪除的數據

咱們都知道windows系統有個回收站,凡是被刪除的文件均可以經過回收站來恢復數據,即使是按住shift鍵永久刪除了,也有不少數據恢復軟件可供使用。那麼Linux下若是數據被刪除了,又該如何恢復呢,Linux系統可沒有回收站,只能經過數據恢復軟件來解決了。
Linux系統有不少開源的數據恢復工具,例如:debugfs、R-Linux、ext3grep、extundelete等,那麼我要給你們介紹的是extundelete,這款工具是用來恢復ext格式(ext三、ext4等)的文件系統被誤刪除的數據。node

1、extundelete恢復原理

在介紹使用extundelete進行恢復數據以前,咱們來簡單瞭解一下inode的知識。在Linux下能夠經過「ls -id」命令來查看某個文件或目錄的inode值,例如查看根目錄的inode值,能夠輸入:c++

[root@localhost ~]# ls -id /
64 /

Linux系統經過rm等命令刪除文件或目錄,僅僅是將文件的inode結點中的扇區指針清零,實際文件或目錄還存儲在磁盤上,這些已被刪除的數據塊在尚未被系統從新分配出去以前,也就是說在尚未被新的數據覆蓋以前,這些數據都尚未真正丟失。
使用extundelete恢復文件時並不依賴特定文件格式,首先extundelete會經過文件系統的inode信息來得到當前文件系統下全部文件的信息,包括存在的和已經刪除的文件,這些信息包括文件名和inode。而後經過inode信息結合日誌去查詢該inode所在的block位置,包括直接塊、間接塊等信息。最後利用dd命令將這些信息備份出來,從而恢復數據文件。windows

2、安裝extundelete軟件

extundelete已經好久沒有更新了,目前最新版本是extundelete-0.2.4。在安裝extundelete以前須要安裝e2fsprogs和e2fsprogs-libs兩個依賴包。ide

[root@localhost tmp]# wget http://zy-res.oss-cn-hangzhou.aliyuncs.com/server/extundelete-0.2.4.tar.bz2
[root@localhost tmp]# yum -y install bzip2 e2fsprogs-devel e2fsprogs gcc-c++ make
[root@localhost tmp]# tar jxvf extundelete-0.2.4.tar.bz2
[root@localhost tmp]# cd extundelete-0.2.4
[root@localhost extundelete-0.2.4]# ./configure
[root@localhost extundelete-0.2.4]# make && make install

成功安裝extundelete後,系統中會生成一個extundelete可執行文件,可經過「extundelete --help」得到此命令的使用方法。工具

3、extundelete命令使用方法

命令格式: extundelete [options] [--] device-file
其中,參數(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,經過指定數據塊大小來打開文件系統,通常用於查看已經知道大小的文件。

4、使用extundelete恢復數據(測試)

(1)模擬刪除數據
建立一個分區,並將該分區格式化爲ext3,將此分區掛載到/data目錄下,拷貝一些目錄和文件到/data下,並刪除一些文件和目錄。
注:通過測試,ext4格式的分區數據恢復失敗,看來extundelete還不支持ext4文件系統。測試

[root@localhost ~]# mkfs.ext3 /dev/sdb1
[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/sdb1 /data
[root@localhost ~]# cp -r /tmp/extundelete-0.2.4 /data/
[root@localhost ~]# cp /etc/redhat-release /data/
[root@localhost ~]# mkdir /data/test
[root@localhost ~]# echo "測試文件" > /data/test/test.txt
[root@localhost ~]# md5sum /data/redhat-release 
137e28b464c4b8e6e4347b36621a38ab  /data/redhat-release
[root@localhost ~]# md5sum /data/test/test.txt 
65be68a792b7ef18ae268b80e15dd55e  /data/test/test.txt
[root@localhost ~]# rm -rf /data/*

(2)數據恢復前的準備工做
必定要記住,數據一旦被誤刪除後,立刻卸載這塊磁盤分區。
[root@localhost ~]# umount /data/
查詢可恢復的數據信息
注:/data是根分區,根分區的inode通常都爲2。debug

extundelete /dev/sdb1 --inode 2
……
File name                                       | Inode number | Deleted status
.                                                 2
..                                                2
lost+found                                        11             Deleted
extundelete-0.2.4                                 524289         Deleted
redhat-release                                    12             Deleted
test                                              393217         Deleted

標註爲Deleted的是已經刪除的文件或目錄,還有對應的inode值。
(3)恢復單個文件指針

[root@localhost /]# extundelete  /dev/sdb1 --restore-file redhat-release
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 80 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
Successfully restored file redhat-release
[root@localhost /]# cd RECOVERED_FILES/
[root@localhost RECOVERED_FILES]# md5sum redhat-release 
137e28b464c4b8e6e4347b36621a38ab  redhat-release

恢復單個文件使用參數「--restore-file」,恢復單個目錄使用參數「--restore-directory」,須要使用文件的相對路徑。/data是根目錄,redhat-release文件的絕對路徑是/data/redhat-release,相對路徑即是redhat-release。
文件恢復成功後,在執行extundelete命令的當前目錄下會建立一個RECOVERED_FILES目錄,用於存放恢復出來的文件。
根據MD5碼來看,redhat-release文件恢復成功。
(4)恢復單個目錄rest

[root@localhost /]# extundelete /dev/sdb1 --restore-directory /test
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 80 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
Searching for recoverable inodes in directory /test ... 
57 recoverable inodes found.
Looking through the directory structure for deleted files ... 
56 recoverable inodes still lost.
[root@localhost /]# cd RECOVERED_FILES/
[root@localhost RECOVERED_FILES]# md5sum test/test.txt 
65be68a792b7ef18ae268b80e15dd55e  test/test.txt
[root@localhost RECOVERED_FILES]# cat test/test.txt 
測試文件

(5)恢復全部數據
使用「--restore-all」參數來恢復全部被刪除的文件和目錄。日誌

[root@localhost /]# extundelete  /dev/sdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 80 groups loaded.
Loading journal descriptors ... 67 descriptors loaded.
Searching for recoverable inodes in directory / ... 
57 recoverable inodes found.
Looking through the directory structure for deleted files ... 
0 recoverable inodes still lost.
[root@localhost /]# cd RECOVERED_FILES/
[root@localhost RECOVERED_FILES]# ls
extundelete-0.2.4  redhat-release  redhat-release.v1  test
[root@localhost RECOVERED_FILES]# du -sh extundelete-0.2.4/
5.2M    extundelete-0.2.4/

經過以上信息看到數據所有都恢復成功了。
(6)按時間段恢復數據
使用「--after」和「--before」參數,能夠指定某個時間段,恢復這個時間段刪除的數據,須要使用總秒數,可經過「date +%s」命令查看。

[root@localhost RECOVERED_FILES]# date +%s
1537513612
extundelete --after 1537513612 --restore-all /dev/sdb1  #恢復某個時間點以後刪除的數據
extundelete --before 1537513612 --restore-all /dev/sdb1  #恢復某個時間點以前刪除的數據
extundelete --after 1537513612 --before 1537513755 --restore-all /dev/sdb1  #恢復一個時間段內刪除的數據

恢復時間段數據的測試內容我就不寫了,你們可自行測試。

相關文章
相關標籤/搜索