首页 > 解决方案 > 复制时更改快照名称的 AWS RDS API 命令

问题描述

使用 AWS RDS API 复制快照时是否可以更改其标识符?我正在使用 Boto3 编写要在 DR 场景中使用的 Lambda 函数。

我之所以尝试更改标识符,是因为我想将当前日期添加到快照标识符中,原因有两个:

我的代码:

client = boto3.client("rds") #dr region
db_identifier = "test-database-dr" 

# Get snapshot automated arn so we can create a manual copy
describe_snapshots = client.describe_db_snapshots(
    SnapshotType= "shared",
    IncludeShared=True,
)

db_arn = (describe_snapshots["DBSnapshots"][-1]["DBSnapshotArn"])

# Create a snapshot copy 
copy_snapshot = client.copy_db_snapshot(
    SourceDBSnapshotIdentifier=db_arn,
    TargetDBSnapshotIdentifier=db_identifier,
    KmsKeyId="xxx",
    SourceRegion="xxx"
)

我得到的错误是:

botocore.errorfactory.DBSnapshotAlreadyExistsFault: An error occurred (DBSnapshotAlreadyExists) when calling the CopyDBSnapshot operation: Cannot create the snapshot because a snapshot with the identifier test-database-dr already exists.

标签: python-3.xamazon-web-servicesboto3

解决方案


您需要将 设置TargetDBSnapshotIdentifier为不同的东西。

client = boto3.client("rds") #dr region
db_identifier = "test-database-dr-SOMETHING-DIFFERENT" 

# Get snapshot automated arn so we can create a manual copy
describe_snapshots = client.describe_db_snapshots(
    SnapshotType= "shared",
    IncludeShared=True,
)

db_arn = (describe_snapshots["DBSnapshots"][-1]["DBSnapshotArn"])

# Create a snapshot copy 
copy_snapshot = client.copy_db_snapshot(
    SourceDBSnapshotIdentifier=db_arn,
    TargetDBSnapshotIdentifier=db_identifier,
    KmsKeyId="xxx",
    SourceRegion="xxx"
)

推荐阅读