All Products
Search
Document Center

CloudOps Orchestration Service:ACS-ECS-RestoreDataWithSnapshot

Last Updated:Nov 25, 2024

Template name

ACS-ECS-RestoreDataWithSnapshot

Execute Now

Template description

Uses a snapshot to restore the data of a disk.

Template type

Automated

Owner

Alibaba Cloud

Input parameters

Parameter

Description

Type

Required

Default value

Limit

regionId

The region ID.

String

Yes

snapshotId

The snapshot ID.

String

Yes

instanceId

The ID of the Elastic Compute Service (ECS) instance.

String

Yes

retainTempDisk

Specifies whether to retain the temporary disk.

Boolean

No

False

workingDir

The directory in which the script is run.

String

No

/root

OOSAssumeRole

The Resource Access Management (RAM) role that is assumed by CloudOps Orchestration Service (OOS).

String

No

""

Output parameters

Parameter

Description

Type

mountDiskOutputs

List

unmountDiskOutputs

List

Permission policy that is required to execute the template

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:AttachDisk",
                "ecs:CreateDisk",
                "ecs:DeleteDisk",
                "ecs:DescribeDisks",
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:DetachDisk",
                "ecs:ModifyDiskAttribute",
                "ecs:RunCommand"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

References

For more information, see ACS-ECS-RestoreDataWithSnapshot.yml at GitHub.

Template content

FormatVersion: OOS-2019-06-01
Description:
  en: Use snapshot to create a temporary cloud disk to restore data
  zh-cn: the description in Chinese
  name-en: ACS-ECS-RestoreDataWithSnapshot
  name-zh-cn: the description in Chinese
  categories:
    - instance_manage
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: the description in Chinese
    AssociationProperty: RegionId
  snapshotId:
    Type: String
    Label:
      en: SnapshotId
      zh-cn: the description in Chinese
    AssociationProperty: ALIYUN::ECS::Snapshot::SnapshotId
  instanceId:
    Type: String
    Label:
      en: InstanceId
      zh-cn: the description in Chinese
    AssociationProperty: ALIYUN::ECS::Instance::InstanceId
    AssociationPropertyMetadata:
      RegionId: regionId
      Status: Running
  retainTempDisk:
    Type: Boolean
    Label:
      en: RetainTempDisk
      zh-cn: the description in Chinese
    Description:
      en: 'Whether to retain the temporary cloud disk. If you want to retain the temporary cloud disk, select Yes to use it as a common cloud disk. You can manually unmount and release the cloud disk when it is not needed'
      zh-cn: the description in Chinese
    Default: false
  workingDir:
    Type: String
    Label:
      en: WorkingDir
      zh-cn: the description in Chinese
    Description:
      en: 'Root permission is required to execute this script. Linux instances: under the home directory of the administrator (root user): /root. Windows instances: do not support OOS script execution. Manually create a temporary cloud disk to recover data'
      zh-cn: the description in Chinese   
    Default: /root
  OOSAssumeRole:
    Type: String
    Label:
      en: OOSAssumeRole
      zh-cn: the description in Chinese
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: checkInstanceStatus
    Action: ACS::CheckFor
    Description:
      en: Check instance status
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: DescribeInstances
      DesiredValues:
        - Running
      Parameters:
        RegionId: '{{ regionId }}'
        InstanceIds:
          - '{{ instanceId }} '
      PropertySelector: Instances.Instance[].Status
    Outputs:
      zoneId:
        Type: String
        ValueSelector: Instances.Instance[].ZoneId
  - Name: createDiskBySnapshot
    Action: ACS::ExecuteApi
    Description:
      en: Use the snapshot to create a temporary cloud disk
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: CreateDisk
      Parameters:
        RegionId: '{{ regionId }}'
        ZoneId: '{{ checkInstanceStatus.zoneId }}'
        SnapshotId: '{{ snapshotId}}'
        DiskName: TempDiskFrom-{{ snapshotId }}
        DiskCategory: cloud_essd
    Outputs:
      diskId:
        Type: String
        ValueSelector: .DiskId
  - Name: modifyDiskAttribute
    Action: ACS::ExecuteApi
    Description:
      en: Modify temporary cloud disk attributes and release them with the instance
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: ModifyDiskAttribute
      Parameters:
        DiskId: '{{ createDiskBySnapshot.diskId }}'
        DeleteWithInstance: true
  - Name: attachDisk
    Action: ACS::ExecuteApi
    Description:
      en: Mount the temporary cloud disk to the ECS instance
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: AttachDisk
      Parameters:
        InstanceId: '{{ instanceId }}'
        DiskId: '{{ createDiskBySnapshot.diskId }}'
  - Name: waitForDiskStatusInUse
    Action: ACS::WaitFor
    Description:
      en: Wait until the cloud disk status changes to In_use
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: DescribeDisks
      Parameters:
        DiskIds:
          - '{{ createDiskBySnapshot.diskId }}'
      PropertySelector: Disks.Disk[].Status
      DesiredValues:
        - In_use
    Retries: 15
    DelayType: Constant
    Delay: 1
  - Name: mountDisk
    Action: ACS::ECS::RunCommand
    Description:
      en: Mount the cloud disk in the ECS instance
      zh-cn: the description in Chinese
    Properties:
      commandType: RunShellScript
      workingDir: '{{ workingDir }}'
      instanceId: '{{ instanceId }}'
      regionId: '{{ regionId }}'
      commandContent: |
        echo "Query the disk and partition"
        fdisk -l
        file_system_by_oos=`blkid | tail -n 1 | awk -F : '{print $1}'`
        file_system_type=`blkid | tail -n 1 | awk '{print $4}' | awk -F \" '{print $2}'`
        echo "File system type: ${file_system_type}"
        if [ ${file_system_type} == "xfs" ]; then 
            echo "Modify the UUID of an xfs file system"
            xfs_repair -L ${file_system_by_oos}
            xfs_admin -U generate ${file_system_by_oos}
        else
            echo "Modify the UUID of an ext2, ext3, or ext4 file system"
            e2fsck -y -f ${file_system_by_oos}
            uuidgen | xargs tune2fs ${file_system_by_oos} -U
        fi
        blkid
        echo "Configure the /etc/fstab file and mount the partition"
        cp /etc/fstab /etc/fstab.bakbyoos -n
        mkdir -p /mntbyoos
        echo `blkid ${file_system_by_oos} | awk '{print $2}' | sed 's/\"//g'` /mntbyoos ${file_system_type} defaults 0 0 >> /etc/fstab
        cat /etc/fstab
        mount ${file_system_by_oos}
        echo "Query the results"
        df -h
      windowsPasswordName: ''
      enableParameter: false
      parameters: {}
      timeout: 600
      username: ''
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
  - Name: whetherRetainTempDisk
    Action: ACS::Choice
    Description:
      en: Choose next task by retainTempDisk Chosen
      zh-cn: the description in Chinese
    Properties:
      DefaultTask: waitForUserRestoreData
      Choices:
        - When:
            Fn::Equals:
              - true
              - '{{ retainTempDisk }}'
          NextTask: ACS::END
  - Name: waitForUserRestoreData
    Action: ACS::Pause
    Description:
      en: Wait for the user to manually restore the required data
      zh-cn: the description in Chinese
  - Name: unmountDisk
    Action: ACS::ECS::RunCommand
    Description:
      en: Unmount the cloud disk in the ECS instance
      zh-cn: the description in Chinese
    Properties:
      commandType: RunShellScript
      workingDir: '{{ workingDir }}'
      instanceId: '{{ instanceId }}'
      regionId: '{{ regionId }}'
      commandContent: |
        echo "Query the partition"
        df -h
        echo "Unmount the temporary partition and modify the /etc/fstab file"
        file_system_by_oos=`df -h | grep /mntbyoos | awk '{print $1}'`
        umount ${file_system_by_oos}
        sed -i '/\/mntbyoos/d' /etc/fstab
        rm -rf /mntbyoos
        echo "Query the results"
        cat /etc/fstab
        df -h
      windowsPasswordName: ''
      enableParameter: false
      parameters: {}
      timeout: 600
      username: ''
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
  - Name: detachDisk
    Action: ACS::ExecuteApi
    Description:
      en: Unmount the temporary cloud disk from the ECS instance
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: DetachDisk
      Parameters:
        InstanceId: '{{ instanceId }}'
        DiskId: '{{ createDiskBySnapshot.diskId }}'
  - Name: waitForDiskStatusAvailable
    Action: ACS::WaitFor
    Description:
      en: Wait until the cloud disk status changes to Available
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: DescribeDisks
      Parameters:
        DiskIds:
          - '{{ createDiskBySnapshot.diskId }}'
      PropertySelector: Disks.Disk[].Status
      DesiredValues:
        - Available
    Retries: 15
    DelayType: Constant
    Delay: 1
  - Name: deleteDisk
    Action: ACS::ExecuteApi
    Description:
      en: Release the temporary cloud disk
      zh-cn: the description in Chinese
    Properties:
      Service: ECS
      API: DeleteDisk
      Parameters:
        DiskId: '{{ createDiskBySnapshot.diskId }}'
Outputs:
  mountDiskOutputs:
    Type: List
    Value: '{{ mountDisk.commandOutput }}'
  unmountDiskOutputs:
    Type: List
    Value: '{{ unmountDisk.commandOutput }}'
Metadata:
  ALIYUN::OOS::Interface:
    ParameterGroups:
      - Parameters:
          - regionId
          - snapshotId
          - instanceId
        Label:
          default:
            zh-cn: the description in Chinese
            en: Select Instances
      - Parameters:
          - retainTempDisk
          - workingDir
        Label:
          default:
            zh-cn: the description in Chinese
            en: Configure Parameters
      - Parameters:
          - OOSAssumeRole
        Label:
          default:
            zh-cn: the description in Chinese
            en: Control Options