Preface
AWS LightSail provides cheap VPS hosting compared to EC2, with a simple management interface.
LightSail allows us to create entire disk snapshots for reliability and portability. If you have a number of LightSail VPSs, managing the snapshots is a kinda headache for sure. So, we have written a shell script with the help of AWS CLI API to create and delete snapshots according to user’s preference, automatically.
The Shell Script
The script was originally written by Govind Raj and further improved by Shafeer P. The current version is kept in our GitHub repository.
Installation and Configuration
- Create an IAM programmatic user with the following permissions for the service LightSail:
GetRegions GetInstance GetInstances GetDiskSnapshot GetDiskSnapshots GetInstanceSnapshot GetInstanceSnapshots GetDisk CreateDiskSnapshot CreateInstanceSnapshot DeleteInstanceSnapshot
If you are unsure, create a user with LightSail full access. Note down the ACCESS_KEY and SECRET_KEY which we require in the next steps.
- Create KEY-VALUE pair tags for instances to take automatic backups.
KEY VALUE TYPE DEFAULT VALUE NOTES backup.enable true Boolean NULL Case-sensitive. Any value other than true will disable automatic backups.
If disabled, existing backups won’t be removed automatically.backup.retention [0-9] Digit 1 Maximum number of backups kept which created with name suffix snapof-
The value 0 will remove all backups.
- Install the AWS CLI on a Linux machine
- Place the backup script in the Linux machine with AWS CLI installed, and modify the ACCESS_KEY, SECRET_KEY you have generated in Step 1.
- Give necessary permissions to the script and create a crontab entry according to your preferred schedule.
Default Behavior and Customization
- The script uses ENVIRONMENT VARIABLE for AWS CLI for better portability. You can also rely on alternate methods for configuration.
- If no retention count is specified for an instance in TAG, the default value 1 is used. You can customize this value in the script variable ret_count_default