ext3檔案系統反刪除利器ext3grep應用實戰

來源:互聯網
上載者:User

標籤:系統管理員   資料安全   開源軟體   資料恢複   資源回收筒   

一、“rm –rf”帶來的困惑

國外一份非常著名的Linux系統管理員規則中有這麼一條“慎用 rm –rf 命令,除非你知道此命令將帶來什麼後果”,可見,這個命令對系統管理員的重要性。在實際的工作中,由此命令帶來的誤刪除資料案例屢見不鮮,很多系統管理員都遇到過或者犯過這樣的錯誤。由於開發人員對命令的不熟悉,或者粗心大意、疏於管理,執行了此命令,資料在一瞬間就被清空了。Linux不具備類似資源回收筒的功能,這就意味著資料丟失。雖然Linux自身提供了恢複資料的機制,但是這個功能基本沒用,要恢複資料,通過常規手段是無法完成的,此時,只有找專業的資料恢複公司來恢複資料,這樣無疑要付出很大的成本和費用,造成無法估量的的損失。

可見,作為系統管理員,一定要有資料安全意識,資料保護意識,嚴格遵守相關維護規則,將這種失誤帶來的損失降低到最低。幸運的是,Linux下提供了一款開源軟體恢複誤刪的資料,利用這個ext3檔案系統資料恢複工具ext3grep可以恢複誤刪除的資料。

二、 ext3grep的安裝與使用


ext3grep是一個開源的ext3檔案系統反移除工具,在ext3grep出現之前,資料被刪除後,通過常規手段恢複基本是不可能的,雖然debugfs命令可以對ext2檔案系統做一些恢複,但是對ext3檔案系統就無能為力了。ext3是一個日誌型檔案系統,ext3grep正是通過分析ext3檔案系統的日誌資訊來恢複被刪除的檔案和資料的。


作業系統環境:CentOS release 6.4。

ext3grep版本:ext3grep-0.10.1。

ext3grep官方網站:http://code.google.com/p/ext3grep/,可以從這裡下載最新的ext3grep版本。這裡下載的是ext3grep-0.10.1.tar.gz。


所需的系統相關包:

[[email protected] ~]# rpm -qa |grep e2fsprogse2fsprogs-1.41.12-21.el6.x86_64e2fsprogs-libs-1.41.12-21.el6.x86_64e2fsprogs-devel-1.41.12-21.el6.x86_64


系統必須要安裝e2fsprogs-libs,不然後面ext3grep的安裝會出現問題。

下面進入編譯安裝階段,過程如下:

[[email protected] /opt]# tar zxvf ext3grep-0.10.1.tar.gz[[email protected] ext3grep-0.10.1]# ./configure[[email protected] ext3grep-0.10.1]# make[[email protected] ext3grep-0.10.1]# make install[[email protected] ext3grep-0.10.1]# ext3grep  -vRunning ext3grep version 0.10.1

這樣,ext3grep就安裝完成了,預設的ext3grep命令放在/usr/local/bin目錄下。ext3grep的使用非常簡單,這裡不做介紹,可以通過“ext3grep  --help”擷取詳細的使用協助。


三、通過ext3grep恢複誤刪除的檔案與目錄


1、資料恢複準則

當發現某個分區的資料被誤刪除後,要做的第一件事是立刻卸載被誤刪除檔案所在的分區,或者重新以唯讀方式掛載此分區。

這麼做的原因其實很簡單:刪除一個檔案,就是中檔案inode節點中的扇區指標清除,同時,釋放這些資料對應的資料區塊,而真實的檔案還存留在磁碟分割中,但是這些被刪除的檔案不一定會一直存留在磁碟中,當這些釋放的資料區塊被作業系統重新分配時,那些被刪除的資料就會被覆蓋,因此,在資料誤刪除後,馬上卸載檔案所在分區可以降低資料區塊中資料被覆蓋的風險,進而提高成功恢複資料的機率。


2、實戰ext3grep恢複檔案


(1).類比資料誤刪除環境

下面通過一個類比環境,詳細介紹利用ext3grep恢複資料檔案的過程。

[[email protected] /]# mkdir /disk  #建立一個掛載點[[email protected] /]# cd /mydata[[email protected] mydata]# dd if=/dev/zero  of=/mydata/disk1  count=102400  #類比磁碟分#區,建立一個空裝置102400+0 records in102400+0 records out52428800 bytes (52 MB) copied, 1.20597 seconds, 43.5 MB/s[[email protected] mydata]# mkfs.ext3 /mydata/disk1  #將空裝置格式化為ext3格式[[email protected] mydata]# mount -o loop /mydata/disk1  /disk  #掛載裝置到/disk目錄下[[email protected] mydata]# cd /disk/[[email protected] disk]# cp /etc/profile  /disk           #複製檔案到類比磁碟分割[[email protected] disk]# cp /boot/initrd-2.6.18-164.11.1.el5xen.img  /disk[[email protected] disk]# echo "ext3grep  test">ext3grep.txt  [[email protected] disk]# mkdir  /disk/ext3grep    [[email protected] disk]# cp /etc/hosts  /disk/ext3grep[[email protected] disk]# pwd/disk[[email protected] disk]# ls -al總計 2512drwxr-xr-x  4 root root    4096 04-07 16:46 .drwxr-xr-x 31 root root    4096 04-07 16:45 ..drwxr-xr-x  2 root root    4096 04-07 16:46 ext3grep-rw-r--r--  1 root root      14 04-07 16:31 ext3grep.txt-rw-------  1 root root 2535991 04-07 16:30 initrd-2.6.18-164.11.1.el5xen.imgdrwx------  2 root root    4096 04-07 16:33 lost+found-rw-r--r--  1 root root    1029 04-07 16:30 profile[[email protected] disk]# md5sum  profile  #擷取檔案校正碼a6e82d979bb95919082d9aceddf56c39 profile[[email protected] disk]# md5sum initrd-2.6.18-164.11.1.el5xen.img   031226080e00d7f312b1f95454e5a1ff  initrd-2.6.18-164.11.1.el5xen.img[[email protected] disk]# md5sum  ext3grep.txt5afe55495cdb666daad667e1cd797dcb  ext3grep.txt[[email protected] disk]# rm -rf /disk/*  #類比誤刪除資料操作[[email protected] disk]# ls(2).卸載磁碟分割執行以下命令卸載磁碟分割:[[email protected] disk]# cd /opt #切換到/opt目錄下[[email protected] /opt]# umount /disk  #卸載類比磁碟分割(3).查詢恢複資料資訊執行如下命令,查詢需要恢複的資料資訊:[[email protected] /opt]# ext3grep  /mydata/disk1  --ls --inode 2

執行該命令後,ext3grep就開始搜尋可以恢複的資料檔案資訊,輸出所示。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4D/B1/wKioL1RXRUjTOrjBAAHdjBihnwE745.jpg" title="123.png" alt="wKioL1RXRUjTOrjBAAHdjBihnwE745.jpg" />

“ext3grep  /mydata/disk1  --ls --inode 2”主要用於掃描當前檔案系統下所有檔案的資訊,包括存在的和已經刪除的檔案,其中含有D標識的就是已被刪除的檔案,如果不記得被刪除的檔案的名稱,可以通過這種方式來擷取要恢複的檔案的名稱。

通過下面的方式可以擷取檔案要恢複的路徑資訊。

[[email protected] /opt]# ext3grep  /mydata/disk1  --dump-namesRunning ext3grep version 0.10.1Number of groups: 7Minimum / maximum journal block: 447 / 4561Loading journal descriptors... sorting... doneThe oldest inode block that is still in the journal, appears to be from 1270629014 = Wed Apr  7 16:30:14 2010Number of descriptors in journal: 63; min / max sequence numbers: 2 / 10Loading disk1.ext3grep.stage2... doneext3grepext3grep.txtext3grep/hostsinitrd-2.6.18-164.11.1.el5xen.imglost+foundprofile

(4).恢複單個檔案

如果要恢複被刪除的某個檔案,通過下面方式即可。

[[email protected] /opt]# ext3grep  /mydata/disk1  --restore-file  ext3grep.txtRunning ext3grep version 0.10.1Number of groups: 7Minimum / maximum journal block: 447 / 4561Loading journal descriptors... sorting... doneThe oldest inode block that is still in the journal, appears to be from 1270629014 = Wed Apr  7 16:30:14 2010Number of descriptors in journal: 63; min / max sequence numbers: 2 / 10Writing output to directory RESTORED_FILES/Loading disk1.ext3grep.stage2... doneRestoring ext3grep.txt

由上面的輸出可知,被刪除的檔案ext3grep.txt已經成功恢複。那麼恢複的資料放到哪裡了呢?在這段操作中,在/opt目錄下執行ext3grep命令,恢複的資料檔案就存放在/opt/ RESTORED_FILES目錄下,也就是說ext3grep會在執行恢複命令的目前的目錄下自動建立一個RESTORED_FILES目錄,這個目錄專門用於存放恢複的資料。

下面是恢複指定目錄下的某個檔案的操作:

[[email protected] /opt]# ext3grep  /mydata/disk1  --restore-file  ext3grep/hostsRunning ext3grep version 0.10.1Number of groups: 7Minimum / maximum journal block: 447 / 4561Loading journal descriptors... sorting... doneThe oldest inode block that is still in the journal, appears to be from 1270629014 = Wed Apr  7 16:30:14 2010Number of descriptors in journal: 63; min / max sequence numbers: 2 / 10Loading disk1.ext3grep.stage2... doneRestoring ext3grep/hosts

這裡要注意的是,“--restore-file”後面指定的是恢複檔案路徑,這個路徑應該是檔案的相對路徑,這裡的相對路徑指的是相對指定裝置的路徑,比如,裝置/mydata/disk1的掛載點是/disk,而ext3grep.txt檔案就在/disk目錄下,因此直接指定檔案名稱就可以了。如果要恢複/disk/ext3grep/hosts檔案,那麼指定的參數應該是“ext3grep/hosts”,也就是上面代碼中所指定的形式。

通過“--restore-inode”參數,只需指定檔案對應的inode值即可恢複檔案,操作如下,其中inode值為12的是profile檔案:

[[email protected] RESTORED_FILES]# ext3grep  /mydata/disk1   --restore-inode 12Running ext3grep version 0.10.1Number of groups: 7Minimum / maximum journal block: 447 / 4561Loading journal descriptors... sorting... doneThe oldest inode block that is still in the journal, appears to be from 1270629014 = Wed Apr  7 16:30:14 2010Number of descriptors in journal: 63; min / max sequence numbers: 2 / 10Writing output to directory RESTORED_FILES/Restoring inode.12

下面是進入RESTORED_FILES目錄,驗證檔案是否成功恢複:

[[email protected] /opt]# cd RESTORED_FILES[[email protected] RESTORED_FILES]# lsext3grep  ext3grep.txt  inode.12[[email protected] RESTORED_FILES]# md5sum ext3grep.txt 5afe55495cdb666daad667e1cd797dcb  ext3grep.txt[[email protected] RESTORED_FILES]# md5sum inode.12a6e82d979bb95919082d9aceddf56c39  inode.12

根據校正結果可知,這個校正碼與檔案被刪除之前的校正碼完全一致,因此,通過這個方式恢複出來的檔案是完整的。


(5).恢複所有已刪除資料

當需要恢複的檔案較少時,通過前面介紹的指定檔案的方式進行逐個恢複是可行的,但是如果要恢複很多個檔案,如1000個以上,還採取逐個指定的方式,效率是非常低下的,此時就要利用ext3grep命令的“--restore-all”參數了,具體操作如下:

[[email protected] /opt]# ext3grep  /mydata/disk1  --restore-all                Running ext3grep version 0.10.1Number of groups: 7Minimum / maximum journal block: 447 / 4561Loading journal descriptors... sorting... doneThe oldest inode block that is still in the journal, appears to be from 1270629014 = Wed Apr  7 16:30:14 2010Number of descriptors in journal: 63; min / max sequence numbers: 2 / 10Loading disk1.ext3grep.stage2... doneRestoring ext3grep.txtRestoring ext3grep/hostsRestoring initrd-2.6.18-164.11.1.el5xen.imgRestoring profile[[email protected] /opt]# cd RESTORED_FILES/[[email protected] RESTORED_FILES]# ls -al總計 2512drwxr-xr-x  4 root root    4096 04-07 16:46 .drwxr-xr-x 31 root root    4096 04-07 16:45 ..drwxr-xr-x  2 root root    4096 04-07 16:46 ext3grep-rw-r--r--  1 root root      14 04-07 16:31 ext3grep.txt-rw-------  1 root root 2535991 04-07 16:30 initrd-2.6.18-164.11.1.el5xen.imgdrwx------  2 root root    4096 04-07 16:33 lost+found-rw-r--r--  1 root root    1029 04-07 16:30 profile

根據這個輸出可知,“--restore-all”參數將指定存放裝置中可以恢複的檔案都恢複出來並放到了RESTORED_FILES目錄中。“--restore-all”參數對恢複大量資料檔案是非常有用的。


本文出自 “技術成就夢想” 部落格,請務必保留此出處http://ixdba.blog.51cto.com/2895551/1571334

ext3檔案系統反刪除利器ext3grep應用實戰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.