自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Linux上恢復(fù)誤刪除的文件或目錄

系統(tǒng) Linux
Linux不像windows有那么顯眼的回收站,不是簡(jiǎn)單的還原就可以了。linux刪除文件還原可以分為兩種情況,一種是刪除以后在進(jìn)程存在刪除信息,一種是刪除以后進(jìn)程都找不到,只有借助于工具還原,這里分別檢查介紹下。

[[344627]]

Linux不像windows有那么顯眼的回收站,不是簡(jiǎn)單的還原就可以了。linux刪除文件還原可以分為兩種情況,一種是刪除以后在進(jìn)程存在刪除信息,一種是刪除以后進(jìn)程都找不到,只有借助于工具還原,這里分別檢查介紹下。

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

這種一般是有活動(dòng)的進(jìn)程存在持續(xù)標(biāo)準(zhǔn)輸入或輸出,到時(shí)文件被刪除后,進(jìn)程PID還是存在。這也就是有些服務(wù)器刪除一些文件但是磁盤不釋放的原因。比如當(dāng)前舉例說明:通過一個(gè)shell終端對(duì)一個(gè)測(cè)試文件做cat追加操作: 

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

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

  1. [root@21yunwei_backup ~]# cat testdelete.py  
  2. hello  py 
  3. 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查看刪除的文件 是否還在: 

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

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

2. 恢復(fù)。

恢復(fù)命令: 

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

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

  1. [root@21yunwei_backup ~]# cd   /proc/20464/fd 
  2. [root@21yunwei_backup fd]# ll 
  3. total 0 
  4. lrwx------ 1 root root 64 Nov 15 18:12 0 > /dev/pts/1 
  5. l-wx------ 1 root root 64 Nov 15 18:12 1 > /root/testdelete.py (deleted) 
  6. lrwx------ 1 root root 64 Nov 15 18:12 2 > /dev/pts/1 

恢復(fù)操作: 

  1. cp 1 /tmp/testdelete.py 

查看文件: 

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

恢復(fù)完成。

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

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

  1. [root@21yunwei_backup 21yunwei]# tree 
  2. ├── deletetest 
  3. │   └── mail 
  4. │       └── test.py 
  5. ├── lost+found 
  6. └── passwd 
  7. 3 directories, 2 files 
  8. [root@21yunwei_backup 21yunwei]# cat /21yunwei/deletetest/mail/test.py  
  9. hello Dj 
  10. [root@21yunwei_backup 21yunwei]# tail  -2  passwd  
  11. haproxy:x:500:502::/home/haproxy:/bin/bash 
  12. tcpdump:x:72:72::/:/sbin/nologin 

執(zhí)行刪除操作: 

  1. [root@21yunwei_backup 21yunwei]# rm  -rf    ./* 
  2. [root@21yunwei_backup 21yunwei]# ll 
  3. 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被覆蓋基本就告別恢復(fù)了。比如停止所在分區(qū)的服務(wù),卸載目錄所在的設(shè)備,有必要的情況下都可以斷網(wǎng)。

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

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

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

  1. umount /dev/vdb1 

或者 

  1. umount /21yunwei 

如果提示設(shè)備busy,可以用fuser命令強(qiáng)制卸載: 

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

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

  1. wget  http://nchc.dl.sourceforge.net/project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2 
  2. tar jxvf extundelete-0.2.4.tar.bz2 
  3. cd  extundelete-0.2.4 
  4. ./configure  
  5. make 
  6. make  install 

掃描誤刪除的文件: 

  1. [root@21yunwei_backup extundelete-0.2.4]# extundelete  --inode 2 /dev/vdb1 
  2. NOTICE: Extended attributes are not restored. 
  3. Loading filesystem metadata ... 8 groups loaded. 
  4. Group: 0 
  5. Contents of inode 2: 
  6. .省略N行 
  7. File name                                       | Inode number | Deleted status 
  8. .                                                 2 
  9. ..                                                2 
  10. lost+found                                        11             Deleted 
  11. deletetest                                        12             Deleted 
  12. passwd                                            14             Deleted 

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

1.恢復(fù)單一文件passwd 

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-file passwd    
  2. NOTICE: Extended attributes are not restored. 
  3. Loading filesystem metadata ... 8 groups loaded. 
  4. Loading journal descriptors ... 46 descriptors loaded. 
  5. Successfully restored file passwd 

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

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

2.恢復(fù)目錄deletetest 

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-directory  deletetest  
  2. NOTICE: Extended attributes are not restored. 
  3. Loading filesystem metadata ... 8 groups loaded. 
  4. Loading journal descriptors ... 46 descriptors loaded. 
  5. Searching for recoverable inodes in directory deletetest ...  
  6. 5 recoverable inodes found. 
  7. Looking through the directory structure for deleted files ...  
  8. [root@21yunwei_backup /]# cat  RECOVERED_FILES/deletetest/mail/test.py  
  9. hello Dj 

3.恢復(fù)所有 

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-all 
  2. NOTICE: Extended attributes are not restored. 
  3. Loading filesystem metadata ... 8 groups loaded. 
  4. Loading journal descriptors ... 46 descriptors loaded. 
  5. Searching for recoverable inodes in directory / ...  
  6. 5 recoverable inodes found. 
  7. Looking through the directory structure for deleted files ...  
  8. 0 recoverable inodes still lost.  
  9. [root@21yunwei_backup /]# cd RECOVERED_FILES/ 
  10. [root@21yunwei_backup RECOVERED_FILES]# tree 
  11. ├── deletetest 
  12. │   └── mail 
  13. │       └── test.py 
  14. └── passwd 
  15. 2 directories, 2 files 

4.恢復(fù)指定inode 

  1. [root@21yunwei_backup /]# extundelete /dev/vdb1 --restore-inode 14 
  2. NOTICE: Extended attributes are not restored. 
  3. Loading filesystem metadata ... 8 groups loaded. 
  4. Loading journal descriptors ... 46 descriptors loaded. 
  5. [root@21yunwei_backup /]# tail  -5   /RECOVERED_FILES/file.14  
  6. mysql:x:497:500::/home/mysql:/bin/false 
  7. nginx:x:496:501::/home/nginx:/sbin/nologin 
  8. zabbix:x:495:497:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin 
  9. haproxy:x:500:502::/home/haproxy:/bin/bash 
  10. tcpdump:x:72:72::/:/sbin/nologin 

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

更多的extundelete用法請(qǐng)參考extundelete –help選項(xiàng)參數(shù)說明,當(dāng)前恢復(fù)所有的操作完成。 

 

責(zé)任編輯:龐桂玉 來源: 程序媛貓妹
相關(guān)推薦

2009-12-21 16:17:01

2018-04-28 15:28:44

數(shù)據(jù)庫(kù)MySQL誤刪除

2024-08-09 10:06:09

2016-11-17 12:46:45

2018-12-11 11:13:25

Linux系統(tǒng)恢復(fù)

2019-01-02 10:32:56

Linux系統(tǒng)文件運(yùn)維

2014-07-02 15:37:49

PLSQL

2022-03-28 19:53:24

Linux恢復(fù)文件意外刪除文件

2017-04-01 18:30:47

MySQL誤刪除數(shù)據(jù)庫(kù)

2010-03-10 15:33:31

Linux誤刪除

2018-04-27 10:33:56

Linux命令chattr

2010-08-17 11:03:01

DB2恢復(fù)誤刪除表

2011-07-04 09:59:01

AD誤刪除

2011-08-01 14:50:10

日志挖掘數(shù)據(jù)庫(kù)

2010-08-12 14:03:24

DB2恢復(fù)誤刪除表

2013-01-18 09:59:35

SQL Server

2016-11-22 19:37:54

Linux恢復(fù)文件

2020-07-20 10:34:21

WindowsWindows 10操作系統(tǒng)

2015-03-30 11:34:19

LinuxFSlint

2014-12-03 08:53:59

eCryptFS加密文件
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)