Class: Fallout::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/fallout/backup.rb

Constant Summary collapse

EXPIRES_AFTER_KEY =
'expires_after'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Backup

Returns a new instance of Backup.



5
6
7
8
9
10
# File 'lib/fallout/backup.rb', line 5

def initialize(options)
  @volume_id = options[:volume]
  @keep = options[:keep].to_i
  @expires_after = Date.today + @keep
  @ec2 = AWS::EC2.new
end

Instance Method Details

#delete_expired_snapshotsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fallout/backup.rb', line 12

def delete_expired_snapshots
  snapshots = @ec2.snapshots.filter('volume-id', @volume_id)
  snapshots = snapshots.map do |ss|
    begin
      da = Date.parse(ss.tags.to_h[EXPIRES_AFTER_KEY])
      if da < Date.today
        ss.delete
        ss
      end
    rescue
      next
    end
  end
  snapshots.compact
end

#runObject



28
29
30
31
32
33
34
35
# File 'lib/fallout/backup.rb', line 28

def run
  @volume = @ec2.volumes[@volume_id]
  raise "Volume does not exist: #{@volume_id}" if @volume.nil? || !@volume.exists?
  desc = "Snapshot for volume #{@volume_id}, will be deleted after #{@expires_after}"
  snapshot = @volume.create_snapshot(desc)
  snapshot.add_tag(EXPIRES_AFTER_KEY, value: @expires_after.to_s)
  [snapshot, @expires_after]
end