Class: MysqlS3Backup::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_s3_backup/bucket.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Bucket

Returns a new instance of Bucket.



5
6
7
8
9
10
# File 'lib/mysql_s3_backup/bucket.rb', line 5

def initialize(name, options)
  @name = name
  @s3_options = options.symbolize_keys.merge(:use_ssl => true)
  connect
  create
end

Instance Method Details

#connectObject



12
13
14
# File 'lib/mysql_s3_backup/bucket.rb', line 12

def connect
  AWS::S3::Base.establish_connection!(@s3_options)
end

#copy(file_name, new_file_name) ⇒ Object



25
26
27
# File 'lib/mysql_s3_backup/bucket.rb', line 25

def copy(file_name, new_file_name)
  AWS::S3::S3Object.copy(file_name, new_file_name, @name)
end

#createObject



16
17
18
19
# File 'lib/mysql_s3_backup/bucket.rb', line 16

def create
  # It doesn't hurt to try to create a bucket that already exists
  AWS::S3::Bucket.create(@name)
end

#delete_all(prefix) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mysql_s3_backup/bucket.rb', line 41

def delete_all(prefix)
  # Wrap the objects' deletion in a loop to ensure we really do delete them all.
  #
  # This should not be necessary but, for reasons I haven't yet fathomed, the
  # `obj.delete` seems to increment some internal iteration pointer by two instead
  # of one.  So instead of deleting each object in turn, it deletes the first, the
  # third, the fifth, etc.
  #
  # The `each` iterator behaves correctly; this does what you'd expect:
  #
  #     AWS::S3::Bucket.objects(@name, :prefix => prefix).each { |obj| puts obj.key }
  #
  # It's only when `delete` gets involved that things go pear shaped.
  while (size = AWS::S3::Bucket.objects(@name, :prefix => prefix).size) > 0
    AWS::S3::Bucket.objects(@name, :prefix => prefix).each { |obj| obj.delete }
  end
end

#fetch(file_name, file) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/mysql_s3_backup/bucket.rb', line 29

def fetch(file_name, file)
  open(file, 'w') do |f|
    AWS::S3::S3Object.stream(file_name, @name) do |chunk|
      f.write chunk
    end
  end
end

#find(prefix) ⇒ Object



37
38
39
# File 'lib/mysql_s3_backup/bucket.rb', line 37

def find(prefix)
  AWS::S3::Bucket.objects(@name, :prefix => prefix).map { |obj| obj.key }
end

#store(file_name, file) ⇒ Object



21
22
23
# File 'lib/mysql_s3_backup/bucket.rb', line 21

def store(file_name, file)
  AWS::S3::S3Object.store(file_name, open(file), @name)
end