How can I manage my snapshots and create backups for my Lightsail instances using AWS CLI commands?

Lesedauer: 5 Minute
0

I want to manage my snapshots and create backups for my Amazon Lightsail instances using AWS Command Line Interface (AWS CLI) commands. How can I do this?

Short description

For a list of Amazon Lightsail AWS CLI commands, see the AWS CLI Command Reference and the Amazon Lightsail API Reference.

The following are scenarios for using AWS CLI commands on your Lightsail instances to manage snapshots and backups:

  • Managing manual backups:
    Create manual backups for your instance.
    List available snapshots.
  • Managing automatic snapshots:
    Verify if automatic snapshots are enabled on your instances.
    Enable automatic snapshots.
    List automatic snapshots and creating a new instance from a backup with a higher bundle size or higher Lightsail plan.

Important: Keep in mind the following when using AWS CLI commands:

macOS:

Remove the decimal point from the timestamp and any digits to the right of the decimal point, and then run the following command:

# date -r 1602175741 -u
Thu Oct  8 16:49:01 UTC 2020

Linux:

Run the following command:

# date -d @1602175741.603 -u
Thu Oct  8 16:49:01 UTC 2020

Windows:

Convert the timestamp using a converter, such as epochconverter.com.

Resolution

Managing manual backups

Create a manual backup for a Lightsail instance

Run the create-instance-snapshot command to create a snapshot of the Lightsail instance.

The following example creates a snapshot of the instance SnapshotTestLightsailInstance1 in the eu-west-1 Region. Replace the --instance-snapshot-name, --instance-name, and --region with the appropriate values for your request.

# 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
        }
    ]
}

List available snapshots

Run the get-instance-snapshots command to list all snapshots for your Lightsail instances. The following example shows details of snapshots available in eu-west-1. Replace the --region with the appropriate values for your request.

# 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  |
+----------------+-----------------------------------------+------------------------------------------------------------------------------------------------------------+-------------------+-----------+-------------+

Managing automatic snapshots

Verify if automatic snapshots are enabled on your instances

Run the following command to verify if your instance has automatic snapshots enabled and to show the defined schedule. Replace TestLightsailInstance1 with your instance name and --region with the appropriate 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 automatic snapshots

Run the enable-add-on command to enable automatic snapshots for your Lightsail instances. The following example creates daily automatic snapshots set to an hourly increment in UTC (08PM UTC). Replace the --resource-name, snapshotTimeOfDay, and --region with the appropriate values for your request.

# 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"
        }
    ]
}

List automatic snapshots and creating a new instance from the backup with higher bundle size or higher Lightsail plan

1.FSPRun the command get-auto-snapshots to list all available automatic snapshots for your Lightsail instances or disk. The following example shows details of snapshots available for the instance TestLightsailInstance1. Replace the --resource-name and --region with the appropriate values for your request.

# 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.FSPRun the create-instances-from-snapshot command to create one or more Lightsail instances from a manual or automatic backup. The following example creates an instance in the eu-west-1 Region using a specific backup and a higher sized bundle. Replace --instance-snapshot-name, --instance-names, bundle-id, and --region with the appropriate values for your request.

# 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
        }
    ]
}

The following example creates a new instance based on the specified backup and a higher size bundle:

# 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    |

Related information

How can I manage my Lightsail instance using AWS CLI commands? 

How can I manage static IP addresses on my Lightsail instances using AWS CLI commands? 

Enabling or disabling automatic snapshots for instances or disks in Amazon Lightsail

Lightsail docs

AWS OFFICIAL
AWS OFFICIALAktualisiert vor 2 Jahren