不過是在作業或者學習中,我們都經常又可以觸及到一個非常危險的命令rm -rf,這個命令會直接洗掉資料,并且不需要我們確認,所以說使用的時候要注意,
那么,如果我們一不小心洗掉了資料該怎么辦呢?
- 首先,我們要知道洗掉資料的兩種場景
1.在執行洗掉檔案時,該檔案正被行程使用
2.是檔案未被使用,而被誤洗掉
- 為什么洗掉的資料還可以恢復?
對于第一種情況:檔案正在使用,資料可以恢復是因為Linux里,
每個檔案都有兩個link計數器:i_count和i_nlink
i_count作用是當一個檔案被行程參考時,它的數值會加1,也就是說
它記錄的是檔案被行程參考的次數
i_nlink的作用是記錄檔案產生硬鏈接的個數,
Linux系統中只有兩個數值都為0時,檔案才會被洗掉
如果此時檔案被占用,那么它的i_count數值不為0
那么就可以把檔案找回來
對于第二種情況:沒有被行程使用的檔案被洗掉,此時i_count和i_link都為0
這個時候檔案的inode連接資訊已經被洗掉了,我們需要通過存放檔案的block單元,
做資料塊的資料找回,
我們看到的資料存盤依賴于inode和block兩個單元
我們洗掉資料時,洗掉的是inode的鏈接
但block資料塊并沒有洗掉
所以我們要通過分析后端的block塊,對檔案進行恢復
注意:
我們通過block塊對資料進行恢復,理論可以找回完整資料,
但是有一個風險:如果有行程不斷往磁盤寫資料時,
需要申請新的block塊,如果系統分配已洗掉檔案的block塊
那么新寫入的資料會覆寫原有資料,這時資料會真正丟失
如果出現這種情況,需要umount目錄所在的磁盤設備
案例演示:
-
第一種場景:檔案在使用程序中被洗掉
-
創建一個測驗檔案,并使用tail命令持續查看
[root@centos7 ~]# mkdir /test
[root@centos7 ~]# cd /test/
[root@centos7 test]# echo "hehehehehe hahahaha">test.txt
[root@centos7 test]# cat test.txt
hehehehehe hahahaha
[root@centos7 test]# tail -f test.txt
hehehehehe hahahaha
- 復制一個ssh渠道,進行洗掉檔案
[root@centos7 ~]# cd /test/
[root@centos7 test]# rm -rf test.txt
[root@centos7 test]# ls
[root@centos7 test]#
- 進行資料恢復
#查看哪個行程正在使用被洗掉的檔案
[root@centos7 test]# lsof | grep test.txt
tail 4296 root 3r REG 253,0 20 736294 /test/test.txt (deleted)
#通過行程號進入檔案句柄
[root@centos7 test]# cd /proc/4296/fd
[root@centos7 fd]# ll
總用量 0
lrwx------ 1 root root 64 3月 2 22:14 0 -> /dev/pts/0
lrwx------ 1 root root 64 3月 2 22:14 1 -> /dev/pts/0
lrwx------ 1 root root 64 3月 2 22:14 2 -> /dev/pts/0
lr-x------ 1 root root 64 3月 2 22:14 3 -> /test/test.txt (deleted)
lr-x------ 1 root root 64 3月 2 22:14 4 -> anon_inode:inotify
#進行資料恢復
[root@centos7 fd]# cp 3 /test/test.txt
[root@centos7 fd]# cd /test/
[root@centos7 test]# ls
test.txt
[root@centos7 test]# cat test.txt
hehehehehe hahahaha
#發現資料已經恢復
- 第二種場景:未被行程使用而檔案被洗掉
- 我看到網上有 專門恢復ext4型別的帖子,有專門的工具,很方便
- 但我的系統是xfs型別的,雖然有教程但是還是有些疑惑,最底下寫出來
- 首先,再準備一塊新磁盤,將其格式化為xfs型別
#首先確認新磁盤添加成功
[root@centos7 ~]# fdisk -l
磁盤 /dev/sdb:21.5 GB, 21474836480 位元組,41943040 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 位元組 / 512 位元組
I/O 大小(最小/最佳):512 位元組 / 512 位元組
#將其格式化為xfs型別
[root@centos7 ~]# fdisk /dev/sdb
依次輸入n-p-1-回車-回車-w
[root@centos7 ~]# mkfs.xfs /dev/sdb1
- 創建測驗目錄,并掛載
[root@centos7 ~]# mkdir /test1
[root@centos7 ~]# mount /dev/sdb1 /test1/
- 創建測驗內容
[root@centos7 ~]# cd /test1/
[root@centos7 test1]# echo "hdhwahdw">file1.txt
[root@centos7 test1]# mkdir haha
- 備份檔案
[root@centos7 test1]# xfsdump -f /opt/xfs_dump /dev/sdb1
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
============================= dump label dialog ==============================
please enter label for this dump session (timeout in 300 sec)
-> xfs_dump
session label entered: "xfs_dump"
--------------------------------- end dialog ---------------------------------
xfsdump: level 0 dump of centos7:/test1
xfsdump: dump date: Wed Mar 3 10:40:05 2021
xfsdump: session id: 9e939503-a58d-4669-bb87-0bec313db8b5
xfsdump: session label: "xfs_dump"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 25536 bytes
xfsdump: /var/lib/xfsdump/inventory created
============================= media label dialog =============================
please enter label for media in drive 0 (timeout in 300 sec)
-> sdb1
media label entered: "sdb1"
--------------------------------- end dialog ---------------------------------
xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 22184 bytes
xfsdump: dump size (non-dir files) : 544 bytes
xfsdump: dump complete: 24 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /opt/xfs_dump OK (success)
xfsdump: Dump Status: SUCCESS
- 洗掉測驗檔案進行恢復
[root@centos7 test1]# rm -rf ./*
[root@centos7 test1]# ls
[root@centos7 test1]# xfsrestore -f /opt/xfs_dump /test1/
- 查看是否恢復成功
[root@centos7 test1]# ls
file1.txt haha
- 到此資料就恢復過來了
雖然實驗做完了,但是我有個疑問,如果有清楚的可以在留言告訴我,就是資料洗掉可能是意外,我沒提前備份怎么辦?
(畢竟這個實驗是按教程走的,不會出現意外)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/266027.html
標籤:其他
