When you manage a Linux ECS instance, deleting files is a common maintenance task. Regularly removing unneeded files helps reduce disk usage. However, if you accidentally delete important files due to human error, a program bug, or malware, you may need to perform data restoration. This topic describes the general workflows and provides an example of how to restore data that was accidentally deleted from a Linux ECS instance.
Background
The data restoration workflows and tools in this topic provide a comprehensive solution for recovering accidentally deleted files. The success of file restoration depends on your specific scenario and the operations you performed. Before you begin, we recommend that you assess your situation and choose the most appropriate data restoration strategy to maximize the chance of a successful recovery.
Prerequisites
-
Before you proceed, read the Workflows section.
-
Data restoration requires specialized knowledge. Ensure you have the necessary skills to avoid causing further damage to the data you intend to recover.
-
Before you attempt data restoration, it is strongly recommended that you create a snapshot of the disk that contains the data to be restored. This creates a backup and prevents data loss from errors during the recovery process.
-
The data restoration tool described in this topic is third-party open source software. Alibaba Cloud makes no guarantees about the success of file restoration.
-
If the data you need to recover is critical and you do not have expertise in data restoration, we recommend not following this tutorial. Instead, consider seeking assistance from a professional data restoration service.
Workflows
Workflow: System fails to start
When you run the rm -rf /* command as the root user on a Linux ECS instance, all files in the root directory are deleted. After you restart the operating system, the ECS instance enters the grub command-line interface. To restore data from the original system disk, you can follow the workflow below.
Workflow for accidental file deletion
When you run the rm -rf <file_or_dir> command on a Linux ECS instance, it deletes files or directories. To restore data from the original system disk, you can follow the workflow below.
If an accidentally deleted file prevents you from connecting to the Linux ECS instance by using SSH, you can connect to the ECS instance by using VNC.
Procedure
The preceding sections describe two common workflows for data restoration after accidental data deletion on a Linux ECS instance. The following steps provide a detailed example based on a specific scenario.
Scenario
-
You ran the
rm -rf /*command, which deleted files and caused the system to fail to start. -
You did not create a snapshot before the deletion, or you prefer not to use a snapshot for data restoration.
-
You want to restore data by using another Linux ECS instance.
Workflow overview
The following flowchart shows the procedure for this scenario.
Procedure
-
Create a snapshot for the ECS instance
Before you start the data restoration, create a snapshot of the system disk or data disk that you want to recover. This backs up the current data and prevents data loss from errors during the recovery process. For more information, see Create a snapshot for a disk.
-
Detach the disk and attach it to a new ECS instance
WarningYou must stop an ECS instance before you can detach its system disk. When you stop the ECS instance in the console, you must select the Force Stop option. Otherwise, the stop operation fails.

Detach the system disk or data disk and attach it to another Linux ECS instance to restore the data. For more information, see Detach or attach a system disk.
WarningWhen you attach the original system disk as a data disk to a new ECS instance in the console, the console may prompt you to initialize the disk because its file system might be corrupted. In this situation, do not select Initialize Now. If you re-initialize the disk from the console, the data that you want to restore may be permanently damaged. Close the dialog box to proceed.

-
Restore the deleted data
Use the testdisk tool to restore the deleted data. The following steps show how to restore a deleted directory on a Linux ECS instance.
-
Run the following command on the new ECS instance to install the testdisk tool.
Alibaba Cloud Linux 2/3
sudo yum install -y testdiskCentOS 6/7/8
sudo yum install -y testdiskUbuntu/Debian
sudo apt install -y testdisk -
Run the following command to check the current disk partitions.
sudo fdisk -luExample 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-67B1-4650-806D-FD0DE6E1210C Device Start End Sectors Size Type /dev/vda1 2048 6143 4096 2M BIOS boot /dev/vda2 6144 415743 409600 200M EFI System /dev/vda3 415744 304087006 303671263 144.8G Linux filesystem Disk /dev/vdb: 40 GiB, 42949672960 bytes, 83886080 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-67B1-4650-806D-FD0DE6E1210C Device Start End Sectors Size Type /dev/vdb1 2048 6143 4096 2M BIOS boot /dev/vdb2 6144 415743 409600 200M EFI System /dev/vdb3 415744 83886046 83470303 39.8G Linux filesystemIn this example,
/dev/vdais the system disk of the current ECS instance, and/dev/vdbis the original system disk attached as a data disk. -
Run the following command to open the specified device with
testdisk.sudo testdisk /dev/vdbThe following information is returned. Select
Proceed(default) and press Enter.
-
Select the partition table type to scan. Typically, you can select Intel. If your data disk uses the GUID Partition Table (
GPT) format, select EFI GPT. After you make your selection, press Enter.
-
Select Analyse and press Enter to analyze the disk.

-
Select Quick Search and press Enter to perform a quick search.

The partition information is displayed in the search results. Select the partition from which you want to restore data and press Enter.

-
Select Deeper Search and press Enter.

-
After the search is complete, a list of recoverable files is displayed. Select Continue and press Enter.

Press P to list the files.

Specific files and folders
Select the files or folders that you want to restore, and then press c to copy them.

All files
Press a to select all files, and then press C to copy them.

-
Select a destination directory and press C to copy the selected files to it. This example uses /home/ecs-user/data_recovery as the destination.

When the message
Copy done! 10 ok, 0 failedappears, the files have been successfully copied.
-
Open a new connection to the ECS instance and navigate to the /home/ecs-user/data_recovery directory to view the files. If you can see the files, the data restoration is successful.

-
References
-
For more information about how to create a snapshot, see Create a snapshot for a disk.
-
For more information about how to detach and attach a disk, see Detach or attach a system disk.
-
For more information about how to re-initialize a system disk, see Re-initialize a system disk (Reset the operating system).
-
For more information about how to create a disk from a snapshot, see Create a disk from a snapshot.
-
For more information about how to connect to an ECS instance by using VNC, see Connect to an instance by using VNC.
-
For more information about how to log on to a Linux ECS instance over SSH, see Use Workbench to log on to a Linux ECS instance.
-
For more information about data restoration tools for Linux ECS instances, see Tools for restoring data on Linux ECS instances.












