-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.py
executable file
·507 lines (441 loc) · 21.7 KB
/
backup.py
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#!/usr/bin/env python
from ArchiveR3 import *
import argparse
from colorlog import ColoredFormatter
try:
from hurry.filesize import size
except ImportError, e:
print e
print 'Hint: try running "pip install hurry.filesize"'
sys.exit(1)
import logging
import math
import re
import subprocess
import sys
import time
class backup:
""" Perform a backup via rsync to an encrypted filesystem hosted in the
cloud. """
def __init__(self):
self.init_vars()
def init_vars(self):
""" Initialize class variables. """
# Lost storage due to overhead of encrypted container.
# ~6.4M fixed overhead.
# Plus 7.5% relative overhead.
self.container_overhead_fixed = 6225865
self.container_overhead_percent = 7.5
self.logfile = 'ArchiveR3-' + time.strftime("%Y%m%d-%H%M%S") + '.log'
def args_process(self):
""" Process command-line arguments. """
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Back up files to an archive. This is performed in '
'phases: 1. create a zeroed file archive container, 2. encrypt '
'the container, and 3. synchronize files with the archive. '
'To specify directories to back up, edit your config file, '
'for example by copying config.generic to config.mine and '
'invoking this tool with it.')
parser.add_argument('config', action='store',
help='Specify an ArchiveR3 config file.')
parser.add_argument('-a', '--auto', action='store_true',
help='Automatically create, encrypt, format, '
'mount, and archive without prompting. '
'Equivalent to --create --encrypt --format'
'--mountcreate')
parser.add_argument('-b', '--bwlimit', type=int,
help='Bandwidth limit during synchronization '
'in KBps. A value of 0 means no limit. '
'Removing the bandwidth limit can be useful if '
'for example the archive is being created on a '
'local filesystem where you do not care about '
'saturating the connection and want to write '
'as quickly as possible.', default=1300)
parser.add_argument('-c', '--cleanup', action='store_true',
help='Perform cleanup operations only, no '
'backup. Cleanup consists of unmounting, '
'unmapping, and removing the loopback device '
'associated with the encrypted container.')
parser.add_argument('--create', action='store_true',
help='Create container without prompting.')
parser.add_argument('--encrypt', action='store_true',
help='Encrypt container without prompting.')
parser.add_argument('--format', action='store_true',
help='Format encrypted container without '
'prompting.')
parser.add_argument('--mountcreate', action='store_true',
help='Create the mount point automatically.')
parser.add_argument('--reprovision', action='store_true',
help='Reprovision the container automatically '
'if too small.')
parser.add_argument('-n', '--nocleanup', action='store_true',
help='Do not perform normal cleanup operations '
'after backing up such as unmounting and '
'unmapping the archive container.')
parser.add_argument('--interactive', action='store_true',
help='Assume the backup is being run '
'interactively. Prompt for the option to resolve '
'basic issues like creating missing directories, '
'rather than safely and conservatively bailing. '
'Note that most blocking operations like the need '
'to create missing mount points can be '
'automatically performed with corresponding '
'options like --mountcreate.')
parser.add_argument('-s', '--skipbackup', action='store_true',
help='Skip backup. This can be useful to test '
'basic mounting and unmounting. Used with '
'--nocleanup this simply mounts the archive and '
'leaves it mounted when the program exits.')
parser.add_argument('-v', '--verbose', action='store_true',
help='Print the status of each file as it is '
'being processed. Otherwise, a progress dot is '
'printed for every 100 files processed. '
'\'processing\' refers not just to backing up '
'files, but also checking to see if they have '
'previously been backed up.')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
self.args = parser.parse_args()
if self.args.auto:
self.args.create = True
self.args.encrypt = True
self.args.format = True
self.args.mountcreate = True
self.args.reprovision = True
def calc_container_overhead(self, container_size):
""" Given a container size, calculate the expected overhead due
to encryption, reserved space for root, journaling, etc. """
container_overhead = self.container_overhead_fixed \
+ (
(container_size
- self.container_overhead_fixed)
*
(self.container_overhead_percent / 100))
container_overhead = int(math.ceil(container_overhead))
return container_overhead
def calc_archive_container(self, archive_size):
""" Given an archive size, calculate the approximate minimum size of
the container necessary to hold the archive. This takes into account
expected overhead due to encrypt, reserved space for root, journaling,
etc. """
archive_container = archive_size \
* ((self.container_overhead_percent + 100) / 100) \
+ self.container_overhead_fixed
archive_container = int(math.ceil(archive_container))
return archive_container
def create_archive(self, archive_dir, container, backup_dir, arc_block):
""" Create an encrypted container. The resulting containersize is only
accurate to the nearest megabyte. Return 1 if any problems or 0 for
success. """
if self.args.create:
status_item('!! CREATE CONTAINER')
status_result('CONFIRMED', 4)
else:
status_item('!! CREATE CONTAINER? (y/n)')
if self.args.create or raw_input() == 'y':
archive_size = int(float(arc_block) /
float(self.config.provision_capacity_percent)
* 100)
status_item('Archive Block Size')
status_result(str(arc_block) + ' (' + size(arc_block) + ')')
status_item('Provision Capacity')
status_result(str(archive_size) + ' (' + size(archive_size) + ')')
status_item('Required Container Size')
# Round to the nearest megabyte to speed up dd blocksize below.
container_size_needed_m = \
int(math.ceil(self.calc_archive_container(archive_size)
/ 1048576))
status_result(str(container_size_needed_m * 1048576) + ' (' +
str(container_size_needed_m) + 'M)')
status_item('Generating Container')
status_result('IN PROGRESS', 2)
try:
if self.args.verbose:
status_item('Command')
status_result('dd if=/dev/zero bs=1048576 status=none ' +
'count=' + str(container_size_needed_m) +
' ' + 'of=' + container)
print
# TODO: need to convert this into a scheme which is not
# dependent on Unix pipes
subprocess.check_call('dd if=/dev/zero bs=1048576 ' +
'status=none ' +
'count=' + str(container_size_needed_m) +
' | pv -s ' +
str(container_size_needed_m * 1048576) +
' | ' + 'dd status=none ' +
'of=' + container, shell=True)
print
except subprocess.CalledProcessError, e:
status_item('Generation Result')
status_result('FAILED: ' + str(e), 3)
return 1
except Exception, e:
status_item('Generation Result')
print e
if re.match('.*No such file or directory', str(e)):
status_result('COMMAND NOT FOUND', 3)
return 1
return 0
else:
return 1
def cleanup(self):
""" Unmount, remove mount point, unmap, and remove the loopback
device associated with the encrypted container. """
# Give the rsync time to gracefully terminate.
status_item('Cleaning Up')
status_result('.', no_newline=True)
time.sleep(1)
status_result('.', no_newline=True)
time.sleep(1)
status_result('.')
time.sleep(1)
if self.archive_mount:
umount(self.archive_mount, remove=True)
if self.container_file:
unmap(self.container_file)
if self.lbdevice:
loopback_delete(self.lbdevice)
def backup(self):
for i, s in enumerate(self.config.archive_list):
section_break()
archive_dir = self.config.archive_list[i]
status_item('Archive')
status_result(archive_dir)
self.container_file = self.config.archive_list[i].split('/')[-2] \
+ '.archive'
self.archive_mount = self.config.mount_dir + self.container_file
container = self.config.backup_dir + self.container_file
self.lbdevice = loopback_exists(container)
if self.args.cleanup:
self.cleanup()
return 2
arc_block = dir_size(archive_dir, block_size=512)
if arc_block == -1:
status_item('ARCHIVE READABILITY')
status_result('FAILED', 3)
return 1
status_item('Container')
status_result(container)
status_item('')
if os.path.isfile(container):
status_result('FOUND', 1)
else:
status_result('NOT FOUND', 2)
if self.create_archive(self.config.archive_list[i],
container, self.config.backup_dir,
arc_block):
return 1
container_size = os.path.getsize(container)
if not container_size:
status_item('Container Size')
status_result('PROBE FAILED', 3)
return 1
if self.args.verbose:
status_item('Archive Size')
status_result(str(arc_block) + ' (' + size(arc_block) + ')')
status_item('Container Size')
status_result(str(container_size) + ' (' +
size(container_size) + ')')
status_item('Estimated Consumption')
container_size_net = container_size - \
self.calc_container_overhead(container_size)
capacity_est = float(arc_block) / float(container_size_net) * 100
if capacity_est < self.config.provision_capacity_reprovision:
capacity_est_condition = 1
else:
capacity_est_condition = 2
status_result(str('%0.2f%%' % capacity_est),
capacity_est_condition, no_newline=True)
status_result(str(arc_block) + '/' + str(container_size_net) + ' '
+ size(arc_block) + '/' + size(container_size_net))
if capacity_est_condition == 2:
status_item('')
status_result('OUT OF SPACE', capacity_est_condition)
if self.args.reprovision:
status_item('!! REPROVISION?')
status_result('CONFIRMED', 4)
else:
status_item('!! REPROVISION? (y/n)')
if self.args.reprovision or raw_input() == 'y':
self.cleanup()
if self.create_archive(self.config.archive_list[i],
container, self.config.backup_dir,
arc_block):
return 1
else:
status_item('Container Generation')
status_result('SUCCESS', 1)
else:
status_item('Capacity')
status_result('EXCEEDED', 3)
return 1
if not self.lbdevice:
loopback_cleanup(container)
self.lbdevice = loopback_next()
if not self.lbdevice:
return 1
if loopback_setup(self.lbdevice, container, self.args.verbose):
return 1
if loopback_encrypted(self.lbdevice, self.config.password_base,
self.config.backup_dir, self.container_file,
self.args.verbose):
if self.args.encrypt:
status_item('!! (RE)ENCRYPT CONTAINER')
status_result('CONFIRMED', 4)
else:
status_item('!! (RE)ENCRYPT CONTAINER? (y/n)')
if self.args.encrypt or raw_input() == 'y':
if loopback_encrypt(self.lbdevice,
self.config.password_base,
self.container_file,
self.args.verbose):
return 1
else:
return 1
archive_map = '/dev/mapper/' + self.container_file
if mapper_check(self.lbdevice, archive_map, self.container_file,
self.config.password_base, self.args.verbose):
return 1
if filesystem_check(archive_map):
if self.args.format:
status_item('!! (RE)FORMAT FILESYSTEM')
status_result('CONFIRMED', 4)
else:
status_item('!! (RE)FORMAT FILESYSTEM? (y/n)')
if self.args.format or raw_input() == 'y':
if filesystem_format(archive_map, self.args.verbose):
return 1
else:
return 1
if mount_check(archive_map, self.archive_mount,
mountcreate=self.args.mountcreate,
verbose=self.args.verbose):
return 1
stat = os.statvfs(self.archive_mount)
cryptfs_size = str((stat.f_blocks -
(stat.f_bfree - stat.f_bavail)) * stat.f_frsize)
if not cryptfs_size:
status_item('Encrypted Filesystem Size')
status_result('PROBE FAILED', 3)
return 1
status_item('Anticipated Consumption')
capacity_act = float(arc_block) / float(cryptfs_size) * 100
# capacity_act_condition will be 1 (green/OK) or 2 (yellow/WARNING)
if capacity_act < self.config.provision_capacity_reprovision:
capacity_act_condition = 1
else:
capacity_act_condition = 2
status_result(str('%0.2f%%' % capacity_act),
capacity_act_condition, no_newline=True)
status_result(str(arc_block) + '/' + cryptfs_size + ' ' +
size(arc_block) + '/' + size(int(cryptfs_size)))
if capacity_act_condition == 2:
status_item('Reprovision? (y/n)')
confirm_reprovision = raw_input()
self.cleanup()
if self.create_archive(self.config.archive_list[i],
container, self.config.backup_dir,
arc_block + self.container_overhead):
return 1
else:
status_item('Reprovision')
status_result('SUCCESS BUT NEED TO RESTART AND USE' +
'WITH --nocleanup OPTION', 2)
return 1
if not self.args.skipbackup:
if sync(archive_dir, self.archive_mount, self.args.bwlimit):
return 1
if not self.args.nocleanup:
self.cleanup()
# Clean up global variables so there is no chance of accidentally
# using them next time through the loop.
self.container_file = ''
self.archive_mount = ''
self.lbdevice = ''
return 0
def main(self):
""" If you call the python as a script, this is what gets executed. """
# These 3 variables are key to performing safe cleanup. they do not
# need to be populated now, but they need to be declared. At any point
# the user may Ctrl-C out, and we will use these variables to clean up.
self.container_file = ''
self.lbdevice = ''
self.archive_mount = ''
try:
self.args_process()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# create console handler
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
formatter = ColoredFormatter('%(asctime)s %(log_color)s'
'%(levelname)-8s %(message)s'
'%(reset)s', datefmt=None,
reset=True, log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'purple',
})
handler.setFormatter(formatter)
logger.addHandler(handler)
time_init = print_header('BACKUP')
logger.info(self.args.config + ': configuration file loading')
self.config = config_read(self.args.config)
if self.config:
logger.info(self.args.config + ': validating configuration')
if config_validate(self.config, self.args.interactive):
logger.error('configuration file invalid')
logger.critical('backup failed')
else:
logger.info(self.args.config +
': configuration file valid')
logger.info('validating log directory ' +
self.config.log_dir)
if dir_validate(self.config.log_dir, create=1, write=1):
return 1
# create file handler
handler = logging.FileHandler(os.path.join(
self.config.log_dir,
self.logfile))
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s '
'%(levelname)-8s '
'%(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
if self.args.bwlimit:
logger.info('bandwidth limit ' + str(self.args.bwlimit)
+ ' KBps')
else:
logger.info('bandwidth limit: none')
logger.info('logfile ' + self.config.log_dir +
self.logfile)
rc = self.backup()
status_item('Backup')
if rc == 1:
status_result('FAILED', 3)
if not self.args.nocleanup:
self.cleanup()
elif rc == 2:
status_result('SKIPPED', 2)
else:
status_result('SUCCESS', 1)
print_footer('backup', time_init)
except KeyboardInterrupt:
print
status_item('Backup')
status_result('ABORT', 3)
if not self.args.nocleanup:
self.cleanup()
status_item('Safe Quit')
status_result('SUCCESS', 1)
pass
if __name__ == '__main__':
sys.stdout = Unbuffered(sys.stdout)
backup = backup()
backup.main()