refactor(backups): make api accept a list of folders

This commit is contained in:
Houkime 2023-04-14 11:20:03 +00:00
parent 919ba1ad03
commit 08739f7ca8
5 changed files with 19 additions and 15 deletions

View file

@ -194,11 +194,11 @@ class Backups:
@staticmethod @staticmethod
def back_up(service: Service): def back_up(service: Service):
"""The top-level function to back up a service""" """The top-level function to back up a service"""
folder = service.get_folders() folders = service.get_folders()
repo_name = service.get_id() repo_name = service.get_id()
service.pre_backup() service.pre_backup()
snapshot = Backups.provider().backuper.start_backup(folder, repo_name) snapshot = Backups.provider().backuper.start_backup(folders, repo_name)
Backups._store_last_snapshot(repo_name, snapshot) Backups._store_last_snapshot(repo_name, snapshot)
service.post_restore() service.post_restore()
@ -238,9 +238,9 @@ class Backups:
@staticmethod @staticmethod
def restore_service_from_snapshot(service: Service, snapshot_id: str): def restore_service_from_snapshot(service: Service, snapshot_id: str):
repo_name = service.get_id() repo_name = service.get_id()
folder = service.get_folders() folders = service.get_folders()
Backups.provider().backuper.restore_from_backup(repo_name, snapshot_id, folder) Backups.provider().backuper.restore_from_backup(repo_name, snapshot_id, folders)
@staticmethod @staticmethod
def restore_snapshot(snapshot: Snapshot): def restore_snapshot(snapshot: Snapshot):

View file

@ -13,7 +13,7 @@ class AbstractBackuper(ABC):
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
def start_backup(self, folder: str, repo_name: str): def start_backup(self, folders: List[str], repo_name: str):
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
@ -26,7 +26,7 @@ class AbstractBackuper(ABC):
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
def restore_from_backup(self, repo_name: str, snapshot_id: str, folder: str): def restore_from_backup(self, repo_name: str, snapshot_id: str, folders: List[str]):
"""Restore a target folder using a snapshot""" """Restore a target folder using a snapshot"""
raise NotImplementedError raise NotImplementedError

View file

@ -57,15 +57,19 @@ class ResticBackuper(AbstractBackuper):
command.extend(args) command.extend(args)
return command return command
def start_backup(self, folder: str, repo_name: str): def start_backup(self, folders: List[str], repo_name: str):
""" """
Start backup with restic Start backup with restic
""" """
# but maybe it is ok to accept a union of a string and an array of strings
assert not isinstance(folders, str)
backup_command = self.restic_command( backup_command = self.restic_command(
repo_name, repo_name,
"backup", "backup",
"--json", "--json",
folder, folders[0],
) )
with subprocess.Popen( with subprocess.Popen(
backup_command, backup_command,
@ -145,7 +149,7 @@ class ResticBackuper(AbstractBackuper):
except ValueError as e: except ValueError as e:
raise ValueError("cannot restore a snapshot: " + output) from e raise ValueError("cannot restore a snapshot: " + output) from e
def restore_from_backup(self, repo_name, snapshot_id, folder): def restore_from_backup(self, repo_name, snapshot_id, folders):
""" """
Restore from backup with restic Restore from backup with restic
""" """

View file

@ -1,6 +1,7 @@
"""Class representing Bitwarden service""" """Class representing Bitwarden service"""
import base64 import base64
import typing import typing
from typing import List
from selfprivacy_api.jobs import Job from selfprivacy_api.jobs import Job
from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus from selfprivacy_api.services.service import Service, ServiceDnsRecord, ServiceStatus
@ -111,9 +112,8 @@ class DummyService(Service):
return "sda1" return "sda1"
@classmethod @classmethod
def get_folders(cls) -> str: def get_folders(cls) -> List[str]:
# for now only a single folder return [cls.location]
return cls.location
@staticmethod @staticmethod
def get_dns_records() -> typing.List[ServiceDnsRecord]: def get_dns_records() -> typing.List[ServiceDnsRecord]:

View file

@ -143,10 +143,10 @@ def test_one_snapshot(backups, dummy_service):
def test_backup_returns_snapshot(backups, dummy_service): def test_backup_returns_snapshot(backups, dummy_service):
service_folder = dummy_service.get_folders() service_folders = dummy_service.get_folders()
provider = Backups.provider() provider = Backups.provider()
name = dummy_service.get_id() name = dummy_service.get_id()
snapshot = provider.backuper.start_backup(service_folder, name) snapshot = provider.backuper.start_backup(service_folders, name)
assert snapshot.id is not None assert snapshot.id is not None
assert snapshot.service_name == name assert snapshot.service_name == name
@ -154,7 +154,7 @@ def test_backup_returns_snapshot(backups, dummy_service):
def test_restore(backups, dummy_service): def test_restore(backups, dummy_service):
service_folder = dummy_service.get_folders() service_folder = dummy_service.get_folders()[0]
file_to_nuke = listdir(service_folder)[0] file_to_nuke = listdir(service_folder)[0]
assert file_to_nuke is not None assert file_to_nuke is not None
path_to_nuke = path.join(service_folder, file_to_nuke) path_to_nuke = path.join(service_folder, file_to_nuke)