When you troubleshoot disks, data disk partitions may be lost. This topic describes the issue that data disk partitions are lost on a Linux instance and how to resolve the issue. This topic also describes the common mistakes in disk usage and the best practices for using disks.
Prerequisites
- A snapshot is created for the data disk that lost a partition. If errors occur during data restoration, you can use the snapshot to roll back the data disk to the state before restoration. For more information, see Create a snapshot of a disk and Roll back a disk by using a snapshot.
- An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the Sign up to Alibaba Cloud page.
Background information
- fdisk: a tool provided in Linux by default for partitioning disks.
- testdisk: a tool used to restore disk partitions or data in Linux. By default, the tool is not provided in Linux. You must install it on your own. For example, you can run the yum install -y testdisk command to install testdisk in CentOS.
- partprobe: a tool provided in Linux by default. The tool is used to enable the kernel to re-read partitions without the need to restart the system.
Methods
Restore partitions by using fdisk
If the preceding operation cannot restore the partitions, you can use testdisk to restore the partitions.
Restore partitions by using testdisk
In this example, the disk device name /dev/xvdb is used. To use testdisk to restore partitions on the /dev/xvdb disk, perform the following steps:
- Run the testdisk /dev/xvdb command. If your data disk has a different device name, replace /dev/xvdb with the actual device name of your data disk. Then, select Proceed (default value) and press the Enter key.
- Select the partition table type for scanning. Typically, the default value Intel is selected. If your data disk uses the GUID Partition Table (GPT) format, select EFI GPT.
- Select Analyse and press the Enter key.
- If partition information is not displayed, select Quick Search and press the Enter key. Partition information is displayed in the command output, as shown in the following figure.
- Select a partition and press the Enter key.
- Select Write to save the partition. Note If the expected partition is not listed, select Deeper Search to continue to search.
- Press the Y key to save the partition.
- Run the partprobe /dev/xvdb command to refresh the partition table. If your data disk has a different device name, replace /dev/xvdb with the actual device name of your data disk.
- Mount the partition again and view data in the data disk.
Restore data by using testdisk
In some cases, you can scan for a disk partition by using testdisk but cannot save the partition. If this happens, you can directly restore data. Perform the following steps:
- Scan for a disk partition by using testdisk. For more information, see Step 1 to Step 4 in the Restore partitions by using testdisk section of this topic.
- Press the P key to list files. The following figure shows the command output.
- Select the file that you want to restore and press the C key.
- Select the destination directory. In this example, the file is restored to the /home directory. If
Copy done! 1 ok, 0 failed
is displayed as shown in the following figure, the file is copied. - Switch to the /home directory to check whether the file is restored. If the file is displayed in the /home directory as shown in the following figure, the file is restored.
Common mistakes and best practices
Data is the core asset of users. A large number of users build websites and create self-managed MySQL, MongoDB, or Redis databases on Elastic Compute Service (ECS) instances. Data loss may cause huge risks to business. This section describes the common mistakes and best practices in data security.
- Common mistakes
The underlying storage of Alibaba Cloud is based on the triplicate technology. As such, some users consider that no risk of data loss exists in the operating system. This is a misunderstanding. The three copies of data stored in the underlying layer provide physical layer protection for data disks. However, if errors occur when you use disks, such as infection with viruses, accidental data deletion, and file system damage, data may still be lost. You must use technologies such as snapshots and geo-redundancy to ensure data security. For more information about three copies, see Triplicate storage.
- Best practices
Data disk partition restoration and data restoration can be used to address data loss but may be unable to restore data as expected. We recommend that you follow the best practices to create automatic or manual snapshots for data and use different backup methods to maximize your data security.
- Apply automatic snapshot policies
You can apply automatic snapshot policies to system disks or data disks to create automatic snapshots for the disks. Note that when a system disk is replaced, an instance expires, or a disk is manually released, the associated automatic snapshots may be released.
If you want automatic snapshots of a disk to be released along with the disk, you can select Delete Automatic Snapshots While Releasing Disk in the Modify Disk Properties dialog box in the ECS console. If you want to retain the automatic snapshots when the disk is released, clear this option.
For more information, see Snapshot FAQ and Create an automatic snapshot policy.
- Create manual snapshots Before you perform important or high-risk operations on a disk such as the following operations, manually create snapshots for the disk:
- Update the kernel.
- Upgrade or change applications.
- Restore data on disks.
Before you restore a disk, you must create a snapshot for the disk. After the snapshot is created, you can perform other operations.
- OSS backup, offline backup, and geo-redundancy
You can back up important data by using OSS backup, offline backup, or geo-redundancy.
- Apply automatic snapshot policies