AWS CLI コマンドを使用して Lightsail インスタンスのスナップショットを管理し、バックアップを作成するにはどうすればよいですか?
最終更新日: 2021 年 1 月 11 日
AWS Command Line Interface (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 からのコマンド出力の制御</t2>をご参照ください。
- AWS CLI エラーの解決に関する一般的な情報については、「AWS CLI コマンドの実行時にエラーが表示されるのはなぜですか?」を参照してください。
- AWS CLI 出力には、Unix エポック時間のタイムスタンプが表示されます。次のいずれかの方法を使用して、タイムスタンプを 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 インスタンスのスナップショットを作成します。
次の例では、インスタンス SnapshotTestLightsailInstance1 を eu-West-1 リージョンに作成します。--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) の 1 時間増分に設定された毎日の自動スナップショットを作成します。--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. コマンド get-auto-saplots を実行して、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. create-instances-from-snapshot コマンドを実行して、手動バックアップまたは自動バックアップから 1 つまたは複数の 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 |