Created
October 3, 2012 14:34
-
-
Save hidesakai/3827239 to your computer and use it in GitHub Desktop.
EC2Snapshot ローテーションバックアップのスクリプト
This file contains hidden or 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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import sys | |
from boto.ec2.connection import EC2Connection | |
from boto.ec2 import connect_to_region | |
AWS_ACCESS_KEY = "" | |
AWS_SECRET_ACCESS_KEY = "" | |
if(len(sys.argv) != 5): | |
print "Usage: ec2-snapshot-rotation-backup.py <num> <volume-id> <Name> <Author>" | |
sys.exit() | |
conn = connect_to_region('ap-northeast-1', aws_access_key_id=AWS_ACCESS_KEY ,aws_secret_access_key=AWS_SECRET_ACCESS_KEY) | |
snap = conn.create_snapshot(sys.argv[2], description='This is Backup Snapshot by ec2-snapshot-rotation-backup.py') | |
snap.add_tag(key='Name', value=sys.argv[3]) | |
snap.add_tag(key='Author', value=sys.argv[4]) | |
snapshot = {} | |
for x in conn.get_all_snapshots(): | |
if(x.volume_id == sys.argv[2]): | |
tmp = {x.id:x.start_time} | |
snapshot.update(tmp) | |
snapshot = sorted(snapshot.items(), key=lambda (k, v): (v, k), reverse=True) | |
for i in range(int(sys.argv[1]), len(snapshot)): | |
conn.delete_snapshot(snapshot[i][0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment