Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Created January 17, 2020 15:23
Show Gist options
  • Save dgulinobw/d4b91438faa3b2bc45c9b3674e913a46 to your computer and use it in GitHub Desktop.
Save dgulinobw/d4b91438faa3b2bc45c9b3674e913a46 to your computer and use it in GitHub Desktop.
AWS Lambda + standalone script to backup all DynamoDB tables.
#!/usr/bin/env python
#based on:
#https://aws.amazon.com/blogs/database/a-serverless-solution-to-schedule-your-amazon-dynamodb-on-demand-backup/
from __future__ import print_function
from datetime import date, datetime, timedelta
import json
import boto3
import time
from botocore.exceptions import ClientError
import os
ddbRegion = os.environ['AWS_DEFAULT_REGION']
ddb = boto3.client('dynamodb', region_name=ddbRegion)
# for deleting old backup. It will search for old backup and will escape deleting last backup days you mentioned in the backup retention
#daysToLookBackup=2
daysToLookBackup= int(os.environ['BackupRetention'])
daysToLookBackupL=daysToLookBackup-1
def lambda_handler(event, context):
ddbTables = ddb.list_tables()
print("Backing up tables: ", ddbTables.get("TableNames"))
for ddbTable in ddbTables.get("TableNames"):
backup_table(ddbTable)
delete_old_backups(ddbTable)
def backup_table(ddbTable):
try:
#create backup
backupName = ddbTable
print('Backup started for: ', backupName)
ddb.create_backup(TableName=ddbTable,BackupName = backupName)
print('Backup has been taken successfully for table:', ddbTable)
except ClientError as e:
print(e)
except ValueError as ve:
print('error:',ve)
except Exception as ex:
print(ex)
def delete_old_backups(ddbTable):
try:
#check recent backup
lowerDate=datetime.now() - timedelta(days=daysToLookBackupL)
#print('lowerDate: ',lowerDate)
upperDate=datetime.now()
#print('upperDate: ',upperDate)
responseLatest = ddb.list_backups(TableName=ddbTable, TimeRangeLowerBound=datetime(lowerDate.year, lowerDate.month, lowerDate.day), TimeRangeUpperBound=datetime(upperDate.year, upperDate.month, upperDate.day))
#print('responseLatest:', responseLatest)
latestBackupCount=len(responseLatest['BackupSummaries'])
print('Total backup count in recent days:',latestBackupCount)
deleteupperDate = datetime.now() - timedelta(days=daysToLookBackup)
#deleteupperDate = datetime.now() + timedelta(days=1)
#print('deleteupperDate: ',deleteupperDate)
# TimeRangeLowerBound is the release of Amazon DynamoDB Backup and Restore - Nov 29, 2017
response = ddb.list_backups(TableName=ddbTable, TimeRangeLowerBound=datetime(2017, 11, 29), TimeRangeUpperBound=datetime(deleteupperDate.year, deleteupperDate.month, deleteupperDate.day))
#print("response: ",response)
#check whether latest backup count is more than two before removing the old backup
if latestBackupCount >= 2:
for record in response['BackupSummaries']:
backupArn = record['BackupArn']
ddb.delete_backup(BackupArn=backupArn)
print(ddbTable, 'has deleted this backup:', backupArn)
else:
print('Recent backup does not meet the deletion criteria')
except ClientError as e:
print(e)
except ValueError as ve:
print('error:',ve)
except Exception as ex:
print(ex)
def main():
lambda_handler("test","test")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment