- 資訊首頁(yè) > 互聯(lián)網(wǎng) > 經(jīng)驗分享 >
- 如何在Linux系統服務(wù)器上恢復誤刪除的文件或目錄
Linux操作系統不像windows操作系統服務(wù)器有那么顯眼的回收站,不是簡(jiǎn)單的還原就可以了。linux系統里刪除文件還原可以分為兩種情況,一種是刪除以后在進(jìn)程存在刪除信息,一種是刪除以后進(jìn)程都找不到,只有借助于工具還原,這里分別檢查介紹下。
這種一般是有活動(dòng)的進(jìn)程存在持續標準輸入或輸出,到時(shí)文件被刪除后,進(jìn)程PID還是存在。這也就是有些服務(wù)器刪除一些文件但是磁盤(pán)不釋放的原因。比如當前舉例說(shuō)明:通過(guò)一個(gè)shell終端對一個(gè)測試文件做cat追加操作:
[root@21yunwei_backup ~]# echo “hello py” > testdelete.py
[root@21yunwei_backup ~]# cat >> testdelete.py
hello delete
另外一個(gè)終端查看這個(gè)文件可以清楚看到內容:
[root@21yunwei_backup ~]# cat testdelete.py hello pyhello delete
此時(shí),在當前服務(wù)器刪除文件rm -f ./testdelete.py
命令查看這個(gè)目錄,文件已經(jīng)不存在了,那么現在我們將其恢復出來(lái)。
1. lsof查看刪除的文件進(jìn)程是否還存在。
這里用到一個(gè)命令lsof,如沒(méi)有安裝請自行yum或者apt-get。類(lèi)似這種情況,我們可以先lsof查看刪除的文件 是否還在:
[root@21yunwei_backup ~]# lsof | grep deletedmysqld 1512 mysql 5u REG 252,3 0 6312397 /tmp/ibzW3Lot (deleted)cat 20464 root 1w REG 252,3 23 1310722 /root/testdelete.py (deleted)
幸運的是這種情況進(jìn)程還存在 ,那么開(kāi)始進(jìn)行恢復 操作。
2. 恢復。
恢復命令:
cp /proc/pid/fd/1 /指定目錄/文件名
進(jìn)入 進(jìn)程目錄,一般是進(jìn)入/proc/pid/fd/,針對當前情況:
[root@21yunwei_backup ~]# cd /proc/20464/fd[root@21yunwei_backup fd]# lltotal 0lrwx—— 1 root root 64 Nov 15 18:12 0 > /dev/pts/1l-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@21yunwei_backup fd]# cat /tmp/testdelete.pyhello pyhello delete
恢復完成。
創(chuàng )建準備刪除的目錄并echo一個(gè) 帶有內容的文件:
[root@21yunwei_backup 21yunwei]# tree.├── deletetest│ └── mail│ └── test.py├── lost found└── passwd3 directories, 2 files[root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py hello Dj[root@21yunwei_backup 21yunwei]# tail -2 passwd haproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
執行刪除操作:
[root@21yunwei_backup 21yunwei]# rm -rf ./*[root@21yunwei_backup 21yunwei]# lltotal 0
現在開(kāi)始進(jìn)行誤刪除文件的恢復。這種情況一般是沒(méi)有守護進(jìn)程或者后臺進(jìn)程對其持續輸入,所以刪除就刪除了,lsof也看不到。就要借助于工具。這里我們采用的工具是extundelete第三方工具?;謴筒襟E如下:
1.停止對當前分區做任何操作,防止inode被覆蓋。inode被覆蓋基本就告別恢復了。比如停止所在分區的服務(wù),卸載目錄所在的設備,有必要的情況下都可以斷網(wǎng)。2.通過(guò)dd命令對當前分區進(jìn)行備份,防止第三方軟件恢復失敗導致數據丟失。適合數據非常重要的情況,這里測試,就沒(méi)有備份,如備份可以考慮如下方式:
dd if=/path/filename of=/dev/vdc1
1.通過(guò)umount命令,對當前設備分區卸載?;蛘遞user 命令。
umount /dev/vdb1 或者 umount /21yunwei
如果提示設備busy,可以用fuser命令強制卸載:
fuser -m -v -i -k /21yunwei
1.下載第三方工具extundelete安裝,搜索誤刪除的文件進(jìn)行還原。
wget http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2tar jxvf extundelete-0.2.4.tar.bz2cd extundelete-0.2.4./configure makemake install
掃描誤刪除的文件:
[root@21yunwei_backup extundelete-0.2.4]# extundelete --inode 2 /dev/vdb1NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Group: 0Contents of inode 2:..省略N行File name | Inode number | Deleted status. 2.. 2lost found 11 Deleteddeletetest 12 Deletedpasswd 14 Deleted
通過(guò)掃描發(fā)現了我們刪除的文件夾,現在執行恢復操作。
[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 mysql:x:497:500::/home/mysql:/bin/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
[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.Searching for 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
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-allNOTICE: 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@21yunwei_backup /]# cd RECOVERED_FILES/[root@21yunwei_backup RECOVERED_FILES]# tree.├── deletetest│ └── mail│ └── test.py└── passwd2 directories, 2 files
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14NOTICE: 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 mysql:x:497:500::/home/mysql:/bin/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
注意恢復inode的時(shí)候,恢復 出來(lái)的文件名和之前不一樣,需要單獨進(jìn)行改名。內容是沒(méi)問(wèn)題的。
更多的extundelete用法請參考extundelete –help選項參數說(shuō)明,當前恢復所有的操作完成。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站