すべてのプロダクト
Search
ドキュメントセンター

CloudOps Orchestration Service:スナップショットを使用したデータの復元

最終更新日:Jan 17, 2025

テンプレート名

ACS-ECS-RestoreDataWithSnapshot

今すぐ実行

テンプレートの説明

スナップショットを使用してディスクのデータを復元します。

テンプレートタイプ

自動化

所有者

Alibaba Cloud

入力パラメーター

パラメーター

説明

タイプ

必須

デフォルト値

制限

regionId

リージョン ID。

String

はい

snapshotId

スナップショット ID。

String

はい

instanceId

Elastic Compute Service ( ECS ) インスタンスの ID。

String

はい

retainTempDisk

一時ディスクを保持するかどうかを指定します。

Boolean

いいえ

False

workingDir

スクリプトを実行するディレクトリ。

String

いいえ

/root

OOSAssumeRole

CloudOps Orchestration Service ( OOS ) によって引き受けられる Resource Access Management ( RAM ) ロール。

String

いいえ

""

出力パラメーター

パラメーター

説明

タイプ

mountDiskOutputs

List

unmountDiskOutputs

List

テンプレートを実行するために必要な権限ポリシー

{
    "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"
        }
    ]
}

参照

詳細については、GitHub のACS-ECS-RestoreDataWithSnapshot.yml をご参照ください。

テンプレートコンテンツ

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: インスタンスの選択
      - Parameters:
          - retainTempDisk
          - workingDir
        Label:
          default:
            zh-cn: the description in Chinese
            en: パラメーターの設定
      - Parameters:
          - OOSAssumeRole
        Label:
          default:
            zh-cn: the description in Chinese
            en: 制御オプション