backup.py

changeset 3
4cad934aa9ce
parent 2
e343594c0014
child 4
d72c4844e791
equal deleted inserted replaced
2:e343594c0014 3:4cad934aa9ce
2 # Borgend Backup instance 2 # Borgend Backup instance
3 # 3 #
4 4
5 import config 5 import config
6 from instance import BorgInstance 6 from instance import BorgInstance
7 from queue import Queue
8 from threading import Thread
7 9
8 class Backup: 10 class Backup:
9 11
10 def __decode_config(self, cfg): 12 def __decode_config(self, cfg):
11 loc0='backup target %d' % self.identifier 13 loc0='backup target %d' % self.identifier
14 16
15 self.loc='backup target "%s"' % self.name 17 self.loc='backup target "%s"' % self.name
16 18
17 self.repository=config.check_string(cfg, 'repository', 19 self.repository=config.check_string(cfg, 'repository',
18 'Target repository', self.loc) 20 'Target repository', self.loc)
21
22 self.archive_prefix=config.check_string(cfg, 'archive_prefix',
23 'Archive prefix', self.loc)
19 24
20 self.archive_template=config.check_string(cfg, 'archive_template', 25 self.archive_template=config.check_string(cfg, 'archive_template',
21 'Archive template', self.loc) 26 'Archive template', self.loc)
22 27
23 self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval', 28 self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval',
28 'Retry interval', self.loc, 33 'Retry interval', self.loc,
29 config.defaults['retry_interval']) 34 config.defaults['retry_interval'])
30 35
31 self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc) 36 self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc)
32 37
33 self.borg_parameters=config.check_list_of_dicts(cfg, 'borg_parameters', 38 self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters',
34 'Borg parameters', self.loc, 39 'Borg parameters', self.loc,
35 default=[]) 40 default=[])
41
42 self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters',
43 'Borg parameters', self.loc,
44 default=[])
36 45
37 46
38 def __init__(self, identifier, cfg): 47 def __init__(self, identifier, cfg):
39 self.identifier=identifier 48 self.identifier=identifier
40 49
41 self.__decode_config(cfg) 50 self.__decode_config(cfg)
42 51
43
44 self.config=config 52 self.config=config
45 self.lastrun=None 53 self.lastrun=None
46 self.current_instance=None 54 self.borg_instance=None
55 self.thread=None
47 56
48 def create(self): 57 def __block_when_running(self):
49 if self.current_instance is not None: 58 assert(self.borg_instance is None and self.thread is None)
50 raise AssertionError('%s running: cannot launch' % self.loc)
51 59
52 archive="%s::%s" % (self.repository, self.archive_template) 60 def __listener(self):
61 for status in iter(self.borg_instance.read, None):
62 # What to do?
63 print(status)
64 # What to do on error
65 # queue.put({'identifier': instance.identifier,
66 # 'operation': instance.operation,
67 # 'status': status})
53 68
54 inst=BorgInstance(self.identifier, 'create', self.borg_parameters, 69 def __launch(self, queue, operation, archive_or_repository, *args):
55 archive, self.paths) 70
56 print(inst.construct_cmdline()) 71 inst=BorgInstance(operation, archive_or_repository, *args)
72 inst.launch()
73
74 t=Thread(target=self.__listener)
75 t.daemon=True
76
77 self.thread=t
78 self.borg_instance=inst
79 self.queue=queue
80
81 t.start()
82
83 def create(self, queue):
84 self.__block_when_running()
85
86 archive="%s::%s%s" % (self.repository,
87 self.archive_prefix,
88 self.archive_template)
89
90 self.__launch(queue, 'create', archive,
91 self.create_parameters, self.paths)
92
93 def prune(self, queue):
94 self.__block_when_running()
95 self.__launch(queue, 'prune', self.repository,
96 [{'prefix': self.archive_prefix}] + self.prune_parameters)
97
98 def join(self):
99 if self.thread:
100 self.thread.join()
57 101
58 102
59 103

mercurial