如何在 Linux 上恢復(fù)誤刪除的文件或目錄
點(diǎn)擊關(guān)注上方“SQL數(shù)據(jù)庫開發(fā)”,
設(shè)為“置頂或星標(biāo)”,第一時間送達(dá)干貨
一、誤刪除文件進(jìn)程還在的情況。
這種一般是有活動的進(jìn)程存在持續(xù)標(biāo)準(zhǔn)輸入或輸出,到時文件被刪除后,進(jìn)程PID還是存在。這也就是有些服務(wù)器刪除一些文件但是磁盤不釋放的原因。比如當(dāng)前舉例說明:通過一個shell終端對一個測試文件做cat追加操作:
[root@21yunwei_backup ~]# echo "hello py"> testdelete.py[root@21yunwei_backup ~]# cat >> testdelete.pyhello delete
另外一個終端查看這個文件可以清楚看到內(nèi)容:
[root@21yunwei_backup ~]# cat testdelete.pyhello pyhello delete
此時,在當(dāng)前服務(wù)器刪除文件rm -f ./testdelete.py
命令查看這個目錄,文件已經(jīng)不存在了,那么現(xiàn)在我們將其恢復(fù)出來。
1、lsof查看刪除的文件進(jìn)程是否還存在。
這里用到一個命令lsof,如沒有安裝請自行yum或者apt-get。類似這種情況,我們可以先lsof查看刪除的文件 是否還在:
[root@21yunwei_backup ~]# lsof | grep deletedmysqld 1512 mysql 5u REG 252,306312397/tmp/ibzW3Lot (deleted)cat 20464 root 1w REG 252,3231310722/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/,針對當(dāng)前情況:
[root@21yunwei_backup ~]# cd /proc/20464/fd[root@21yunwei_backup fd]# lltotal 0lrwx------ 1 root root 64Nov1518:120> /dev/pts/1l-wx------ 1 root root 64Nov1518:121> /root/testdelete.py (deleted)lrwx------ 1 root root 64Nov1518:122> /dev/pts/1
恢復(fù)操作:
cp 1/tmp/testdelete.py查看文件:
[root@21yunwei_backup fd]# cat /tmp/testdelete.pyhello pyhello delete
恢復(fù)完成。
二、誤刪除的文件進(jìn)程已經(jīng)不存在,借助于工具還原。
創(chuàng)建準(zhǔn)備刪除的目錄并echo一個 帶有內(nèi)容的文件:
[root@21yunwei_backup 21yunwei]# tree.├── deletetest│ └── test.py├── lost+found└── passwd3 directories, 2 files[root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.pyhello Dj[root@21yunwei_backup 21yunwei]# tail -2 passwdhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
執(zhí)行刪除操作:
[root@21yunwei_backup 21yunwei]# rm -rf ./*[root@21yunwei_backup 21yunwei]# lltotal 0
現(xiàn)在開始進(jìn)行誤刪除文件的恢復(fù)。這種情況一般是沒有守護(hù)進(jìn)程或者后臺進(jìn)程對其持續(xù)輸入,所以刪除就刪除了,lsof也看不到。就要借助于工具。這里我們采用的工具是extundelete第三方工具。恢復(fù)步驟如下:
1.停止對當(dāng)前分區(qū)做任何操作,防止inode被覆蓋。inode被覆蓋基本就告別恢復(fù)了。比如停止所在分區(qū)的服務(wù),卸載目錄所在的設(shè)備,有必要的情況下都可以斷網(wǎng)。2.通過dd命令對當(dāng)前分區(qū)進(jìn)行備份,防止第三方軟件恢復(fù)失敗導(dǎo)致數(shù)據(jù)丟失。適合數(shù)據(jù)非常重要的情況,這里測試,就沒有備份,如備份可以考慮如下方式:
dd if=/path/filename of=/dev/vdc11.通過umount命令,對當(dāng)前設(shè)備分區(qū)卸載。或者fuser 命令。
umount /dev/vdb1 或者 umount /21yunwei如果提示設(shè)備busy,可以用fuser命令強(qiáng)制卸載:
fuser -m -v -i -k /21yunwei1.下載第三方工具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./configuremakemake 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 11Deleteddeletetest 12Deletedpasswd 14Deleted
通過掃描發(fā)現(xiàn)了我們刪除的文件夾,現(xiàn)在執(zhí)行恢復(fù)操作。
1、恢復(fù)單一文件passwd
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwdNOTICE: 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/passwdmysql:x:497:500::/home/mysql:/bin/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
2、恢復(fù)目錄deletetest
[root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-directory deletetestNOTICE: 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.pyhello Dj
3、恢復(fù)所有
[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.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└── passwd2 directories, 2 files
4、恢復(fù)指定inode
[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.14mysql:x:497:500::/home/mysql:/bin/falsenginx:x:496:501::/home/nginx:/sbin/nologinzabbix:x:495:497:ZabbixMonitoringSystem:/var/lib/zabbix:/sbin/nologinhaproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
注意恢復(fù)inode的時候,恢復(fù) 出來的文件名和之前不一樣,需要單獨(dú)進(jìn)行改名。內(nèi)容是沒問題的。
更多的extundelete用法請參考extundelete –help選項參數(shù)說明,當(dāng)前恢復(fù)所有的操作完成。
——End——
后臺回復(fù)關(guān)鍵字:1024,獲取一份精心整理的技術(shù)干貨 后臺回復(fù)關(guān)鍵字:進(jìn)群,帶你進(jìn)入高手如云的交流群。 推薦閱讀
一款SQL自動檢查神器,再也不用擔(dān)心SQL出錯了! SQL 語句中 where 條件后 寫上1=1 是什么意思 國產(chǎn)數(shù)據(jù)庫建模工具,看到界面第一眼,良心了! 快手公司廁所裝坑位計時器,網(wǎng)友:再也不能帶薪拉屎了! 這是一個能學(xué)到技術(shù)的公眾號,歡迎關(guān)注
點(diǎn)擊「閱讀原文」了解SQL訓(xùn)練營
