How to schedule nightly backups of Amazon Lightsail Instance by leveraging AWS Command Line Interface (CLI)

Following are simple instructions on how to programmatically create a snapshot of your Amazon Lightsail Instance using AWS CLI. 

First of all, we’ll need to install the AWS Command Line Interface on your Lightsail Linux instance. To do so, visit the Amazon CLI installation page and follow the instruction to set it up: http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-linux.html

Once AWS CLI is installed on your Lightsail Linux instance, we can go ahead and configure it. Run the following command:

You’ll get asked to supply your AWS Access Key ID, as well as AWS Secret Access Key. Then set your default region name to wherever your AWS Lightsail instances reside and set the output to JSON format, it should look something like this once configured:

When AWS is configured, you can start using AWS commands to control your Lightsail environment. For the full list of available commands visit: http://docs.aws.amazon.com/cli/latest/reference/lightsail/index.html

For example, let’s list all available/existing Lightsail instance snapshots (assuming you have already created some snapshot backups).

We can do that by issuing the following command:

The output of the above command should produce something like this:

The command to initiate the creation of the snapshot is as follows:

So, let’s say I want to backup my Lightsail instance called “joe0.com-80month” and create a snapshot called: “joe0.com-27.July.2017“.

It’s as easy as to run the command from the command line in Linux:

This is what the output will look like:

If you were to login to Lightsail website, under the snapshots tab of your instance, you’d see that the new backup was created called: “joe0.com-27.July.2017

Now, let’s say that you want to automate this process and run it as a batch job every midnight. Nothing simpler than that.

Simply create a shell file on your Lightsail instance (most likely the best place is at /home/ec2-user/), you can call it: aws-snapshot.sh and paste in the above command. If you want to the date automatically added to the snapshot backup, you can configure it as follows (make sure to adjust the below code for your own snapshot name and region):

Now you can go ahead and schedule it as a cron job that runs every day at midnight. To do so

First, edit the current Cronjobs:

Then add your script entry there.

Save cron job and check to see cron job is set:

And that’s it, your instance will be automatically backed up every day at midnight.

 

Tips… you can install JQ from https://stedolan.github.io/jq/ for work with JSON files using the command line, or install it on your Lightsail instance by using:

Then you can run various cool commands. such as ask AWS how many snapshot backups do you currently have in your console:

Let’s ask AWS how many snapshot backups do you currently have in total:

The output will look like this:

Or you can query the names of all your currently created snapshots:

The output will look like this:

Or you can sort them by name:

The output:

 

This script is now available on Github if you’d like to contribute your changes:

https://github.com/JozefJarosciak/Lightsail-Backup-and-Clean-Up-Shell-Script

 

Enjoy!