#!/usr/bin/env ruby require "ec2_snapshot" require "optparse" require "date" options = Hash.new optparse = OptionParser.new do |opts| opts.banner = "Usage: ec2-snapshot [options]" options[:volume_type] = :all opts.on("--volume-type TYPE", [:all, :root, :data], "Volume types that need to be snapshotted (all/root/data). Defaults to all.") do |volume_type| options[:volume_type] = volume_type end options[:aws_access_key] = ENV["AWS_ACCESS_KEY_ID"] opts.on("--aws-access-key KEY", "AWS Access Key.Either set ENV[\"AWS_ACCESS_KEY_ID\"] or provide it as an option.") do |access_key| options[:aws_access_key] = access_key end options[:aws_secret_access_key] = ENV["AWS_SECRET_ACCESS_KEY"] opts.on("--aws-secret-access-key KEY", "AWS Secret Access Key. Either set ENV[\"AWS_SECRET_ACCESS_KEY\"] or provide it as an option.") do |secret_access_key| options[:aws_secret_access_key] = secret_access_key end opts.on("--aws-region REGION", "AWS Region. Mandatory. Example regions are eu-west-1 and us-west-1") do |region| options[:aws_region] = region end opts.on("--before BEFORE", "Shell command to run before volume is snapshotted. Optional.") do |before| options[:before] = before end opts.on("--after AFTER", "Shell command to run after volume is snapshotted. Optional.") do |after| options[:after] = after end opts.on("--delete-older-than MONTHS", "Delete snapshots for this instance that are older than # months. Optional.") do |months| options[:delete_older_than] = months.to_i end options[:verbose] = false opts.on("-v", "--verbose", "Display progress output.") do |verbose| options[:verbose] = verbose end opts.on("-h", "--help", "Display this screen.") do puts opts exit end end optparse.parse! if options[:aws_access_key].nil? or options[:aws_secret_access_key].nil? or options[:aws_region].nil? Kernel.abort("Not all mandatory options have been provided. Exiting.") end instance = Ec2Snapshot::Instance.new(options) instance.enable_rootvol_snapshot if [:all, :root].include?(options[:volume_type]) instance.enable_datavol_snapshot if [:all, :data].include?(options[:volume_type]) instance.create_snapshots instance.delete_snapshots(Date.today << options[:delete_older_than]) if options.include?(:delete_older_than)