温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - How to schedule an RDS snapshot and restore in the same script
amazon-rds amazon-web-services aws-glue boto3 python

python - 如何安排RDS快照并在同一脚本中还原

发布于 2020-04-08 11:53:57

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

查看更多

提问者
luke henry
被浏览
39
John Rotenstein 2020-02-01 12:41

您可以使用服务员

waiter = client.get_waiter('db_cluster_snapshot_available')

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

请参阅:类RDS.Waiter.DBClusterSnapshotAvailable