Help us understand the problem. What is going on with this article?

AWSでebsのスナップショットを自動作成

More than 5 years have passed since last update.

【AWS】クラウド時代のバックアップ管理術  ~tagを活用したsnapshotの世代管理~
http://shanon-tech.blogspot.jp/2013/02/awstagsnapshot.html
にある、タグを使ったスナップショット管理方式を参考に、
python botoを使って行うようにしたスクリプト

create_snapshot.rb
# coding: utf-8

import boto.ec2

region = 'ap-northeast-1'
aws_access_key = ''
aws_secret_key = ''

ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key)

# タグ'backup'が'ON'のEC2インスタンスをバックアップ対象とする
instances = [i for r in ec2.get_all_instances(filters={'tag-key':'backup', 'tag-value':'ON'}) for i in r.instances]

for instance in instances:
  print "backup instance: %s" % instance.id

  # バックアップ世代数を、instanceのタグ'generation'から取得
  generation = int(instance.tags.get('generation'))
  print "backup generation: %s" % generation

  volumes = ec2.get_all_volumes(filters={'attachment.instance-id': instance.id})

  for v in volumes:
    # 新しいsnapshotを作成する
    print "create snapshot: %s" % v.id
    v.create_snapshot(description='snapshot: ' + v.id)

    # start_timeの降順で並び替え、古い世代のsnapshotを削除する
    snaps = v.snapshots()
    sorted_snaps = sorted(snaps, key=lambda snap: snap.start_time, reverse=True)
    for i, snap in enumerate(sorted_snaps):
      if i >= generation:
        print "delete snapshot: %s" % snap.id
        snap.delete()
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした