user2155404
user2155404

Reputation: 133

list automated RDS snapshots created today and copy to other region using boto3

We are building an automated DR cold site on other region, currently are working on retrieving a list of RDS automated snapshots created today, and passed them to another function to copy them to another AWS region.

The issue is with RDS boto3 client where it returned a unique format of date, making filtering on creation date more difficult.

today = (datetime.today()).date()
rds_client = boto3.client('rds')
snapshots = rds_client.describe_db_snapshots(SnapshotType='automated')

harini = "datetime("+ today.strftime('%Y,%m,%d') + ")"
print harini

print snapshots

for i in snapshots['DBSnapshots']:

  if i['SnapshotCreateTime'].date() == harini:
      print(i['DBSnapshotIdentifier'])
      print (today)

despite already converted the date "harini" to the format 'SnapshotCreateTime': datetime(2015, 1, 1), the Lambda function still unable to list out the snapshots.

Upvotes: 0

Views: 2221

Answers (2)

Orest Gulman
Orest Gulman

Reputation: 432

The code below will take automated snapshots created today.

import boto3
from datetime import date, datetime

region_src = 'us-east-1'

client_src = boto3.client('rds', region_name=region_src)
date_today = datetime.today().strftime('%Y-%m-%d')

def get_db_snapshots_src():
    response = client_src.describe_db_snapshots(
        SnapshotType = 'automated',
        IncludeShared=False,
        IncludePublic=False
    )
    snapshotsInDay = []
    for i in response["DBSnapshots"]:
        if i["SnapshotCreateTime"].strftime('%Y-%m-%d') == date.isoformat(date.today()):
            snapshotsInDay.append(i)
    return snapshotsInDay

Upvotes: 0

Joseph
Joseph

Reputation: 542

The better method is to copy the files as they are created by invoking a lambda function using a cloud watch event.

See step by step instruction: https://geektopia.tech/post.php?blogpost=Automating_The_Cross_Region_Copy_Of_RDS_Snapshots

Alternatively, you can issue a copy for each snapshot regardless of the date. The client will raise an exception and you can trap it like this

# Written By GeekTopia
#
# Copy All Snapshots for an RDS Instance To a new region
# --Free to use under all conditions
# --Script is provied as is. No Warranty, Express or Implied

import json
import boto3
from botocore.exceptions import ClientError
import time
destinationRegion = "us-east-1"
sourceRegion = 'us-west-2'
rdsInstanceName = 'needbackups'

def lambda_handler(event, context):
    #We need two clients
    # rdsDestinationClient -- Used to start the copy processes. All cross region 
copies must be started from the destination and reference the source
    # rdsSourceClient      -- Used to list the snapshots that need to be copied. 

    rdsDestinationClient = boto3.client('rds',region_name=destinationRegion)
    rdsSourceClient=boto3.client('rds',region_name=sourceRegion)

    #List All Automated for A Single Instance 

    snapshots = rdsSourceClient.describe_db_snapshots(DBInstanceIdentifier=rdsInstanceName,SnapshotType='automated')

    for snapshot in snapshots['DBSnapshots']:
        #Check the the snapshot is NOT in the process of being created
        if snapshot['Status'] == 'available':

            #Get the Source Snapshot ARN. - Always use the ARN when copying snapshots across region
            sourceSnapshotARN = snapshot['DBSnapshotArn']

            #build a new snapshot name
            sourceSnapshotIdentifer = snapshot['DBSnapshotIdentifier']
            targetSnapshotIdentifer ="{0}-ManualCopy".format(sourceSnapshotIdentifer)
            targetSnapshotIdentifer = targetSnapshotIdentifer.replace(":","-")

            #Adding a delay to stop from reaching the api rate limit when there are large amount of snapshots -
            #This should never occur in this use-case, but may if the script is modified to copy more than one instance. 
            time.sleep(.2)            

            #Execute copy
            try:
                copy = rdsDestinationClient.copy_db_snapshot(SourceDBSnapshotIdentifier=sourceSnapshotARN,TargetDBSnapshotIdentifier=targetSnapshotIdentifer,SourceRegion=sourceRegion)
                print("Started Copy of Snapshot {0} in {2} to {1} in {3} ".format(sourceSnapshotIdentifer,targetSnapshotIdentifer,sourceRegion,destinationRegion))    
            except ClientError as ex:
                if ex.response['Error']['Code'] == 'DBSnapshotAlreadyExists':
                    print("Snapshot  {0} already exist".format(targetSnapshotIdentifer))
                else:
                    print("ERROR: {0}".format(ex.response['Error']['Code']))

    return {
        'statusCode': 200,
        'body': json.dumps('Opearation Complete')
    }

Upvotes: 2

Related Questions