如何自动化RDS快照副本从爱尔兰到N.Virginia,并使用更新的root密码启动RD



请帮助我。

我需要一个python或shell脚本,以复制从爱尔兰到n.virginia的RDS快照,并使用修改后的root密码在N.Virginia中还原RDS实例。

下面的脚本将将最新(今天创建的(RDS快照(自动化/手动(复制到目标区域,还删除了RDS快照旧<保留日,并通过AWS SNS服务发送通知。

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)

最新更新