-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsnap.py
executable file
·484 lines (376 loc) · 12.4 KB
/
rsnap.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
#!/usr/bin/env python2
from __future__ import print_function
import argparse
import collections
import ConfigParser
import os
import subprocess
import sys
import time
from datetime import datetime, timedelta
class ExecutionError(Exception):
def __init__(self, returncode, output):
self.returncode = returncode
self.output = output
def repr(self):
return "ExecutionError: (%(code)d, %(output)s" % {
'code': self.returncode,
'output': self.output
}
class MissingPathError(Exception):
pass
class StorageProfile(object):
@classmethod
def get_subclasses(cls):
for subclass in cls.__subclasses__():
for x in subclass.get_subclasses():
yield x
yield subclass
@classmethod
def get_subclass(cls, name):
for cls in cls.get_subclasses():
if getattr(cls, 'NAME', None) == name:
return cls
raise TypeError(name)
def __init__(self, basedir):
self._basedir = basedir
@property
def basedir(self):
return self._basedir + '/' + self.NAME
def get_current_storage(self):
raise NotImplementedError()
def get_previous_storage(self):
raise NotImplementedError()
class SnapshotProfile(StorageProfile):
NAME = 'snapshot'
def get_current_storage(self):
now = time.time()
dt = datetime.fromtimestamp(now)
fmt = dt.strftime('%Y.%m.%d-%H.%M.%S.%f')
return self.basedir + '/' + fmt
def get_previous_storage(self):
try:
return self.basedir + '/' + sorted(os.listdir(self.basedir))[-1]
except (IndexError, OSError):
return None
class CyclicProfile(StorageProfile):
def __init__(self, *args, **kwargs):
now = kwargs.pop('now', datetime.now())
super(CyclicProfile, self).__init__(*args, **kwargs)
self.now = now
self.g = self.backcounter()
self.curr_id = next(self.g)
def get_current_storage(self):
return os.path.realpath(self.basedir + '/' + self.curr_id)
def get_previous_storage(self):
for x in self.g:
if x == self.curr_id:
return None
test = self.basedir + '/' + x
if os.path.exists(test):
return os.path.realpath(test)
def backcounter(self):
raise StopIteration()
class SubdailyProfile(CyclicProfile):
"""
288 copies by default (one each 5 minutes)
"""
NAME = 'subdaily'
def __init__(self, *args, **kwargs):
self.interval = kwargs.pop('interval', 5*60)
super(SubdailyProfile, self).__init__(*args, **kwargs)
def backcounter(self):
ts = time.mktime(self.now.timetuple())
ts = self.interval * (ts // self.interval)
now = datetime.fromtimestamp(ts)
day = timedelta(days=1)
diff = timedelta(seconds=0)
while True:
curr = now - diff
yield '%02d.%02d.%02d' % (curr.hour, curr.minute, curr.second)
diff = diff + timedelta(seconds=self.interval)
if diff >= day:
break
class MonthlyProfile(CyclicProfile):
"""
12 copies
"""
NAME = 'monthly'
def backcounter(self):
for m in range(self.now.month + 12, self.now.month, -1):
m = m % 12 or 12
yield '%02d' % m
class WeeklyProfile(CyclicProfile):
"""
52 copies (or 53...)
"""
NAME = 'weekly'
def backcounter(self):
year = timedelta(days=367)
diff = timedelta(weeks=0)
while True:
week = (self.now - diff).isocalendar()[1]
yield '%02d' % week
diff = diff + timedelta(weeks=1)
if diff >= year:
break
class HourlyProfile(SubdailyProfile):
"""
24 copies
"""
NAME = 'hourly'
def __init__(self, *args, **kwargs):
kwargs['interval'] = 60*60
super(HourlyProfile, self).__init__(*args, **kwargs)
class MonthdayProfile(CyclicProfile):
"""
30-31 copies
"""
NAME = 'monthday'
def backcounter(self):
if self.now.month == 1:
prev = self.now.replace(year=self.now.year - 1, month=12)
else:
prev = self.now.replace(month=self.now.month - 1)
counter = self.now
while counter > prev:
yield '%02d' % counter.day
counter = counter - timedelta(days=1)
class WeekdayProfile(CyclicProfile):
"""
7 copies
"""
NAME = 'weekday'
def backcounter(self):
for wd in range(self.now.isoweekday() + 7, self.now.isoweekday(), -1):
wd = wd % 7 or 7
yield '%d' % wd
class ArgumentSet(dict):
def copy(self):
return self.__class__(**self)
def merge(self, *argsets):
for argset in argsets:
self.update(argset)
def as_command_line(self):
ret = []
for (k, v) in self.items():
# Drop this argument
if v is None:
continue
# Command line arguments use '-'
k = k.replace('_', '-')
# Short options
if len(k) == 1:
if v is True:
# Add as -x
ret.append('-%s' % k)
elif v is False:
# Skip negatives
continue
else:
# Short options cannot have arguments
raise ValueError(self)
# Long options
else:
if v is True:
ret.append('--%s' % k)
elif v is False:
ret.append('--no-%s' % k)
else:
ret.append('--%s=%s' % (k, v))
return ret
class RSnap(object):
RSYNC_BIN = '/usr/bin/rsync'
RSYNC_OPTS = {
'acls': False,
'archive': True,
'delete': True,
'fake-super': True,
'hard-links': True,
'inplace': False,
'numeric-ids': True,
'one-file-system': True,
'partial': True,
'progress': True,
'rsh': 'ssh -oPasswordAuthentication=no -oStrictHostKeyChecking=no',
'rsync-path': 'rsync --fake-super',
'verbose': False,
'xattrs': True
}
def __init__(self, source, storage, profile='default',
rsync_bin=None, rsync_opts=None):
self.rsync_bin = rsync_bin or self.RSYNC_BIN
self.rsync_opts = self.RSYNC_OPTS
self.rsync_opts.update(rsync_opts or {})
self.rsync_opts = ArgumentSet(**self.rsync_opts)
self.source = source
self.storage = storage
try:
profile_is_class = issubclass(profile, StorageProfile)
except TypeError:
profile_is_class = False
if not profile_is_class:
profile_cls = (StorageProfile.get_subclass(profile)
or StorageProfile)
self.profile = profile_cls(basedir=self.storage)
else:
self.profile = profile
def _get_base_command_line(self, opts=None):
argset = self.rsync_opts.copy()
if opts:
argset.merge(opts)
return [self.rsync_bin] + argset.as_command_line()
def _get_excludes(self):
excludes_file = self.storage + '/exclude.lst'
try:
fh = open(excludes_file, 'r')
fh.close()
return excludes_file
except IOError:
raise MissingPathError(excludes_file)
def _get_latest(self, destination):
return (
os.path.basename(destination.rstrip('/')),
os.path.dirname(os.path.realpath(destination)) + "/latest"
)
def build(self):
kwargs = ArgumentSet(**self.rsync_opts)
try:
# Not sure if one implies the other
kwargs['exclude-from'] = self._get_excludes()
kwargs['delete-excluded'] = True
except MissingPathError:
pass
# If link_dest is None its unset, it's OK
link_dest = self.profile.get_previous_storage()
kwargs['link-dest'] = link_dest
dest = self.profile.get_current_storage()
if self.source.endswith('/'):
dest += '/'
return (self.rsync_bin, self.source, dest), kwargs
def run(self):
(cmd_bin, cmd_src, cmd_dst), cmd_kwargs = self.build()
# FIXME: Use a logger
cmd = (
[cmd_bin] +
cmd_kwargs.as_command_line() +
[cmd_src, cmd_dst]
)
cmd_str = ["'" + x + "'" for x in cmd]
cmd_str = ' '.join(cmd_str)
print(cmd_str)
# Try to create cmd_dst parents
try:
os.makedirs(cmd_dst)
except OSError:
pass
# Execute
try:
res = subprocess.check_output(cmd)
except subprocess.CalledProcessError as e:
if e.returncode not in (23,): # whitelisted codes
raise ExecutionError(returncode=e.returncode, output=e.output)
# Update latest link
(latest_src, latest_dst) = self._get_latest(cmd_dst)
try:
os.unlink(latest_dst)
except OSError as e:
pass
try:
# Basenaming link source provides better isolation of backup
os.symlink(latest_src, latest_dst)
except OSError as e:
print("Unable to link '%s' to '%s': %s" % (
latest_src, latest_dst, repr(e))
)
def build_argparser():
parser = argparse.ArgumentParser()
parser.add_argument(
'--rsync-bin',
default='/usr/bin/rsync',
required=False)
parser.add_argument(
'-c', '--config',
required=False)
parser.add_argument(
'--storage',
required=False),
parser.add_argument(
'--profile',
required=False)
parser.add_argument(
nargs='?',
dest='source')
return parser
def operations_from_config(fp):
parser = ConfigParser.SafeConfigParser()
parser.readfp(fp)
ret = []
for sect in [sect for sect in parser.sections() if sect != 'DEFAULT']:
values = {x: parser.get(sect, x)
for x in ('profile', 'source', 'storage')}
try:
values['rsync_bin'] = parser.get(sect, 'rsync-bin')
except ConfigParser.NoOptionError:
pass
values['rsync_opts'] = {
k[len('rsync-opt-'):]: v
for (k, v) in parser.items(sect)
if k.startswith('rsync-opt-')}
transformations = {
'True': True,
'False': False,
'None': None,
'': None
}
values['rsync_opts'] = {
(k, transformations.get(v, v))
for (k, v) in values['rsync_opts'].items()
}
ret.append((sect, values))
return ret
def operations_from_args(args):
return [('Command Line', {
'storage': args['storage'],
'source': args['source'],
'profile': args['profile']
})]
def main():
parser = build_argparser()
args = vars(parser.parse_args(sys.argv[1:]))
# Check arguments
if not args['config']:
manual_reqs = ('profile', 'storage', 'source')
for arg in manual_reqs:
if not args.get(arg):
parser.print_usage()
errmsg = "'%(arg)s' is required without config"
errmsg = errmsg % {'arg': arg}
print(errmsg, file=sys.stderr)
sys.exit(1)
# Build operations
if args['config']:
with open(args['config']) as fp:
operations = operations_from_config(fp)
else:
operations = operations_from_args(args)
# Run operations
for (name, item) in operations:
try:
rsnap = RSnap(
source=item['source'],
storage=item['storage'],
profile=item['profile'],
rsync_bin=item.get('rsync_bin', None),
rsync_opts=item.get('rsync_opts', None))
rsnap.run()
except ExecutionError as e:
errmsg = "Backup of %(src)s failed: %(code)s"
errmsg = errmsg % {'src': item['source'], 'code': e.returncode}
print(errmsg, file=sys.stderr)
for line in e.output.split('\n'):
print("\t" + line)
continue
print("Backup of %(name)s: OK" % {'name': name})
if __name__ == '__main__':
main()