Linux中誤刪文件的恢復方法

Linux中誤刪文件的恢復方法

做爲Linux的從業者大家對"rm -rf"這個命令肯定又愛又恨。

他的簡潔霸氣,勢不可擋,可謂"十步殺一人,千里不留行",

他乾淨徹底,不留痕跡,狠狠按下回車鍵後,“事了拂衣去,深藏功與名”。

但是,這個世界總是遺憾常有,而後悔藥不常有,刪錯後多半會呆呆地盯着屏幕,莫名奇妙地會"哀吾生之須臾,羨長江之無窮"。

今天小編就給大家介紹的就是一款好用的"後悔藥"——extundelete

準備工作

下載安裝包:http://extundelete.sourceforge.net/

環境準備:

[root@lh 1]# yum -y install e2fsprogs-libs e2fsprogs e2fsprogs-devel
[root@lh 1]# rpm -q e2fsprogs-libs e2fsprogs e2fsprogs-devel

安裝extundelete:

[root@lh 1]# tar jxvf extundelete-0.2.4.tar.bz2
[root@lh 1]# cd extundelete-0.2.4
[root@lh extundelete-0.2.4]# ./configure && make && make install 

製造車禍現場

注:如果您已經誤刪了文件請跳過此章

格式化一個分區

[root@lh 1]# mkfs.ext4 /dev/sdc1

掛載並製造一些文件

[root@lh 1]# mkdir /diskc
[root@lh 1]# mount /dev/sdc1 /diskc
[root@lh 1]# cd /diskc
[root@lh diskc]# cp /etc/passwd ./
[root@lh diskc]# cp /bin/ls ./
[root@lh diskc]# mkdir mlryj
[root@lh diskc]# echo "Welcome to 麻辣軟硬件" > mlryj/1.txt

查看製造文件的md5值

[root@lh diskc]# md5sum passwd ls mlryj/1.txt
4b2c3e7fb5baf6f98307068d9c337620  passwd
c75a745aeabe3449655f2e7d3b081791  ls
dd69abb0cab322377ac06f6648f9ec8a  mlryj/1.txt

查看文件詳情

[root@lh diskc]# ll
總用量 140
drwx------ 2 root root  16384 3月  14 17:40 lost+found
-rwxr-xr-x 1 root root 117024 3月  14 17:43 ls
drwxr-xr-x 2 root root   4096 3月  14 17:43 mlryj
-rw-r--r-- 1 root root   1935 3月  14 17:43 passwd

刪除所有文件

[root@lh diskc]# rm -rf *

##找回被刪除的文件
###取消掛載

[root@lh 1]# umount /diskc

###可用的命令

此時使用如下三個命令:

1.查看可以恢復哪些文件:

extundelete /dev/sdc1 --inode 2

2.恢復某文件:

extundelete /dev/sdc1 --restore-file somefile

3.恢復某目錄:

extundelete /dev/sdc1 --restore-directory /somedir

4.恢復所有文件:

extundelete /dev/sdc1 --restore-all

命令執行後恢復後的文件存儲在當前文件夾下的RECOVERED_FILES目錄中
###文件恢復及驗證

恢復單個文件和目錄就不再贅述了,我們直接恢復所有文件

[root@lh 1]# extundelete /dev/sdc1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 64 groups loaded.
Loading journal descriptors ... 55 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@lh 1]# ls
RECOVERED_FILES

查看文件是否成功找回

[root@lh 1]# cd RECOVERED_FILES/
[root@lh RECOVERED_FILES]# ls -l
總用量 124
-rw-r--r-- 1 root root 117024 3月  14 17:49 ls
drwxr-xr-x 2 root root   4096 3月  14 17:49 mlryj
-rw-r--r-- 1 root root   1935 3月  14 17:49 passwd
[root@lh RECOVERED_FILES]# md5sum passwd ls mlryj/1.txt
4b2c3e7fb5baf6f98307068d9c337620  passwd
c75a745aeabe3449655f2e7d3b081791  ls
dd69abb0cab322377ac06f6648f9ec8a  mlryj/1.txt
[root@lh RECOVERED_FILES]# cat mlryj/1.txt 
Welcome to 麻辣軟硬件

我們可以看到,文件的md5值和內容都沒有變化,恢復很成功了。
但是,雖然有此利器,我們使用"rm -rf"時,腦袋還是要謹慎一些,不要冒無謂的風險。至少從現在看:
只是目錄的"總用量"和恢復的文件權限發生了變化。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章