Linux不像windows有那么顯眼的回收站,不是簡單的還原就可以了。linux刪除文件還原可以分為兩種情況:一種是刪除以后在進程存在刪除信息,另一種是刪除以后進程都找不到,只有借助于工具還原。這里分別簡單介紹一下。
一、誤刪除文件進程還在的情況
這種一般是有活動的進程存在持續標準輸入或輸出,到時文件被刪除后,進程PID還是存在。這也就是有些服務器刪除一些文件但是磁盤不釋放的原因。比如,當前舉例說明:通過一個shell終端對一個測試文件做cat追加操作:
[root@21yunwei_backup ~]# echo "hello py"> testdelete.py
[root@21yunwei_backup ~]# cat >> testdelete.py
hello delete
另外一個終端查看這個文件可以清楚看到內容:
[root@21yunwei_backup ~]# cat testdelete.py
hello py
hello delete
此時,在當前服務器刪除文件rm -f ./testdelete.py
命令查看這個目錄,文件已經不存在了,那么現在我們將其恢復出來。
1、lsof查看刪除的文件進程是否還存在
這里用到一個命令lsof,如沒有安裝請自行yum或者apt-get。類似這種情況,我們可以先lsof查看刪除的文件是否還在:
[root@21yunwei_backup ~]# lsof | grep deleted
mysqld 1512 mysql 5u REG 252,306312397/tmp/ibzW3Lot (deleted)
cat 20464 root 1w REG 252,3231310722/root/testdelete.py (deleted)
幸運的是這種情況進程還存在 ,那么開始進行恢復 操作。
2、恢復
恢復命令:
cp /proc/pid/fd/1/指定目錄/文件名
進入進程目錄,一般是進入/proc/pid/fd/,針對當前情況:
[root@21yunwei_backup ~]# cd /proc/20464/fd
[root@21yunwei_backup fd]# ll
total 0
lrwx------ 1 root root 64Nov1518:120> /dev/pts/1
l-wx------ 1 root root 64Nov1518:121> /root/testdelete.py (deleted)
lrwx------ 1 root root 64Nov1518:122> /dev/pts/1
恢復操作:
cp 1/tmp/testdelete.py
查看文件:
[root@21yunwei_backup fd]# cat /tmp/testdelete.py
hello py
hello delete
恢復完成。
二、誤刪除的文件進程已經不存在,借助于工具還原
創建準備刪除的目錄并echo一個 帶有內容的文件:
[root@21yunwei_backup 21yunwei]# tree
.
├── deletetest
│ └── test.py
├── lost+found
└── passwd
3 directories, 2 files
[root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py
hello Dj
[root@21yunwei_backup 21yunwei]# tail -2 passwd
haproxy500:502::/home/haproxy:/bin/bash
tcpdump72:72::/:/sbin/nologin
執行刪除操作:
[root@21yunwei_backup 21yunwei]# rm -rf ./*
[root@21yunwei_backup 21yunwei]# ll
total 0
現在開始進行誤刪除文件的恢復。這種情況一般是沒有守護進程或者后臺進程對其持續輸入,所以刪除就刪除了,lsof也看不到。就要借助于工具。這里我們采用的工具是extundelete第三方工具?;謴筒襟E如下:
●停止對當前分區做任何操作,防止inode被覆蓋。inode被覆蓋基本就告別恢復了。比如停止所在分區的服務,卸載目錄所在的設備,有必要的情況下都可以斷網。2.通過dd命令對當前分區進行備份,防止第三方軟件恢復失敗導致數據丟失。適合數據非常重要的情況,這里測試,就沒有備份,如備份可以考慮如下方式:
dd if=/path/filename of=/dev/vdc1
●通過umount命令,對當前設備分區卸載?;蛘遞user 命令。
umount /dev/vdb1 或者 umount /21yunwei
如果提示設備busy,可以用fuser命令強制卸載:
fuser -m -v -i -k /21yunwei
●下載第三方工具extundelete安裝,搜索誤刪除的文件進行還原。
wget http://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
make
make install
掃描誤刪除的文件:
[root@21yunwei_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 11Deleted
deletetest 12Deleted
passwd 14Deleted
通過掃描發現了我們刪除的文件夾,現在執行恢復操作。
1、恢復單一文件passwd
[root@21yunwei_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@21yunwei_backup /]# tail -5 RECOVERED_FILES/passwd
mysql497:500::/home/mysql:/bin/false
nginx496:501::/home/nginx:/sbin/nologin
zabbix495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologin
haproxy500:502::/home/haproxy:/bin/bash
tcpdump72:72::/:/sbin/nologin
2、恢復目錄deletetest
[root@21yunwei_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.
Searchingfor recoverable inodes in directory deletetest ...
5 recoverable inodes found.
Looking through the directory structure for deleted files ...
[root@21yunwei_backup /]# cat RECOVERED_FILES/deletetest/mail/test.py
hello Dj
3、恢復所有
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 8 groups loaded.
Loading journal descriptors ... 46 descriptors loaded.
Searchingfor recoverable inodes in directory / ...
5 recoverable inodes found.
Looking through the directory structure for deleted files ...
0 recoverable inodes still lost.
[root@21yunwei_backup /]# cd RECOVERED_FILES/
[root@21yunwei_backup RECOVERED_FILES]# tree
.
├── deletetest
│ └── test.py
└── passwd
2 directories, 2 files
4、恢復指定inode
[root@21yunwei_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@21yunwei_backup /]# tail -5/RECOVERED_FILES/file.14
mysql497:500::/home/mysql:/bin/false
nginx496:501::/home/nginx:/sbin/nologin
zabbix495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologin
haproxy500:502::/home/haproxy:/bin/bash
tcpdump72:72::/:/sbin/nologin
注意恢復inode的時候,恢復出來的文件名和之前不一樣,需要單獨進行改名。內容是沒問題的。
更多的extundelete用法請參考extundelete –help選項參數說明,當前恢復所有的操作完成。
來源:www.21yunwei.com/archives/6030版權歸原作者所有,如有侵權,請聯系刪除。
-
Linux
+關注
關注
87文章
11345瀏覽量
210403 -
終端
+關注
關注
1文章
1156瀏覽量
30006
原文標題:如何在Linux上恢復誤刪除的文件或目錄?
文章出處:【微信號:gh_c472c2199c88,微信公眾號:嵌入式微處理器】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
服務器數據恢復—LINUX系統刪除/格式化的數據恢復可行性分析
服務器數據恢復—EXT3文件系統下誤刪除數據的恢復案例
![服務器數據<b class='flag-5'>恢復</b>—EXT3<b class='flag-5'>文件</b>系統下<b class='flag-5'>誤刪除</b>數據的<b class='flag-5'>恢復</b>案例](https://file.elecfans.com/web2/M00/6B/71/poYBAGMr1U6AOot4AAB4laMRdgw508.png)
NetApp數據恢復—NetApp存儲WAFL文件系統下誤刪除的數據恢復案例
NetApp數據恢復—NetApp存儲誤刪除的數據恢復案例
![NetApp數據<b class='flag-5'>恢復</b>—NetApp存儲<b class='flag-5'>誤刪除</b>的數據<b class='flag-5'>恢復</b>案例](https://file.elecfans.com/web2/M00/AE/D5/pYYBAGSdOliAdIbSAAKbpHzE5Cs839.png)
虛擬機數據恢復—KVM虛擬機被誤刪除的數據恢復案例
![虛擬機數據<b class='flag-5'>恢復</b>—KVM虛擬機被<b class='flag-5'>誤刪除</b>的數據<b class='flag-5'>恢復</b>案例](https://file.elecfans.com/web2/M00/AD/F3/poYBAGSadTWACtx5AANUKGY87Ws406.png)
服務器數據恢復—Isilon存儲集群誤刪除的數據恢復案例
![服務器數據<b class='flag-5'>恢復</b>—Isilon存儲集群<b class='flag-5'>誤刪除</b>的數據<b class='flag-5'>恢復</b>案例](https://file.elecfans.com/web2/M00/AD/0F/pYYBAGSMBraAIOmFAAKsmcn50m0325.png)
Oracle數據恢復—Oracle刪除數據不用怕!這些數據恢復方法了解一下
服務器數據恢復—誤刪除KVM虛擬機的數據恢復案例
![服務器數據<b class='flag-5'>恢復</b>—<b class='flag-5'>誤刪除</b>KVM虛擬機的數據<b class='flag-5'>恢復</b>案例](https://file1.elecfans.com/web2/M00/EF/20/wKgZomZv4QyAEsMCAANjsWz4Gu4221.png)
NetApp數據恢復—WAFL文件系統下誤刪除數據的數據恢復案例
Netapp數據恢復—Netapp存儲中卷被誤刪除的數據恢復案例
![Netapp數據<b class='flag-5'>恢復</b>—Netapp存儲中卷被<b class='flag-5'>誤刪除</b>的數據<b class='flag-5'>恢復</b>案例](https://file.elecfans.com/web2/M00/9E/D3/poYBAGQ3mRaAGJ0dAAFv0TEin8E740.png)
評論