天天看點

linux删除檔案_如何在 Linux 上恢複誤删除的檔案或目錄

(給Linux愛好者加星标,提升Linux技能)

轉自:21運維

http://www.21yunwei.com/archives/6030

Linux不像windows有那麼顯眼的資源回收筒,不是簡單的還原就可以了。linux删除檔案還原可以分為兩種情況,一種是删除以後在程序存在删除資訊,一種是删除以後程序都找不到,隻有借助于工具還原,這裡分别檢查介紹下。

一、誤删除檔案程序還在的情況。

這種一般是有活動的程序存在持續标準輸入或輸出,到時檔案被删除後,程序PID還是存在。這也就是有些伺服器删除一些檔案但是磁盤不釋放的原因。比如目前舉例說明:通過一個shell終端對一個測試檔案做cat追加操作:

[[email protected]_backup ~]# echo  "hello  py"> testdelete.py[[email protected]_backup ~]# cat  >>testdelete.py hello delete
           

另外一個終端檢視這個檔案可以清楚看到内容:

[[email protected]_backup ~]# cat testdelete.py hello  pyhello delete
           

此時,在目前伺服器删除檔案

rm -f ./testdelete.py

指令檢視這個目錄,檔案已經不存在了,那麼現在我們将其恢複出來。

1. lsof檢視删除的檔案程序是否還存在。

這裡用到一個指令lsof,如沒有安裝請自行yum或者apt-get。類似這種情況,我們可以先lsof檢視删除的檔案 是否還在:

[[email protected]_backup ~]# lsof | grep deletedmysqld    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/,針對目前情況:

[[email protected]_backup ~]# cd   /proc/20464/fd[[email protected]_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
           

恢複操作:

cp 1/tmp/testdelete.py
           

檢視檔案:

[[email protected]_backup fd]# cat  /tmp/testdelete.pyhello  pyhello delete
           

恢複完成。

二、誤删除的檔案程序已經不存在,借助于工具還原。

建立準備删除的目錄并echo一個 帶有内容的檔案:

[[email protected]_backup 21yunwei]# tree.├── deletetest│   └── mail│       └── test.py├── lost+found└── passwd3 directories, 2 files[[email protected]_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py hello Dj[[email protected]_backup 21yunwei]# tail  -2  passwd haproxy:x:500:502::/home/haproxy:/bin/bashtcpdump:x:72:72::/:/sbin/nologin
           

執行删除操作:

[[email protected]_backup 21yunwei]# rm  -rf    ./*[[email protected]_backup 21yunwei]# lltotal 0
           

現在開始進行誤删除檔案的恢複。這種情況一般是沒有守護程序或者背景程序對其持續輸入,是以删除就删除了,lsof也看不到。就要借助于工具。這裡我們采用的工具是extundelete第三方工具。恢複步驟如下:

1.停止對目前分區做任何操作,防止inode被覆寫。inode被覆寫基本就告别恢複了。比如停止所在分區的服務,解除安裝目錄所在的裝置,有必要的情況下都可以斷網。2.通過dd指令對目前分區進行備份,防止第三方軟體恢複失敗導緻資料丢失。适合資料非常重要的情況,這裡測試,就沒有備份,如備份可以考慮如下方式:

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

1.通過umount指令,對目前裝置分區解除安裝。或者fuser 指令。

umount /dev/vdb1 或者 umount /21yunwei
           

如果提示裝置busy,可以用fuser指令強制解除安裝:

fuser -m -v -i -k /21yunwei
           

1.下載下傳第三方工具extundelete安裝,搜尋誤删除的檔案進行還原。

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
           

掃描誤删除的檔案:

[[email protected]_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
           
通過掃描發現了我們删除的檔案夾,現在執行恢複操作。
           

1.恢複單一檔案passwd

[[email protected]_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。檢視恢複的檔案:

[[email protected]_backup /]# tail  -5  RECOVERED_FILES/passwd mysql: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.恢複目錄deletetest

[[email protected]_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 ... [[email protected]_backup /]# cat  RECOVERED_FILES/deletetest/mail/test.py hello Dj
           

3.恢複所有

[[email protected]_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. [[email protected]_backup /]# cd RECOVERED_FILES/[[email protected]_backup RECOVERED_FILES]# tree.├── deletetest│   └── mail│       └── test.py└── passwd2 directories, 2 files
           

4.恢複指定inode

[[email protected]_backup /]# extundelete /dev/vdb1 --restore-inode 14NOTICE: Extended attributes are not restored.Loading filesystem metadata ... 8 groups loaded.Loading journal descriptors ... 46 descriptors loaded.[[email protected]_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
           
注意恢複inode的時候,恢複 出來的檔案名和之前不一樣,需要單獨進行改名。内容是沒問題的。
           

更多的extundelete用法請參考extundelete –help選項參數說明,目前恢複所有的操作完成。

- EOF -

推薦閱讀   點選标題可跳轉

1、10 大黑客專用的 Linux 作業系統

2、Manjaro 和 Arch Linux 有何不同,哪個更好?

3、Linux 核心的 100% 自由版本 GNU Linux-libre 5.8 釋出

看完本文有收獲?請分享給更多人

關注「Linux 愛好者」加星标,提升Linux技能

linux删除檔案_如何在 Linux 上恢複誤删除的檔案或目錄

好文章,我在看❤️

繼續閱讀