Linux下rm誤刪恢復 extundelete
誤刪之后要第一時間卸載(umount)該分區,或者以只讀的方式來掛載(mount)該分區,否則覆寫了誰也沒辦法恢復。如果誤刪除的是根分區,最好直接斷電,進入單用戶模式,以只讀的方式掛在分區,然后再進行恢復。
安裝
整體安裝步驟
# 源碼下載
wget https://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
# 解壓
tar -jxvf extundelete-0.2.4.tar.bz2
# 進入目錄并編譯安裝
cd extundelete-0.2.4
./configure
sudo make && make install
如果上面這幾步都完美運行沒有報錯并得到類似以下的輸出那么安裝就順利成功了,下面的報錯總結即可跳過。
cd src
extundelete -v
輸出:
extundelete version 0.2.4
libext2fs version 1.44.1
Processor is little endian.
執行make命令會在src目錄下生成 extundelete 可執行文件,可在此直接執行恢復命令, 執行 make install 會將程序安裝在 /usr/local/bin/ 下。
安裝報錯總結
1. 編譯配置時報錯
如果在編譯配置 ./configure
時報錯:
$ ./configure
Configuring extundelete 0.2.4
configure: error: Can't find ext2fs library
說明確是 ext2fs 庫,直接安裝即可:
sudo apt-get install e2fslibs-dev e2fslibs-dev
配置這一步成功的話正常輸出應為:
$ ./configure
Configuring extundelete 0.2.4
Writing generated files to disk
2. 編譯時報錯
如果在編譯 make
時報錯:
$ make && make install
make -s all-recursive
Making all in src
insertionops.cc: In function ‘std::ostream& operator<<(std::ostream&, const ext2_inode&)’:
insertionops.cc:36:36: error: ‘const struct ext2_inode’ has no member named ‘i_dir_acl’; did you mean ‘i_file_acl’?os << "Directory ACL: " << inode.i_dir_acl << std::endl;^~~~~~~~~i_file_acl
Makefile:437: recipe for target 'extundelete-insertionops.o' failed
make[2]: *** [extundelete-insertionops.o] Error 1
Makefile:268: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
Makefile:208: recipe for target 'all' failed
make: *** [all] Error 2
需要打一個補丁:
wget https://sourceforge.net/p/extundelete/tickets/5/attachment/extundelete-0.2.4-e2fsprogs.patch.txt
patch -p1<extundelete-0.2.4-e2fsprogs.patch.txt
./configure
sudo make && make install
恢復誤刪文件
所有的 extundelete
的命令可以通過 extundelete --help
來查看,這里介紹幾個常用的。
-
查看要恢復的文件的分區并卸載
df -Th sudo umount /media
-
查看可以恢復的數據
sudo extundelete /dev/sda --inode 2
2是分區根目錄的 inode 值。
-
恢復單個目錄
指定要恢復的目錄名,如果是空目錄,則不會恢復。
extundelete /dev/sda --restore-directory tbx/
當執行恢復文件的命令后,會在執行命令的當前的目錄下生成 RECOVERED_FILES 目錄,恢復的文件都會放入此目錄中。如未生成目錄,即為失敗。
-
恢復單個文件
指定要恢復的文件名,如果幾k大小的小文件,有很大幾率恢復失敗。
sudo extundelete /dev/sda --restore-file openssh-7.7p1.tar.gz
-
恢復刪除的全部文件
無需指定文件名或目錄名,恢復全部刪除的數據。
sudo extundelete /dev/sda --restore-all
Ref:
https://sourceforge.net/p/extundelete/tickets/5/
https://os.51cto.com/art/202012/633744.htm