Linux怎么恢復(fù)誤刪內(nèi)容-創(chuàng)新互聯(lián)

本篇內(nèi)容介紹了“Linux怎么恢復(fù)誤刪內(nèi)容”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

目前創(chuàng)新互聯(lián)已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、巴南網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

誤刪除文件進(jìn)程還在的情況

這種一般是有活動(dòng)的進(jìn)程存在持續(xù)標(biāo)準(zhǔn)輸入或輸出,到時(shí)文件被刪除后,進(jìn)程PID還是存在。這也就是有些服務(wù)器刪除一些文件但是磁盤不釋放的原因。比如當(dāng)前舉例說明:

通過一個(gè) shell終端對(duì)一個(gè)測(cè)試文件做cat追加操作:

[root@21yunwei_backup ~]# echo  "hello  py" > testdelete.py
[root@21yunwei_backup ~]# cat  >> testdelete.py 
hello delete

另外一個(gè)終端查看這個(gè)文件可以清楚看到內(nèi)容:

[root@21yunwei_backup ~]# cat testdelete.py 
hello  py
hello delete

此時(shí),在當(dāng)前服務(wù)器刪除文件rm -f ./testdelete.py

命令查看這個(gè)目錄,文件已經(jīng)不存在了,那么現(xiàn)在我們將其恢復(fù)出來。

1,lsof查看刪除的文件進(jìn)程是否還存在。這里用到一個(gè) 命令lsof,如沒有安裝請(qǐng)自行yum或者apt-get。類似這種情況,我們可以先lsof查看刪除的文件 是否還在:

[root@21yunwei_backup ~]# lsof | grep deleted
mysqld     1512   mysql    5u      REG              252,3          0    6312397 /tmp/ibzW3Lot (deleted)
cat       20464    root    1w      REG              252,3         23    1310722 /root/testdelete.py (deleted)

幸運(yùn)的是這種情況進(jìn)程還存在 ,那么開始進(jìn)行恢復(fù) 操作。

2,恢復(fù)。

恢復(fù)命令:

cp /proc/pid/fd/1  /指定目錄/文件名

進(jìn)入 進(jìn)程目錄,一般是進(jìn)入/proc/pid/fd/,針對(duì)當(dāng)前情況:

[root@21yunwei_backup ~]# cd   /proc/20464/fd
[root@21yunwei_backup fd]# ll
total 0
lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1
l-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

恢復(fù)操作:

cp 1 /tmp/testdelete.py

查看文件:

[root@21yunwei_backup fd]# cat  /tmp/testdelete.py
hello  py
hello delete

恢復(fù)完成。

誤刪除的文件進(jìn)程已經(jīng)不存在,借助于工具還原

創(chuàng)建準(zhǔn)備刪除的目錄并echo一個(gè) 帶有內(nèi)容的文件:

[root@21yunwei_backup 21yunwei]# tree
.
├── deletetest
│   └── mail
│       └── 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 
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

執(zhí)行刪除操作:

[root@21yunwei_backup 21yunwei]# rm  -rf    ./*
[root@21yunwei_backup 21yunwei]# ll
total 0

現(xiàn)在開始進(jìn)行誤刪除文件的恢復(fù)。這種情況一般是沒有守護(hù)進(jìn)行或者后臺(tái)進(jìn)程對(duì)其持續(xù)輸入,所以刪除就刪除 了,lsof也看不到。就要借助于工具。這里我們采用的工具是extundelete第三方工具。恢復(fù)步驟如下:

1,停止對(duì)當(dāng)前分區(qū)做任何操作,防止inode被覆蓋。inode被覆蓋基本就告別自行車了。比如停止所在分區(qū)的服務(wù),卸載目錄所在的設(shè)備,有必要的情況下都可以斷網(wǎng)。

2,通過dd命令對(duì) 當(dāng)前分區(qū)進(jìn)行備份,防止第三方軟件恢復(fù)失敗導(dǎo)致數(shù)據(jù)丟失。適合數(shù)據(jù)非常重要的情況,這里測(cè)試,就沒有備份,如備份可以考慮如下方式:

dd if=/path/filename of=/dev/vdc1

3,通過umount命令,對(duì)當(dāng)前設(shè)備分區(qū)卸載?;蛘遞user 命令。

umount /dev/vdb1 或者 umount /21yunwei

如果提示設(shè)備busy,可以用fuser命令強(qiáng)制卸載:fuser -m -v -i -k /21yunwei

4,下載第三方工具extundelete安裝,搜索誤刪除的文件進(jìn)行還原。

wget  /tupian/20230522/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                                        11             Deleted
deletetest                                        12             Deleted
passwd                                            14             Deleted

通過掃描發(fā)現(xiàn)了我們刪除的文件夾,現(xiàn)在執(zhí)行恢復(fù)操作。

(1)恢復(fù)單一文件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

恢復(fù)文件是放到了當(dāng)前目錄RECOVERED_FILES。

查看恢復(fù)的文件:

[root@21yunwei_backup /]# tail  -5  RECOVERED_FILES/passwd 
mysql:x:497:500::/home/mysql:/bin/false
nginx:x:496:501::/home/nginx:/sbin/nologin
zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

(2)恢復(fù)目錄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.
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

(3)恢復(fù)所有

[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.
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
└── passwd
 
2 directories, 2 files

(4),恢復(fù)指定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 
mysql:x:497:500::/home/mysql:/bin/false
nginx:x:496:501::/home/nginx:/sbin/nologin
zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
haproxy:x:500:502::/home/haproxy:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin

注意恢復(fù)inode的時(shí)候,恢復(fù) 出來的文件名和之前不一樣,需要單獨(dú)進(jìn)行改名。內(nèi)容是沒問題的。

“Linux怎么恢復(fù)誤刪內(nèi)容”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章名稱:Linux怎么恢復(fù)誤刪內(nèi)容-創(chuàng)新互聯(lián)
瀏覽地址:http://muchs.cn/article42/ddschc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站營銷、品牌網(wǎng)站設(shè)計(jì)、域名注冊(cè)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)