-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackups.rb
executable file
·410 lines (358 loc) · 14.8 KB
/
backups.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env ruby
#
# rdiff-backup and duplicity wrapper script
# SjG <[email protected]>
# $Id: backups.rb,v 1.16 2007/09/14 17:35:26 samuel Exp $
version = 0.2
version_date = "1 February 2007"
#
require 'yaml'
require 'optparse'
require 'net/smtp'
require 'pp'
def target_string_base(desc)
cmd = ""
if ! is_local(desc)
cmd += "#{desc['username']}@" unless desc['username'].nil? || desc['username'].empty?
cmd += desc['host'] || 'localhost'
end
cmd
end
def msg(text)
$theReport += text.to_s + "\n"
puts text
end
def local_command(cmd, config)
if config['testing'] || config['echo-commands']
msg "CMD: #{cmd}"
end
if ! config['testing']
begin
res =`#{cmd} 2>&1`
if $? != 0
msg "ERROR! Command reported error condition. Full command response: "+res
$success = false
else
msg "RES: "+res if config['verbose']
end
rescue
msg "ERROR! "+$!
$success = false
end
end
return res
end
def local_or_remote_cmd(command,command_name,target,config)
if (command && ! command.empty?)
msg "Running "+command_name if config['verbose']
cmd = ""
cmd += "#{config['ssh-command']} " if ! is_local(target)
cmd += target_string_base(target) + " " + command
local_command(cmd, config)
end
end
def is_local(target)
! target['host'] || target['host'].empty? || target['host'] == "local"
end
def test_restore_rdiff_backup(thisBackup, config)
cmd = "#{config['rdiff-command']} #{thisBackup['rdiff-restore-flags']} --force --restore-as-of now "
cmd += target_string_base(thisBackup['destination'])+"::" if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}/backup-metadata.txt /tmp/backup-metadata.txt"
msg "Restoring up backup-metadata.txt from #{thisBackup['source']['host']} to /tmp/backup-metadata.txt for checking" if config['verbose']
local_command(cmd, config)
test_restored_file(thisBackup, config)
end
def backup_rdiff_backup(thisBackup, config)
cmd = "#{config['rdiff-command']} #{thisBackup['rdiff-backup-flags']} --exclude-device-files "
thisBackup['source']['exclude'].split(%r{[,\s]+}).each {|thisExcl| cmd += "--exclude #{thisExcl} "} if thisBackup['source']['exclude'] && !thisBackup['source']['exclude'].empty?
cmd += target_string_base(thisBackup['source'])+"::" if ! is_local(thisBackup['source'])
cmd += "#{thisBackup['source']['directory']} "
cmd += target_string_base(thisBackup['destination'])+"::" if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}"
msg "Backing up #{thisBackup['source']['directory']} on #{thisBackup['source']['host']}" if config['verbose']
local_command(cmd, config)
end
def verify_signatures_rdiff_backup(thisBackup, config)
cmd = "#{config['rdiff-command']} #{thisBackup['rdiff-verify-flags']} --exclude-device-files --verify "
thisBackup['source']['exclude'].split(%r{[,\s]+}).each {|thisExcl| cmd += "--exclude #{thisExcl} "} if thisBackup['source']['exclude'] && !thisBackup['source']['exclude'].empty?
cmd += target_string_base(thisBackup['source'])+"::" if ! is_local(thisBackup['source'])
cmd += "#{thisBackup['source']['directory']} "
cmd += target_string_base(thisBackup['destination'])+"::" if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}"
msg "Verifying #{thisBackup['source']['directory']} on #{thisBackup['source']['host']}" if config['verbose']
local_command(cmd, config)
end
def cleanup_rdiff_backup(thisBackup, config)
cmd = "#{config['rdiff-command']} #{thisBackup['rdiff-cleanup-flags']} --force remove-older-than #{thisBackup['preserve']} "
cmd += target_string_base(thisBackup['destination'])+"::" if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}"
msg "Purging increments older that #{thisBackup['preserve']}" if config['verbose']
local_command(cmd, config)
end
def duplicity_dest_spec(thisBackup)
if is_local(thisBackup['destination'])
cmd = "s3+http://"
else
thisBackup['destination']['protocol'] = thisBackup['destination']['protocol'] || "scp"
case thisBackup['destination']['protocol']
when "scp"
cmd = "scp://"
when "ftp"
cmd = "ftp://"
ENV['FTP_PASSWORD']= thisBackup['destination']['password']
when "s3"
cmd = "s3+http://"
ENV['AWS_ACCESS_KEY_ID']=thisBackup['destination']['duplicity-aws-access-key-id'] if thisBackup['destination']['duplicity-aws-access-key-id']
ENV['AWS_SECRET_ACCESS_KEY']=thisBackup['destination']['duplicity-aws-secret-access-key'] if thisBackup['destination']['duplicity-aws-secret-access-key']
end
end
cmd
end
def backup_duplicity(thisBackup, config)
cmd = "#{config['duplicity-command']} #{thisBackup['duplicity-backup-flags']} --exclude-device-files "
thisBackup['source']['exclude'].split(%r{[,\s]+}).each {|thisExcl| cmd += "--exclude #{thisExcl} "} if thisBackup['source']['exclude'] && !thisBackup['source']['exclude'].empty?
cmd += "#{thisBackup['source']['directory']} "
cmd += duplicity_dest_spec(thisBackup)
ENV['PASSPHRASE'] = thisBackup['destination']['pgp_passphrase']
cmd += target_string_base(thisBackup['destination']) if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}"
msg "Backing up #{thisBackup['source']['directory']} on #{thisBackup['source']['host']}" if config['verbose']
local_command(cmd, config)
end
def cleanup_duplicity(thisBackup, config)
cmd = "#{config['duplicity-command']} remove-older-than #{thisBackup['preserve']} --force #{thisBackup['duplicity-cleanup-flags']} "
cmd += duplicity_dest_spec(thisBackup)
ENV['PASSPHRASE'] = thisBackup['destination']['pgp_passphrase']
cmd += target_string_base(thisBackup['destination']) if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}"
msg "Purging increments older that #{thisBackup['preserve']}" if config['verbose']
local_command(cmd, config)
end
def test_restore_duplicity(thisBackup, config)
cmd = "#{config['duplicity-command']} #{thisBackup['duplicity-restore-flags']} --file-to-restore backup-metadata.txt "
cmd += duplicity_dest_spec(thisBackup)
ENV['PASSPHRASE'] = thisBackup['destination']['pgp_passphrase']
cmd += target_string_base(thisBackup['destination']) if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']} /tmp/backup-metadata.txt"
msg "Restoring up backup-metadata.txt from #{thisBackup['source']['host']} to /tmp/backup-metadata.txt for checking" if config['verbose']
local_command(cmd, config)
test_restored_file(thisBackup, config)
end
def verify_signatures_duplicity(thisBackup, config)
cmd = "#{config['duplicity-command']} #{thisBackup['duplicity-verify-flags']} --verify --exclude-device-files "
thisBackup['source']['exclude'].split(%r{[,\s]+}).each {|thisExcl| cmd += "--exclude #{thisExcl} "} if thisBackup['source']['exclude'] && !thisBackup['source']['exclude'].empty?
cmd += "#{thisBackup['source']['directory']} "
cmd += duplicity_dest_spec(thisBackup)
ENV['PASSPHRASE'] = thisBackup['destination']['pgp_passphrase']
cmd += target_string_base(thisBackup['destination']) if ! is_local(thisBackup['destination'])
cmd += "#{thisBackup['destination']['directory']}"
msg "Verifying #{thisBackup['source']['directory']} on #{thisBackup['source']['host']}" if config['verbose']
local_command(cmd, config)
end
def test_restored_file(thisBackup, config)
if ! config['testing']
file_cont = []
begin
fin = File.open("/tmp/backup-metadata.txt", "r")
while line = fin.gets
file_cont.push(line.chomp)
end
rescue
msg "ERROR! "+$!
$success = false
ensure
fin.close unless fin.nil?
end
if file_cont[0] != thisBackup['temp-file']
# restore failed!
msg "Failed! [#{file_cont[0]}] != [#{thisBackup['temp-file']}]"
$success = false
else
# succeeded!
msg "Succeeded!"
end
cmd = "rm /tmp/backup-metadata.txt"
local_command(cmd, config)
end
end
def write_testfile(thisBackup, config)
if is_local(thisBackup['source'])
if config['testing'] || config['echo-commands']
msg "Write temp file #{thisBackup['source']['directory']}/backup-metadata.txt"
end
if ! config['testing']
begin
fout = File.open("#{thisBackup['source']['directory']}/backup-metadata.txt", "w")
fout.puts("#{thisBackup['temp-file']}")
rescue
msg "ERROR! "+$!
$success = false
ensure
fout.close unless fout.nil?
end
end
else
if config['testing'] || config['echo-commands']
msg "Write temp file #{thisBackup['source']['host']}::#{thisBackup['source']['directory']}/backup-metadata.txt"
end
if ! config['testing']
local_or_remote_cmd("timestamp #{thisBackup['source']['directory']} \"#{thisBackup['temp-file']}\"",
"Write temp file", thisBackup['source'], config)
end
end
end
# -----------------------------------------------------
# Main Operation
# -----------------------------------------------------
start_time = Time.new
task_file = 'backup-tasks.yaml'
$theReport = ""
$success = true
allitems = true
config_over_rides = {}
OptionParser.new do |opts|
opts.banner = "Usage: backups.rb [options]"
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
config_over_rides['verbose'] = v
end
opts.on("-b", "--backup-tasks task-file", "Backup task yaml file (default backup-tasks.yaml)") do |b|
task_file = b;
end
opts.on("-t", "--[no-]test", "Run in test mode (do not issue commands)") do |t|
config_over_rides['testing'] = t
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "backup.rb version #{version}: #{version_date}"
exit
end
end.parse!
begin
config = YAML.load_file('backup-config.yaml')
config.merge!(config_over_rides)
backups = YAML.load_file(task_file)
msg '-='*35 + '-'
msg Time.new
msg 'Starting backup process...'
msg '-='*35 + '-'
msg 'Running in test mode -- no actual commands will be issued' if config['testing']
backups.each do
|thisBackup|
op_time = Time.new
msg '-'*71
msg "Backup task: #{thisBackup['name']}\n\n" if thisBackup['name'] && ! thisBackup['name'].empty?
thisBackup['temp-file'] = "backup-rt-"+Time.now.localtime.strftime("%Y-%m-%d")+"."+(1..4).collect { (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }.join+".txt"
thisBackup['mail-out'] = ""
if thisBackup['option-set'] && ! thisBackup['option-set'].empty?
config['option-sets'][thisBackup['option-set']].each do
|key,val| thisBackup[key] = thisBackup[key] || val
end
end
['source','destination'].each do
|target|
if thisBackup[target]['location'] && !thisBackup[target]['location'].empty?
['host','directory','protocol','username','password','pgp-passphrase','exclude'].each do
|var| thisBackup[target][var] = thisBackup[target][var] ||
config['locations'][thisBackup[target]['location']][var] || ''
end
end
end
['preserve','rdiff-backup-flags','rdiff-restore-flags','rdiff-verify-flags',
'rdiff-cleanup-flags','duplicity-backup-flags','duplicity-restore-flags',
'duplicity-verify-flags','duplicity-cleanup-flags'].each do
|param|
thisBackup[param] = thisBackup[param] || config[param]
end
if ! thisBackup.has_key?('disabled') || ! thisBackup['disabled']
# a few more defaults
thisBackup['source']['host'] = thisBackup['source']['host'] || "local"
thisBackup['destination']['host'] = thisBackup['destination']['host'] || "local"
if thisBackup['destination']['host'] == 'local'
if ! FileTest.exists?(thisBackup['destination']['directory'])
dirList = thisBackup['destination']['directory'].split(/\//)
dirList.each {|seg| puts "[#{seg}]"}
dirPath = ""
dirList.each do
|pathelem|
if ! pathelem.empty?
dirPath += '/' + pathelem
if ! FileTest.exists?(dirPath)
Dir.mkdir(dirPath)
end
end
end
end
end
if thisBackup['roundtrip-test']
write_testfile(thisBackup, config)
end
local_or_remote_cmd(thisBackup['pre-src'],"Source pre-backup script",thisBackup['source'],config)
local_or_remote_cmd(thisBackup['pre-dest'],"Destination pre-backup script",thisBackup['destination'],config)
case thisBackup['backup-engine']
when "rdiff-backup"
backup_rdiff_backup(thisBackup, config)
when "duplicity"
backup_duplicity(thisBackup, config)
end
local_or_remote_cmd(thisBackup['post-src'],"Source post-backup script",thisBackup['source'],config)
local_or_remote_cmd(thisBackup['post-dest'],"Destination post-backup script",thisBackup['destination'],config)
if thisBackup['verify-signatures']
case thisBackup['backup-engine']
when "rdiff-backup"
verify_signatures_rdiff_backup(thisBackup, config)
when "duplicity"
verify_signatures_duplicity(thisBackup, config)
end
end
if thisBackup['roundtrip-test']
case thisBackup['backup-engine']
when "rdiff-backup"
test_restore_rdiff_backup(thisBackup, config)
when "duplicity"
test_restore_duplicity(thisBackup, config)
end
end
if thisBackup['preserve']
# run cleanup script
case thisBackup['backup-engine']
when "rdiff-backup"
cleanup_rdiff_backup(thisBackup, config)
when "duplicity"
cleanup_duplicity(thisBackup, config)
end
end
else
msg "Operations for this task disabled in backup-tasks.yaml"
allitems = false
end
msg "Operation completed in #{Time.new - op_time} seconds."
end
msg '-='*35 + '-'
msg "Whole enchilada completed in #{Time.new - start_time} seconds."
msg '-='*35 + '-'
rescue
# caught a critical unhandled error in the backup process.
msg "Encountered critical error: #{$!}"
[email protected] {|line| msg line}
msg "Backup aborting"
$success = false
ensure
# We try to send the mail. If there is a fatal error here, it would be impossible to report anyway.
if config['email-report']
theMessage = "From: #{config['report-from']}\n" +
"To: #{config['report-to']}\n" +
"Subject: "
theMessage += config['report-success-subject'] if ($success)
theMessage += config['report-skipped-subject'] if ($success && ! allitems)
theMessage += config['report-error-subject'] unless ($success)
theMessage += "\n\n" + $theReport
Net::SMTP.start(config['smtp-server']||'localhost', 25,
config['smtp-helo-domain']||'localhost.localdomain',
config['smtp-user']||nil, config['smtp-password']||nil, :login) do |smtp|
smtp.send_message theMessage, config['report-from'], config['report-to'].split(%r{,\s*})
end
end
end