Class: MysqlBackup::Storage::S3

Inherits:
MysqlBackup::Storage show all
Defined in:
lib/mysql_backup/storage/s3.rb

Instance Attribute Summary collapse

Attributes inherited from MysqlBackup::Storage

#log

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ S3

Takes:

:access_key_id     => 'abc',
:secret_access_key => '123'
:bucket => 'name_of_the_backup_bucket'


16
17
18
19
# File 'lib/mysql_backup/storage/s3.rb', line 16

def initialize args = {}
  super
  connect! args unless args[:no_connect]
end

Instance Attribute Details

#bucketObject

The name of the bucket storing backup objects



6
7
8
# File 'lib/mysql_backup/storage/s3.rb', line 6

def bucket
  @bucket
end

#bucket_objObject



103
104
105
# File 'lib/mysql_backup/storage/s3.rb', line 103

def bucket_obj
  @bucket_obj ||= AWS::S3::Bucket.find bucket
end

Instance Method Details

#conditional_save(args) ⇒ Object

identifier is a MysqlBackup::Entity::Identifier object.



32
33
34
35
36
37
38
39
# File 'lib/mysql_backup/storage/s3.rb', line 32

def conditional_save args
  identifier = args[:identifier]
  if include? identifier
    log && log.info("not saving; object exists in (#{@bucket}): #{identifier}")
  else
    save args
  end
end

#connect!(args) ⇒ Object



107
108
109
# File 'lib/mysql_backup/storage/s3.rb', line 107

def connect! args
  AWS::S3::Base.establish_connection! :access_key_id => args[:access_key_id], :secret_access_key => args[:secret_access_key]
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/mysql_backup/storage/s3.rb', line 45

def include? key
  @existing_objects ||= {}
  @existing_objects.include? key.to_s
end

#mark_as_existing(key) ⇒ Object



50
51
52
53
# File 'lib/mysql_backup/storage/s3.rb', line 50

def mark_as_existing key
  @existing_objects ||= {}
  @existing_objects[key.to_s] = nil
end

#read_existing_objectsObject



55
56
57
58
59
60
# File 'lib/mysql_backup/storage/s3.rb', line 55

def read_existing_objects
  yield_objects do |o|
    mark_as_existing s3_object_to_identifier(o).to_s
    true
  end
end

#retrieve_backup_and_then_yield_file(group) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/mysql_backup/storage/s3.rb', line 87

def retrieve_backup_and_then_yield_file group
  Tempfile.open 'backup_retrieval' do |f|
    group.identifiers.each do |i|
      AWS::S3::S3Object.stream i.to_s, bucket do |chunk|
        f.write chunk
      end
    end
    f.seek 0
    yield f
  end
end

#rm(identifier) ⇒ Object



41
42
43
# File 'lib/mysql_backup/storage/s3.rb', line 41

def rm identifier
  AWS::S3::S3Object.delete identifier.to_s, bucket
end

#s3_object_to_identifier(o) ⇒ Object



99
100
101
# File 'lib/mysql_backup/storage/s3.rb', line 99

def s3_object_to_identifier o
  MysqlBackup::Entity::Identifier.build_object :string => o.key, :timestamp => o.last_modified
end

#save(args) ⇒ Object

identifier is a MysqlBackup::Entity::Identifier object.



23
24
25
26
27
28
# File 'lib/mysql_backup/storage/s3.rb', line 23

def save args
  identifier = args[:identifier]
  log && log.info("saving to S3(#{@bucket}): #{identifier}")
  AWS::S3::S3Object.store identifier.to_s, args[:file].open, @bucket
  mark_as_existing identifier.to_s
end

#yield_backup_objectsObject



73
74
75
76
77
78
# File 'lib/mysql_backup/storage/s3.rb', line 73

def yield_backup_objects
  yield_objects do |o|
    i = MysqlBackup::Entity::Identifier.build_object :string
    yield o if i
  end
end

#yield_identifiersObject



80
81
82
83
84
85
# File 'lib/mysql_backup/storage/s3.rb', line 80

def yield_identifiers
  yield_objects do |o|
    i = s3_object_to_identifier o
    yield i if i
  end
end

#yield_objects(n_keys = 1000) ⇒ Object

:nodoc:



62
63
64
65
66
67
68
69
70
71
# File 'lib/mysql_backup/storage/s3.rb', line 62

def yield_objects n_keys = 1000 #:nodoc:
  objs = []
  while objs
    objs = bucket_obj.objects :max_keys => n_keys, :marker => (objs.empty? ? nil : objs.last.key)
    objs.each do |o|
      yield o
    end
    objs = nil if objs.empty? || objs.length < n_keys
  end
end