2016-12-05 66 views
1

我開始將我的代碼遷移到boto 3,並且我注意到一個很好的補充是服務員。如何使用boto3服務器從大RDS實例中獲取快照

我想創建一個數據庫實例的快照,我想檢查它的可用性之前,我恢復與我的代碼。

我的做法是這樣的:

# Notice: Step : Check snapshot availability [1st account - Oregon] 
print "--- Check snapshot availability [1st account - Oregon] ---" 
new_snap = client1.describe_db_snapshots(DBSnapshotIdentifier=new_snapshot_name)['DBSnapshots'][0] 
# print pprint.pprint(new_snap) #debug 


waiter = client1.get_waiter('db_snapshot_completed') 
print "Manual snapshot is -pending-" 
sleep(60) 
waiter.wait(
       DBSnapshotIdentifier = new_snapshot_name, 
       IncludeShared = True, 
       IncludePublic = False 
      ) 

print "OK. Manual snapshot is -available-" 

,但文件說,它查詢狀態每15秒40次。那是10分鐘。然而,一個相當大的數據庫將需要更多。

我該如何使用服務員來減輕這一點?

回答

1

如果你喜歡,你可以不用服務員。

從該服務員的文檔: 每15秒輪詢RDS.Client.describe_db_snapshots(),直到達到成功狀態。 40次失敗檢查後將返回錯誤。

基本上,這意味着它執行以下操作:

RDS = boto3.client('rds') 
RDS.describe_db_snapshots() 

您可以只運行,但過濾你的快照ID,這裏是語法。 http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_snapshots

response = client.describe_db_snapshots(
    DBInstanceIdentifier='string', 
    DBSnapshotIdentifier='string', 
    SnapshotType='string', 
    Filters=[ 
     { 
      'Name': 'string', 
      'Values': [ 
       'string', 
      ] 
     }, 
    ], 
    MaxRecords=123, 
    Marker='string', 
    IncludeShared=True|False, 
    IncludePublic=True|False 
) 

這最終會看起來像這樣:

snapshot_description = RDS.describe_db_snapshots(DBSnapshotIdentifier='YOURIDHERE') 

那麼你可以循環,直到返回快照是可用的。所以這是一個非常粗略的想法。

import boto3 
import time 
RDS = boto3.client('rds') 
RDS.describe_db_snapshots() 
snapshot_description = RDS.describe_db_snapshots(DBSnapshotIdentifier='YOURIDHERE') 
while snapshot_description['DBSnapshots'][0]['Status'] != 'available' : 
    print("still waiting") 
    time.sleep(15)  
    snapshot_description = RDS.describe_db_snapshots(DBSnapshotIdentifier='YOURIDHERE') 
+0

在我使用Boto3之前,我已經準備好了,但是在Boto3中新增加了一些內容,我想用更優雅和直接開箱的東西代替這些while循環。但是,正如我所看到的,只要服務員不能參數化,對於需要10分鐘以上的操作,它們幾乎是無用的......:\ –

+0

我明白了你的觀點,我看不到一種方法來覆蓋計時器雖然。可能值得看看boto3的github回購,看看他們做了什麼。 – Danny

+0

可能有辦法使用自定義服務員,我還沒有找到它。 – Danny

1

侍者配置parameters'delay」和 'MAX_ATTEMPTS次' 這樣的:

waiter = rds_client.get_waiter('db_instance_available') 
print("waiter delay: " + str(waiter.config.delay)) 

waiter.py on github

0

我認爲對方的回答提到了這個解決方案,但在這裏它是明確。

[snip] 
... 
# Create your waiter 
waiter_db_snapshot = client1.get_waiter('db_snapshot_completed') 

# Increase the max number of tries as appropriate 
waiter_db_snapshot.config.max_attempts = 120 

# Add a 60 second delay between attempts 
waiter_db_snapshot.config.delay = 60 

print "Manual snapshot is -pending-" 
.... 
[snip]