在處理磁碟相關問題時,您可能會遇到資料盤分區或資料丟失的情況。本文將介紹Linux系統下常見的資料盤分區丟失問題及相應工具的使用方法。
前提條件
在修複資料前,您必須先對分區丟失的資料盤建立快照,在快照建立完成後再嘗試修複。如果在修複過程中出現問題,您可以通過快照復原將資料盤還原到修複之前的狀態。具體操作,請參見手動建立單個快照和使用快照復原雲端硬碟。
背景資訊
有關本文介紹的幾款工具的功能及相關資訊,請參閱以下內容。
工具名稱 | 是否預設安裝 | 工具介紹 |
fdisk | 是 | Linux系統磁碟分割管理工具。 |
testdisk | 否 | 恢複Linux系統的磁碟分割或者資料的工具。 |
partprobe | 是 | 主要用於在不重啟系統時讓kernel重新讀取磁碟分割資訊。 |
恢複磁碟分割或資料
在使用Linux執行個體時,如果因人為誤操作、程式異常或電腦病毒影響而導致磁碟分割表受損或資料異常丟失,您可以嘗試以下三種方法進行處理。
(條件可選)確認磁碟資訊
運行以下命令,以確認當前磁碟資訊。
sudo fdisk -lu回顯結果樣本如下。
Disk /dev/vda: 145 GiB, 155692564480 bytes, 304087040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F51132A7-xxxx-xxxx-xxxx-FD0DE6E1210C樣本中,包含一塊磁碟裝置/dev/vda,因分區表損壞,不顯示Device資訊。
通過fdisk恢複分區
對資料盤分區時,分區磁碟的起止扇區一般使用預設的值,所以可以先嘗試直接使用fdisk命令建立分區進行恢複。
執行 fdisk /dev/xvdb 命令重新建立分區,然後掛載並驗證資料是否恢複:
[root@Aliyun ~]# fdisk /dev/xvdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@Aliyun ~]# mount /dev/xvd
xvda xvda1 xvdb xvdb1
[root@Aliyun ~]# mount /dev/xvdb
xvdb xvdb1
[root@Aliyun ~]# mount /dev/xvdb1 /mnt/
[root@Aliyun ~]# ls /mnt/
123.sh configclient data diamond install_edsd.sh install.sh ip.qz如果在使用fdisk恢複分區後仍無法掛載,或者掛載成功但無法查看相應檔案,您可以選擇通過testdisk恢複分區或者通過testdisk恢複資料。
通過testdisk恢複分區
這裡假設雲端硬碟的裝置名稱為/dev/xvdb。按以下步驟安裝並使用testdisk工具恢複分區:
運行以下命令,以安裝
testdisk。Alibaba Cloud Linux 3/2,CentOS 6/7/8
sudo yum install -y testdiskUbuntu/Debian
sudo apt install -y testdisk運行testdisk /dev/xvdb(根據實際情況替換裝置名稱),再選擇Proceed(預設值)後按斷行符號鍵。
TestDisk 7.0, Data Recovery Utility, April 2015 Christophe GRENIER <grenier@cgsecurity.org> http://www.cgsecurity.org TestDisk is free software, and comes with ABSOLUTELY NO WARRANTY. Select a media (use Arrow keys, then press Enter): >Disk /dev/xvdb - 5368 MB / 5120 MiB >[Proceed ] [ Quit ] Note: Disk capacity must be correctly detected for a successful recovery. If a disk listed above has incorrect size, check HD jumper settings, BIOS detection, and install the latest OS patches and disk drivers.選擇分區表類型進行掃描:一般選擇Intel。如果您的資料盤採用GPT分區,選擇EFI GPT。
TestDisk 7.0, Data Recovery Utility, April 2015 Christophe GRENIER <grenier@cgsecurity.org> http://www.cgsecurity.org Disk /dev/xvdb - 5368 MB / 5120 MiB Please select the partition table type, press Enter when done. >[Intel ] Intel/PC partition [EFI GPT] EFI GPT partition map (Mac i386, some x86_64...) [Humax ] Humax partition table [Mac ] Apple partition map [None ] Non partitioned media [Sun ] Sun Solaris partition [XBox ] XBox partition [Return ] Return to disk selection Note: Do NOT select 'None' for media with only a single partition. It's very rare for a disk to be 'Non-partitioned'.選擇Analyse後按斷行符號鍵。
Disk /dev/xvdb - 5368 MB / 5120 MiB CHS 652 255 63 - sector size=512 [ Analyse ] Analyse current partition structure and search for lost partitions [ Advanced ] Filesystem Utils [ Geometry ] Change disk geometry [ Options ] Modify options [ MBR Code ] Write TestDisk MBR code to first sector [ Delete ] Delete all data in the partition table [ Quit ] Return to disk selection Note: Correct disk geometry is required for a successful recovery. 'Analyse' process may give some warnings if it thinks the logical geometry is mismatched.如果您沒有看到任何分區資訊,選擇Quick Search後按斷行符號鍵快速搜尋。
Disk /dev/xvdb - 5368 MB / 5120 MiB - CHS 652 255 63 Current partition structure: Partition Start End Size in sectors No partition is bootable *=Primary bootable P=Primary L=Logical E=Extended D=Deleted >[Quick Search] Try to locate partitionQuick Search 搜尋完成後,顯示找到的 Linux 分區(標記為 Primary bootable,大小 10483712 扇區),Structure 狀態為 Ok。
Disk /dev/xvdb - 5368 MB / 5120 MiB - CHS 652 255 63 Partition Start End Size in sectors >* Linux 0 32 33 652 180 40 10483712 Structure: Ok. Use Up/Down Arrow keys to select partition. Use Left/Right Arrow keys to CHANGE partition characteristics: *=Primary bootable P=Primary L=Logical E=Extended D=Deleted Keys A: add partition, L: load backup, T: change type, P: list files, Enter: to continue選中分區後,按斷行符號鍵。
選擇Write儲存分區。
說明如果不是您需要的分區,可以選擇Deeper Search繼續搜尋。
確認分區資訊無誤後,選擇 Write 將分區結構寫入磁碟。
Disk /dev/xvdb - 5368 MB / 5120 MiB - CHS 652 255 63 Partition Start End Size in sectors 1 * Linux 0 32 33 652 180 40 10483712 [ Quit ] [Deeper Search] [> Write ] Write partition structure to disk按Y鍵確認儲存分區。
TestDisk 7.0, Data Recovery Utility, April 2015 Christophe GRENIER <grenier@cgsecurity.org> http://www.cgsecurity.org write partition table, confirm ? (Y/N)運行partprobe /dev/xvdb(根據實際情況替換裝置名稱)手動重新整理分區表。
重新掛載分區,查看資料盤裡的資料情況。
輸出中
ip.qz即為恢複的目標檔案。[root@Aliyun home]# mount /dev/xvdb1 /mnt/ [root@Aliyun home]# ls /mnt/ 123.sh configclient data diamond install_edsd.sh install.sh ip.qz logs lost+found test
通過testdisk恢複資料
在某些情況下,您可以用testdisk掃描出磁碟分割,但是無法儲存分區,此時,您可以嘗試直接恢複檔案。具體操作步驟如下所示:
用testdisk掃描出磁碟分割。具體操作,請參見通過testdisk恢複分區的第1步到第5步。
按P鍵列出檔案。
返回結果如下。
* Linux 0 32 33 652 180 40 10483712 Directory / drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 . drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 .. drwx------ 0 0 16384 21-Feb-2017 11:56 lost+found -rw-r--r-- 0 0 1701 21-Feb-2017 11:57 install_edsd.sh -rw-r--r-- 0 0 5848 21-Feb-2017 11:57 install.sh >-rw-r--r-- 0 0 12136 21-Feb-2017 11:57 ip.gz -rw-r--r-- 0 0 0 21-Feb-2017 11:57 test drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 123.sh drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 configclient drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 data drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 diamond drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 logs Next Use Right to change directory, h to hide deleted files q to quit, : to select the current file, a to select all files C to copy the selected files, c to copy the current file選中要恢複的檔案,再按C鍵。
選擇目標目錄。本樣本中以恢複到/home為例。
Please select a destination where /ip.gz will be copied. Keys: Arrow keys to select another directory C when the destination is correct Q to quit Directory / drwxr-xr-x 0 0 4096 11-Jan-2017 09:32 . drwxr-xr-x 0 0 4096 11-Jan-2017 09:32 .. dr-xr-xr-x 0 0 4096 25-Jul-2016 16:23 boot drwxr-xr-x 0 0 2940 21-Feb-2017 12:30 dev drwxr-xr-x 0 0 4096 21-Feb-2017 12:12 etc > drwxr-xr-x 0 0 4096 16-Feb-2017 11:48 home drwx------ 0 0 16384 12-May-2016 19:58 lost+found drwxr-xr-x 0 0 4096 12-Aug-2015 22:22 media drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 mnt drwxr-xr-x 0 0 4096 12-Aug-2015 22:22 opt dr-xr-xr-x 0 0 0 16-Feb-2017 21:35 proc dr-xr-x--- 0 0 4096 21-Feb-2017 11:57 root drwxr-xr-x 0 0 560 21-Feb-2017 12:12 run drwxr-xr-x 0 0 4096 12-Aug-2015 22:22 srv dr-xr-xr-x 0 0 0 16-Feb-2017 21:35 sys drwxrwxrwt 0 0 4096 21-Feb-2017 12:34 tmp drwxr-xr-x 0 0 4096 16-Feb-2017 11:48 usr drwxr-xr-x 0 0 4096 16-Feb-2017 21:35 var lrwxrwxrwx 0 0 7 3-May-2016 13:48 bin lrwxrwxrwx 0 0 7 3-May-2016 13:48 lib lrwxrwxrwx 0 0 9 3-May-2016 13:48 lib64 lrwxrwxrwx 0 0 8 3-May-2016 13:48 sbin複製完成後,終端顯示
Copy done! 1 ok, 0 failed,表示ip.gz檔案已成功複製到目標目錄。目錄列表確認該檔案已存在於目標資料分割中:* Linux 0 32 33 652 180 40 10483712 Directory / Copy done! 1 ok, 0 failed drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 . drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 .. drwx------ 0 0 16384 21-Feb-2017 11:56 lost+found -rw-r--r-- 0 0 1701 21-Feb-2017 11:57 install_edsd.sh -rw-r--r-- 0 0 5848 21-Feb-2017 11:57 install.sh -rw-r--r-- 0 0 12136 21-Feb-2017 11:57 ip.gz -rw-r--r-- 0 0 0 21-Feb-2017 11:57 test -rw-r--r-- 0 0 0 21-Feb-2017 11:57 123.sh drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 configclient drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 data drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 diamond drwxr-xr-x 0 0 4096 21-Feb-2017 11:57 logs切換到/home目錄查看。
可以看到
ip.qz檔案已恢複成功。[root@Aliyun /]# ls /home/ admin ip.qz [root@Aliyun /]#
常見誤區與最佳實務
資料是使用者的核心資產,很多使用者在ECS執行個體上構建網站、自建資料庫(MySQL/MongoDB/Redis)。資料丟失會給使用者的業務帶來巨大的風險。本節介紹了資料安全方面的常見誤區和最佳實務。
常見誤區
阿里雲的底層儲存基於三副本機制,因此一些使用者誤認為作業系統內的資料沒有任何丟失風險。實際上,這是一種誤解。底層儲存的三副本確實為資料磁碟提供了物理層的保護,但如果系統內部使用雲端硬碟時出現邏輯性問題,例如病毒感染、誤刪資料或檔案系統損壞等情況,資料丟失的風險依然存在。在這種情況下,您需藉助快照、異地備份等相關技術,儘可能保障資料的安全性。關於三副本的介紹,請參見ESSD 雲端硬碟資料可靠性。
最佳實務
資料盤分區恢複及資料恢複是應對資料丟失問題的最後一道防線,但並不一定能夠確保資料的恢複。強烈建議您參考以下最佳實務,通過建立資料快照(無論是自動還是手動)以及實施各類備份方案,以最大程度地保障資料的安全性。
啟用自動快照
根據實際業務,對系統硬碟、資料盤建立自動快照。
建立手動快照
在進行以下重要或高風險操作之前,請務必手動為磁碟建立快照。
系統升級核心
應用升級變更
磁碟資料恢複
在進行磁碟恢複之前,務必首先對磁碟建立快照,待快照完成後再進行相應的操作。
OSS、線下、異地備份
您可以根據實際情況選擇使用OSS、線下或異地等方式備份重要資料。
相關文檔
有關LInux執行個體資料恢複的整體思路及最佳實務,請參見Linux執行個體資料恢複最佳實務。