[PYTHON] A script that takes a snapshot of an EBS volume

Also as a post test from kobito. EC2 is a script I wrote earlier because it will be a backup if you take a snapshot of the EBS volume.

class AWSBackuper(object):
    def __init__(self, access_key, secret_key, region="ap-northeast-1"):
        self.access_key = access_key
        self.secret_key = secret_key
        self.region = region
        self.set_ec2connection()

    def set_ec2connection(self):
        self.ec2connection = boto.ec2.connect_to_region(self.region, aws_access_key_id=self.access_key, aws_secret_access_key=self.secret_key)

    def create_snapshot(self, volume_id, description=None):
        self.ec2connection.create_snapshot(volume_id, description)

    def rotate_snapshot(self, volume_id, num):
        snapshot = {}
        for x in self.ec2connection.get_all_snapshots():
            if(x.volume_id == volume_id):
                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(num), len(snapshot)):
            try:
                self.ec2connection.delete_snapshot(snapshot[i][0])
            except:
                continue

    def add_tags_to_snapshot(self, volume_id):
        tags = self.ec2connection.get_all_volumes(volume_id)[0].tags

        for snapshot in self.ec2connection.get_all_snapshots():
            if(snapshot.volume_id == volume_id):
                for key, value in tags.items():
                    snapshot.add_tag(key, value)

if __name__ == '__main__':

    execfile(sys.argv[1])
    backuper = AWSBackuper(access_key, secret_key)

    for volume_id in volume_ids:
        backuper.create_snapshot(volume_id, 'auto backup')
        print "%s is backed up." % (volume_id)
        backuper.rotate_snapshot(volume_id, generation_limit)
        print "%s is rotated snapshots." % (volume_id)
        backuper.add_tags_to_snapshot(volume_id)
        print "%s is added tags." % (volume_id)

This script is started by specifying the python script that will be the config file.

# your volume_ids 
access_key, secret_key = 'YOUR ACCESS KEY', 'YOUR SECRET KEY'
volume_ids = [
    'vol-target-id1',
    'vol-target-id2',
    'vol-target-id3',
]
generation_limit = 7

All you have to do is start it like this

python ebs-backuper.py config.py

Recommended Posts

A script that takes a snapshot of an EBS volume
A script that just gets an RSS feed
A script that outputs a list of SoftLayer portal users
A set of script files that do wordcloud in Python3
A Python script that compares the contents of two directories
A memorandum of filter commands that you might forget in an instant
A script that morphologically parses a specified URL
A set of integers that satisfies ax + by = 1.
"Python Kit" that calls a Python script from Swift
A python script that gets the number of jobs for a specified condition from indeed.com
A script that combines margins when pasting a number of graphs on tiles with gnuplot
A script that combines multiple pages of a PDF file into one page without margins
What is a rational decision that maximizes the chances of encountering an "ideal home"?
A script that can perform stress tests according to the number of CPU cores
File overwrite confirmation with option that takes a file object as an argument with Python argparse
An article that gives you a little understanding of the rigid sphere collision algorithm
A script that pings the registered server and sends an email with Gmail a certain number of times when it fails