If your business requires data consistency, you can enable the application-consistent snapshot feature when you create a snapshot-consistent group. The feature ensures that applications maintain data consistency. This topic describes how to create application-consistent snapshots for Linux and Windows Elastic Compute Service (ECS) instances in the ECS console or by using Alibaba Cloud SDK for Go.
Background information
If you enable the application-consistent snapshot feature when you create a snapshot-consistent group, the system creates application- or file system-consistent snapshots based on the actual scenario. The following table describes the two types of snapshots.
Snapshot type | Description | Scenario |
Snapshot type | Description | Scenario |
Application-consistent snapshot | When the system is creating application-consistent snapshots, the system suspends write operations to applications to ensure that all existing write operation results are synchronized to cloud disks. This ensures data consistency on cloud disks and in applications such as databases. Application-consistent snapshots are identified by the AppConsistent:True tag. Application-consistent snapshots can be created by using one of the following methods based on the operating system: In Linux, the application-consistent snapshot feature is implemented by using custom shell scripts based on your applications. When you use custom scripts, Alibaba Cloud does not guarantee application consistency. In Windows, built-in features such as Volume Shadow Copy Service (VSS) are provided to implement the application-consistent snapshot feature.
| Application-consistent snapshots are suitable for applications that require data consistency, such as databases and critical business systems, and help quickly restore applications for disaster recovery. |
File system-consistent snapshot | If you do not configure scripts or the scripts contain invalid settings, file system-consistent snapshots are created instead of application-consistent snapshots. File system-consistent snapshots ensure the consistency of file system data at the point in time when the snapshots are created. File system-consistent snapshots are identified by the FsConsistent:True tag. File system-consistent snapshots can be created by using one of the following methods based on the operating system: In Linux, if no application scripts are available, file system-consistent snapshots are created. In Windows, if the Contain Writers by Default option is disabled, file system-consistent snapshots are created.
| File system-consistent snapshots are suitable for scenarios in which overall file system consistency is required, such as file servers and document management systems. The snapshots help meet backup requirements and ensure the integrity of file hierarchies. |
Limits
Application-consistent snapshots are supported only by Enterprise SSDs (ESSDs) for which the multi-attach feature is disabled.
You can create application-consistent snapshots for cloud disks that are attached to the same ECS instance. You cannot create application-consistent snapshots for cloud disks that are attached to different ECS instances.
Prerequisites
An ECS instance is created and runs one of the following operating system versions:
Windows: Windows Server 2012 R2 or later.
Linux: CentOS 7.6 or later, Ubuntu 18.04 or later, or Alibaba Cloud Linux 2.
The ECS instance is in the Running state. Cloud Assistant Agent is installed on the instance. For more information, see Install Cloud Assistant Agent.
If you want to use Alibaba Cloud SDK for Go to create application-consistent snapshots, make sure that the following requirements are met:
Procedure
Create application-consistent snapshots in the ECS console
Create application-consistent snapshots by using Alibaba Cloud SDK for Go
Step 1: Configure a RAM role for an ECS instance
Log on to the Resource Access Management (RAM) console.
Create a RAM role, grant the RAM role permissions to create application-consistent snapshots, and then attach the RAM role to the ECS instance. For more information, see Instance RAM roles. Take note of the following parameters:
RAM Role Name: Specify a name for the RAM role. Example: AppSnapshotRoleName.
Selected Trusted Entity: Select Alibaba Cloud Service.
Policy: Create a custom policy with a specified name. Example: AppSnapshotPolicy. The following sample code provides an example of the policy content:
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ecs:DescribeSnapshot*",
"ecs:CreateSnapshot*",
"ecs:TagResources",
"ecs:DescribeDisks"
],
"Resource": [
"*"
],
"Condition": {}
}
]
}
This policy grants the permissions to query snapshot information, create snapshots, add tags, and query cloud disk information.
Step 2: Create application-consistent snapshots
Linux instance
Windows instance
On a Linux instance, compile custom shell scripts, including prescript.sh and postscript.sh, based on the deployed application to ensure application consistency. The following section describes how to create application-consistent snapshots for a Linux instance.
Prepare the scripts based on the application on the ECS instance and upload the scripts to the ECS instance in advance.
Script | Description | Path | Type | Permissions | Content |
prescript.sh | Used to suspend all write operations to an application before you perform operations that may affect the application. This prevents data inconsistency due to new data generated during the operations. | /tmp/prescript.sh | Shell | Only the root user has read, write, and execute permissions on the scripts. | The script content is compiled based on your business requirements. For more information, see Best practices for creating application-consistent snapshots for a Linux instance on which MySQL is deployed. |
postscript.sh | After an operation that may affect an application, such as data backup, is complete, the postscript.sh script is run to restore the application and resume write operations. | /tmp/postscript.sh |
For information about how to upload files to an ECS instance, see Use Workbench to upload a file to and download a file from an instance.
Note
If the script settings, such as the permissions, storage path, or script name, are invalid, file system-consistent snapshots are created instead of application-consistent snapshots.
Navigate to the Create Snapshot-Consistent Group page.
Log on to the ECS console.
In the left-side navigation pane, choose .
In the top navigation bar, select the region where the ECS instance resides.
Click the Snapshot-consistent Group tab, then click Create Snapshot-consistent Group.
In the Create Snapshot dialog box, configure the parameters described in the following table to create a snapshot-consistent group.
By default, the Resource Type parameter is set to Instance.
Select a Linux instance from the Select Instances drop-down list. Then, in the Select Cloud Disks section, select the ESSDs for which you want to create snapshots from the instance.
Configure the parameters in the Application-consistent Snapshot section.
If you select Enable Application-consistent Snapshot and correctly configure scripts, application-consistent snapshots are created.
If you select Enable Application-consistent Snapshot but do not configure or incorrectly configure scripts, file system-consistent snapshots are created.
Note
If you do not install Cloud Assistant on the instance and you select Enable Application-consistent Snapshot, the required Cloud Assistant plug-in is automatically installed on the instance.
You can also select Enable to enable the File System I/O Suspension and Resume feature and specify a timeout period.
Note
If you enable the File System I/O Suspension and Resume feature, the system suspends I/O operations to file systems when snapshots are being created to prevent data inconsistency caused by ongoing I/O operations. If you set the Timeout Period parameter to a value greater than 16 and the I/O operation suspension is not complete within the period, the I/O operation suspension fails, which may cause data inconsistency issues.
Click OK.
After the snapshot-consistent group is created, a message that contains the Cloud Assistant command ID and the task ID is displayed. You can check whether application-consistent snapshots are created based on the task ID.
Navigate to the Create Snapshot-Consistent Group page.
Log on to the ECS console.
In the left-side navigation pane, choose .
In the top navigation bar, select the region where the ECS instance resides.
Click the Snapshot-consistent Group tab, then click Create Snapshot-consistent Group.
Windows instances use VSS to ensure application consistency. This section describes how to enable the application-consistent snapshot feature for a Windows instance.
Navigate to the Create Snapshot-Consistent Group page.
Log on to the ECS console.
In the left-side navigation pane, choose .
In the top navigation bar, select the region where the ECS instance resides.
Click the Snapshot-consistent Group tab, then click Create Snapshot-consistent Group.
In the Create Snapshot dialog box, configure the parameters described in the following table to create a snapshot-consistent group.
By default, the Resource Type parameter is set to Instance.
Select a Windows instance from the Select Instances drop-down list. Then, in the Select Cloud Disks section, select the ESSDs for which you want to create snapshots from the instance.
Configure the parameters in the Application-consistent Snapshot section.
If you select Enable Application-consistent Snapshot and Contain Writers by Default, application-consistent snapshots are created.
If you select only Enable Application-consistent Snapshot, file system-consistent snapshots are created.
Note
If you do not install Cloud Assistant on the instance and you select Enable Application-consistent Snapshot, the required Cloud Assistant plug-in is automatically installed on the instance.
Click OK.
After the snapshot-consistent group is created, a message that contains the Cloud Assistant command ID and the task ID is displayed. You can check whether application-consistent snapshots are created based on the task ID.
Step 3: Check whether application-consistent snapshots are created
After you create the snapshot-consistent group, go to the ECS Cloud Assistant page and check whether application-consistent snapshots are created. Then, go to the Snapshots page and view information about the snapshot-consistent group and application-consistent snapshots in the group.
On the ECS Cloud Assistant page, check whether application-consistent snapshots are created.
Log on to the ECS console.
In the left-side navigation pane, choose .
Click the Command Execution Result tab.
Find the task ID obtained in the previous step in the Task ID column and click the task ID to view the execution results.

The value of ExitCode is 0
as shown in the preceding figure. This indicates that application-consistent snapshots are created as expected on Cloud Assistant. In this case, the ID of the snapshot-consistent group appears in the command output.
Note
If the value of ExitCode is not 0
, an error occurred. Troubleshoot the issue based on the value in the ExitCode column. For more information, see Error codes.
View information about the created snapshot-consistent group and snapshots in the group.
In the left-side navigation pane, choose .
Click the Snapshot-consistent Groups tab. Find the snapshot-consistent group that you created and click the ID of the snapshot-consistent group to view snapshot details.
In the Snapshot Information section, check whether a snapshot is an application-consistent snapshot or a file system-consistent snapshot based on the tag.
If the AppConsistent:True
tag appears, an application-consistent snapshot is created.

If the FsConsistent:True
tag appears, a file system-consistent snapshot is created.

Step 1: Configure a RAM role for an ECS instance
Call the AttachInstanceRamRole operation to attach a RAM role to the ECS instance. In this example, a RAM role named AppSnapshotRoleName is used.
Sample code of Alibaba Cloud SDK for Go:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
func main() {
client, err := ecs.NewClientWithAccessKey(
"cn-hangzhou",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
request := ecs.CreateAttachInstanceRamRoleRequest()
request.Scheme = "https"
request.RamRoleName = "AppSnapshotRoleName"
request.InstanceIds = "[\"i-bp17r83nppqf141v****\"]"
response, err := client.AttachInstanceRamRole(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Println(response.String())
}
A response similar to the following one is returned. For more information about the parameters in the response, see AttachInstanceRamRole.
Step 2: Call the RunCommand operation to create application-consistent snapshots for the instance
Linux instance
Windows instance
Call the RunCommand operation to use Cloud Assistant to create application-consistent snapshots for one or more Linux instances.
Sample code of Alibaba Cloud SDK for Go:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
func main() {
client, err := ecs.NewClientWithAccessKey(
"cn-hangzhou",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
request := ecs.CreateRunCommandRequest()
request.Scheme = "https"
request.Type = "RunShellScript"
request.CommandContent = "acs-plugin-manager --exec --plugin app-snapshot-plugin --params=-RamRoleName=\"AppSnapshotRoleName\",-EnableFsFreeze=true,-TimeoutInSeconds=30,-PreScriptPath=\"/tmp/prescript.sh\",-PostScriptPath=\"/tmp/postscript.sh\",-ExcludeDiskId=\"\",-Name=\"LinuxApp1\""
request.InstanceId = &[]string{"i-bp17r83nppqf141v****"}
response, err := client.RunCommand(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
Description of the CommandContent parameter:
A response similar to the following one is returned. For information about the parameters in the response, see RunCommand.
Call the RunCommand operation to use Cloud Assistant to create application-consistent snapshots for one or more Windows instances.
Sample code of Alibaba Cloud SDK for Go:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
func main() {
client, err := ecs.NewClientWithAccessKey(
"cn-hangzhou",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
request := ecs.CreateRunCommandRequest()
request.Scheme = "https"
request.Type = "RunBatScript"
request.CommandContent = "acs-plugin-manager --exec --plugin app-snapshot-plugin-win --params=-RamRoleName=\"AppSnapshotRoleName\",-EnableWriters=true,-Description=\"AppSnapshot\",-ExcludeDiskId=\"\",-Name=\"APPSnapshot-1\""
request.InstanceId = &[]string{"i-bp11vqwgh574****"}
request.Timeout = "1800"
response, err := client.RunCommand(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
Description of the CommandContent parameter:
acs-plugin-manager --exec --plugin app-snapshot-plugin-win
: runs the app-snapshot-plugin-win
Cloud Assistant plug-in.
--params=
: the parameters of the plug-in. The following table describes the parameters.
Parameter | Type | Required | Description |
ExcludeDiskId | String | No | The cloud disks to exclude from the snapshots. |
Name | String | Yes | The name of the snapshot-consistent group. |
Description | String | No | The description of the snapshot-consistent group. |
RamRoleName | String | Yes | The RAM role attached to the instance. For information about the name of the RAM role, see Configure a RAM role for an ECS instance. |
EnableWriters | Boolen | No | Specifies whether to create application-consistent snapshots. Valid values: Default value: true. |
A response similar to the following one is returned. For information about the parameters in the response, see RunCommand.
Step 3: Call the DescribeInvocationResults operation to check whether application-consistent snapshots are created
Call the DescribeInvocationResults operation to check whether the Cloud Assistant command is run as expected.
Sample code of Alibaba Cloud SDK for Go:
package main
import (
"fmt"
"os"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)
func main() {
client, err := ecs.NewClientWithAccessKey(
"cn-hangzhou",
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))
request := ecs.CreateDescribeInvocationResultsRequest()
request.Scheme = "https"
request.InvokeId = "t-hz01qsegaxi****"
request.InstanceId = "i-bp17r83nppqf141v****"
request.CommandId = "c-hz01qsegaxd****"
response, err := client.DescribeInvocationResults(request)
if err != nil {
fmt.Print(err.Error())
}
fmt.Printf("response is %#v\n", response)
}
A response similar to the following one is returned. For information about the parameters in the response, see DescribeInvocationResults.
ExitCode indicates the error code. A value of 0
indicates that the command is run as expected. A value other than 0
indicates that an error occurred. Troubleshoot the issue based on the error code. For more information, see Error codes.
Output indicates the command output, which is Base64-encoded.
If the command is run as expected, the value of Output is Base64-encoded and contains the ID of the created snapshot-consistent group, as shown in the following example:
[snapshotgroup="ssg-bp170v57ca9j01jb****"][message="Finish whole Processes of Snapshot successfully"]
Error codes
Error code (ExitCode) | Description |
Error code (ExitCode) | Description |
0 | The application-consistent snapshots are created as expected. |
1 | One or more conditions are not met. One of the following errors may occur: The cloud disk category is not supported. The snapshot name is invalid. Network connectivity issues occurred. No RAM role is attached to the instance. The operating system of the instance is not supported.
|
2 | The type or number of the parameters that follow the --params string is invalid. |
3 | One of the following errors may occur: |
4 | The snapshot-consistent group cannot be created. |
5 | The snapshot-consistent group is not in the expected state. |
6 | The request to create the snapshot-consistent group timed out. |
7 | A disk snapshot in the snapshot-consistent group is not in the expected state. |
8 | Tags cannot be added to the snapshot. |
9 | The postscript.sh script cannot be executed. |
10 | The postscript.sh script cannot be executed. |
11 | I/O operations cannot be suspended. |
12 | I/O operations cannot be resumed. |
13 | No RAM role is attached to the instance. |
14 | The number of snapshots exceeded the upper limit. |
15 | The snapshot is not in the expected state. |
16 | The snapshot cannot be created because the previous snapshot is being created and the instant access feature is disabled. |
255 | An unknown error occurred. |