Linux數據恢復工具之extundelete

      網友投稿 862 2025-03-31

      簡介

      在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

      Linux數據恢復工具之extundelete

      [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/

      Linux 任務調度

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:Excel中身份證號怎么查重(Excel身份證號碼查重)
      下一篇:excel表格列字母變數字的教程(excel表列字母變數字怎么辦)
      相關文章
      亚洲国产精品一区二区九九 | 亚洲精品色播一区二区| 色拍自拍亚洲综合图区| 亚洲Av永久无码精品三区在线| 浮力影院亚洲国产第一页| 国产亚洲蜜芽精品久久| 怡红院亚洲红怡院在线观看| 无码天堂va亚洲va在线va| 最新亚洲人成网站在线观看| 亚洲av无码专区青青草原| 亚洲av日韩av永久在线观看| 久久久久亚洲AV无码去区首| 妇女自拍偷自拍亚洲精品| 国产成人精品亚洲一区| 深夜国产福利99亚洲视频| 亚洲成A∨人片天堂网无码| 久久久亚洲精品视频| 亚洲国产一区二区三区| 婷婷亚洲天堂影院| 亚洲男人的天堂在线va拉文| 亚洲五月午夜免费在线视频| 亚洲色欲久久久综合网东京热| 国产精品久久久亚洲| 亚洲视频在线一区| 亚洲国产日韩在线成人蜜芽| 亚洲无吗在线视频| 久久精品国产亚洲av品善| 亚洲av日韩片在线观看| 亚洲人午夜射精精品日韩| 亚洲日产韩国一二三四区| 亚洲av日韩av高潮潮喷无码| 亚洲精品视频观看| 亚洲熟女精品中文字幕| 处破女第一次亚洲18分钟| 国产亚洲欧洲Aⅴ综合一区 | 亚洲V无码一区二区三区四区观看 亚洲αv久久久噜噜噜噜噜 | 久久综合亚洲色HEZYO国产| 久久亚洲国产午夜精品理论片| 久久亚洲AV成人无码电影| 亚洲1区1区3区4区产品乱码芒果 | 亚洲人成人无码.www石榴|