如何使用 AWS CLI 命令管理我的快照并为 Lightsail 实例创建备份?

3 分钟阅读
0

我想使用 AWS 命令行界面(AWS CLI)命令管理我的快照并为 Amazon Lightsail 实例创建备份。

解决方案

注意:

管理手动备份

使用 AWS CLI 命令来管理实例的手动备份并列出可用快照。

为 Lightsail 实例创建手动备份

运行 create-instance-snapshot 命令,以创建 Lightsail 实例的快照。以下示例会在 eu-west-1 AWS 区域中创建实例 SnapshotTestLightsailInstance1 的快照:

# aws lightsail create-instance-snapshot --instance-name TestLightsailInstance1 --instance-snapshot-name SnapshotTestLightsailInstance1{
    "operations": [
        {
            "id": "d3196be7-3dc6-4508-b335-16ce45f11c90",
            "resourceName": "SnapshotTestLightsailInstance1",
            "resourceType": "InstanceSnapshot",
            "createdAt": 1602180831.638,
            "location": {
                "availabilityZone": "all",
                "regionName": "eu-west-1"
            },
            "isTerminal": false,
            "operationDetails": "TestLightsailInstance1",
            "operationType": "CreateInstanceSnapshot",
            "status": "Started",
            "statusChangedAt": 1602180831.638
        },
        {
            "id": "df237a33-bca9-4fc3-8f46-ea5d12606f5c",
            "resourceName": "TestLightsailInstance1",
            "resourceType": "Instance",
            "createdAt": 1602180831.638,
            "location": {
                "availabilityZone": "eu-west-1a",
                "regionName": "eu-west-1"
            },
            "isTerminal": false,
            "operationDetails": "SnapshotTestLightsailInstance1",
            "operationType": "CreateInstanceSnapshot",
            "status": "Started",
            "statusChangedAt": 1602180831.638
        }
    ]
}

**注意:**请将 instance-snapshot-nameinstance-nameregion 替换为您的值。

列出可用快照

运行 get-instance-snapshots 命令,以列出您的 Lightsail 实例的所有快照。以下示例显示了 eu-west-1 区域中可用快照的详细信息:

# aws lightsail get-instance-snapshots --region eu-west-1 --query 'instanceSnapshots[].{name:name,createdAt:createdAt,resourceType:resourceType,state:state,fromInstanceName:fromInstanceName,sizeInGb:sizeInGb}' --output table
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                                                GetInstanceSnapshots                                                                                                 |
+----------------+-----------------------------------------+------------------------------------------------------------------------------------------------------------+-------------------+-----------+-------------+
|    createdAt   |            fromInstanceName             |                                                   name                                                     |   resourceType    | sizeInGb  |    state    |
+----------------+-----------------------------------------+------------------------------------------------------------------------------------------------------------+-------------------+-----------+-------------+
|  1602180831.638|  TestLightsailInstance1                 |  SnapshotTestLightsailInstance1                                                                            |  InstanceSnapshot |  40       |  available  |
+----------------+-----------------------------------------+------------------------------------------------------------------------------------------------------------+-------------------+-----------+-------------+

**注意:**请将 region 替换为您的区域。

管理自动备份

使用 AWS CLI 命令来验证您的实例是否已启用自动快照,并启用自动快照。您还可以列出自动快照,并从具有更高捆绑包大小或更高 Lightsail 计划的备份中创建新实例。

验证您的实例是否已启用自动快照

运行以下命令来验证您的实例是否已启用自动快照并显示定义的计划:

# aws lightsail get-instances --region eu-west-1  --query 'instances[].{addOns:addOns,name:name,publicIpAddress:publicIpAddress,AutoMatciSnapshotStatus:(addOns[].status),Schedule:(addOns[].snapshotTimeOfDay)}' --output text| grep -w "TestLightsailInstance1"['Enabled']     ['20:00']       [{'name': 'AutoSnapshot', 'status': 'Enabled', 'snapshotTimeOfDay': '20:00'}]   TestLightsailInstance1  3.250.xx.xx

**注意:**请将 TestLightsailInstance1 替换为您的实例名称,请将 region 替换为您的区域。

启用自动快照

运行 enable-add-on 命令来为您的 Lightsail 实例启用自动快照。以下示例创建了每日自动快照,其设置为每小时增量,以 UTC(08PM UTC)为单位:

# aws lightsail enable-add-on --region eu-west-1 --resource-name TestLightsailInstance1 --add-on-request addOnType=AutoSnapshot,autoSnapshotAddOnRequest={snapshotTimeOfDay=20:00}{
    "operations": [
        {
            "id": "823bb162-9848-4897-b845-8f41c375801a",
            "resourceName": "TestLightsailInstance1",
            "resourceType": "Instance",
            "createdAt": 1602181856.652,
            "location": {
                "availabilityZone": "eu-west-1",
                "regionName": "eu-west-1"
            },
            "isTerminal": false,
            "operationDetails": "EnableAddOn - AutoSnapshot",
            "operationType": "EnableAddOn",
            "status": "Started"
        }
    ]
}

**注意:**请将 resource-namesnapshotTimeOfDayregion 替换为您的值。

列出自动快照,并从具有更高捆绑包尺寸或更高 Lightsail 计划的备份中创建新实例

运行 get-auto-snapshots 命令来列出您的 Lightsail 实例或磁盘的所有可用自动快照。以下示例显示了实例 TestLightsailInstance1 的可用快照的详细信息:

# aws lightsail get-auto-snapshots --region eu-west-1 --resource-name TestLightsailInstance1{
    "resourceName": "TestLightsailInstance1",
    "resourceType": "Instance",
    "autoSnapshots": [
        {
            "date": "2020-10-08",
            "createdAt": 1602188663.0,
            "status": "Success",
            "fromAttachedDisks": []
        }
    ]
}

**注意:**请将 resource-nameregion 替换为您的值。

运行 create-instances-from-snapshot 命令,以从手动或自动备份创建 Lightsail 实例。以下示例使用特定备份和更大尺寸的捆绑包来在 eu-west-1 区域中创建实例:

# aws lightsail create-instances-from-snapshot --region eu-west-1 --instance-snapshot-name SnapshotTestLightsailInstance1 --instance-names RestoredTestLightsailInstance1-New  --availability-zone eu-west-1a --bundle-id large_2_0{
    "operations": [
        {
            "id": "09f7d1bb-90f4-48dc-b304-543499e11208",
            "resourceName": "RestoredTestLightsailInstance1-New",
            "resourceType": "Instance",
            "createdAt": 1602182374.625,
            "location": {
                "availabilityZone": "eu-west-1a",
                "regionName": "eu-west-1"
            },
            "isTerminal": false,
            "operationType": "CreateInstancesFromSnapshot",
            "status": "Started",
            "statusChangedAt": 1602182374.625
        }
    ]
}

**注意:**请将 instance-snapshot-nameinstance-namesbundle-idregion 替换为您的值。

以下是您创建的 Lightsail 实例 RestoredTestLightsailInstance1-New 的详细信息示例:

# aws lightsail get-instances --region eu-west-1 --query 'instances[].{name:name,createdAt:createdAt,blueprintId:blueprintId,blueprintName:blueprintName,publicIpAddress:publicIpAddress}' --output table |grep -i RestoredTestLightsailInstance1-New
|  wordpress      |  WordPress     |  1602182374.625 |  RestoredTestLightsailInstance1-New      |  34.247.xx.xx    |

相关信息

Lightsail 的 AWS CLI 命令参考

Lightsail API 参考

如何使用 AWS CLI 命令管理我的 Lightsail 实例?

Lightsail 文档

AWS 官方
AWS 官方已更新 3 个月前