小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Linux EXT文件系統(tǒng)恢復(fù)誤刪文件的方法

 ldjsld 2015-11-20
我們?cè)诠芾頂?shù)據(jù)庫和系統(tǒng)的時(shí)候,經(jīng)常需要做rm 刪除文件的操作。由于Linux是沒有回收站的,rm刪除了文件或者目錄以后,數(shù)據(jù)是無法從Windows所謂的回收站中找到并恢復(fù)的。這樣的話,數(shù)據(jù)被誤刪除了以后,想要恢復(fù)我們一般需要從備份中,或者找數(shù)據(jù)恢復(fù)公司來恢復(fù)數(shù)據(jù)。但是,在某些比較特殊的情況下,使用了以下方法,我們還是可以找回部分?jǐn)?shù)據(jù)的。
這里我們主要介紹兩種數(shù)據(jù)恢復(fù)的方法。第一種是針對(duì)文件在文件系統(tǒng)中已經(jīng)被刪除了,但是,打開這個(gè)文件的進(jìn)程還存在。第二種針對(duì)文件在文件系統(tǒng)中已經(jīng)被刪除了,目前也沒有任何進(jìn)程打開著這個(gè)文件,但是文件在刪除以后沒有其他對(duì)文件系統(tǒng)的變更操作。
 
1.           從/proc文件系統(tǒng)恢復(fù)數(shù)據(jù)
在Linux系統(tǒng)中,文件被刪除了,只要打開文件的進(jìn)程沒有被關(guān)閉,那么恭喜你,這個(gè)文件重新恢復(fù)出來的可能性非常大。因?yàn)長inux操作系統(tǒng)在刪除文件時(shí),會(huì)判斷打開這個(gè)文件的所有進(jìn)程是否都已經(jīng)關(guān)閉,如果還有一個(gè)進(jìn)程沒有關(guān)閉,那么這個(gè)文件的空間將不會(huì)釋放。只有所有打開這個(gè)文件的進(jìn)程都關(guān)閉以后,這個(gè)文件的空間才會(huì)釋放。這也是為什么在Linux下有時(shí)候我們刪除文件,文件的空間無法釋放掉的原因。
這種情況下,我們可以嘗試從/proc文件系統(tǒng)中將文件恢復(fù)出來。
/proc 文件系統(tǒng)是一種內(nèi)核和內(nèi)核模塊用來向進(jìn)程 (process) 發(fā)送信息的機(jī)制 (所以叫做 /proc)。通過這個(gè)偽文件系統(tǒng)讓你可以和內(nèi)核內(nèi)部數(shù)據(jù)結(jié)構(gòu)進(jìn)行交互。你可以獲取對(duì)應(yīng)進(jìn)程的有用信息,在運(yùn)行中 (on the fly) 通過改變內(nèi)核參數(shù)修改部分設(shè)置。它與其他文件系統(tǒng)不同,/proc 是存在于內(nèi)存之中而不是硬盤上。
接下來我們模擬一下數(shù)據(jù)誤刪除的過程,來看看在進(jìn)程沒有關(guān)閉的情況下,怎么從/proc中恢復(fù)數(shù)據(jù)。
首先,我們有一個(gè)echo_red.sh的文件,我們?cè)跁?huì)話session 1查看一下這個(gè)文件的內(nèi)容。
此時(shí),在另外一個(gè)會(huì)話session 2中有一個(gè)進(jìn)程在修改這個(gè)文件:
然后這個(gè)文件在會(huì)話session 1中被我們“誤刪除”掉了:
 
Session 1 Session 2
[root@test1 /home/woqu]
#ll
總用量 4
-rw-r--r-- 1 root 93 10月 16 17:49 echo_red.sh
 
[root@test1 /home/woqu]
#cat echo_red.sh
echo_red()
{
    # echo a message with red color
    echo -e "\e[1;31m$@\e[m"
    return 0
}
 
 
  [root@test1 /home/woqu]
#cat >echo_red.sh
echo_red()
{
    # echo a message with red color
    echo -e "\e[1;31m$@\e[m"
    return 0
}
 
[root@test1 /home/woqu]
#rm -f echo_red.sh
 
[root@test1 /home/woqu]
#ll
總用量 0
 
 
 
此時(shí),我們發(fā)現(xiàn)文件被“誤刪除”了,需要恢復(fù)數(shù)據(jù),那么我們需要怎么做列?
l   磁盤備份
發(fā)現(xiàn)誤刪除以后,我們需要立刻停止對(duì)該分區(qū)的寫操作。
在恢復(fù)之前,如果可能的話,建議通過dd命令將磁盤整個(gè)備份起來,以避免操作的時(shí)候損壞了磁盤上相關(guān)數(shù)據(jù)。
 
l   確定進(jìn)程號(hào)和文件句柄號(hào)
首先,我們需要確定打開這個(gè)文件的進(jìn)程號(hào),以及進(jìn)程打開這個(gè)文件的文件號(hào)。最直接的辦法就是lsof |grep -i delete:
[root@test1 /home/woqu]
#lsof |grep -i delete
cat       11791  root    1w      REG              253,0       94    1048589 /home/woqu/echo_red.sh (deleted)
這里一共有9列,各列列名如下:
COMMAND     PID  USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
也就是說,打開這個(gè)文件的進(jìn)程是11791,而/home/woqu/echo_red.sh對(duì)應(yīng)該進(jìn)程的文件句柄是1w。也就是說文件句柄號(hào)是1。
l   恢復(fù)誤刪除文件
然后,我們就可以直接將這個(gè)文件的內(nèi)容拷貝出來:
 [root@test1 /root]
#cp /proc/11791/fd/1 echo_red.sh
 
[root@test1 /root]
#cat echo_red.sh
echo_red()
{
    # echo a message with red color
    echo -e "\e[1;31m$@\e[m"
    return 0
}
如上所示,數(shù)據(jù)文件恢復(fù)出來了,內(nèi)容也是一模一樣的。
 
2.           Extundelete工具恢復(fù)
對(duì)于使用ext3,ext4文件系統(tǒng)的Linux系統(tǒng)有一個(gè)比較好的工具可以用于數(shù)據(jù)恢復(fù),那就是extundelete。當(dāng)然其他的文件系統(tǒng)當(dāng)然也有類似的恢復(fù)工具。
由于大部分Linux發(fā)行版都是以ext3,ext4作為默認(rèn)文件系統(tǒng)的,我們這里以extundelete為例演示數(shù)據(jù)刪除以后恢復(fù)的相關(guān)步驟。
老規(guī)矩,首先我們需要制造一個(gè)“誤刪除”的現(xiàn)場。
現(xiàn)在我們的/home/mysql下有多個(gè)目錄,其中一個(gè)目錄為script:
[root@test1 /home/mysql]
#ll
total 28
drwxr-xr-x 2 mysql 4096 Jul 21 14:42 bin
drwxr-xr-x 2 mysql 4096 Oct 12 17:52 conf
drwxr-xr-x 3 mysql 4096 Sep 26 14:57 data
drwxr-xr-x 4 mysql 4096 Oct 16 15:24 program
drwxr-xr-x 2 root  4096 Oct 16 18:16 script
drwxr-xr-x 4 mysql 4096 Oct 16 15:25 source
drwxr-xr-x 7 mysql 4096 May 31 11:27 thirdparty
這個(gè)script目錄下有一些文件,如下:
[root@test1 /home/mysql]
#tree script/
script/
├── get_mysql_fdflag.sh
├── mysqlreport.sh
└── test_o_direct.c
由于某種原因,/home/mysql/script被誤刪除了。
[root@test1 /home/mysql]
#rm -fr script/
 
l   磁盤備份
發(fā)現(xiàn)誤刪除以后,我們需要立刻停止對(duì)該分區(qū)的寫操作,避免inode被重用。
接下來就需要用extundelete工具對(duì)它進(jìn)行恢復(fù)。在恢復(fù)之前如果可能的話,建議通過dd命令將磁盤整個(gè)備份起來,以避免操作的時(shí)候損壞了磁盤上相關(guān)數(shù)據(jù)。萬一extundelete或者類似的工具無法恢復(fù)數(shù)據(jù),這些數(shù)據(jù)交給專業(yè)的硬盤恢復(fù)公司也更容易找回?cái)?shù)據(jù)一些。
 
l   umount分區(qū)
做完了備份,我們首先做的第一步,需要將誤刪除數(shù)據(jù)的磁盤分區(qū)首先umount掉,這也是避免該分區(qū)的數(shù)據(jù)被損壞的一個(gè)步驟。在我們的模擬環(huán)境,我們需要:
[root@test1 /root]
#umount /home/
 
l   安裝extundelete
如果你機(jī)器上并沒有安裝extundelete的話,首先,你需要把這個(gè)工具安裝好。目前最新的extundelete版本是0.2.4,安裝方法如下:
yum -y install e2fsprogs*
wget  http://nchc.dl./project/extundelete/extundelete/0.2.4/extundelete-0.2.4.tar.bz2
tar xjf extundelete-0.2.4.tar.bz2
cd extundelete-0.2.4/
./configure
make
make install
 
l   查找誤刪除文件
通過extundelete可以查看哪些文件被刪除了。在我們的模擬場景下,可以這樣使用extundelete --inode 2 /dev/VolGroup/home查看/home分區(qū)下各個(gè)文件和目錄的詳細(xì)信息。這里/dev/VolGroup/home指的是/home對(duì)應(yīng)的分區(qū)。對(duì)于ext系列的文件系統(tǒng),編號(hào)為2的inode中包含了該分區(qū)下的各個(gè)文件和目錄信息。輸出信息如下:
[root@test1 /root]
#extundelete --inode 2 /dev/VolGroup/home
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 400 groups loaded.
Group: 0
Contents of inode 2:
0000 | ed 41 00 00 00 10 00 00 87 99 5e 52 87 99 5e 52 | .A........^R..^R
0010 | 87 99 5e 52 00 00 00 00 00 00 05 00 08 00 00 00 | ..^R............
0020 | 00 00 00 00 05 00 00 00 21 24 00 00 00 00 00 00 | ........!$......
0030 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0040 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0050 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0060 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0070 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0080 | 1c 00 00 00 74 63 29 04 74 63 29 04 b8 23 27 8a | ....tc).tc)..#'.
0090 | e0 3e 2d 52 00 00 00 00 00 00 00 00 00 00 02 ea | .>-R............
00a0 | 07 06 3c 00 00 00 00 00 21 00 00 00 00 00 00 00 | ..<.....!.......
00b0 | 73 65 6c 69 6e 75 78 00 00 00 00 00 00 00 00 00 | selinux.........
00c0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00d0 | 00 00 00 00 00 00 00 00 00 00 00 00 73 79 73 74 | ............syst
00e0 | 65 6d 5f 75 3a 6f 62 6a 65 63 74 5f 72 3a 68 6f | em_u:object_r:ho
00f0 | 6d 65 5f 72 6f 6f 74 5f 74 3a 73 30 00 00 00 00 | me_root_t:s0....
 
Inode is Allocated
File mode: 16877
Low 16 bits of Owner Uid: 0
Size in bytes: 4096
Access time: 1381931399
Creation time: 1381931399
Modification time: 1381931399
Deletion Time: 0
Low 16 bits of Group Id: 0
Links count: 5
Blocks count: 8
File flags: 0
File version (for NFS): 0
File ACL: 0
Directory ACL: 0
Fragment address: 0
Direct blocks: 9249, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Indirect block: 0
Double indirect block: 0
Triple indirect block: 0
 
File name                                       | Inode number | Deleted status
.                                                 2
..                                                2
lost+found                                        11
mysql                                             262145
cdrom.repo                                        12
woqu                                              2883585
我們這里最關(guān)心的還是mysql目錄的信息。這里我們知道m(xù)ysql的Inode為262145。于是我們可以再次用extundelete --inode 來查看mysql目錄的詳細(xì)信息:
[root@test1 /root]
#extundelete --inode 262145 /dev/VolGroup/home
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 400 groups loaded.
Group: 32
Contents of inode 262145:
0000 | c0 41 59 02 00 10 00 00 71 9a 5e 52 a8 99 5e 52 | .AY.....q.^R..^R
0010 | a8 99 5e 52 00 00 00 00 59 02 0c 00 08 00 00 00 | ..^R....Y.......
0020 | 00 00 08 00 54 00 00 00 0a f3 01 00 04 00 00 00 | ....T...........
0030 | 00 00 00 00 00 00 00 00 01 00 00 00 20 20 10 00 | ............  ..
0040 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0050 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0060 | 00 00 00 00 fc 9e be d7 00 00 00 00 00 00 00 00 | ................
0070 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
0080 | 1c 00 00 00 98 8a f7 bb 98 8a f7 bb 84 eb 44 c0 | ..............D.
0090 | ae be 3e 52 b4 1d 94 e3 00 00 00 00 00 00 00 00 | ..>R............
00a0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00b0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00c0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00d0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00e0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
00f0 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ................
 
Inode is Allocated
File mode: 16832
Low 16 bits of Owner Uid: 601
Size in bytes: 4096
Access time: 1381931633
Creation time: 1381931432
Modification time: 1381931432
Deletion Time: 0
Low 16 bits of Group Id: 601
Links count: 12
Blocks count: 8
File flags: 524288
File version (for NFS): 3619593980
File ACL: 0
Directory ACL: 0
Fragment address: 0
Direct blocks: 127754, 4, 0, 0, 1, 1056800, 0, 0, 0, 0, 0, 0
Indirect block: 0
Double indirect block: 0
Triple indirect block: 0
 
File name                                       | Inode number | Deleted status
.                                                 262145
..                                                2
.mozilla                                          262146
.bash_profile                                     262152
.gnome2                                           262150
.emacs                                            262195
.bash_logout                                      262194
.bashrc                                           262149
bin                                               262154
conf                                              262155
data                                              262156
script                                            404044     Deleted
thirdparty                                        262158
program                                           264107
.viminfo                                          262765
.bash_history                                     262193
.bzr.log                                          262153
.mysql_history                                    273588
source                                            402793
.ssh                                              414601
這里我們誤刪除的script目錄在這里被標(biāo)記為Deleted狀態(tài)了。
 
l   恢復(fù)誤刪除數(shù)據(jù)
extundelete可以通過--restore-inode將指定inode對(duì)應(yīng)的文件恢復(fù)出來,也可以使用--restore-all將所有狀態(tài)為已經(jīng)Deleted的文件和目錄恢復(fù)回來。restore-inode主要用于恢復(fù)單個(gè)文件;restore-all用于恢復(fù)所有的文件目錄。另外,還有--restore-file,--restore-files,--restore-directory來恢復(fù)指定目錄或者文件。
另外,如果你知道刪除的時(shí)間,那么可以指定--after或者--before來指定誤刪除的時(shí)間。
恢復(fù)數(shù)據(jù)的時(shí)候,extundelete將在當(dāng)前目錄下新建RECOVERED_FILES文件夾,并把恢復(fù)出來的數(shù)據(jù)文件或者目錄存放在該目錄中。
比如,我們使用--restore-inode恢復(fù)數(shù)據(jù),恢復(fù)264111號(hào)inode文件如下:
[root@test1 /root/RECOVERED_FILES]
#extundelete --restore-inode 264111 /dev/VolGroup/home
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 400 groups loaded.
Loading journal descriptors ... 31810 descriptors loaded.
 
[root@test1 /root/RECOVERED_FILES]
#ll file. 264111
-rw-r--r-- 1 root 43816 10月 16 15:42 file.264111
如上,它恢復(fù)出來的文件會(huì)被重命名為file.$Inode_no(這里是file.264111)放在RECOVERED_FILES目錄中。需要完全恢復(fù)數(shù)據(jù)的話,只需要將文件拷貝回原目錄,并重命名。
 
使用restore-all恢復(fù)的話,目錄名和文件名都會(huì)恢復(fù)回來,你可以在當(dāng)前目錄的RECOVERED_FILES目錄下找到對(duì)應(yīng)的文件和目錄如下:
[root@test1 /root/RECOVERED_FILES]
#ll mysql/
total 16
drwxr-xr-x 4 root 4096 Oct 16 15:42 script
你只需要將script拷貝到原目錄就好了。
 
3.           終極解決方案
當(dāng)然,以上的兩個(gè)方法都是萬不得已才使用的。最好的DBA和SA永遠(yuǎn)不是四處奔忙的救火隊(duì)員。最好的辦法是先做好預(yù)防工作,在發(fā)生之前盡量保證不出問題,而rm誤刪除文件的預(yù)防就是對(duì)重要數(shù)據(jù)進(jìn)行備份以及rm -i。
做了別名以后,刪除數(shù)據(jù)的時(shí)候,rm命令就會(huì)提示你,文件是否確定要?jiǎng)h除:
[root@test1 /root/RECOVERED_FILES/mysql/script]
#rm sock
rm:是否刪除普通文件 "sock"?
其他避免誤刪除等故障的方法可以參考《遠(yuǎn)離故障的十大原則》。當(dāng)然,最重要的還是日常對(duì)這種不可逆操作的謹(jǐn)慎和小心,并及時(shí)做好備份。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多