borg-qt/borg_qt/borg_interface.py

168 lines
4.8 KiB
Python
Raw Normal View History

2019-02-02 14:54:37 +01:00
import os
import shutil
2019-02-02 11:27:38 +01:00
import subprocess
import json
from PyQt5.QtCore import QThread
2019-02-07 21:35:14 +01:00
from helper import BorgException
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
class BorgQtThread(QThread):
def __init__(self):
super().__init__()
self.create_pocess()
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def stop(self):
self.p.kill()
self.json_err = None
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def create_pocess(self):
self.create_command()
self.p = subprocess.Popen(self.command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf8')
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def run(self):
self.json_output, self.json_err = self.p.communicate()
self.p.wait()
self.process_json_error(self.json_err)
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def process_json_error(self, json_err):
if json_err:
error = json_err.splitlines()[0]
if 'stale' in error:
pass
else:
err = json.loads(error)
raise BorgException(err['message'])
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
class ListThread(BorgQtThread):
def create_command(self):
self.command = ['borg', 'list', '--log-json', '--json']
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def run(self):
super().run()
self._process_json_archives()
return self.archives
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def _process_json_archives(self):
self.archives = []
if self.json_output:
output = json.loads(self.json_output)
for i in output['archives']:
self.archives.append(i)
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
class InfoThread(BorgQtThread):
def create_command(self):
self.command = ['borg', 'info', '--log-json', '--json']
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def run(self):
super().run()
self._process_json_repo_stats()
return self.stats
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def _process_json_repo_stats(self):
if self.json_output:
output = json.loads(self.json_output)
self.stats = output['cache']['stats']
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
class BackupThread(BorgQtThread):
2019-02-02 14:54:37 +01:00
"""A class to create a backup with borg.
Args:
prefix (str) the prefix for the archive name.
includes (list) a list of all the paths to backup.
excludes (list) a list of all the paths to exclude from the backup.
"""
2019-02-02 11:27:38 +01:00
def __init__(self, includes, excludes=None, prefix=None):
2019-02-10 16:17:26 +01:00
self.includes = includes
2019-02-07 21:35:14 +01:00
self._process_excludes(excludes)
self._process_prefix(prefix)
2019-02-02 11:27:38 +01:00
super().__init__()
def create_command(self):
self.command = ['borg', 'create', '--log-json', '--json',
('::'
+ self.prefix
+ '{now:%Y-%m-%d_%H:%M:%S}')]
self.command.extend(self.includes)
if self.excludes:
self.command.extend(self.excludes)
2019-02-07 21:35:14 +01:00
def _process_prefix(self, prefix):
if prefix:
self.prefix = prefix + "_"
else:
self.prefix = ""
def _process_excludes(self, excludes):
processed_items = []
if excludes:
for item in excludes:
processed_items.extend(['-e', item])
self.excludes = processed_items
else:
self.excludes = processed_items
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
class RestoreThread(BorgQtThread):
2019-02-02 14:54:37 +01:00
"""A lass to restore a backup with borg.
2019-02-02 11:27:38 +01:00
2019-02-02 14:54:37 +01:00
Args:
archive_name (str) the name of the archive to restore.
restore_path (str) the path where to restore should get stored at.
"""
def __init__(self, archive_name, restore_path):
self.archive_name = archive_name
self.restore_path = restore_path
2019-02-07 21:35:14 +01:00
super().__init__()
2019-02-02 11:27:38 +01:00
2019-02-07 21:35:14 +01:00
def create_command(self):
self.command = ['borg', 'extract', '--log-json',
('::' + self.archive_name)]
2019-02-02 14:54:37 +01:00
2019-02-07 21:35:14 +01:00
def create_pocess(self):
self.create_command()
self.p = subprocess.Popen(self.command,
2019-02-02 14:54:37 +01:00
cwd=self.restore_path,
stdin=subprocess.PIPE,
2019-02-02 16:37:38 +01:00
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf8')
2019-02-07 21:35:14 +01:00
class DeleteThread(BorgQtThread):
2019-02-02 16:37:38 +01:00
"""A lass to restore a backup with borg.
Args:
archive_name (str) the name of the archive to restore.
"""
def __init__(self, archive_name):
self.archive_name = archive_name
2019-02-07 21:35:14 +01:00
super().__init__()
2019-02-02 16:37:38 +01:00
2019-02-07 21:35:14 +01:00
def create_command(self):
self.command = ['borg', 'delete', '--log-json',
('::' + self.archive_name)]
2019-02-02 16:37:38 +01:00
2019-02-07 21:35:14 +01:00
class MountThread(BorgQtThread):
"""A lass to restore a backup with borg.
Args:
archive_name (str) the name of the archive to restore.
"""
def __init__(self, archive_name, mount_path):
self.archive_name = archive_name
self.mount_path = mount_path
super().__init__()
def create_command(self):
self.command = ['borg', 'mount', '--log-json',
('::' + self.archive_name), self.mount_path]