Use fdisk and testdisk to restore lost partitions or recover data on Linux ECS instance data disks.
Prerequisites
A snapshot is created for the data disk that lost partitions. If errors occur during restoration, use the snapshot to roll back the data disk. See Create snapshot manually and Roll back a disk using a snapshot.
Background
The following tools can restore data on Linux instances.
|
Tool name |
Default installation |
Tool description |
|
fdisk |
Yes |
A partition management tool in Linux. |
|
testdisk |
No |
A tool to restore disk partitions or data in Linux. |
|
partprobe |
Yes |
A tool that lets the kernel reread partition information without restarting. |
Restore partitions or data
If the partition table is corrupted or data is lost due to accidental operations, program anomalies, or viruses, use one of the following methods.
(Optional) Check disk information
Check the current disk information:
sudo fdisk -lu
Sample output:
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
In this example, the /dev/vda disk is displayed. The Device field is missing, which indicates a corrupted partition table.
Use fdisk to restore partitions
In most cases, partitioning a data disk uses default start and end sectors. Run fdisk to re-create partitions on the data disk.
Run the fdisk /dev/xvdb command to re-create the partition, then mount and verify data recovery:
[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
If the partition cannot be mounted after running fdisk, or if files are not visible after mounting, use testdisk to restore partitions or use testdisk to restore data.
Use testdisk to restore partitions
This example uses the /dev/xvdb disk. To restore partitions with testdisk, perform the following steps:
-
Install
testdisk.Alibaba Cloud Linux 3, Alibaba Cloud Linux 2, CentOS 6, CentOS 7, or CentOS 8
sudo yum install -y testdiskUbuntu or Debian
sudo apt install -y testdisk -
Run testdisk /dev/xvdb. Replace /dev/xvdb with your data disk device name. Select Proceed (default) and press Enter.
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. -
Select the partition table style. The default is Intel. For GPT disks, select 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'. -
Select Analyse and press Enter.
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. -
If no partition information is displayed, select Quick Search and press Enter.
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 partitionAfter the Quick Search completes, the found Linux partition is displayed (marked as Primary bootable, size 10483712 sectors), and the Structure status shows 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 -
Select a partition and press Enter.
-
Select Write to save the partition.
NoteIf the expected partition is not listed, select Deeper Search to continue searching.
After confirming the partition information is correct, select Write to write the partition structure to the disk.
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 -
Press Y to save the partition.
TestDisk 7.0, Data Recovery Utility, April 2015 Christophe GRENIER <grenier@cgsecurity.org> http://www.cgsecurity.org write partition table, confirm ? (Y/N) -
Run partprobe /dev/xvdb to refresh the partition table. Replace /dev/xvdb with your data disk device name.
-
Remount the partition and verify the data.
The
ip.qzfile in the output is the recovered target file.[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
Use testdisk to restore data
If testdisk finds a partition but cannot save it, you can still recover individual files. Perform the following steps:
-
Scan for the disk partition with testdisk. See Steps 1 to 5 in Use testdisk to restore partitions.
-
Press P to list files.
Files are displayed.
* 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 -
Select a file to restore and press C.
-
Select a destination directory. In this example, the file is restored to /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 sbinAfter the copy is complete, the terminal displays
Copy done! 1 ok, 0 failed, indicating thatip.gzhas been successfully copied to the destination directory. The directory listing confirms the file is present in the target partition:* 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 -
Go to the /home directory to verify the file is restored.
The
ip.qzfile has been successfully restored. The file appears in the /home directory, confirming successful restoration.[root@Aliyun /]# ls /home/ admin ip.qz [root@Aliyun /]#
Misunderstandings and best practices
Data loss on ECS instances can severely impact business. This section covers common misunderstandings and best practices for data security.
Common misunderstandings
Alibaba Cloud uses triplicate storage, which leads some users to assume no data loss risk exists. However, triplicate storage only protects data at the physical layer. Logical issues such as viruses, accidental deletions, or file system corruption can still cause data loss. Use snapshots and geo-redundancy to improve data security. See ESSD cloud disk data reliability.
Best practices
Partition and data restoration may not always succeed. Follow these best practices to maximize data security with snapshots and backups.
-
Apply automatic snapshot policies
Apply automatic snapshot policies to system disks or data disks.
ImportantWhen a system disk is replaced, an instance expires, or a disk is manually released, associated automatic snapshots may be released. To release automatic snapshots along with the disk, select Delete Automatic Snapshots While Releasing Disk in the Modify Attributes dialog box in the ECS console. To retain them, clear this option. See Snapshot FAQ and Create an automatic snapshot policy.
-
Create manual snapshots
Create manual snapshots before the following high-risk operations:
-
Update the kernel.
-
Upgrade or change applications.
-
Restore data on disks.
Always create a snapshot before restoring a disk. Proceed with restoration only after the snapshot completes.
-
-
OSS backup, offline backup, and geo-redundancy
Back up important data with OSS, offline backup, or geo-redundancy based on your business requirements.
References
See Best prac