Class: Backup::S3Actor

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

Direct Known Subclasses

ChunkingS3Actor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ S3Actor

Returns a new instance of S3Actor.



11
12
13
14
15
16
17
18
19
# File 'lib/backup/s3_helpers.rb', line 11

def initialize(config)
  @config       = config
  @rotation_key = c[:rotation_object_key] ||= 'backup_rotation_index.yml'
  @access_key   = c[:aws_access] ||= ENV['AMAZON_ACCESS_KEY_ID']
  @secret_key   = c[:aws_secret] ||= ENV['AMAZON_SECRET_ACCESS_KEY']
  @bucket_key   = "#{@access_key}.#{c[:backup_path]}"
  @s3 = RightAws::S3.new(@access_key, @secret_key)
  @bucket = @s3.bucket(@bucket_key, true)
end

Instance Attribute Details

#configObject (readonly) Also known as: c

Returns the value of attribute config.



8
9
10
# File 'lib/backup/s3_helpers.rb', line 8

def config
  @config
end

#rotationObject

Returns the value of attribute rotation.



6
7
8
# File 'lib/backup/s3_helpers.rb', line 6

def rotation
  @rotation
end

Instance Method Details

#cleanup(generation, keep) ⇒ Object

Expire old objects



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/backup/s3_helpers.rb', line 60

def cleanup(generation, keep)
  puts "Cleaning up #{generation} #{keep}"
  
  new_rotation = self.rotation
  keys = new_rotation[generation]
        
  diff = keys.size - keep
  
  1.upto( diff ) do
    extra_key = keys.shift
    delete extra_key
  end
        
  # store updated index
  self.rotation = new_rotation
end

#delete(object_key) ⇒ Object

Remove a file from s3



40
41
42
43
# File 'lib/backup/s3_helpers.rb', line 40

def delete(object_key)
  puts "delete: #{object_key}"
  @bucket.key(object_key).delete
end

#put(last_result) ⇒ Object

Send a file to s3



32
33
34
35
36
37
# File 'lib/backup/s3_helpers.rb', line 32

def put(last_result)
  object_key = Rotator.timestamped_prefix(last_result)
  puts "put: #{object_key}"
  @bucket.put(object_key, open(last_result))
  object_key
end

#verify_rotation_hierarchy_exists(hierarchy) ⇒ Object

Make sure our rotation index exists and contains the hierarchy we’re using. Create it if it does not exist



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backup/s3_helpers.rb', line 47

def verify_rotation_hierarchy_exists(hierarchy)
  index = self.rotation
  if index
    verified_index = index.merge(init_rotation_index(hierarchy)) { |m,x,y| x ||= y }
    unless (verified_index == index)
      self.rotation = verified_index
    end
  else
    self.rotation = init_rotation_index(hierarchy)
  end
end