Sat, 20 Jan 2018 23:19:16 +0000
Display original/deduplicatd size if no progress percentage available
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # Borgend Backup instance |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | # |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
2 | 5 | import config |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
6 | import logging |
5 | 7 | import time |
2 | 8 | from instance import BorgInstance |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
9 | from queue import Queue |
8 | 10 | from threading import Thread, Lock, Timer |
2 | 11 | |
6 | 12 | loglevel_translation={ |
13 | 'CRITICAL': logging.CRITICAL, | |
14 | 'ERROR': logging.ERROR, | |
15 | 'WARNING': logging.WARNING, | |
16 | 'DEBUG': logging.DEBUG, | |
17 | 'INFO': logging.INFO | |
18 | } | |
19 | ||
20 | def translate_loglevel(x): | |
21 | if x in loglevel_translation: | |
22 | return loglevel_translation[x] | |
23 | else: | |
24 | return logging.ERROR | |
25 | ||
15 | 26 | def safe_get_int(t, x): |
27 | if x in t: | |
28 | tmp=t[x] | |
29 | if isinstance(tmp, int): | |
30 | return tmp | |
31 | return None | |
32 | ||
33 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | class Backup: |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | |
2 | 36 | def __decode_config(self, cfg): |
37 | loc0='backup target %d' % self.identifier | |
38 | ||
39 | self.name=config.check_string(cfg, 'name', 'Name', loc0) | |
40 | ||
41 | self.loc='backup target "%s"' % self.name | |
42 | ||
43 | self.repository=config.check_string(cfg, 'repository', | |
44 | 'Target repository', self.loc) | |
45 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
46 | self.archive_prefix=config.check_string(cfg, 'archive_prefix', |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
47 | 'Archive prefix', self.loc) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
48 | |
2 | 49 | self.archive_template=config.check_string(cfg, 'archive_template', |
50 | 'Archive template', self.loc) | |
51 | ||
52 | self.backup_interval=config.check_nonneg_int(cfg, 'backup_interval', | |
53 | 'Backup interval', self.loc, | |
54 | config.defaults['backup_interval']) | |
55 | ||
56 | self.retry_interval=config.check_nonneg_int(cfg, 'retry_interval', | |
57 | 'Retry interval', self.loc, | |
58 | config.defaults['retry_interval']) | |
59 | ||
60 | self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc) | |
61 | ||
6 | 62 | self.common_parameters=config.check_list_of_dicts(cfg, 'common_parameters', |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
63 | 'Borg parameters', self.loc, |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
64 | default=[]) |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
65 | |
6 | 66 | self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters', |
67 | 'Create parameters', self.loc, | |
68 | default=[]) | |
69 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
70 | self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters', |
6 | 71 | 'Prune parameters', self.loc, |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
72 | default=[]) |
2 | 73 | |
74 | ||
75 | def __init__(self, identifier, cfg): | |
76 | self.identifier=identifier | |
77 | ||
78 | self.__decode_config(cfg) | |
79 | ||
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
80 | self.config=config |
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | self.lastrun=None |
5 | 82 | self.lastrun_success=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
83 | self.borg_instance=None |
5 | 84 | self.current_operation=None |
7 | 85 | self.thread_log=None |
8 | 86 | self.thread_res=None |
87 | self.timer=None | |
10 | 88 | self.scheduled_operation=None |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
89 | self.lock=Lock() |
10 | 90 | self.status_update_callback=None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
91 | |
7 | 92 | def is_running(self): |
93 | with self.lock: | |
8 | 94 | running=self.__is_running_unlocked() |
95 | return running | |
96 | ||
97 | def __is_running_unlocked(self): | |
98 | running=self.current_operation | |
99 | if not running: | |
100 | # Consistency check | |
101 | assert((not self.borg_instance and not self.thread_log and | |
102 | not self.thread_res)) | |
7 | 103 | return running |
104 | ||
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
105 | def __block_when_running(self): |
7 | 106 | running=self.is_running() |
107 | assert(not running) | |
2 | 108 | |
7 | 109 | def __log_listener(self): |
110 | logging.debug('Log listener thread waiting for entries') | |
111 | success=True | |
112 | for status in iter(self.borg_instance.read_log, None): | |
113 | logging.debug(str(status)) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
114 | t=status['type'] |
15 | 115 | |
116 | errors_this_message=False | |
117 | callback=None | |
118 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
119 | if t=='progress_percent': |
15 | 120 | current=safe_get_int(status, 'current') |
121 | total=safe_get_int(status, 'total') | |
122 | if current is not None and total is not None: | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
123 | with self.lock: |
17 | 124 | self.current_operation['progress_current']=current |
125 | self.current_operation['progress_total']=total | |
14
5a988a2c2624
UI: progress percentange support (Borg doesn't seem to be reporting) + error indicator in tray: B?
Tuomo Valkonen <tuomov@iki.fi>
parents:
12
diff
changeset
|
126 | status, callback=self.__status_unlocked() |
15 | 127 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
128 | elif t=='archive_progress': |
15 | 129 | original_size=safe_get_int(status, 'original_size') |
130 | compressed_size=safe_get_int(status, 'compressed_size') | |
131 | deduplicated_size=safe_get_int(status, 'deduplicated_size') | |
132 | if original_size is not None and original_size is not None and deduplicated_size is not None: | |
133 | with self.lock: | |
17 | 134 | self.current_operation['original_size']=original_size |
135 | self.current_operation['compressed_size']=compressed_size | |
136 | self.current_operation['deduplicated_size']=deduplicated_size | |
15 | 137 | status, callback=self.__status_unlocked() |
138 | ||
139 | elif t=='progress_message': | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
140 | pass |
15 | 141 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
142 | elif t=='file_status': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
143 | pass |
15 | 144 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
145 | elif t=='log_message': |
6 | 146 | if 'levelname' not in status: |
147 | status['levelname']='ERROR' | |
148 | if 'message' not in status: | |
149 | status['message']='UNKNOWN' | |
150 | if 'name' not in status: | |
151 | status['name']='borg' | |
15 | 152 | lvl=translate_loglevel(status['levelname']) |
153 | logging.log(lvl, status['name'] + ': ' + status['message']) | |
154 | if lvl>=logging.WARNING: | |
155 | errors_this_message=True | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
156 | elif t=='exception': |
15 | 157 | errors_this_message=True |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
158 | elif t=='unparsed_error': |
15 | 159 | errors_this_message=True |
160 | ||
161 | if errors_this_message: | |
162 | with self.lock: | |
16
d0ffae5550ef
Added offline symbol B⦙ (no offline detection yet)
Tuomo Valkonen <tuomov@iki.fi>
parents:
15
diff
changeset
|
163 | self.current_operation['errors']=True |
15 | 164 | status, callback=self.__status_unlocked() |
165 | ||
166 | if callback: | |
167 | callback(self, status) | |
6 | 168 | |
7 | 169 | logging.debug('Waiting for borg subprocess to terminate in log thread') |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
170 | |
6 | 171 | self.borg_instance.wait() |
172 | ||
7 | 173 | logging.debug('Borg subprocess terminated; terminating log listener thread') |
174 | ||
175 | def __result_listener(self): | |
10 | 176 | with self.lock: |
177 | status, callback=self.__status_unlocked() | |
178 | if callback: | |
179 | callback(self, status) | |
180 | ||
7 | 181 | logging.debug('Result listener thread waiting for result') |
182 | ||
183 | res=self.borg_instance.read_result() | |
184 | ||
185 | success=True | |
186 | ||
187 | logging.debug('Borg result: %s' % str(res)) | |
188 | ||
189 | if res==None: | |
190 | success=False | |
191 | ||
192 | logging.debug('Waiting for borg subprocess to terminate in result thread') | |
193 | ||
8 | 194 | success=success and self.borg_instance.wait() |
7 | 195 | |
8 | 196 | logging.debug('Borg subprocess terminated (success: %s); terminating result listener thread' % str(success)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
197 | |
12 | 198 | self.thread_log.join() |
199 | ||
5 | 200 | with self.lock: |
10 | 201 | if self.current_operation['operation']=='create': |
202 | self.lastrun=self.current_operation['when_monotonic'] | |
5 | 203 | self.lastrun_success=success |
7 | 204 | self.thread_res=None |
12 | 205 | self.thread_log=None |
206 | self.borg_instance=None | |
207 | self.current_operation=None | |
208 | self.__schedule_unlocked() | |
10 | 209 | status, callback=self.__status_unlocked() |
210 | if callback: | |
211 | callback(self, status) | |
7 | 212 | |
10 | 213 | def __do_launch(self, queue, op, archive_or_repository, *args): |
214 | inst=BorgInstance(op['operation'], archive_or_repository, *args) | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
215 | inst.launch() |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
216 | |
7 | 217 | t_log=Thread(target=self.__log_listener) |
218 | t_log.daemon=True | |
2 | 219 | |
7 | 220 | t_res=Thread(target=self.__result_listener) |
221 | t_res.daemon=True | |
222 | ||
223 | self.thread_log=t_log | |
224 | self.thread_res=t_res | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
225 | self.borg_instance=inst |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
226 | self.queue=queue |
10 | 227 | self.current_operation=op |
228 | self.current_operation['when_monotonic']=time.monotonic() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
229 | |
7 | 230 | t_log.start() |
231 | t_res.start() | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
232 | |
10 | 233 | def __launch(self, op, queue): |
8 | 234 | if self.__is_running_unlocked(): |
235 | logging.info('Cannot start %s: already running %s' | |
236 | % (operation, self.current_operation)) | |
237 | return False | |
238 | else: | |
239 | if self.timer: | |
240 | logging.debug('Unscheduling timed operation due to launch of operation') | |
241 | self.timer=None | |
10 | 242 | self.scheduled_operation=None |
8 | 243 | |
10 | 244 | logging.debug("Launching '%s' on '%s'" % (op['operation'], self.name)) |
8 | 245 | |
10 | 246 | if op['operation']=='create': |
8 | 247 | archive="%s::%s%s" % (self.repository, |
248 | self.archive_prefix, | |
249 | self.archive_template) | |
2 | 250 | |
10 | 251 | self.__do_launch(queue, op, archive, |
8 | 252 | self.common_parameters+self.create_parameters, |
253 | self.paths) | |
10 | 254 | elif op['operation']=='prune': |
255 | self.__do_launch(queue, op, self.repository, | |
8 | 256 | ([{'prefix': self.archive_prefix}] + |
257 | self.common_parameters + | |
258 | self.prune_parameters)) | |
259 | else: | |
10 | 260 | logging.error("Invalid operaton '%s'" % op['operation']) |
8 | 261 | self.__schedule_unlocked() |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
262 | |
8 | 263 | return True |
264 | ||
265 | def create(self, queue): | |
10 | 266 | op={'operation': 'create', 'detail': 'manual'} |
8 | 267 | with self.lock: |
10 | 268 | res=self.__launch(op, queue) |
8 | 269 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
270 | |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
271 | def prune(self, queue): |
10 | 272 | op={'operation': 'prune', 'detail': 'manual'} |
8 | 273 | with self.lock: |
10 | 274 | res=self.__launch(op, queue) |
8 | 275 | return res |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
276 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
277 | # TODO: Decide exact (manual) abort mechanism. Perhaps two stages |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
278 | def abort(self): |
5 | 279 | with self.lock: |
280 | if self.borg_instance: | |
281 | self.borg_instance.terminate() | |
7 | 282 | thread_log=self.thread_log |
283 | thread_res=self.thread_res | |
284 | ||
285 | if thread_log: | |
286 | thread_log.terminate() | |
287 | ||
288 | if thread_res: | |
289 | thread_res.terminate() | |
290 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
291 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
292 | def join(self): |
8 | 293 | logging.debug('Waiting for borg listener threads to terminate') |
7 | 294 | |
295 | with self.lock: | |
296 | thread_log=self.thread_log | |
297 | thread_res=self.thread_res | |
298 | ||
299 | if thread_log: | |
300 | thread_log.join() | |
301 | ||
302 | if thread_res: | |
303 | thread_res.join() | |
304 | ||
305 | assert(self.thread_log==None and self.thread_res==None) | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
306 | |
8 | 307 | def __queue_timed_operation(self): |
308 | with self.lock: | |
10 | 309 | op=self.scheduled_operation |
310 | self.scheduled_operation=None | |
8 | 311 | self.timer=None |
312 | ||
313 | if self.__is_running_unlocked(): | |
314 | logging.info('Aborted queue operation, as an operation is already running') | |
315 | else: | |
316 | # TODO: Queue on 'repository' and online status for SSH, etc. | |
317 | ||
318 | # TODO: UI comms. queue? | |
10 | 319 | self.__launch(op, None) |
8 | 320 | |
321 | def __next_operation_unlocked(self): | |
322 | # TODO: pruning as well | |
5 | 323 | now=time.monotonic() |
324 | if not self.lastrun: | |
10 | 325 | initial_interval=self.retry_interval |
326 | if initial_interval==0: | |
327 | initial_interval=self.backup_interval | |
328 | if initial_interval==0: | |
329 | return None | |
330 | else: | |
331 | return {'operation': 'create', | |
332 | 'detail': 'initial', | |
333 | 'when_monotonic': now+initial_interval} | |
5 | 334 | elif not self.lastrun_success: |
10 | 335 | if self.retry_interval==0: |
336 | return None | |
337 | else: | |
338 | return {'operation': 'create', | |
339 | 'detail': 'retry', | |
340 | 'when_monotonic': self.lastrun+self.retry_interval} | |
5 | 341 | else: |
342 | if self.backup_interval==0: | |
10 | 343 | return None |
5 | 344 | else: |
10 | 345 | return {'operation': 'create', |
346 | 'detail': None, | |
347 | 'when_monotonic': self.lastrun+self.backup_interval} | |
348 | ||
349 | def __schedule_unlocked(self): | |
350 | if self.current_operation: | |
351 | return self.current_operation | |
352 | else: | |
353 | op=self.__next_operation_unlocked() | |
354 | ||
355 | if op: | |
356 | now=time.monotonic() | |
357 | delay=max(0, op['when_monotonic']-now) | |
358 | logging.info("Scheduling '%s' (detail: %s) of '%s' in %d seconds" % | |
359 | (op['operation'], op['detail'], self.name, delay)) | |
360 | tmr=Timer(delay, self.__queue_timed_operation) | |
361 | self.scheduled_operation=op | |
362 | self.timer=tmr | |
363 | tmr.start() | |
364 | ||
365 | return op | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
366 | |
8 | 367 | def schedule(self): |
368 | with self.lock: | |
369 | return self.__schedule_unlocked() | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
370 | |
10 | 371 | def set_status_update_callback(self, callback): |
372 | with self.lock: | |
373 | self.status_update_callback=callback | |
8 | 374 | |
10 | 375 | def status(self): |
376 | with self.lock: | |
377 | res=self.__status_unlocked() | |
378 | return res[0] | |
379 | ||
380 | def __status_unlocked(self): | |
381 | callback=self.status_update_callback | |
382 | if self.current_operation: | |
383 | status=self.current_operation | |
384 | status['type']='current' | |
385 | elif self.scheduled_operation: | |
386 | status=self.scheduled_operation | |
387 | status['type']='scheduled' | |
388 | else: | |
389 | status={'type': 'nothing'} | |
390 | ||
391 | status['name']=self.name | |
392 | ||
15 | 393 | if 'errors' not in status: |
394 | status['errors']=False | |
395 | ||
10 | 396 | if 'when_monotonic' in status: |
397 | status['when']=(status['when_monotonic'] | |
398 | -time.monotonic()+time.time()) | |
399 | ||
400 | return status, callback | |
401 | ||
402 | ||
403 |