When you troubleshoot disks, you may encounter the loss of data disk partitions. This topic describes data disk partition loss in Linux and the solutions. This topic also describes the common mistakes and best practices for using disks to avoid the risk of data loss.
- 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 the restoration. For more information, see Create a normal snapshot and Roll back a disk by using a snapshot.
- An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the Alibaba Cloud official website.
- fdisk: a tool provided by Linux 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 by Linux. The tool is used to enable the kernel to re-read partitions without restarting the system.
Restore a partition by using fdisk
If the preceding operation cannot restore the partition, you can use testdisk to restore the partition.
Restore a partition by using testdisk
A disk device named /dev/xvdb is used in this example. To use testdisk to restore a partition, follow these steps:
- Run the testdisk /dev/xvdb command (you can replace the device name), select Proceed (default value), and then press the Enter key:
- Select the partition table type for scanning. Typically, the default value Intel is selected. Select EFI GPT if your data disk uses the GPT format.
- Select Analyse and press the Enter key.
- Select Quick Search and press the Enter key if the partition information is not displayed.
The partition information is displayed in the command output, as shown in the following figure.
- Select the partition and press the Enter key.
- Select Write to save the partition.
Note Select Deeper Search to continue to search if the expected partition is not listed.
- Press the Y key to save the partition.
- Run the partprobe /dev/xvdb command (you can replace the device name) to manually refresh the partition table.
- Mount the partition again and view the data in the data disk.
Restore data by using testdisk
In some cases, you can scan and locate a disk partition by using testdisk. However, you cannot save the partition. In this case, you can directly restore the data. Follow these steps:
- Scan and locate a disk partition by using testdisk. For more information, see Step 1 to Step 4 in Restore a partition by using testdisk.
- 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.
Copy done! 1 ok, 0 failedis displayed, the file is copied, as shown in the following figure.
- Switch to the /home directory to view details.
If the file is displayed, 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 databases such as MySQL, MongoDB, and Redis on ECS instances. Data loss may cause huge risks to businesses. This section describes the common mistakes and best practices in data security.
- Common mistakes
The underlying storage of Alibaba Cloud is based on triplicate technology. Therefore, some users consider that no risk of data loss in the operating system exists. 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 to the cloud disk logic in the system, such as infection with viruses, accidental data deletion, and file system damage, the 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 are the final solutions to data loss problems, but they may not restore data as expected. We recommend that you follow the best practices to create automatic or manual snapshots for data and run different backup schemes to maximize your data security.
- Apply automatic snapshot policies
Automatic snapshot policies are applied to system and data disks to create automatic snapshots for the disks. Note that after the system disk is replaced, the instance expires, or the disk is manually released, the corresponding 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 Property dialog box in the ECS console. If you want to retain the automatic snapshots, you can clear this option.
- Create manual snapshots
Before you perform important or high-risk operations, you must manually create snapshots for the disk. These operations include:
- 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