Head in the Cloud

My journey to work in cloud technology

lambda : python3 : backupRotation

January 8, 2019 · AWS, Portfolio

This is a simple function that rotates the backup files in an s3 bucket to maintain a 30 day archive. Some variables have obfuscated.

!/usr/bin/python3
import boto3
from datetime import datetime, timedelta
today_date = str(datetime.now().today().strftime("%y%m%d"))
one_month_delta = datetime.now().today() - timedelta(days=31)
one_month_date = str(one_month_delta.strftime("%y%m%d"))
s3 = boto3.client('s3')
my_bucket = 'mybucket'
object_list = s3.list_objects(Bucket = my_bucket)
file_list = []
for object in object_list['Contents']:
    file_list.append(object['Key'])
for file in file_list:
    if one_month_date in file:
        s3.delete_object(Bucket= my_bucket, Key = file)