Skip to content

Instantly share code, notes, and snippets.

@BenRomberg
Created January 15, 2018 16:48
Show Gist options
  • Save BenRomberg/08b6a1af4d7bc8890286bf9d32b70728 to your computer and use it in GitHub Desktop.
Save BenRomberg/08b6a1af4d7bc8890286bf9d32b70728 to your computer and use it in GitHub Desktop.
DynamoDB table backup using AWS Lambda (including cleanup of obsolete backups after a specified time)
import datetime
import os
import boto3
import logging
DAYS_UNTIL_OBSOLETE = 90
BACKUP_NAME_PREFIX = 'auto_'
BACKUP_TABLE_TAG_KEY = 'backup'
BACKUP_TABLE_TAG_VALUE = 'true'
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
def lambda_handler(event, context):
client = boto3.client('dynamodb')
timestamp = datetime.datetime.now().strftime('%Y%m%d-%H%M%S')
created_backups = 0
tables = client.list_tables()
for table_name in tables['TableNames']:
table_description = client.describe_table(TableName=table_name)
table_tags = client.list_tags_of_resource(ResourceArn=table_description['Table']['TableArn'])
backup_enabled = False
for tag in table_tags['Tags']:
if tag['Key'] == BACKUP_TABLE_TAG_KEY and tag['Value'] == BACKUP_TABLE_TAG_VALUE:
backup_enabled = True
if not backup_enabled:
continue
LOGGER.info('creating backup for table ' + table_name)
client.create_backup(TableName=table_name, BackupName=BACKUP_NAME_PREFIX + table_name + '_' + timestamp)
created_backups += 1
removed_backups = 0
obsolete_time = datetime.datetime.now() - datetime.timedelta(days=DAYS_UNTIL_OBSOLETE)
obsolete_backups = client.list_backups(TimeRangeUpperBound=obsolete_time)
LOGGER.info('removing obsolete backups')
for obsolete_backup in obsolete_backups['BackupSummaries']:
if not obsolete_backup['BackupName'].startswith(BACKUP_NAME_PREFIX):
continue
LOGGER.info('removing backup ' + obsolete_backup['BackupName'])
client.delete_backup(BackupArn=obsolete_backup['BackupArn'])
removed_backups += 1
return 'Created ' + str(created_backups) + ' backups, removed ' + str(removed_backups)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment