Alibaba Cloud allows you to create application-consistent snapshots for Elastic Compute Service (ECS) instances by calling API operations or by using ECS SDK for Go. This feature is applicable to scenarios such as batch O&M and custom development. This topic describes how to use ECS SDK for Go to create application-consistent snapshots for Linux and Windows instances.

Prerequisites

  • The Elastic Compute Service (ECS) instance runs one of the following operating systems:
    • Windows: Windows Server 2019, Windows Server 2016, and Windows Server 2012 R2.
    • Linux: CentOS 7.6 or later, Ubuntu 18.04 or later, and Alibaba Cloud Linux 2.1903 LTS 64-bit.
  • The disks of the ECS instance are enhanced SSDs (ESSDs) and the file systems are ext3, ext4, XFS, or NTFS. Network or shared file systems are not supported.
  • The Cloud Assistant client is installed on the instance. For more information, see Install the Cloud Assistant client.
    Note By default, ECS instances created from public images after December 1, 2017 are pre-installed with the Cloud Assistant client.

Background information

The following table describes the differences between file system-consistent snapshots and application-consistent snapshots.
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. This ensures that the application data and database transactions remain consistent. When application-consistent snapshots are used for rollbacks, no data corruption or loss occurs, logs are not rolled back when databases start, and applications start in a consistent state.

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

The application-consistent snapshot feature is implemented in different manners based on the operating system:
  • Windows: The feature is implemented by using Volume Shadow Copy Service (VSS).
  • Linux: The feature is implemented by using custom shell scripts. You must write your own scripts based on the application. 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 the consistency of the file systems. File system-consistent snapshots can be used 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 file system-consistent snapshot feature is implemented based on the operating system:
  • Windows: If no VSS writers are available, file system-consistent snapshots are created by default.
  • Linux: If no application scripts are available, file system-consistent snapshots are created by default.

Preparations

Before you call API operations to create file system- or application-consistent snapshots, make the following preparations:

  1. Create a RAM role for the application-consistent snapshot feature.
    For more information, see Step 1 to Step 4 in Step 1: Configure a RAM role for the ECS instance.
  2. Download and install the Go runtime environment.
    For more information, see Download and install.
  3. Download and install ECS SDK for Go.
    For more information, see Get started.

After the preceding preparations are made, perform the procedure described in the following table based on the operating system of your instance.

Procedure for Linux instances Procedure for Windows instances
  1. Step 1: Configure a RAM role for an ECS instance
  2. Step 2: Call the RunCommand operation to create the file system-consistent snapshots for Linux instances
  3. Step 3: Call the DescribeInvocationResults operation to view the snapshot creation results
  1. Step 1: Configure a RAM role for an ECS instance
  2. Step 2: Call the RunCommand operation to create application-consistent snapshots for Windows instances
  3. Step 3: Call the DescribeInvocationResults operation to view the snapshot creation results

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 ECS SDK for Go:
package main
import (
    "fmt"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
)

func main() {
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",       //Specify the region ID of the instance.
        "<accessKeyId>",     //Specify your AccessKey ID.
        "<accessKeySecret>") //Specify your AccessKey secret.

    //Call the AttachInstanceRamRole operation to attach a 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. Call results

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

Call the RunCommand operation to create file system-consistent snapshots for one or more Linux instances.

Sample code of ECS SDK for Go:
package main

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

func main() {
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",    //Specify the region ID of the instance.
        "<accessKeyId>",  //Specify your AccessKey ID.
        "<accessKeySecret>") //Specify your AccessKey secret.

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

    request.Type = "RunShellScript"
    //For information about the command used to create file system-consistent snapshots, see CommandContent. 
    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 plug-in used to run Cloud Assistant commands.
  • --params= indicates the configuration parameters of the plug-in. The following table describes these 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: enables the instance access feature.
    • false: disables the instance access feature.
    InstantAccessRentationDays Integer No The number of days for which the instance 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 instance snapshot.
    Description String No The description of the instance snapshot.
    RamRoleName String Yes The RAM role attached to the instance. For more information, see Step 1: Configure a RAM role for an ECS instance.
    PreScriptPath String No The path of the application pre-freeze script (tmp/prescript.sh). The prescript.sh script must meet the following conditions:
    • Only the root user is granted read, write, and execute permissions (chmod 700) on the script as the owner.
    • Script content is customized based on applications.
    Note To create application-consistent snapshots for Linux instances, you must specify this parameter. If script configurations such as permissions, save 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 conditions:
    • Only the root user is granted read, write, and execute permissions (chmod 700) on the script as the owner.
    • Script content is customized based on applications.
    Note To create application-consistent snapshots for Linux instances, you must specify this parameter. If script configurations such as permissions, save 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 an instance snapshot.

    Default value: True.

    TimeoutInSeconds Integer No The timeout period for I/O freeze.

    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

Step 2: Call the RunCommand operation to create application-consistent snapshots for Windows instances

Call the RunCommand operation to create application-consistent snapshots for one or more Windows instances.

Sample code of ECS SDK for Go:
package main

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

func main() {
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",       //Specify the region ID of the instance.
        "<accessKeyId>",     //Specify your AccessKey ID.
        "<accessKeySecret>") //Specify your AccessKey secret.

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

    request.Type = "RunBatScript"
    //For information about the command used to create application-consistent snapshots, see CommandContent. 
    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 plug-in used to run Cloud Assistant commands.
  • --params= indicates the configuration parameters of the plug-in. The following table describes these 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: enables the instance access feature.
    • false: disables the instance access feature.
    InstantAccessRentationDays Integer No The number of days for which the instance 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 instance snapshot.
    Description String No The description of the instance snapshot.
    RamRoleName String Yes The RAM role attached to the instance. For more information, see Step 1: Configure a RAM role for an ECS instance.
    EnableWriters Boolen No Specifies whether to create application-consistent snapshots. Valid values:
    • true: creates application-consistent snapshots.
    • false: creates file system-consistent snapshots.
    Default value: true.
A response similar to the following one is displayed. For more information about the parameters in the response, see RunCommand. Response for the Windows instance

Step 3: Call the DescribeInvocationResults operation to view the snapshot creation results

Call the DescribeInvocationResults operation to check whether the Cloud Assistant commands are run.

Sample code of ECS SDK for Go:
package main

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

func main() {
    client, err := ecs.NewClientWithAccessKey(
        "cn-hangzhou",    //Specify the region ID of the instance.
        "<accessKeyId>",  //Specify your AccessKey ID.
        "<accessKeySecret>") //Specify your AccessKey secret.

    //Call the DescribeInvocationResults operation to view the snapshot creation results.
    request := ecs.CreateDescribeInvocationResultsRequest()
    request.Scheme = "https"

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

    response, err := client.DescribeInvocationResults(request)
    if err != nil {
        fmt.Print(err.Error())
    }
    fmt.Printf("response is %#v\n", response)
}
A response to the following one is displayed. For more information about the parameters in the response, see DescribeInvocationResults. Check the results
  • ExitCode indicates the error code. When the value of ExitCode is 0, commands are run in a proper manner. When the value of ExitCode is not 0, an error occurred. Troubleshoot the error based on the returned error code. For more information, see Error codes.
  • Output indicates the command output, which is Base64-encoded.
    If the commands are run in a proper manner, the content of Output is Base64-encoded and contains the ID of the created instance snapshot, 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 may be returned in the ExitCode column. If an application-consistent snapshot cannot be created, you can troubleshoot the error based on the returned error code.
Error code (ExitCode) Description
0 The application-consistent snapshot is created.
1 One or more specified conditions are not met. Examples:
  • The disk category is not supported.
  • The snapshot name is invalid.
  • The network connection fails.
  • No RAM roles are attached to the instance.
  • The operating system of the instance is not supported.
2 The type or number of the parameters that follow --params is invalid.
3 One of the following errors may occur:
  • No ESSDs are attached to the instance.
  • The RAM role attached to the instance has no permissions to call snapshot-related API operations.
4 The instance snapshot cannot be created.
5 The instance snapshot does not run normally.
6 The instance snapshot creation request timed out.
7 A single disk snapshot in the instance snapshot does not run as expected.
8 Tags cannot be added to the created snapshot.
9 The application pre-freeze script cannot be executed.
10 The application post-thaw script cannot be executed.
11 The I/O of file systems cannot be frozen.
12 The I/O of file systems cannot be thawed.
13 No RAM roles are attached to the instance.
14 The number of snapshots exceeds the limit.
15 The created snapshot cannot run normally.
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.