Schedule automatic Backup in Dynamodb.

History:

Requeriments:

  • Aws account and dynamodb table for backup.

Starting to Work:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}
check = dynamo.list_backups(
TableName=name,
Limit=100,
TimeRangeUpperBound = current_time - timedelta(days=7)
#TimeRangeLowerBound = datetime(2015, 1, 1)
)
import boto3
import sys
from datetime import datetime, timedelta
import calendar
import json
import gc
from pprint import pprint
dynamo = boto3.client('dynamodb')
ses = boto3.client('ses')
sns = boto3.client('sns')
email_from = 'e_ariel@hotmail.es'
email_to = 'earielli@itshell.org'
email_cc = 'e_ariel@hotmail.es'
emaiL_subject = 'Dynamodb backup sucessful in the '
email_body = 'Dynamodb backup sucessful in the '
current_time = datetime.now()def make_backup(name):
try:
response = dynamo.create_backup(
TableName=name,
BackupName=name+'_bkp_'+ '%s-%s-%s_%s.%s.%s' % (current_time.year, current_time.month, current_time.day,current_time.hour, current_time.minute, current_time.second)
#time.strftime("%Y%m%d%H")
)
print(response)
send_email(name)
return 0
except:
send_sns()
sys.exit("Se produjo un error realizando el paso backup!")

def delete_backup(name):
try:
print("Deleting")
print(current_time)
print(current_time - timedelta(days=7))

check = dynamo.list_backups(
TableName=name,
Limit=100,
TimeRangeUpperBound = current_time - timedelta(days=1)
#TimeRangeLowerBound = datetime(2015, 1, 1)
)

print(check)
for backup in check['BackupSummaries']:
arn = backup['BackupArn']
print("ARN to delete: "+arn)
deletedArn = dynamo.delete_backup(
BackupArn=arn
)
print(deletedArn['BackupDescription']['BackupDetails']['BackupStatus'])
except:
sys.exit("Se produjo un error realizando el paso de limpieza de backups viejos!!")
def send_sns():
sns_message = sns.publish(
TopicArn='arn:aws:sns:us-west-2:881653854182:dynamodb_bkp_failed',
Message='Dynamodb backup failed please check!'
)

def send_email(name):
response = ses.send_email(
Source = email_from,
Destination={
'ToAddresses': [
email_to,
],
'CcAddresses': [
email_cc,
]
},
Message={
'Subject': {
'Data': emaiL_subject + name + ' table'
},
'Body': {
'Text': {
'Data': email_body + name + ' table'
}
}
}
)
# response is a function for dynamodb this develop the backup of table.
def lambda_handler(event, context):
print(event['TableName'])
if make_backup(event['TableName']) == 0:
delete_backup(event['TableName'])
check e-mail
import boto3
import sys
from datetime import datetime, timedelta
import calendar
import json
import gc
from pprint import pprint
dynamo = boto3.client('dynamodb')
ses = boto3.client('ses')
email_from = 'e_ariel@hotmail.es'
email_to = 'earielli@itshell.org'
email_cc = 'e_ariel@hotmail.es'
emaiL_subject = 'Dynamodb backup Failed! in '
email_body = 'The backup for dynamodb failed! Please review '
current_time = datetime.now()def make_backup(name):
try:
response = dynamo.create_backup(
TableName=name,
BackupName=name+'_bkp_'+ '%s-%s-%s_%s.%s.%s' % (current_time.year, current_time.month, current_time.day,current_time.hour, current_time.minute, current_time.second)
#time.strftime("%Y%m%d%H")
)
print(response)
return 0
except:
send_email(name)
sys.exit("Se produjo un error realizando el paso backup!")

def delete_backup(name):
try:
print("Deleting")
print(current_time)
print(current_time - timedelta(days=7))

check = dynamo.list_backups(
TableName=name,
Limit=100,
TimeRangeUpperBound = current_time - timedelta(minutes=1)
#TimeRangeLowerBound = datetime(2015, 1, 1)
)

print(check)
for backup in check['BackupSummaries']:
arn = backup['BackupArn']
print("ARN to delete: "+arn)
deletedArn = dynamo.delete_backup(
BackupArn=arn
)
print(deletedArn['BackupDescription']['BackupDetails']['BackupStatus'])
except:
sys.exit("Se produjo un error realizando el paso de limpieza de backups viejos!!")

def send_email(name):
response = ses.send_email(
Source = email_from,
Destination={
'ToAddresses': [
email_to,
],
'CcAddresses': [
email_cc,
]
},
Message={
'Subject': {
'Data': emaiL_subject + name + ' table'
},
'Body': {
'Text': {
'Data': email_body + name + ' table'
}
}
}
)
# response is a function for dynamodb this develop the backup of table.
def lambda_handler(event, context):
print(event['TableName'])
if make_backup(event['TableName']) == 0:
delete_backup(event['TableName'])

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store