-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-backup.rb
330 lines (253 loc) · 5.46 KB
/
server-backup.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
require 'open3'
require 'optparse'
require 'fileutils'
require 'tmpdir'
Config = {
:dests => [],
:srcs => [],
:servername => [],
:passphrase => ""
}
Options = {}
class Destination
# The url/path for duplicity
def duplicityURL
nil
end
# Additional options for duplicity
def duplicityOptions
[]
end
# Additional env for duplicity
def duplicityENV
{}
end
def prettyName
nil
end
end
class DestinationS3 < Destination
@bucket_name
@keyID
@key
def bucket(bucket_name)
@bucket_name = bucket_name
end
def accessKey(keyID, key)
@keyID = keyID
@key = key
end
def duplicityENV
env = {}
env["AWS_ACCESS_KEY_ID"] = @keyID
env["AWS_SECRET_ACCESS_KEY"] = @key
super.merge env
end
def prettyName
"s3:#{@bucket_name}"
end
def duplicityURL
"s3+http://#{@bucket_name}/#{Config[:servername].downcase}"
end
def duplicityOptions
options = [
"--s3-use-new-style"
]
super << options
end
end
class Source
@src
def initialize(src)
@src = src
end
def prettyName
@src
end
# The url/path for duplicity
def duplicityURL
nil
end
# Additional options for duplicity
def duplicityOptions
[]
end
# Additional env for duplicity
def duplicityENV
{}
end
# Run before the backup
def pre
end
# Run after the backup
def post
end
# A shot name for the dest
def destName
@src
end
end
class SourceDir < Source
def duplicityURL
@src
end
def destName
File.basename(@src)
end
end
class SourceMysql < Source
@tmpdir
@db_user
@db_password
def prettyName
"mysql:#{@src}"
end
def destName
"mysql-#{@src}"
end
def pre
@tmpdir = Dir.mktmpdir("mysql-backup")
at_exit {
FileUtils.rm_rf @tmpdir
}
createMySQLConfigFile
cmd = "mysqldump --defaults-extra-file=#{File.join(@tmpdir, "my.cnf")} -u#{db_user} #{@src} > #{File.join(@tmpdir, "dump.sql")}"
puts cmd if Options[:verbose]
success = system(cmd)
raise "Error creating dump" unless success
end
def createMySQLConfigFile
open(File.join(@tmpdir, "my.cnf"), "w") do |f|
f.write <<-conf
[client]
password=#{db_password}
conf
end
end
def db_password
@db_password || Config[:db_password]
end
def db_user
@db_user || Config[:db_user]
end
def auth(user, password)
@db_password = password
@db_user = user
end
def post
FileUtils.rm_rf @tmpdir
end
def duplicityURL
@tmpdir
end
def duplicityOptions
["--exclude=#{File.join(@tmpdir, "my.cnf")}", "--allow-source-mismatch"]
end
end
def set(key, value)
Config[key] = value
end
def dest(type, &block)
destination = Object.const_get("Destination" + type.to_s().capitalize).new
block.arity < 1 ? destination.instance_eval(&block) : block.call(source) if block_given?
Config[:dests] << destination
end
def src(type, src, &block)
source = Object.const_get("Source" + type.to_s().capitalize).new(src)
block.arity < 1 ? source.instance_eval(&block) : block.call(source) if block_given?
Config[:srcs] << source
end
def load_config(file)
load file,true
end
class ServerBackup
def initialize
@failed = {}
end
def run
Config[:srcs].each do |src|
runFor(src)
end
puts
if @failed.size > 0
puts 'During backups a error did occour:'
@failed.each do |name, log|
puts "#{name} failed:"
puts log
puts
end
exit 1
else
puts 'Everything succeded =)'
exit 0
end
end
def runFor(src)
puts "Backup #{src.prettyName}"
begin
src.pre
Config[:dests].each do |dest|
print " -> #{dest.prettyName}..."
ok = false
log = nil
begin
ok, log = runDuplicity(src, dest)
rescue Exception => e
ok = false
log = e.message
end
if ok
puts "ok."
else
@failed["#{src.prettyName}->#{dest.prettyName}"] = log
puts "failed."
end
end
src.post
rescue Exception => e
@failed["#{src.prettyName}"] = e.message
puts "failed."
end
end
def runDuplicity(src, dest)
env = {
'PASSPHRASE' => Config[:passphrase]
}
env.update dest.duplicityENV
env.update src.duplicityENV
options = []
options << dest.duplicityOptions
options << src.duplicityOptions
options << src.duplicityURL
options << File.join(dest.duplicityURL, src.destName)
cmd = ["duplicity", *options].join " "
exit_status = 0
log = nil
puts cmd if Options[:verbose]
Open3.popen2e(env, cmd) { |stdin, stdout_stderr, wait_thr|
exit_status = wait_thr.value
if exit_status != 0
log = stdout_stderr.readlines()
end
}
return exit_status == 0, log
end
end
optparse = OptionParser.new do |opts|
opts.banner = "Usage server-backup.rb [options]"
Options[:conf] = '/etc/server-backup.conf'
opts.on '-c', '--config FILE', 'Use a config file' do |file|
Options[:conf] = file
end
Options[:verbose] = false
opts.on '-v', '--verbose', 'Verbose output' do
Options[:verbose] = true
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
load_config Options[:conf]
ServerBackup.new.run