前置和后置脚本的其他用例 - Amazon EBS
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

前置和后置脚本的其他用例

除了使用前置和后置脚本自动生成应用程序一致性快照外,您还可以同时或单独使用前置和后置脚本,以在创建快照之前或之后自动执行其他管理任务。例如:

  • 创建快照之前使用前置脚本来应用补丁。这可以帮助您在应用每周或每月定期软件更新后创建快照。

    注意

    如果您选择仅运行前置脚本,则默认情况下会启用默认创建崩溃一致性快照

  • 创建快照后使用后置脚本应用补丁。这可以帮助您在应用每周或每月定期软件更新之前创建快照。

其他用例入门

本节介绍将前置和/或后置脚本用于应用程序一致性快照以外的用例中时需要执行的步骤。

为前置和/或后置脚本准备目标实例
  1. 在目标实例上安装 SSM Agent(如果尚未安装)。如果目标实例上已安装 SSM Agent,请跳过此步骤。

  2. 确保 SSM Agent 正在运行。有关更多信息,请参阅正在检查 SSM Agent 状态并启动代理

  3. 为 Amazon EC2 实例设置 Systems Manager。有关更多信息,请参阅《Amazon Systems Manager 用户指南》中的为 Amazon EC2 实例设置 Systems Manager

您必须创建一个 SSM 命令文档,其中包含要运行的命令的前置和/或后置脚本。

您可以使用下面的 SSM 文档空白模板创建 SSM 文档,并在相应的文档部分中添加前置和后置脚本命令。

请注意以下几点:
  • 您负责确保 SSM 文档为工作负载执行正确和必需的操作。

  • SSM 文档必须包含 allowedValues 的必填字段,包括 pre-scriptpost-scriptdry-run。Amazon Data Lifecycle Manager 将根据这些部分的内容在您的实例上执行命令。如果您的 SSM 文档没有这些部分,则 Amazon Data Lifecycle Manager 会将其视为执行失败。

###===============================================================================### # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ###===============================================================================### schemaVersion: '2.2' description: SSM Document Template for Amazon Data Lifecycle Manager Pre/Post script feature parameters: executionId: type: String default: None description: (Required) Specifies the unique identifier associated with a pre and/or post execution allowedPattern: ^(None|[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})$ command: # Data Lifecycle Manager will trigger the pre-script and post-script actions during policy execution. # 'dry-run' option is intended for validating the document execution without triggering any commands # on the instance. The following allowedValues will allow Data Lifecycle Manager to successfully # trigger pre and post script actions. type: String default: 'dry-run' description: (Required) Specifies whether pre-script and/or post-script should be executed. allowedValues: - pre-script - post-script - dry-run mainSteps: - action: aws:runShellScript description: Run Database freeze/thaw commands name: run_pre_post_scripts precondition: StringEquals: - platformType - Linux inputs: runCommand: - | #!/bin/bash ###===============================================================================### ### Error Codes ###===============================================================================### # The following Error codes will inform Data Lifecycle Manager of the type of error # and help guide handling of the error. # The Error code will also be emitted via AWS Eventbridge events in the 'cause' field. # 1 Pre-script failed during execution - 201 # 2 Post-script failed during execution - 202 # 3 Auto thaw occurred before post-script was initiated - 203 # 4 Pre-script initiated while post-script was expected - 204 # 5 Post-script initiated while pre-script was expected - 205 # 6 Application not ready for pre or post-script initiation - 206 ###===============================================================================### ### Global variables ###===============================================================================### START=$(date +%s) # For testing this script locally, replace the below with OPERATION=$1. OPERATION={{ command }} # Add all pre-script actions to be performed within the function below execute_pre_script() { echo "INFO: Start execution of pre-script" } # Add all post-script actions to be performed within the function below execute_post_script() { echo "INFO: Start execution of post-script" } # Debug logging for parameters passed to the SSM document echo "INFO: ${OPERATION} starting at $(date) with executionId: ${EXECUTION_ID}" # Based on the command parameter value execute the function that supports # pre-script/post-script operation case ${OPERATION} in pre-script) execute_pre_script ;; post-script) execute_post_script ;; dry-run) echo "INFO: dry-run option invoked - taking no action" ;; *) echo "ERROR: Invalid command parameter passed. Please use either pre-script, post-script, dry-run." exit 1 # return failure ;; esac END=$(date +%s) # Debug Log for profiling the script time echo "INFO: ${OPERATION} completed at $(date). Total runtime: $((${END} - ${START})) seconds."
注意

如果出现以下情况,则需要执行此步骤:

  • 创建或更新使用自定义 IAM 角色的启用前置/后置脚本的快照策略。

  • 使用命令行创建或更新使用默认值的启用前置/后置脚本的快照策略。

如果您使用控制台创建或更新启用脚本前/后脚本的快照策略,该策略使用默认角色管理快照(AWSDataLifecycleManagerDefaultRole),请跳过此步骤。在这种情况下,我们会自动将AWSDataLifecycleManagerSSMFullAccess策略附加到该角色。

您必须确保用于策略的 IAM 角色授予 Amazon Data Lifecycle Manager 权限,以执行在策略作为目标的实例上运行前置和后置脚本所需的 SSM 操作的权限。

Amazon Data Lifecycle Manager 提供了包含所需权限的托管策略 (AWSDataLifecycleManagerSSMFullAccess)。您可以将此策略附加到您的 IAM 角色以管理快照,从而确保其包含这些权限。

重要

使用前脚本和后置脚本时, AWSDataLifecycleManagerSSMFullAccess 托管策略使用aws:ResourceTag条件键来限制对特定 SSM 文档的访问。要允许 Amazon Data Lifecycle Manager 访问 SSM 文档,您必须确保您的 SSM 文档带有 DLMScriptsAccess:true 标签。

或者,您可以手动创建自定义策略或将所需权限直接分配给您使用的 IAM 角色。您可以使用 AWSDataLifecycleManagerSSMFullAccess 托管策略中定义的相同权限,但是,aws:ResourceTag条件密钥是可选的。如果您决定不使用该条件键,则无需使用 DLMScriptsAccess:true 标记您的 SSM 文档。

使用以下方法之一将AWSDataLifecycleManagerSSMFullAccess策略添加到您的 IAM 角色。

Console
将托管策略附加到您的自定义角色
  1. 通过 https://console.aws.amazon.com/iam/ 打开 IAM 控制台。

  2. 在导航面板中,选择 Roles(角色)。

  3. 搜索并选择用于管理快照的自定义角色。

  4. 权限选项卡上,选择添加权限附加策略

  5. 搜索并选择AWSDataLifecycleManagerSSMFullAccess托管策略,然后选择添加权限

Amazon CLI
将托管策略附加到您的自定义角色

使用 attach-role-policy 命令。对于 ---role-name,请指定您自定义角色的名称。对于 --policy-arn,请指定 arn:aws:iam::aws:policy/AWSDataLifecycleManagerSSMFullAccess

$ aws iam attach-role-policy \ --policy-arn arn:aws:iam::aws:policy/AWSDataLifecycleManagerSSMFullAccess \ --role-name your_role_name
Console
创建快照生命周期策略
  1. 通过以下网址打开 Amazon EC2 控制台:https://console.aws.amazon.com/ec2/

  2. 在导航窗格中,依次选择 Elastic Block StoreLifecycle Manager (生命周期管理器),然后选择 Create lifecycle policy (创建生命周期策略)。

  3. 选择策略类型页面上,选择 EBS 快照策略,然后选择下一步

  4. 目标资源部分中,执行以下操作:

    1. 对于目标资源类型,请选择 Instance

    2. 对于目标资源标签,请指定识别要备份的实例的资源标签。仅备份具有指定标签的资源。

  5. 对于 IAM 角色,可以选择 AWSDataLifecycleManagerDefaultRole(用于管理快照的默认角色),也可以选择您创建并准备使用前脚本和发布脚本的自定义角色。

  6. 根据需要配置计划和其他选项。我们建议您将快照创建时间计划在与您工作负载相匹配的时间段,例如在维护窗口期间。

  7. 前置和后置脚本部分中,选择启用前置和后置脚本,然后执行以下操作:

    1. 选择自定义 SSM 文档

    2. 对于自动化选项,请选择与要运行的脚本相匹配的选项。

    3. 对于 SSM 文档,请选择您准备的 SSM 文档。

  8. 如果需要,请配置以下其他选项:

    • 脚本超时 – 如果脚本运行尝试尚未完成,则在此超时期间后,Amazon Data Lifecycle Manager 的尝试失败。如果脚本未在其超时期间内完成,Amazon Data Lifecycle Manager 的尝试失败。超时期间分别适用于前置和后置脚本。最小的默认超时期间为 10 秒。最长超时期间为 120 秒。

    • 重试失败的脚本 – 选择此选项可重试未在其超时期间内完成的脚本。如果前置脚本失败,则 Amazon Data Lifecycle Manager 会重试整个快照创建过程,包括运行前置和后置脚本。如果后置脚本失败,则 Amazon Data Lifecycle Manager 将仅重试后置脚本;在这种情况下,前置脚本将完成并且可能已创建快照。

    • 默认创建崩溃一致性快照 – 如果前置脚本运行失败,则选择此选项以默认创建崩溃一致性快照。如果未启用前置和后置脚本,则这是 Amazon Data Lifecycle Manager 的默认快照创建行为。如果您启用了重试,则只有在所有重试尝试都用尽之后,Amazon Data Lifecycle Manager 才会默认创建崩溃一致性快照。如果前置脚本失败并且您没有默认创建崩溃一致性快照,则 Amazon Data Lifecycle Manager 将不会在该计划运行期间为实例创建快照。

  9. 选择创建默认策略

    注意

    如果发生 Role with name AWSDataLifecycleManagerDefaultRole already exists 错误,请参阅 故障排除 来了解更多信息。

Amazon CLI
创建快照生命周期策略

使用create-lifecycle-policy命令,并将Scripts参数包含在中CreateRule。有关参数的更多信息,请参阅 Amazon Data Lifecycle Manager API Reference

$ aws dlm create-lifecycle-policy \ --description "policy_description" \ --state ENABLED \ --execution-role-arn iam_role_arn \ --policy-details file://policyDetails.json

其中 policyDetails.json 包含以下内容。

{ "PolicyType": "EBS_SNAPSHOT_MANAGEMENT", "ResourceTypes": [ "INSTANCE" ], "TargetTags": [{ "Key": "tag_key", "Value": "tag_value" }], "Schedules": [{ "Name": "schedule_name", "CreateRule": { "CronExpression": "cron_for_creation_frequency", "Scripts": [{ "Stages": ["PRE" | "POST" | "PRE","POST"], "ExecutionHandlerService":"AWS_SYSTEMS_MANAGER", "ExecutionHandler":"ssm_document_name|arn", "ExecuteOperationOnScriptFailure":true|false, "ExecutionTimeout":timeout_in_seconds (10-120), "MaximumRetryCount":retries (0-3) }] }, "RetainRule": { "Count": retention_count } }] }