• Jozef Jarosciak
Jozef Jarosciak Blog

Strive not to be a success, but rather to be of value.

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

July 29, 2017 by joe0

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:

aws configure

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:

aws lightsail get-instance-snapshots

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

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

 create-instance-snapshot
--instance-snapshot-name <value>
--instance-name <value>
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

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:

aws lightsail create-instance-snapshot --instance-snapshot-name joe0.com-29.July.2017 --instance-name joe0.com-80month --region us-east-2

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:

crontab -e

Then add your script entry there.

0 0 * * * /home/ec2-user/aws-snapshot.sh

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

crontab -l

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:

sudo yum install jq

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:

aws lightsail get-instance-snapshots | jq '.[]|length'

The output will look like this:

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

 aws lightsail get-instance-snapshots | jq '.[][].name'

The output will look like this:

Or you can sort them by name:

aws lightsail get-instance-snapshots | jq '.[] | sort_by(.createdAt) | .[].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!

 

Posted in: Operating Systems Tagged: linux

Jozef Jarosciak – About Me

Site Search

Categories

  • Cloud & Big Data (64)
  • Databases (26)
  • Information Technology (45)
  • Operating Systems (86)
  • Slovak Language Posts (50)
  • Software Development (114)
  • Uncategorized Posts (67)

Links

  • 0. ToolBox
  • 1. FB Comments
  • Bananagrams Solver
  • Block Puzzle Solver
  • Canadian Mortgage Calculator
  • ETX.ca
  • How to buy Dogecoin in Canada
  • Multiverse Infiniverse Search
  • ScrabbleDB.com
  • TryToBreak.com
  • Usenet Archives

Copyright © 2022 Jozef Jarosciak Blog.

Omega WordPress Theme by ThemeHall