forked from backup/backup
/
s3.rb
118 lines (96 loc) · 3.25 KB
/
s3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# encoding: utf-8
module Backup
module Syncer
class S3 < Base
##
# Amazon Simple Storage Service (S3) Credentials
attr_accessor :access_key_id, :secret_access_key
##
# Amazon S3 bucket name and path to sync to
attr_accessor :bucket, :path
##
# Directories to sync
attr_accessor :directories
##
# Flag to enable mirroring
attr_accessor :mirror
##
# Additional options for the s3sync cli
attr_accessor :additional_options
##
# 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
def initialize(&block)
load_defaults!
@path ||= 'backups'
@directories ||= Array.new
@mirror ||= false
@additional_options ||= []
instance_eval(&block) if block_given?
@path = path.sub(/^\//, '')
end
##
# 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)
def perform!
set_environment_variables!
directories.each do |directory|
Logger.message("#{ self.class } started syncing '#{ directory }'.")
Logger.silent( run("#{ utility(:s3sync) } #{ options } '#{ directory }' '#{ bucket }:#{ path }'") )
end
unset_environment_variables!
end
##
# Returns all the specified S3Sync options, concatenated, ready for the CLI
def options
([verbose, recursive, mirror] + additional_options).compact.join("\s")
end
##
# Returns S3Sync syntax for enabling mirroring
def mirror
'--delete' if @mirror
end
##
# Returns S3Sync syntax for syncing recursively
def recursive
'--recursive'
end
##
# Returns S3Sync syntax for making output verbose
def verbose
'--verbose'
end
##
# Syntactical suger for the DSL for adding directories
def directories(&block)
return @directories unless block_given?
instance_eval(&block)
end
##
# Adds a path to the @directories array
def add(path)
@directories << path
end
##
# 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
def set_environment_variables!
ENV['AWS_ACCESS_KEY_ID'] = access_key_id
ENV['AWS_SECRET_ACCESS_KEY'] = secret_access_key
ENV['AWS_CALLING_FORMAT'] = 'SUBDOMAIN'
end
##
# Sets the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY back to nil
def unset_environment_variables!
ENV['AWS_ACCESS_KEY_ID'] = nil
ENV['AWS_SECRET_ACCESS_KEY'] = nil
ENV['AWS_CALLING_FORMAT'] = nil
end
end
end
end