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

Background information

In a Linux instance, you can use one of the following tools to restore data on a data disk:
  • 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

After you restart a Linux instance, the partitions and data of data disks may be lost. This may be because you did not configure the partitions to be mounted automatically on instance startup in the /etc/fstab file. In this case, you can manually mount the data disk partitions. If an error message appears indicating that the partition table is lost when you manually mount the data disk partitions, you can use one of the following methods to restore the partitions or data:

Restore partitions by using fdisk

Typically, default values apply to the start and end sectors of partitions when you partition a data disk. To restore partitions on the data disk, you can run the fdisk command to re-create partitions on the data disk. For more information, see Initialize a data disk whose size does not exceed 2 TiB on a Linux instance. 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:

  1. 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.
    Run the testdisk /dev/xvdb command
  2. 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.
    Scan the partition table type for scanning
  3. Select Analyse and press the Enter key.
    Select Analyse
  4. If partition information is not displayed, select Quick Search and press the Enter key.
    Select Quick Search
    Partition information is displayed in the command output, as shown in the following figure. Command output
  5. Select a partition and press the Enter key.
  6. Select Write to save the partition.
    Note If the expected partition is not listed, select Deeper Search to continue to search.
    Select Write
  7. Press the Y key to save the partition.
    Press the Y key
  8. 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.
  9. Mount the partition again and view data in the data disk.
    Mount the partition again

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:

  1. 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.
  2. Press the P key to list files.
    The following figure shows the command output. Press the P key
  3. Select the file that you want to restore and press the C key.
  4. Select the destination directory. In this example, the file is restored to the /home directory.
    Select the destination directory
    If Copy done! 1 ok, 0 failed is displayed as shown in the following figure, the file is copied. The file is copied
  5. 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. 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.