backups: Unmount repositories before and after backup

[sunil: Make the umount code specific to SSH repositories]
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
Guillermo Lopez Alejos 2022-05-02 21:33:34 +02:00 committed by Sunil Mohan Adapa
parent d7a1ea03a3
commit 957ddf5a2d
No known key found for this signature in database
GPG Key ID: 43EA1CFF0AA7C5F2
2 changed files with 18 additions and 0 deletions

View File

@ -133,6 +133,10 @@ class BaseBorgRepository(abc.ABC):
def prepare():
"""Prepare the repository for operations."""
@staticmethod
def cleanup():
"""Cleanup the repository after operations."""
def get_info(self):
"""Return Borg information about a repository."""
output = self.run(['info', '--path', self.borg_path])
@ -393,8 +397,13 @@ class SshBorgRepository(BaseBorgRepository):
if not self.is_usable():
raise errors.SshfsError('Remote host not verified')
self._umount_ignore_errors() # In case the connection is stale.
self.mount()
def cleanup(self):
"""Cleanup the repository after operations by unmounting."""
self._umount_ignore_errors()
@property
def hostname(self):
"""Return hostname from the remote path."""
@ -440,6 +449,13 @@ class SshBorgRepository(BaseBorgRepository):
self._run('sshfs', ['umount', '--mountpoint', self._mountpoint])
def _umount_ignore_errors(self):
"""Run unmount operation and ignore any exceptions thrown."""
try:
self.umount()
except Exception as exception:
logger.warning('Unable to unmount repository', exc_info=exception)
def remove(self):
"""Remove a repository from the kvstore and delete its mountpoint"""
self.umount()

View File

@ -321,3 +321,5 @@ class Schedule:
logger.info('Cleaning up in repository %s backup archive %s',
self.repository_uuid, archive['name'])
repository.delete_archive(archive['name'])
repository.cleanup()