全部產品
Search
文件中心

CloudOps Orchestration Service:ACS-ECS-RestoreDataWithSnapshot

更新時間:Sep 06, 2024

模板名稱

ACS-ECS-RestoreDataWithSnapshot 使用快照恢複資料

立即執行

模板描述

使用快照建立臨時雲端硬碟恢複資料

模板類型

自動化

所有者

Alibaba Cloud

輸入參數

參數名稱

描述

類型

是否必填

預設值

約束

regionId

地區ID

String

snapshotId

快照ID

String

instanceId

執行個體ID

String

retainTempDisk

是否保留臨時雲端硬碟

Boolean

False

workingDir

運行目錄

String

/root

OOSAssumeRole

OOS扮演的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"
        }
    ]
}

詳情

ACS-ECS-RestoreDataWithSnapshot詳情

模板內容

FormatVersion: OOS-2019-06-01
Description:
  en: Use snapshot to create a temporary cloud disk to restore data
  zh-cn: 使用快照建立臨時雲端硬碟恢複資料
  name-en: ACS-ECS-RestoreDataWithSnapshot
  name-zh-cn: 使用快照恢複資料
  categories:
    - instance_manage
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: 地區ID
    AssociationProperty: RegionId
  snapshotId:
    Type: String
    Label:
      en: SnapshotId
      zh-cn: 快照ID
    AssociationProperty: ALIYUN::ECS::Snapshot::SnapshotId
  instanceId:
    Type: String
    Label:
      en: InstanceId
      zh-cn: 執行個體ID
    AssociationProperty: ALIYUN::ECS::Instance::InstanceId
    AssociationPropertyMetadata:
      RegionId: regionId
      Status: Running
  retainTempDisk:
    Type: Boolean
    Label:
      en: RetainTempDisk
      zh-cn: 是否保留臨時雲端硬碟
    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: 是否保留臨時雲端硬碟,如果您希望保留臨時雲端硬碟,可以選擇“是”,將其作為普通雲端硬碟使用,不需要時您可以手動卸載並釋放該雲端硬碟
    Default: false
  workingDir:
    Type: String
    Label:
      en: WorkingDir
      zh-cn: 運行目錄
    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: 執行該指令碼需要root許可權。Linux系統執行個體預設在管理員(root使用者)的home目錄下,即/root。Windows系統執行個體不支援OOS指令碼執行,請手動建立臨時雲端硬碟恢複資料
    Default: /root
  OOSAssumeRole:
    Type: String
    Label:
      en: OOSAssumeRole
      zh-cn: OOS扮演的RAM角色
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: checkInstanceStatus
    Action: ACS::CheckFor
    Description:
      en: Check instance status
      zh-cn: 檢查執行個體狀態
    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: 使用快照建立一塊臨時雲端硬碟
    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: 修改臨時雲端硬碟屬性,隨執行個體釋放
    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: 將臨時雲端硬碟掛載到ECS執行個體上
    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: 等待雲端硬碟狀態變為使用中
    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: 在ECS執行個體中掛載雲端硬碟
    Properties:
      commandType: RunShellScript
      workingDir: '{{ workingDir }}'
      instanceId: '{{ instanceId }}'
      regionId: '{{ regionId }}'
      commandContent: |
        echo "查看磁碟和分區"
        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}"
        if [ ${file_system_type} == "xfs" ]; then 
            echo "修改xfs檔案系統的UUID"
            xfs_repair -L ${file_system_by_oos}
            xfs_admin -U generate ${file_system_by_oos}
        else
            echo "修改ext2、ext3或ext4檔案系統的UUID"
            e2fsck -y -f ${file_system_by_oos}
            uuidgen | xargs tune2fs ${file_system_by_oos} -U
        fi
        blkid
        echo "配置/etc/fstab檔案並掛載分區"
        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 "查看結果"
        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: 根據是否保留臨時雲端硬碟選擇下一步任務
    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: 等待使用者手動恢複所需資料
  - Name: unmountDisk
    Action: ACS::ECS::RunCommand
    Description:
      en: Unmount the cloud disk in the ECS instance
      zh-cn: 在ECS執行個體中卸載雲端硬碟
    Properties:
      commandType: RunShellScript
      workingDir: '{{ workingDir }}'
      instanceId: '{{ instanceId }}'
      regionId: '{{ regionId }}'
      commandContent: |
        echo "查看分區"
        df -h
        echo "卸載臨時分區並修改/etc/fstab檔案"
        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 "查看結果"
        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: 從ECS執行個體上卸載臨時雲端硬碟
    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: 等待雲端硬碟狀態變為待掛載
    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: 釋放臨時雲端硬碟
    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: 選擇執行個體
            en: Select Instances
      - Parameters:
          - retainTempDisk
          - workingDir
        Label:
          default:
            zh-cn: 配置參數
            en: Configure Parameters
      - Parameters:
          - OOSAssumeRole
        Label:
          default:
            zh-cn: 進階選項
            en: Control Options