Last active
December 17, 2015 17:18
-
-
Save linyows/5644632 to your computer and use it in GitHub Desktop.
Create snapshot of volume on AWS
This file contains 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/env ruby | |
# README | |
# | |
# Create snapshot | |
# =============== | |
# | |
# Create snapshot of volume. and generation management. | |
# | |
# Usage | |
# ----- | |
# | |
# add to `/etc/cron.d/aws`: | |
# | |
# ```sh | |
# SHELL=/bin/bash | |
# PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin | |
# MAILTO=root | |
# AWS_ACCESSKEY=????? | |
# AWS_SECRETKEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
# | |
# */10 * * * * /var/batches/create_snapshot.rb vol-xxxxxxx 5 "Database Volume" | |
# ``` | |
# | |
# Author | |
# ------ | |
# | |
# - [linyows](https://github.com/linyows) | |
# | |
# License | |
# ------- | |
# | |
# MIT | |
require 'aws-sdk' | |
access_key = ENV['AWS_ACCESSKEY'] | |
secret_key = ENV['AWS_SECRETKEY'] | |
if access_key.nil? || access_key.empty? || secret_key.nil? || secret_key.empty? | |
puts "Empty AWS_ACCESSKEY or AWS_SECRETKEY" | |
exit 0 | |
end | |
if ARGV.size < 2 | |
puts "Usage: #{$0} <volume_id> <generation_count> <snapshot_description> <ec2_endpoint> <dry-run>" | |
exit 0 | |
end | |
volume_id = ARGV[0] | |
generation_count = ARGV[1].to_i | |
description = "#{ARGV[2] + ' ' if ARGV[2]}#{Time.now.strftime('%Y/%m/%d %H:%M')}" | |
endpoint = ARGV[3] || 'ec2.us-east-1.amazonaws.com' | |
dryrun = ARGV[4] || false | |
AWS.config(access_key_id: access_key, | |
secret_access_key: secret_key, | |
ec2_endpoint: endpoint) | |
ec2 = AWS::EC2.new | |
if dryrun | |
puts "volume: #{ec2.volumes[volume_id]}, description: #{description}" | |
else | |
ec2.snapshots.create(volume: ec2.volumes[volume_id], | |
description: description) | |
end | |
snapshots = ec2.snapshots.filter('volume-id', volume_id) | |
targeted_snapshots = snapshots.each_with_object([]) { |snapshot, result| | |
next if snapshot.status != :completed | |
next unless snapshot.tags[:Name].nil? | |
result << snapshot | |
}.compact.sort_by { |snapshot| | |
snapshot.start_time | |
}.reverse | |
snapshots_to_del = [] | |
targeted_snapshots.each_with_index do |snapshot, i| | |
snapshots_to_del << snapshot if i >= generation_count | |
end | |
snapshots_to_del.each do |snapshot| | |
if dryrun | |
puts "#{snapshot.id}: #{snapshot.start_time}" | |
else | |
begin | |
snapshot.delete | |
rescue => e | |
ap e | |
end | |
end | |
end |
Author
linyows
commented
Nov 1, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment