Skip to content

Instantly share code, notes, and snippets.

@najeira
Created November 17, 2011 03:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save najeira/1372274 to your computer and use it in GitHub Desktop.
Save najeira/1372274 to your computer and use it in GitHub Desktop.
Create snapshot of Amazon RDS by Python
# -*- 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