1

Please help me on this.

I need a python or shell script for copy rds snapshot from ireland to N.virginia and restore the RDS instance in N.virginia with modified root password.

  • What have you tried so far? How do you wish to "automate" it -- Python, shell, powershell, etc? Have you been able to do it successfully via the console, and now it's just a matter of automating it? Feel free to Edit your question to provide more details. – John Rotenstein Feb 21 '18 at 14:31
  • Thanks John. Iam looking for python script. – pradheep purushothaman Feb 22 '18 at 04:32
  • What have you tried so far? For tips on asking a good question, see: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – John Rotenstein Feb 22 '18 at 15:52
  • Hello John, I have tried below link. But iam getting below error. link : https://mysteriouscode.io/blog/copying-rds-snapshot-to-another-region-for-cross-region-recovery/ Error :Response:{ "errorMessage": "An error occurred(InvalidParameterValue) when calling the CopyDBSnapshot operation: The parameter SourceDBSnapshotIdentifier is not a valid identifier. Identifiers must begin with a letter; must contain only ASCII letters, digits, and hyphens; and must not end with a hyphen – pradheep purushothaman Feb 23 '18 at 04:57
  • Feel free to Edit your question rather than trying to squeeze details into a comment. Are you saying that you are using the code from that blog? What is the value of `SourceDBSnapshotIdentifier` that you are passing? – John Rotenstein Feb 24 '18 at 01:13

1 Answers1

0

The script below will copy latest (today created) RDS snapshot(automated/manual) to destination region also delete RDS snapshots older < retention days, and send notifications via AWS SNS service.

import re
import boto3
from datetime import date, datetime, timedelta

databases = ['testdb']

sns_arn = "arn:aws:sns:us-east-1:01234567890:AWS_LAMBDA_NOTIFICATIONS"

region_src = 'us-east-1'
region_dst = 'us-east-2'

client_rds_src = boto3.client('rds', region_name=region_src)
client_rds_dst = boto3.client('rds', region_name=region_dst)

sns = boto3.client('sns', region_name=region_src)

subject_alert = "AWS LAMBDA FUNCTION ALERT"
subject_notify = "AWS LAMBDA FUNCTION NOTIFICATION"

retention_days = 3

retentionDate = datetime.today() - timedelta(retention_days)
retentionDate = retentionDate.strftime('%Y-%m-%d')

date_today = datetime.today().strftime('%Y-%m-%d')

# Filter RDS snapshots by SnapshotType and SnapshotCreateTime in in source region
def get_rds_snapshots_src():
    response = client_rds_src.describe_db_snapshots(
        SnapshotType='automated',
        IncludeShared=False,
        IncludePublic=False
    )
    rds_snapshotsInDay = []
    for i in response["DBSnapshots"]:
        if i['DBInstanceIdentifier'] not in databases:
            continue
        if i['Status'] != 'available':
            message_error = ("Automated RDS snapshot: " + i['DBSnapshotIdentifier'] + " for instance " + i[
                'DBInstanceIdentifier'] + " has " + i['Status'] + " status in " + region_src + " region.")
            send_sns(subject_alert, message_error)
            continue
        if i['SnapshotCreateTime'].strftime('%Y-%m-%d') == date.isoformat(date.today()):
            rds_snapshotsInDay.append(i)
    return rds_snapshotsInDay


# Filter RDS snapshots by SnapshotType and SnapshotCreateTime in destination region
def get_rds_snapshots_dst():
    response = client_rds_dst.describe_db_snapshots(
        SnapshotType='manual',
        IncludeShared=False,
        IncludePublic=False
    )
    if len(response['DBSnapshots']) == 0:
        message_error = ("No manual RDS snapshots found " + region_dst)
        send_sns(subject_alert, message_error)
        exit(0)
    snapshotsDelete = []
    for i in response['DBSnapshots']:
        if i['SnapshotCreateTime'].strftime('%Y-%m-%d') <= retentionDate:
            snapshotsDelete.append(i)
    return snapshotsDelete


# Copying RDS snapshots from source region to destination region
def copy_rds_snapshots(snapshot_arn, target_snap_id):
    try:
        response = client_rds_dst.copy_db_snapshot(
            SourceDBSnapshotIdentifier=snapshot_arn,
            TargetDBSnapshotIdentifier=target_snap_id,
            CopyTags=True
        )

    except Exception as e:
        raise e


# Sending email alert/notification via AWS SNS service
def send_sns(subject, message):
    # print("Sending SNS alert")
    response = sns.publish(
        TargetArn=sns_arn,
        MessageStructure='string',
        Subject=subject,
        Message=message
    )


def lambda_handler(event, context):
    message = ""
    rds_snapshots_dst = get_rds_snapshots_dst()
    if len(rds_snapshots_dst) == 0:
        message_error = ("No RDS snapshots found in " + region_dst + " that needs to be deleted.")
        send_sns(subject_alert, message_error)
    elif rds_snapshots_dst:
        if message:
            message += "\n"
        # Deleting RDS snapshots where SnapshotCreateTime is <= retentionDate in destination region
        for i in rds_snapshots_dst:
            try:
                created_date = i["SnapshotCreateTime"].strftime('%Y-%m-%d')
                client_rds_dst.delete_db_snapshot(DBSnapshotIdentifier=i['DBSnapshotIdentifier'])
                message += ("RDS snapshot: " + i[
                    "DBSnapshotIdentifier"] + " created: " + created_date + " for RDS instance: " + i[
                                "DBInstanceIdentifier"] + " in " + region_dst + " region is DELETED" + ".\n")
            except Exception as e:
                raise e

    rds_snapshots_src = get_rds_snapshots_src()
    # print(*rds_snapshots_list_src, sep=".\n")
    if len(rds_snapshots_src) == 0:
        message_error = ("No automated daily RDS snapshots found in " + region_src)
        send_sns(subject_alert, message_error)
    elif rds_snapshots_src:
        if message:
            message += "\n"
        # Running function "Copying RDS snapshots from source region to destination region"
        c = 0
        for i in rds_snapshots_src:
            if c < 5:
                target_snap_id = (re.sub('rds:', '', i["DBSnapshotIdentifier"]))
                # client_rds_dst.copy_db_snapshot(SourceDBSnapshotIdentifier=i["DBSnapshotArn"], TargetDBSnapshotIdentifier=target_snap_id, CopyTags=True)
                try:
                    copy_rds_snapshots(i["DBSnapshotArn"], target_snap_id)
                    message += ("Started copying latest RDS snapshot: " + target_snap_id + " for RDS instance: " + i[
                        "DBInstanceIdentifier"] + " from: " + region_src + " to: " + region_dst + ".\n")
                except Exception as e:
                    raise e
                c = c + 1
            else:
                message_error = ("There are > then 5 RDS snapshots needs to be copied to " + region_dst)
                send_sns(subject_alert, message_error)
                exit(0)

    if message:
        send_sns(subject_notify, message)
        print(message)
    else:
        message_error = "Message wasn't generated by script, check lambda function!"
        send_sns(subject_alert, message_error)
Orest Gulman
  • 255
  • 1
  • 3
  • 17