如何在同一个脚本中安排 RDS 快照和恢复

How to schedule an RDS snapshot and restore in the same script

所以我正在安排一个 AWS python 作业(通过 AWS Glue Python shell),它应该克隆一个 MySQL RDS 数据库(最好的方法拍摄快照并恢复?)并对数据库执行 sql 查询。我在 Python Shell 上有 boto3 库,我加载了一个 SQL Python 库。我目前有这个代码

import boto3
client = boto3.client('rds')
# Create a snapshot of the database
snapshot_response = client.create_db_snapshot(
    DBSnapshotIdentifier='snapshot-identifier',
    DBInstanceIdentifier='instance-db',
)

# Restore db from snapshot
restore_response = client.restore_db_instance_from_db_snapshot(
    DBInstanceIdentifier = 'restored-db',
    DBSnapshotIdentifier = 'snapshot-identifier',
)

# Code that will perform sql queries on the restored-db database.

但是,client.restore_db_instance_from_db_snapshot 失败,因为它表示正在创建快照。所以我明白这意味着这些调用是异步的。但我不确定如何让这个快照恢复工作(通过使它们同步——不是一个好主意?)或通过其他方式。提前感谢您的帮助:).

您可以使用服务员:

waiter = client.get_waiter('db_cluster_snapshot_available')

Polls RDS.Client.describe_db_cluster_snapshots() every 30 seconds until a successful state is reached. An error is returned after 60 failed checks.

参见:class RDS.Waiter.DBClusterSnapshotAvailable