All Products
Search
Document Center

Elastic Compute Service:Create application-consistent snapshots

Last Updated:Apr 01, 2024

The application-consistent snapshot technique can be used to ensure the consistent state for application data when this technique captures the original data stored on disks to create snapshots. This ensures that all ongoing transactions are completed, the data in memory is written to the disks, and applications are in a consistent state. You can use application-consistent snapshots to restore the applications to a consistent state. This prevents a complex log rollback process and enables fast and stable application startup. This topic describes how to create application-consistent snapshots for Linux and Windows instances in the Elastic Compute Service (ECS) console or by using Alibaba Cloud SDK for Go.

Prerequisites

  • The ECS instance based on which you want to create application-consistent snapshots runs one of the following operating systems:

    • Windows: Windows Server 2022, Windows Server 2019, Windows Server 2016, or Windows Server 2012 R2

    • Linux: CentOS 7.6 or later, Ubuntu 18.04 or later, Alibaba Cloud Linux 2, or Alibaba Cloud Linux 3

  • The disks that are attached to the ECS instance are enhanced SSDs (ESSDs) at performance levels 0, 1, 2, or 3 (PL0, PL1, PL2, or PL3 ESSDs) or ESSD AutoPL disks. The file systems that are mounted to the disks are ext3, ext4, XFS, or New Technology File System (NTFS). Network file systems or shared file systems are not supported.

  • Cloud Assistant Agent is installed on the ECS instance. For information about how to install Cloud Assistant Agent, see Install Cloud Assistant Agent.

    Note

    By default, Cloud Assistant Agent is pre-installed on instances that are created from public images after December 1, 2017.

  • The Go runtime environment is downloaded and installed.

    For more information, see Download and install.

  • Alibaba Cloud SDK for Go is downloaded and installed.

    For more information, see Get started with Alibaba Cloud Classic SDK for Go.

Background information

By default, the snapshots that you create are crash-consistent. If you enable the application-consistent snapshot feature when you create a snapshot, the system creates an application-consistent snapshot or a file system-consistent snapshot based on the actual scenario.

Snapshot type

Description

Implementation

Application-consistent snapshot

Application-consistent snapshots back up data stored in memory and database transactions in progress when the snapshots are created to ensure consistency between application system data and database transactions. The application-consistent snapshots can be used for data rollbacks to prevent data corruption and data loss and to ensure that logs are not rolled back when databases start and that applications start in a consistent state.

Application-consistent snapshots are identified by the APPConsistent:True tag.

The implementation methods of the application-consistent snapshot feature vary based on operating systems.

  • For Windows operating systems, the feature is implemented by using Volume Shadow Copy Service (VSS).

  • For Linux operating systems, the feature is implemented by using custom shell scripts. You must write your own scripts based on your applications. When you use custom scripts, Alibaba Cloud does not take responsibility for application consistency issues.

File system-consistent snapshot

If the application-consistent snapshot feature is enabled but relevant conditions are not met, file system-consistent snapshots are created.

File system-consistent snapshots synchronize the memory and disk information of file systems when the snapshots are created and freeze write operations on file systems to ensure file system consistency. You can use file system-consistent snapshots to prevent operating systems from performing disk checks (chkdsk) and file system consistency checks (fsck).

File system-consistent snapshots are identified by the FsConsistent:True tag.

The implementation methods of the file system-consistent snapshot feature vary based on operating systems.

  • For Windows operating systems, file system-consistent snapshots are created by default if no VSS writers are available.

  • For Linux operating systems, file system-consistent snapshots are created by default if no application scripts are available.

Procedure

Create application-consistent snapshots in the ECS console

Step 1: Configure a RAM role for an ECS instance

  1. Log on to the RAM console by using your Alibaba Cloud account.

  2. Create a RAM role for the application-consistent snapshot feature. For more information, see Create a RAM role for a trusted Alibaba Cloud service.

    The following figure shows how to create a RAM role named AppSnapshotRoleName.应用一致性ram角色

  3. Create a permission policy for the application-consistent snapshot feature. For more information, see Create custom policies.

    快照权限

    Create the AppSnapshotPolicy policy to grant permissions to query snapshot details, create snapshots, configure tags, and query disk details. You can use the following policy content:

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "ecs:DescribeSnapshot*",
                    "ecs:CreateSnapshot*",
                    "ecs:TagResources",
                    "ecs:DescribeDisks"
                ],
                "Resource": [
                    "*"
                ],
                "Condition": {}
            }
        ]
    }
  4. Attach the AppSnapshotPolicy policy to the AppSnapshotRoleName RAM role. For more information, see Grant permissions to a RAM role.

    授权

  5. Attach the AppSnapshotRoleName RAM role to the ECS instance. For more information, see Use instance RAM roles to control access to resources.

Step 2: Enable the application-consistent snapshot feature for the ECS instance

Linux instance

On Linux instances, you can customize shell scripts as application pre-freeze and post-thaw scripts based on the applications to achieve application consistency. This section describes how to enable the application-consistent snapshot feature for a Linux instance.

  1. Write the application pre-freeze and post-thaw scripts based on the applications deployed on the Linux instance and upload the scripts to the instance.

    You can use File Transfer Protocol (FTP) or Cloud Assistant to upload the application pre-freeze and post-thaw scripts to the instance.

    • Application pre-freeze script: Grant read, write, and execute permissions on the script only to the root user. Set the storage path of the script to /tmp/prescript.sh.

    • Application post-thaw script: Grant read, write, and execute permissions on the script only to the root user. Set the storage path of the script to /tmp/postscript.sh.

    Important

    If script configurations such as the permissions, storage path, or file name are invalid, file system-consistent snapshots are created.

  2. Go to the Instance page in the ECS console.

    1. Log on to the ECS console.

    2. In the left-side navigation pane, choose Instances & Images > Instances.

    3. In the top navigation bar, select the region where the ECS instance is located.image.png

  3. Find the instance for which you want to enable the application-consistent snapshot feature and choose 更多 > Disk and Image > Create Snapshot-consistent Group in the Actions column.

  4. In the Create Snapshot-consistent Group dialog box, configure the parameters for the snapshot-consistent group.

    1. Select the disks for which you want to create the snapshot-consistent group and configure other snapshot parameters.

    2. Configure the parameters in the Application-consistent Snapshot section.

      • If you select Enable Application-consistent Snapshot and Enable File System Freeze and Thaw and configure valid scripts, application-consistent snapshots are created.

      • If you select Enable Application-consistent Snapshot and Enable File System Freeze and Thaw but do not configure valid scripts, file system-consistent snapshots are created.

      Note

      If the Cloud Assistant plug-in is not installed on your Linux instance, the plug-in is automatically installed on the instance after you select Enable Application-consistent Snapshot.

  5. Then, click OK.

    After you create the snapshot-consistent group, a message indicating the Cloud Assistant command ID and the task ID (InvokeId) is displayed, as shown in the following figure. You can check whether application-consistent snapshots are created based on the task ID.命令执行id

Windows instance

For Windows instances, you can use VSS to implement application consistency. This section describes how to enable the application-consistent snapshot feature for a Windows instance.

  1. Go to the Instance page in the ECS console.

    1. Log on to the ECS console.

    2. In the left-side navigation pane, choose Instances & Images > Instances.

    3. In the top navigation bar, select the region where the ECS instance is located.image.png

  2. Find the instance for which you want to enable the application-consistent snapshot feature and choose 更多 > Disk and Image > Create Snapshot-consistent Group in the Actions column.

  3. In the Create Snapshot-consistent Group dialog box, configure the parameters for the snapshot-consistent group.

    1. Select the disks for which you want to create the snapshot-consistent group and configure other snapshot parameters.

    2. 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 the Cloud Assistant plug-in is not installed on your instance, the plug-in is automatically installed on the instance after you select Enable Application-consistent Snapshot.

  4. Then, click OK.

    After the snapshot-consistent group is created, a message indicating the Cloud Assistant command ID and the task ID appears, as shown in the following figure. You can check whether application-consistent snapshots are created based on the task ID.命令执行id

Step 3: Check whether application-consistent snapshots are created

After you perform the preceding steps, you can go to the ECS Cloud Assistant page in the ECS console to check whether application-consistent snapshots are created and go to the Snapshots page in the ECS console to view the details of the created snapshot-consistent group and disk snapshots.

  1. On the ECS Cloud Assistant page, check whether application-consistent snapshots are created.

    1. Log on to the ECS console.

    2. In the left-side navigation pane, choose Maintenance & Monitoring > Cloud Assistant.

    3. Click the Command Execution Result tab.

    4. Find the task ID obtained in the previous step in the Task ID column and click the task ID to view the execution results.

      image.png

      If application-consistent snapshots are created, the return value of the ExitCode parameter is 0 and the ID of the snapshot-consistent group is displayed in the command output, as shown in the preceding figure.

      Note

      If the return value of the ExitCode parameter is not 0, an error occurred. Troubleshoot the error based on the error code in the ExitCode column. For more information, see the Error codes section in this topic.

  2. On the Snapshots page, view the details of the created snapshot-consistent group and disk snapshots.

    1. In the left-side navigation pane, choose Storage & Snapshots > Snapshots.

    2. Click the Snapshot-consistent Groups tab, find the snapshot-consistent group that you created, and then click the ID of the snapshot-consistent group to view the details of the snapshot-consistent group.

    3. In the Snapshot Information section, click the ID of a disk snapshot.

      image.png

    4. In the Basic Information section, check whether the disk snapshot is an application-consistent or file system-consistent snapshot based on the tags of the snapshot.

      • Application-consistent snapshots are identified by the APPConsistent:True tag.

        image.png

      • File system-consistent snapshots are identified by the FsConsistent:True tag.

        image.png

Create application-consistent snapshots by using SDK for Go

Step 1: Configure a RAM role for an ECS instance

Call the AttachInstanceRamRole operation to attach the AppSnapshotRoleName RAM role to the ECS instance.

Sample code of SDK for Go:

package main
import (
    "fmt"
    "os"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
		// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
    // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources in your account is compromised. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. We recommend that you use STS tokens, which provide higher security. 
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",       
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),     
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) 

    // Call the AttachInstanceRamRole operation to attach the specified RAM role to the instance.
    request := ecs.CreateAttachInstanceRamRoleRequest()
    request.Scheme = "https"

    request.RamRoleName = "AppSnapshotRoleName"          // Specify the name of the RAM role.
    request.InstanceIds = "[\"i-bp17r83nppqf141v****\"]" // Specify the ID of the instance.

    response, err := client.AttachInstanceRamRole(request)
    if err != nil {
        fmt.Print(err.Error())
    }

    fmt.Println(response.String())
}

A response similar to the following one is displayed. For more information about the parameters in the response, see AttachInstanceRamRole.调用结果

Step 2: Call the RunCommand operation to create file system-consistent snapshots

Linux instance

Call the RunCommand operation, which is related to Cloud Assistant, to create file system-consistent snapshots for one or more Linux instances.

Sample code of SDK for Go:

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
		// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
    // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources in your account is compromised. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. We recommend that you use STS tokens, which provide higher security. 
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",    // Specify the region ID of the instance.
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),     
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))

    // Call the RunCommand operation to create file system-consistent snapshots for the instance. 
    request := ecs.CreateRunCommandRequest()
    request.Scheme = "https"

    request.Type = "RunShellScript"
    // You can view the command used to create file system-consistent snapshots in the CommandContent value. 
    request.CommandContent = "acs-plugin-manager --exec --plugin app-snapshot-plugin --params=-RamRoleName=\"AppSnapshotRoleName\",-InstantAccess=true,-EnableFsFreeze=true,-TimeoutInSeconds=30,-PreScriptPath=\"/tmp/prescript.sh\",-PostScriptPath=\"/tmp/postscript.sh\",-InstantAccessRentationDays=1,-ExcludeDiskId=\"\",-Name=\"LinuxApp1\""
    request.InstanceId = &[]string{"i-bp17r83nppqf141v****"} // Specify the ID of the instance.

    response, err := client.RunCommand(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf("response is %#v\n", response)
}

Components of the CommandContent value:

  • acs-plugin-manager --exec --plugin app-snapshot-plugin: indicates the app-snapshot-plugin Cloud Assistant plug-in.

  • --params= indicates the parameters of the plug-in. The following table describes the parameters.

    Parameter

    Type

    Required

    Description

    ExcludeDiskId

    String

    No

    The disks to exclude from the snapshots.

    InstantAccess

    Boolean

    No

    Specifies whether to enable the instant access feature. Valid values:

    • true

    • false

    Note

    Starting 11:00 on October 12, 2023, the instant access feature is upgraded. New snapshots of ESSDs at performance levels 0, 1, 2, and 3 and ESSD AutoPL disks are immediately available after creation without the need for additional configurations. For more information, see Use the instant access feature.

    InstantAccessRentationDays

    Integer

    No

    The number of days for which the instant access feature remains enabled. The instant access feature is automatically disabled when the specified duration ends.

    Valid values: 1 to 65536. Default value: 1.

    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 more information, see the Step 1: Configure a RAM role for an ECS instance section of this topic.

    PreScriptPath

    String

    No

    The path of the application pre-freeze script (tmp/prescript.sh). The prescript.sh script must meet the following requirements:

    • Only the root user is granted the read, write, and execute permissions (chmod 700) on the script as the owner.

    • The script is customized based on your application.

    Note

    To create application-consistent snapshots for Linux instances, specify this parameter. If script configurations such as the permissions, storage path, or file name are invalid, file system-consistent snapshots are created.

    PostScriptPath

    String

    No

    The path of the application post-thaw script (/tmp/postscript.sh). The postscript.sh script must meet the following requirements:

    • Only the root user is granted the read, write, and execute permissions (chmod 700) on the script as the owner.

    • The script is customized based on your application.

    Note

    To create application-consistent snapshots for Linux instances, specify this parameter. If script configurations such as the permissions, storage path, or file name are invalid, file system-consistent snapshots are created.

    EnableFsFreeze

    Boolean

    No

    Specifies whether to enable Linux fsfreeze to put file systems into the read-only state before you create snapshots.

    Default value: True.

    TimeoutInSeconds

    Integer

    No

    The I/O freeze timeout period.

    Default value: 30. Unit: seconds.

    ScriptTimeoutInSeconds

    Integer

    No

    The timeout period for script execution.

    Default value: 1800. Unit: seconds.

A response similar to the following one is displayed. For more information about the parameters in the response, see RunCommand.linux

Windows instance

Call the RunCommand operation, which is related to Cloud Assistant, to create application-consistent snapshots for one or more Windows instances.

Sample code of SDK for Go:

package main

import (
    "fmt"
  	"os"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
    // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
    // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources in your account is compromised. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. We recommend that you use STS tokens, which provide higher security. 
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",       // Specify the region ID of the instance.
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),     
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"))

    request := ecs.CreateRunCommandRequest()
    request.Scheme = "https"

    request.Type = "RunBatScript"
    // You can view the command used to create application-consistent snapshots in the CommandContent value. 
    request.CommandContent = "acs-plugin-manager --exec --plugin app-snapshot-plugin-win --params=-RamRoleName=\"AppSnapshotRoleName\",-InstantAccess=true,-EnableWriters=true,-Description=\"AppSnapshot\",-InstantAccessRentationDays=1,-ExcludeDiskId=\"\",-Name=\"APPSnapshot-1\""
    request.InstanceId = &[]string{"i-bp11vqwgh574****"} // Specify the ID of the instance.
    request.Timeout = "1800"

    response, err := client.RunCommand(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf("response is %#v\n", response)
}

Components of the CommandContent value:

  • acs-plugin-manager --exec --plugin app-snapshot-plugin-win: indicates the app-snapshot-plugin-win Cloud Assistant plug-in.

  • --params= indicates the parameters of the plug-in. The following table describes the parameters.

    Parameter

    Type

    Required

    Description

    ExcludeDiskId

    String

    No

    The disks to exclude from the snapshots.

    InstantAccess

    Boolean

    No

    Specifies whether to enable the instant access feature. Valid values:

    • true

    • false

    Note

    Starting 11:00 on October 12, 2023, the instant access feature is upgraded. New snapshots of ESSDs at performance levels 0, 1, 2, and 3 and ESSD AutoPL disks are immediately available after creation without the need for additional configurations. For more information, see Use the instant access feature.

    InstantAccessRentationDays

    Integer

    No

    The number of days for which the instant access feature remains enabled. The instant access feature is automatically disabled when the specified duration ends.

    Valid values: 1 to 65536. Default value: 1.

    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 more information, see the Step 1: Configure a RAM role for an ECS instance section of this topic.

    EnableWriters

    Boolen

    No

    Specifies whether to create application-consistent snapshots. Valid values:

    • true

    • false

    Default value: true.

A response similar to the following one is displayed. For more information about the parameters in the response, see RunCommand.win查询结果

Step 3: Call the DescribeInvocationResults operation to check whether snapshots are created

Call the DescribeInvocationResults operation to check whether the Cloud Assistant command is run.

Sample code of SDK for Go:

package main

import (
    "fmt"
    "os"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
    // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured in the code runtime. 
    // If the project code is leaked, the AccessKey pair may be leaked and the security of all resources in your account is compromised. The following sample code shows how to use environment variables to obtain an AccessKey pair and use the AccessKey pair to call API operations. We recommend that you use STS tokens, which provide higher security. 
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",    // Specify the region ID of the instance.
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),     
        os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) 

    // Call the DescribeInvocationResults operation to check whether snapshots are created.
    request := ecs.CreateDescribeInvocationResultsRequest()
    request.Scheme = "https"

    request.InvokeId = "t-hz01qsegaxi****"        // The task ID of the command. You can obtain the task ID in the response in the previous step. 
    request.InstanceId = "i-bp17r83nppqf141v****" // The instance ID.
    request.CommandId = "c-hz01qsegaxd****"       // The command ID. You can obtain the command ID in the response in the previous step. 

    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 displayed. For more information about the parameters in the response, see DescribeInvocationResults.查看结果

  • ExitCode indicates the error code. A value of 0 indicates that the command was run successfully. A numeric value other than 0 indicates that an error occurred. Troubleshoot the issue based on the error code. For more information, see the Error codes section of this topic.

  • Output indicates the command output, which is Base64-encoded.

    If the command was run successfully, 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

After you perform the preceding operations to create an application-consistent snapshot, an error code (ExitCode) may be returned. If an application-consistent snapshot cannot be created, you can troubleshoot the error based on the returned error code. The following table describes the common error codes.

Error code (ExitCode)

Description

0

The application-consistent snapshot is created.

1

One or more conditions are not met. Examples:

  • The 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

Examples:

  • No ESSDs are attached to the instance.

  • The RAM role attached to the instance does not have permissions to call snapshot-related API operations.

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 application pre-freeze script cannot be executed.

10

The application post-thaw script cannot be executed.

11

The I/O of the file systems cannot be frozen.

12

The I/O of the file systems cannot be thawed.

13

No RAM role is attached to the instance.

14

The number of snapshots exceeds 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 has occurred.

References

You can create application-consistent snapshots for MySQL or SQL Server databases. For more information, see Best practices for creating application-consistent snapshots for a Linux instance on which MySQL is deployed and Best practice for creating application-consistent snapshots for a Windows instance on which SQL Server is deployed.