首页 > 解决方案 > 如何在同一脚本中安排 RDS 快照和还原

问题描述

所以我正在安排一个 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失败是因为它说正在创建快照。所以我明白这意味着这些调用是异步的。但我不确定如何让这个快照恢复工作(通过使它们同步 - 不是一个好主意?)或通过其他方式。我在这里先向您的帮助表示感谢 :)。

标签: pythonamazon-web-servicesboto3amazon-rdsaws-glue

解决方案


您可以使用服务员

waiter = client.get_waiter('db_cluster_snapshot_available')

每 30 秒轮询RDS.Client.describe_db_cluster_snapshots()一次,直到达到成功状态。60 次检查失败后返回错误。

请参阅:类 RDS.Waiter.DBClusterSnapshotAvailable


推荐阅读