Skip to content

Instantly share code, notes, and snippets.

@CYPI
Created August 30, 2018 21:06
Show Gist options
  • Save CYPI/5adb9b24f0ebebf45955acb72d5561db to your computer and use it in GitHub Desktop.
Save CYPI/5adb9b24f0ebebf45955acb72d5561db to your computer and use it in GitHub Desktop.
script to automatically snapshot local ebs volumes. Keep a pre-define number of snapshot and update them.
import os
import sys
import time
import boto3
from botocore.exceptions import ClientError
def aws_ec2_session():
try:
session = boto3.Session(profile_name=none)
return session.client('ec2')
except ClientError as e:
print e
sys.exit()
def find_ec2_nodes(tag):
filter = [{'Name':'tag:Name', 'Values':tag}]
try:
instances = ec2.describe_instances(Filters=filter)
return instances
except ClientError as e:
print e
sys.exit()
def find_volumeids(nodes):
volumeids = []
if nodes:
for instance in nodes["Reservations"]:
for block_device in instance["Instances"]:
for ebs in block_device["BlockDeviceMappings"]:
volumeids.append(ebs["Ebs"]["VolumeId"])
return volumeids
else:
return 'no volume found for {}'.format(nodes)
def find_snapshots(tags):
try:
findsnapshots = ec2.describe_snapshots(Filters=[{'Name': 'tag:Name',
'Values': tags}])
return findsnapshots['Snapshots']
except ClientError as e:
print e
sys.exit()
def create_snapshot(volumesids, description, tags):
try:
snapshot = ec2.create_snapshot(VolumeId=volumeids[0], Description=description,
TagSpecifications=[{'ResourceType':'snapshot',
'Tags': [{'Key': 'Name', 'Value': tags}]}])
return snapshot['SnapshotId']
except ClientError as e:
print e
sys.exit()
def delete_snapshot(snapshotid):
try:
ec2.delete_snapshot(SnapshotId=snapshotid)
return 'deleted snapshot {}'.format(snapshotid)
except ClientError as e:
print e
sys.exit()
def find_oldest_snapshot(snapshots):
old = {}
for snapshot in snapshots:
if not old:
old['starttime'] = snapshot['StartTime']
old['snapid'] = snapshot['SnapshotId']
elif old['starttime'] > snapshot['StartTime']:
old['starttime'] = snapshot['StartTime']
old['snapid'] = snapshot['SnapshotId']
return old['snapid']
if __name__ == '__main__':
ec2 = aws_ec2_session()
airflow_nodes = find_ec2_nodes(['Data:Airflow-Scheduler'])
volumeids = find_volumeids(airflow_nodes)
found_snapshot = find_snapshots(['airflow'])
num_found_snapshot = len(found_snapshot)
max_snapshot = 3
if num_found_snapshot == max_snapshot:
print '{} snapshots: {}'.format(num_found_snapshot,
[snapshot['SnapshotId'] for snapshot in found_snapshot])
oldest = find_oldest_snapshot(found_snapshot)
print 'deleting oldest snapshot: {}'.format(oldest)
delete_snapshot(oldest)
new_snap = create_snapshot(volumeids, 'Data:Airflow-Scheduler', 'airflow')
print 'created a new snapshot: {}'.format(new_snap)
elif num_found_snapshot < max_snapshot:
new_snap = create_snapshot(volumeids, 'Data:Airflow-Scheduler', 'airflow')
print '{} snapshots. Creating a new snapshot: {}'.format(num_found_snapshot, new_snap)
elif num_found_snapshot > num_found_snapshot:
oldest = find_oldest_snapshot(found_snapshot)
delete_snapshot(oldest)
print 'Too many snapshots({}).deleting oldest snapshot: {}'.format(num_found_snapshot, oldest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment