Class: Backup::S3Actor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ S3Actor

Returns a new instance of S3Actor.



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

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]}"
  Base.establish_connection!(
    :access_key_id     => @access_key,
    :secret_access_key => @secret_key
  )
  begin
    # Look for our bucket, if it's not there, try to create it.
    @bucket = Bucket.find @bucket_key
  rescue NoSuchBucket
    @bucket = Bucket.create @bucket_key
    @bucket = Bucket.find @bucket_key
  end   
end

Instance Attribute Details

#configObject (readonly) Also known as: c

Returns the value of attribute config.



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

def config
  @config
end

#rotationObject

Returns the value of attribute rotation.



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

def rotation
  @rotation
end

Instance Method Details

#cleanup(generation, keep) ⇒ Object

Expire old objects



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/backup/s3_helpers.rb', line 71

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

#delete(object_key) ⇒ Object

Remove a file from s3



52
53
54
# File 'lib/backup/s3_helpers.rb', line 52

def delete(object_key)
  S3Object.delete object_key, @bucket.name
end

#put(last_result) ⇒ Object

Send a file to s3



42
43
44
45
46
47
48
49
# File 'lib/backup/s3_helpers.rb', line 42

def put(last_result)
  puts last_result
  object_key = Rotator.timestamped_prefix(last_result)
  S3Object.store object_key,
                 open(last_result),
                 @bucket.name
  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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/backup/s3_helpers.rb', line 58

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