How do I use AWS CLI commands to manage my snapshots and create backups for my Lightsail instances?

4 minute read
0

I want to use AWS Command Line Interface (AWS CLI) commands to manage my snapshots and create backups for my Amazon Lightsail instances.

Resolution

Note:

Manage manual backups

Use AWS CLI commands to manage manual backups for your instance and list available snapshots.

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

Note: Replace instance-snapshot-name, instance-name, and region with your values.

List available snapshots

Run the get-instance-snapshots command to list all snapshots for your Lightsail instances. The following example shows details of available snapshots in 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  |
+----------------+-----------------------------------------+------------------------------------------------------------------------------------------------------------+-------------------+-----------+-------------+

Note: Replace region with your Region.

Manage automatic backups

Use AWS CLI commands to verify if automatic snapshots are turned on for your instances, and turn on automatic snapshots. You can also list automatic snapshots, and create a new instance from a backup with a higher bundle size or higher Lightsail plan.

Verify if automatic snapshots are turned on for your instances

Run the following command to verify if automatic snapshots are turned on for your instance and to show the defined schedule:

# 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

Note: Replace TestLightsailInstance1 with your instance name and region with your Region.

Turn on automatic snapshots

Run the enable-add-on command to turn on automatic snapshots for your Lightsail instances. The following example creates daily automatic snapshots that are set to an hourly increment in 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"
        }
    ]
}

Note: Replace resource-name, snapshotTimeOfDay, and region with your values.

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

Run the get-auto-snapshots command to list all available automatic snapshots for your Lightsail instances or disk. The following example shows details of available snapshots for the instance 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": []
        }
    ]
}

Note: Replace resource-name and region with your values.

Run the create-instances-from-snapshot command to create Lightsail instances from a manual or automatic backup. The following example uses a specific backup and a higher sized bundle to create an instance in the eu-west-1 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
        }
    ]
}

Note: Replace instance-snapshot-name, instance-names, bundle-id, and region with your values.

The following is an example of the details for the Lightsail instance RestoredTestLightsailInstance1-New that you created:

# 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

AWS CLI Command Reference for Lightsail

Lightsail API Reference

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

Lightsail docs

AWS OFFICIAL
AWS OFFICIALUpdated 2 months ago