-
-
Save lukaszx0/605049 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
=begin | |
backshot.rb is a Ruby program driving | |
Amazon EBS <http://aws.amazon.com/ebs/> | |
to generate snapshots for backup purposes. | |
Hourly, daily, weekly, and monthly snapshots | |
are generated by default. | |
Requires amazon-ec2 gem: | |
<http://github.com/grempe/amazon-ec2> | |
To run this program configure your Amazon ID, key, and | |
choose which EBS volume to backup. Finally, add a | |
cron job to run this program hourly. | |
A big thanks goes out to <http://chi.mp> | |
for sponsering this program. | |
Copyright 2009 Darrin Eden | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
=end | |
require 'rubygems' | |
require 'EC2' | |
require 'yaml' | |
require 'date' | |
ACCESS_KEY_ID = 'your access key' | |
SECRET_ACCESS_KEY = 'your secret key' | |
VOLUME = 'vol-01234567' | |
SNAPSHOTS = '/etc/snapshots.yml' | |
class Snapshots | |
def snaps | |
@snaps ||= {} | |
end | |
def units | |
@units ||= {} | |
end | |
def create(unit_time) | |
ec2 = EC2::Base.new( :access_key_id => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY ) | |
previous = snaps[ unit_time ] | |
snap = ec2.create_snapshot( :volume_id => VOLUME ) | |
snaps[ unit_time ] = snap.snapshotId | |
units[ unit_time ] = DateTime.parse( snap.startTime ).send( unit_time ) | |
unless previous.nil? | |
ec2.delete_snapshot( :snapshot_id => previous ) | |
end | |
end | |
end | |
if File.exists?( SNAPSHOTS ) | |
shots = YAML::load_file( SNAPSHOTS ) | |
else | |
shots = Snapshots.new | |
end | |
%w(hour yday cweek month).each do |unit_time| | |
unless shots.units[ unit_time] == DateTime.now.new_offset.send( unit_time ) | |
shots.create( unit_time ) | |
end | |
end | |
File.open( SNAPSHOTS, 'w' ) { |f| f.puts shots.to_yaml } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment