由於自己手殘,刪除了自己寫了好久的一個項目main.c文件。當時我那個難受啊,但是後來自己百度,查看博客,終於找到了一個好的辦法恢復因自己手殘而刪除的重要文件。
-
ubuntu恢復已刪除的文件軟件
extundelete
-
安裝方法
sudo apt-get install extundelete
-
使用方法
我們可以使用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 ./RESTORED_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 '/' (it
must be one of the paths returned by --dump-names).
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.
--output-dir 'path' Restore files in the output dir 'path'.
By default the restored files are created under current directory 'RECOVERED_FILES'.
--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.
我們知道當我們不小心刪除了有用的文件,我們一般是比較容易知道刪除的時間的,因此,使用時間這個option可以很快並且精確的恢復出我們想要的文件。那這個dtime怎麼生成。請參考如下命令:
date //查看一當前時間
date -d "2020-5-4 17:55:33" +%s //生成dtime的索引
-
恢復命令
首先需要生成dtime 的索引
date //查看一當前時間 date -d "2020-5-4 17:55:33" +%s //生成dtime的索引
sudo extundelete /dev/sda1 --after 1588586319--restore-all
注:/dev/sda1中sda的選擇可能會有不同,自己可以一個一個試,我的sda1是可以的。
恢復完之後,系統會在你所在的當前目錄下生成一個RECOVERED_FILES 的文件,那個文件裏面會存放你所刪除的文件及其他們所在的路徑。
然後我們只需要將我們所需要的文件或文件夾拷貝出來就ok了。
-
extundelete原理
由於 在linux系統中,超級塊描述了分區的信息,一個分區被分爲兩個部分,索引節點表和數據塊區,這個在格式化的時候就定下來了。文件(目錄也是文件的一種,只不過它的內容是描述目錄下的文件的)由索引節點描述,索引節點描述了文件的修改時間,文件的名稱,文件的數據塊地址等等。並且,linux對於文件刪除操作是個懶動作,刪除文件時系統只是將文件對應的索引節點及其擁有的數據塊置爲free(將nlink=0),而並沒有做其他清空的,只有當這個索引節點或者數據塊被真正用到的時候纔會修改裏面的數據。這就爲我們文件修復提供了可趁之機。由於系統中的索引節點是固定大小的,因此可以很輕鬆的遍歷掃描系統中所有的索引節點,找出free的索引節點並檢查其數據塊是否已經被用,如果沒有則可修復並修復。同時,由於索引節點裏的時間等信息也是保留的,因此就可以根據時間來恢復特定的被刪除的文件。
文件誤刪除後的注意事項
從上面的分析可知,誤刪文件後,儘量不要做大的數據操作,以避免被刪除的文件的數據塊被重新使用,導致數據完全丟失。