Created
April 25, 2020 14:36
-
-
Save manhtai/26838afa3fa44b20e8955457a9abb16a to your computer and use it in GitHub Desktop.
Auto backup Lightsail instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import boto3 | |
from datetime import datetime, timedelta | |
from os import getenv | |
from sys import stdout | |
from time import time | |
DEFAULT_RETENTION_DAYS = 1 | |
AUTO_SNAPSHOT_SUFFIX = 'auto' | |
def handler(event, context): | |
client = boto3.client('lightsail') | |
retention_days = int(getenv('RETENTION_DAYS', DEFAULT_RETENTION_DAYS)) | |
retention_period = timedelta(days=retention_days) | |
print('Maintaining snapshots for ' + str(retention_days) + ' days') | |
_snapshot_instances(client) | |
_prune_snapshots(client, retention_period) | |
def _snapshot_instances(client, time=time, out=stdout): | |
for page in client.get_paginator('get_instances').paginate(): | |
for instance in page['instances']: | |
snapshot_name = '{}-system-{}-{}'.format(instance['name'], | |
int(time() * 1000), | |
AUTO_SNAPSHOT_SUFFIX) | |
client.create_instance_snapshot(instanceName=instance['name'], | |
instanceSnapshotName=snapshot_name) | |
print('Created Snapshot name="{}"'.format(snapshot_name), file=out) | |
def _prune_snapshots(client, retention_period, datetime=datetime, out=stdout): | |
for page in client.get_paginator('get_instance_snapshots').paginate(): | |
for snapshot in page['instanceSnapshots']: | |
name, created_at = snapshot['name'], snapshot['createdAt'] | |
now = datetime.now(created_at.tzinfo) | |
is_automated_snapshot = name.endswith(AUTO_SNAPSHOT_SUFFIX) | |
has_elapsed_retention_period = now - created_at > retention_period | |
if (is_automated_snapshot and has_elapsed_retention_period): | |
client.delete_instance_snapshot(instanceSnapshotName=name) | |
print('Deleted Snapshot name="{}"'.format(name), file=out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment