Class: Backup::Syncer::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/backup/syncer/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Configuration::Helpers

#clear_defaults!, #getter_methods, #load_defaults!, #setter_methods

Methods included from CLI

#mkdir, #rm, #run, #utility

Constructor Details

#initialize(&block) ⇒ S3

Instantiates a new S3 Syncer object and sets the default configuration specified in the Backup::Configuration::Syncer::S3. Then it sets the object defaults if particular properties weren’t set. Finally it’ll evaluate the users configuration file and overwrite anything that’s been defined



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/backup/syncer/s3.rb', line 32

def initialize(&block)
  load_defaults!

  @path               ||= 'backups'
  @directories        ||= Array.new
  @mirror             ||= false
  @additional_options ||= []

  instance_eval(&block) if block_given?

  @path = path.sub(/^\//, '')
end

Instance Attribute Details

#access_key_idObject

Amazon Simple Storage Service (S3) Credentials



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

def access_key_id
  @access_key_id
end

#additional_optionsObject

Additional options for the s3sync cli



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

def additional_options
  @additional_options
end

#bucketObject

Amazon S3 bucket name and path to sync to



13
14
15
# File 'lib/backup/syncer/s3.rb', line 13

def bucket
  @bucket
end

#directories(&block) ⇒ Object

Syntactical suger for the DSL for adding directories



17
18
19
# File 'lib/backup/syncer/s3.rb', line 17

def directories
  @directories
end

#mirrorObject

Returns S3Sync syntax for enabling mirroring



21
22
23
# File 'lib/backup/syncer/s3.rb', line 21

def mirror
  @mirror
end

#pathObject

Amazon S3 bucket name and path to sync to



13
14
15
# File 'lib/backup/syncer/s3.rb', line 13

def path
  @path
end

#secret_access_keyObject

Amazon Simple Storage Service (S3) Credentials



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

def secret_access_key
  @secret_access_key
end

Instance Method Details

#add(path) ⇒ Object

Adds a path to the @directories array



94
95
96
# File 'lib/backup/syncer/s3.rb', line 94

def add(path)
  @directories << path
end

#optionsObject

Returns all the specified S3Sync options, concatenated, ready for the CLI



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

def options
  ([verbose, recursive, mirror] + additional_options).compact.join("\s")
end

#perform!Object

Performs the S3Sync operation First it’ll set the Amazon S3 credentials for S3Sync before invoking it, and once it’s finished syncing the files and directories to Amazon S3, it’ll unset these credentials (back to nil values)



50
51
52
53
54
55
56
57
58
59
# File 'lib/backup/syncer/s3.rb', line 50

def perform!
  set_s3sync_credentials!

  directories.each do |directory|
    Logger.message("#{ self.class } started syncing '#{ directory }'.")
    Logger.silent( run("#{ utility(:s3sync) } #{ options } '#{ directory }' '#{ bucket }:#{ path }'") )
  end

  unset_s3sync_credentials!
end

#recursiveObject

Returns S3Sync syntax for syncing recursively



75
76
77
# File 'lib/backup/syncer/s3.rb', line 75

def recursive
  '--recursive'
end

#set_s3sync_credentials!Object

In order for S3Sync to know what credentials to use, we have to set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables, these evironment variables will be used by S3Sync



102
103
104
105
# File 'lib/backup/syncer/s3.rb', line 102

def set_s3sync_credentials!
  ENV['AWS_ACCESS_KEY_ID']     = access_key_id
  ENV['AWS_SECRET_ACCESS_KEY'] = secret_access_key
end

#unset_s3sync_credentials!Object

Sets the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY back to nil



109
110
111
112
# File 'lib/backup/syncer/s3.rb', line 109

def unset_s3sync_credentials!
  ENV['AWS_ACCESS_KEY_ID']     = nil
  ENV['AWS_SECRET_ACCESS_KEY'] = nil
end

#verboseObject

Returns S3Sync syntax for making output verbose



81
82
83
# File 'lib/backup/syncer/s3.rb', line 81

def verbose
  '--verbose'
end