[wip] Work on borg local repo

This commit is contained in:
ljf 2019-01-01 16:42:48 +01:00
parent a7af79d93e
commit 9f5b826078
2 changed files with 128 additions and 106 deletions

View file

@ -1888,13 +1888,37 @@ class TarBackupMethod(BackupMethod):
class BorgBackupMethod(BackupMethod): class BorgBackupMethod(BackupMethod):
def __init__(self, repo=None):
super(TarBackupMethod, self).__init__(repo)
if not self.repo.domain:
filesystem.mkdir(self.repo.path, parent=True)
else:
#Todo Initialize remote repo
pass
@property @property
def method_name(self): def method_name(self):
return 'borg' return 'borg'
def backup(self): def backup(self):
""" Backup prepared files with borg """ """ Backup prepared files with borg """
super(CopyBackupMethod, self).backup() super(BorgBackupMethod, self).backup()
for path in self.manager.paths_to_backup:
source = path['source']
dest = os.path.join(self.repo.path, path['dest'])
if source == dest:
logger.debug("Files already copyed")
return
dest_parent = os.path.dirname(dest)
if not os.path.exists(dest_parent):
filesystem.mkdir(dest_parent, 0o750, True, uid='admin')
if os.path.isdir(source):
shutil.copytree(source, dest)
else:
shutil.copy(source, dest)
# TODO run borg create command # TODO run borg create command
raise YunohostError('backup_borg_not_implemented') raise YunohostError('backup_borg_not_implemented')

View file

@ -91,7 +91,7 @@ class BackupRepository(object):
except Exception as e: except Exception as e:
raise YunohostError('backup_cant_save_repositories_file', reason=e) raise YunohostError('backup_cant_save_repositories_file', reason=e)
def __init__(self, created=True, location, name=None, description=None, method=None, def __init__(self, created, location, name=None, description=None, method=None,
encryption=None, quota=None): encryption=None, quota=None):
self.location = location self.location = location
@ -126,127 +126,125 @@ class BackupRepository(object):
repositories.pop(self.name) repositories.pop(self.name)
BackupRepository.save() BackupRepository.save()
if purge: if purge:
self.purge() self.purge()
def save(self): def save(self):
BackupRepository.reposirories[self.name] = self.__dict__ BackupRepository.reposirories[self.name] = self.__dict__
BackupRepository.save() BackupRepository.save()
def _split_location(self): def _split_location(self):
"""
Split a repository location into protocol, user, domain and path
"""
location_regex = r'^((?P<protocol>ssh://)?(?P<user>[^@ ]+)@(?P<domain>[^: ]+:))?(?P<path>[^\0]+)$'
location_match = re.match(location_regex, self.location)
if location_match is None:
raise YunohostError('backup_repositories_invalid_location',
location=location)
self.protocol = location_match.group('protocol')
self.user = location_match.group('user')
self.domain = location_match.group('domain')
self.path = location_match.group('path')
def backup_repository_list(name, full=False):
""" """
List available repositories where put archives Split a repository location into protocol, user, domain and path
""" """
repositories = BackupRepository.load() location_regex = r'^((?P<protocol>ssh://)?(?P<user>[^@ ]+)@(?P<domain>[^: ]+:))?(?P<path>[^\0]+)$'
location_match = re.match(location_regex, self.location)
if full: if location_match is None:
return repositories raise YunohostError('backup_repositories_invalid_location',
else: location=location)
return repositories.keys()
self.protocol = location_match.group('protocol')
self.user = location_match.group('user')
self.domain = location_match.group('domain')
self.path = location_match.group('path')
def backup_repository_list(name, full=False):
"""
List available repositories where put archives
"""
repositories = BackupRepository.load()
if full:
return repositories
else:
return repositories.keys()
def backup_repository_info(name, human_readable=True, space_used=False):
"""
Show info about a repository
Keyword arguments:
name -- Name of the backup repository
"""
repository = BackupRepository.get(name)
if space_used:
repository.compute_space_used()
repository = repository.__dict__
if human_readable:
if 'quota' in repository:
repository['quota'] = binary_to_human(repository['quota'])
if 'used' in repository and isinstance(repository['used'], int):
repository['used'] = binary_to_human(repository['used'])
return repository
def backup_repository_info(name, human_readable=True, space_used=False): @is_unit_operation()
""" def backup_repository_add(operation_logger, location, name, description=None,
Show info about a repository methods=None, quota=None, encryption="passphrase"):
"""
Add a backup repository
Keyword arguments: Keyword arguments:
name -- Name of the backup repository location -- Location of the repository (could be a remote location)
""" name -- Name of the backup repository
repository = BackupRepository.get(name) description -- An optionnal description
quota -- Maximum size quota of the repository
encryption -- If available, the kind of encryption to use
"""
repository = BackupRepository(
location, name, description, methods, quota, encryption)
if space_used: try:
repository.compute_space_used() repository.save()
except MoulinetteError:
raise YunohostError('backup_repository_add_failed',
repository=name, location=location)
repository = repository.__dict__ logger.success(m18n.n('backup_repository_added',
if human_readable: repository=name, location=location))
if 'quota' in repository:
repository['quota'] = binary_to_human(repository['quota'])
if 'used' in repository and isinstance(repository['used'], int):
repository['used'] = binary_to_human(repository['used'])
return repository
@is_unit_operation() @is_unit_operation()
def backup_repository_add(operation_logger, location, name, description=None, def backup_repository_update(operation_logger, name, description=None,
methods=None, quota=None, encryption="passphrase"): quota=None, password=None):
""" """
Add a backup repository Update a backup repository
Keyword arguments: Keyword arguments:
location -- Location of the repository (could be a remote location) name -- Name of the backup repository
name -- Name of the backup repository """
description -- An optionnal description repository = BackupRepository.get(name)
quota -- Maximum size quota of the repository
encryption -- If available, the kind of encryption to use
"""
repository = BackupRepository(
location, name, description, methods, quota, encryption)
try: if description is not None:
repository.save() repository.description = description
except MoulinetteError:
raise YunohostError('backup_repository_add_failed',
repository=name, location=location)
logger.success(m18n.n('backup_repository_added', if quota is not None:
repository=name, location=location)) repository.quota = quota
try:
repository.save()
except MoulinetteError:
raise YunohostError('backup_repository_update_failed', repository=name)
logger.success(m18n.n('backup_repository_updated', repository=name,
location=repository['location']))
@is_unit_operation() @is_unit_operation()
def backup_repository_update(operation_logger, name, description=None, def backup_repository_remove(operation_logger, name, purge=False):
quota=None, password=None): """
""" Remove a backup repository
Update a backup repository
Keyword arguments: Keyword arguments:
name -- Name of the backup repository name -- Name of the backup repository to remove
"""
repository = BackupRepository.get(name)
if description is not None: """
repository.description = description repository = BackupRepository.get(name)
repository.delete(purge)
if quota is not None: logger.success(m18n.n('backup_repository_removed', repository=name,
repository.quota = quota path=repository['path']))
try:
repository.save()
except MoulinetteError:
raise YunohostError('backup_repository_update_failed', repository=name)
logger.success(m18n.n('backup_repository_updated', repository=name,
location=repository['location']))
@is_unit_operation()
def backup_repository_remove(operation_logger, name, purge=False):
"""
Remove a backup repository
Keyword arguments:
name -- Name of the backup repository to remove
"""
repository = BackupRepository.get(name)
repository.delete(purge)
logger.success(m18n.n('backup_repository_removed', repository=name,
path=repository['path']))