Created
November 17, 2011 03:16
-
-
Save najeira/1372274 to your computer and use it in GitHub Desktop.
Create snapshot of Amazon RDS by Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import datetime | |
from boto import rds | |
#設定 | |
AccessKey = '****' | |
AccessSecret = '****' | |
DbinstanceId = '****' | |
Region = 'ap-northeast-1' | |
SnapshotFormat = '%(name)s-%(date)s' | |
Generation = 3 | |
#スナップショット名 | |
today = datetime.date.today() | |
snapshot_id = SnapshotFormat % { | |
'name': DbinstanceId, 'date': today.strftime('%Y%m%d')} | |
try: | |
#接続 | |
conn = rds.connect_to_region(Region, | |
aws_access_key_id=AccessKey, aws_secret_access_key=AccessSecret) | |
print 'connected.' | |
#スナップショットを作成 | |
try: | |
conn.create_dbsnapshot(snapshot_id, DbinstanceId) | |
print 'created new snapshot.' | |
except Exception, e: | |
if 'DBSnapshotAlreadyExists' != e.code: | |
raise | |
print 'snapshot already exists.' | |
#古いスナップショットを取得 | |
oldday = datetime.date.today() - datetime.timedelta(days=Generation) | |
old_snapshot_id = SnapshotFormat % { | |
'name': DbinstanceId, 'date': oldday.strftime('%Y%m%d')} | |
try: | |
old_snapshot = conn.get_all_dbsnapshots(snapshot_id=old_snapshot_id) | |
#古いスナップショットを削除 | |
if old_snapshot: | |
conn.delete_dbsnapshot(old_snapshot) | |
print 'deleted old snapshot.' | |
except Exception, e: | |
if 'DBSnapshotNotFound' != e.code: | |
raise | |
print 'finish.' | |
except Exception, e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment