如何使用 AWS CLI 命令管理我的快照并为 Lightsail 实例创建备份?
上次更新日期:2021 年 1 月 11 日
我想使用 AWS 命令行界面 (AWS CLI) 命令管理我的快照并为我的 Amazon Lightsail 实例创建备份。我该如何操作?
简短描述
有关 Amazon Lightsail AWS CLI 命令的列表,请参阅 AWS CLI 命令参考和 Amazon Lightsail API 参考。
以下是在 Lightsail 实例上使用 AWS CLI 命令管理快照和备份的情景:
- 管理手动备份:
为您的实例创建手动备份。
列出可用的快照。 - 管理自动快照:
验证是否在您的实例上启用了自动快照。
启用自动快照。
列出自动快照并从具有更高捆绑包大小或更高 Lightsail 计划的备份中创建新实例。
重要提示:使用 AWS CLI 命令时请记住以下事项:
- 如果在运行 AWS CLI 命令时收到错误,请确保您使用的是最新版本的 AWS CLI。
- 默认的 AWS CLI 输出为 JSON。您可以使用默认值,也可以将 --output json 附加到命令以接收输出,如以下示例所示。有关更多信息,请参阅控制 AWS CLI 的命令输出。
- 有关解决 AWS CLI 错误的一般信息,请参阅运行 AWS CLI 命令时为什么会收到错误?
- AWS CLI 输出以 Unix Epoch 时间格式显示时间戳。使用以下一种方法将时间戳转换成 UTC:
macOS:
从时间戳中删除小数点和小数点右侧的任何数字,然后运行以下命令:
# date -r 1602175741 -u
Thu Oct 8 16:49:01 UTC 2020
Linux:
运行以下命令:
# date -d @1602175741.603 -u
Thu Oct 8 16:49:01 UTC 2020
Windows:
使用转换器对时间戳进行转换,如 epochconverter.com。
解决方法
管理手动备份
为 Lightsail 实例创建手动备份
运行 create-instance-snapshot 命令以创建 Lightsail 实例的快照。
以下示例在 eu-west-1 区域中创建实例 SnapshotTestLightsailInstance1 的快照。将 --instance-snapshot-name、--instance-name 和 --region 替换为您的请求中的相应值。
# 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
}
]
}
列出可用的快照
运行 get-instance-snapshots 命令以列出 Lightsail 实例的所有快照。以下示例显示了 eu-west-1 中可用快照的详细信息。将 --region 替换为您的请求中的相应值。
# 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 |
+----------------+-----------------------------------------+------------------------------------------------------------------------------------------------------------+-------------------+-----------+-------------+
管理自动快照
验证是否在您的实例上启用了自动快照
运行以下命令以验证您的实例是否启用了自动快照并显示了定义的计划。将 TestLightsailInstance1 替换为您的实例名称,并将 --region 替换为适当的区域。
# 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
启用自动快照
运行 enable-add-on 命令以为您的 Lightsail 实例启用自动快照。以下示例创建每日自动快照,设置为每小时增量,以 UTC (08PM UTC) 为单位。将 --resource-name, snapshotTimeOfDay 和 --region 替换为您的请求中的相应值。
# 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"
}
]
}
列出自动快照并从具有更高捆绑包大小或更高 Lightsail 计划的备份中创建新实例
1.FSP 运行命令 get-auto-snapshots 以为您的 Lightsail 实例或磁盘列出所有可用的自动快照。以下示例显示了可用于实例 TestLightsailInstance1 的快照的详细信息。将 --resource-name 和 --region 替换为您的请求中的相应值。
# 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": []
}
]
}
2.FSP 运行 create-instances-from-snapshot 命令以从手动或自动备份中创建一个或多个 Lightsail 实例。以下示例使用特定备份和更大尺寸的捆绑包在 eu-west-1 区域中创建一个实例。将 --instance-snapshot-name、--instance-names、bundle-id 和 --region 替换为您的请求中的相应值。
# 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
}
]
}
以下示例基于指定的备份和更高大小的捆绑包创建一个新实例:
# 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 |