Class: Backup::Connection::S3

Inherits:
Object
  • Object
show all
Includes:
Backup::CommandHelper
Defined in:
lib/backup/connection/s3.rb

Constant Summary collapse

MAX_S3_FILE_SIZE =
5368709120 - 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Backup::CommandHelper

#log, #run

Constructor Details

#initialize(adapter = false) ⇒ S3

Initializes the S3 connection, setting the values using the S3 adapter



13
14
15
16
17
18
19
20
21
# File 'lib/backup/connection/s3.rb', line 13

def initialize(adapter = false)
  if adapter
    self.adapter            = adapter
    self.procedure          = adapter.procedure
    self.final_file         = adapter.final_file
    self.tmp_path           = adapter.tmp_path.gsub('\ ', ' ')
    load_storage_configuration_attributes
  end
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def access_key_id
  @access_key_id
end

#adapterObject

Returns the value of attribute adapter.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def adapter
  @adapter
end

#final_fileObject

Returns the value of attribute final_file.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def final_file
  @final_file
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def host
  @host
end

#procedureObject

Returns the value of attribute procedure.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def procedure
  @procedure
end

#s3_bucketObject

Returns the value of attribute s3_bucket.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def s3_bucket
  @s3_bucket
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def secret_access_key
  @secret_access_key
end

#tmp_pathObject

Returns the value of attribute tmp_path.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def tmp_path
  @tmp_path
end

#use_sslObject

Returns the value of attribute use_ssl.



10
11
12
# File 'lib/backup/connection/s3.rb', line 10

def use_ssl
  @use_ssl
end

Instance Method Details

#connectionObject

Establishes a connection with Amazon S3 using the credentials provided by the user



30
31
32
33
34
35
# File 'lib/backup/connection/s3.rb', line 30

def connection
  @_connection ||= Fog::AWS::Storage.new(
    :aws_access_key_id => access_key_id,
    :aws_secret_access_key => secret_access_key
  )
end

#destroy(file, bucket_as_string) ⇒ Object

Destroys file from a bucket on Amazon S3



63
64
65
66
# File 'lib/backup/connection/s3.rb', line 63

def destroy(file, bucket_as_string)
  self.connection.put_bucket(s3_bucket)
  connection.delete_object(s3_bucket, file)
end

#static_initialize(procedure) ⇒ Object

Sets values from a procedure, rather than from the adapter object



24
25
26
27
# File 'lib/backup/connection/s3.rb', line 24

def static_initialize(procedure)
  self.procedure = procedure
  load_storage_configuration_attributes(true)
end

#storeObject

Initializes the file transfer to Amazon S3 This can only run after a connection has been made using the #connect method



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/backup/connection/s3.rb', line 39

def store
  #TODO: need to add logic like this to restore: `cat /mnt/backups/part.xx >>restore.tgz`
  tmp_file_path = File.join(tmp_path, final_file)
  store_files = []
  if File.stat(File.join(tmp_path, final_file)).size >= MAX_S3_FILE_SIZE
    #we need to split!
    `split -b #{MAX_S3_FILE_SIZE}  #{tmp_file_path} #{tmp_file_path}.`
    store_files += `ls  #{tmp_file_path}.*`.split
    log("Splitting '#{final_file}' into #{store_files.length} parts as it is too large for s3.")
  else
    store_files << tmp_file_path
  end

  #lets make sure it exists
  self.connection.put_bucket(s3_bucket)
  
  store_files.each do |tmp_file|
    file_name = File.basename(tmp_file)
    log("Saving '#{file_name}' to s3 bucket '#{s3_bucket}'")
    self.connection.put_object(s3_bucket, file_name, open(tmp_file))
  end
end