linux 误删除mysql表能恢复吗,Linux误删数据恢复
引子
指在鍵上飄,難免會濕手套。當你按下shift+del鍵后,會不會突然心里涼透,當你執行rm -rf后,會不會馬上去搜索哪個國家入境不需要簽證。或者你還會遇到如下的情況:
root@4xem7:~# alias
alias cd='rm -rf'
alias d='docker'
數據恢復原理淺析
如圖所示,硬盤上數據的存放有一個類似于書本目錄的東西存在,就是圖中最上面那一排圓形。硬盤格式化后,被分割成了很多個小塊,就像蜂巢一樣,一個個的小房間,就是上圖中間部分的長方塊,這個小房間才是真正存放數據的地方,最下面的數字,是房間號,也就是inode號。如果我們要使用文件A,在最上面的目錄里面就能很快的知道文件A在1號房間。
?什么情況下,被誤刪的數據能恢復
誤刪情況發生后,第一重要的事情就是卸載誤刪數據所在的分區,而不是關機,絕對不能關機,為什么?現在所有的系統優化,其本質就是把硬盤IO轉換成內存IO,就是把本該到硬盤里面去讀寫的內容,提前預載到內存里面去。你若正常關機,redis持久化的數據,可能有好幾個G,mysql的InnoDB_Buffer_Pool緩沖池里面的東西(可能有好幾十個G),這些東西往硬盤里面一寫,你還想恢復數據,等我發明時光機吧。為什么說馬上卸載分區,數據就能恢復。看下圖,rm -rf刪除了A文件。
其實,系統只是刪除了目錄里面的文件指向,文件A還好好的趟在1號房間里面。所以這種情況,文件是能很輕松的恢復的。
?什么情況下,被誤刪的數據不能恢復
承接上一張圖,目錄里面顯示1號房間是空的,可以重新放數據進去,若此時,硬盤又接收到寫入請求(比方說你腦抽,把數據恢復軟件安裝到了誤刪數據所在的分區),就會把數據寫入1號房間:
此時,你若還想恢復數據,只能等我發明時光機了。
extundelete恢復舉例
extundelete可以恢復ext3、ext4文件系統下被誤刪的文件,看其恢復原理xfs文件系統應該也是可以恢復的。
安裝extundelete
在安裝extundelete前,需要安裝e2fsprogs和e2fsprogs-libs,自行用yum安裝就可以。
[root@dish ~]# rpm -qa |grep e2fsprogs
e2fsprogs-libs-1.41.12-22.el6.x86_64
e2fsprogs-1.41.12-22.el6.x86_64
epel源里面有extundelete,直接安裝就行。
[root@dish ~]# yum install extundelete -y
[root@dish ~]# rpm -ql extundelete
/usr/bin/extundelete
/usr/share/doc/extundelete-0.2.4
/usr/share/doc/extundelete-0.2.4/LICENSE
/usr/share/doc/extundelete-0.2.4/README
extundelete常用參數解釋
Options:
--superblock 顯示超級塊信息,默認選項.
--journal 顯示日志信息.
--after dtime 表示在某段時間之后被刪除的文件或目錄.
--before dtime 表示在某段時間之前被刪除的文件或目錄.
Actions:
--inode ino Show info on inode 'ino'.
--block blk Show info on block 'blk'.
--restore-inode ino[,ino,...]
恢復指定inode號的文件,恢復出來的文件會放在當前目錄下的RECOVERED_FILES文件夾中
--restore-file 'path' 恢復指定路徑的文件
--restore-files 'path' 恢復在路徑中列出的所有文件
--restore-all 嘗試恢復所有目錄和文件
恢復文件
?測試文件
[root@dish reco]# for i in A B C D;do echo "this is file$i">$i;done
[root@dish reco]# cat A B C D
this is fileA
this is fileB
this is fileC
this is fileD
[root@dish reco]# find ./ -type f |xargs md5sum >./file.md5
[root@dish reco]# cat file.md5
d41d8cd98f00b204e9800998ecf8427e ./file.md5
c2f9bc9edb959d08dc838af14c5cd8ff ./C
afc688bfcd0666302ed55da472ecfbb8 ./B
0a540f40e1e8c3e5ad50571f1621d5b3 ./A
2c3fdd8c8e23a1ba2d0381981f337348 ./D
[root@dish reco]# rm -rf A B C D
[root@dish reco]# ls
file.md5
?卸載磁盤分區
發現數據誤刪后,只做一件事,卸載數據所在分區
[root@dish reco]# cd
[root@dish ~]# umount /dev/vdb1
?查詢可恢復的數據
首先查看數據所在目錄reco的inode號
[root@dish ~]# extundelete /dev/vdb1 --inode 2 |grep reco
reco 131073
reco目錄的inode號131073
[root@dish ~]# extundelete /dev/vdb1 --inode 131073 |tail
Triple indirect block: 0
File name | Inode number | Deleted status
. 131073
.. 2
A 131074 Deleted
B 131075 Deleted
C 131076 Deleted
D 131077 Deleted
file.md5 131078
如上所示,標記為Deleted狀態的就是誤刪的文件,同時還能看到對應文件的inode值,有了這些,我們就可以恢復文件了。
[root@dish ~]# extundelete /dev/vdb1 --restore-file reco/C
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 2858 descriptors loaded.
Successfully restored file reco/C
[root@dish ~]# extundelete /dev/vdb1 --restore-file reco/D
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 2858 descriptors loaded.
Successfully restored file reco/D
--restore-file 后面的reco/C是個相對路徑,其原本的路徑是/bak/reco/C,而bak目錄掛載的就是/dev/vdb1。文件恢復成功后,會在你執行恢復命令的當前目錄生成一個RECOVERED_FILES目錄,恢復出來的文件就在里面。
[root@dish ~]# cd RECOVERED_FILES/reco/
[root@dish reco]# md5sum -c file.md5
./C: OK
./B: OK
./A: OK
./D: OK
恢復出來的文件經過MD5校驗全通過。恢復成功。
恢復目錄
[root@dish reco]# mkdir mulu
[root@dish reco]# cd !$
cd mulu
[root@dish mulu]# touch {01..09}
[root@dish mulu]# ls
01 02 03 04 05 06 07 08 09
[root@dish mulu]# cd ..
[root@dish reco]# rm -rf mulu
[root@dish reco]# cd
[root@dish ~]# r umount
umount /dev/vdb1
[root@dish ~]# extundelete /dev/vdb1 --restore-directory /reco/mulu
NOTICE: Extended attributes are not restored.
Loading filesystem metadata ... 160 groups loaded.
Loading journal descriptors ... 2857 descriptors loaded.
Searching for recoverable inodes in directory /reco/mulu ...
10 recoverable inodes found.
Looking through the directory structure for deleted files ...
2 recoverable inodes still lost.
[root@dish reco]# ls mulu/
01 02 03 04 05 06 07 08
目錄被成功恢復,注意,目錄里面的空文件是無法恢復的,09就是一個空文件。
恢復所有文件
需要恢復的文件數量過多的時候,可以用這個選項。
[root@dish tmp]# extundelete /dev/vdb1 --restore-all
恢復某個時間點之后刪除的文件
這個應該是用的最多的場景了,比方說需要恢復一個小時內被刪除的文件。
extundelete --after `echo $(date +%s)-3600 |bc` --restore-all /dev/vdb1
一個小時3600秒,根據自己需求修改上面的減數。
原創文章,轉載請注明: 轉載自笛聲
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的linux 误删除mysql表能恢复吗,Linux误删数据恢复的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓好处在哪里(安卓好处)
- 下一篇: mysql error 1114,mys