feat(backups): add restore_snapshot and restore_service_from_snapshot

This commit is contained in:
Houkime 2023-02-22 14:45:11 +00:00
parent 39a97cf6d8
commit f2aab38085
3 changed files with 27 additions and 2 deletions

View file

@ -4,12 +4,14 @@ from selfprivacy_api.models.backup.snapshot import Snapshot
from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass
from selfprivacy_api.services import get_service_by_id
from selfprivacy_api.services.service import Service from selfprivacy_api.services.service import Service
from selfprivacy_api.backup.providers.provider import AbstractBackupProvider from selfprivacy_api.backup.providers.provider import AbstractBackupProvider
from selfprivacy_api.backup.providers import get_provider from selfprivacy_api.backup.providers import get_provider
from selfprivacy_api.graphql.queries.providers import BackupProvider from selfprivacy_api.graphql.queries.providers import BackupProvider
# Singleton has a property of being persistent between tests. I don't know what to do with this yet
# class Backups(metaclass=SingletonMetaclass): # class Backups(metaclass=SingletonMetaclass):
class Backups: class Backups:
"""A singleton controller for backups""" """A singleton controller for backups"""
@ -63,3 +65,15 @@ class Backups:
repo_name = service.get_id() repo_name = service.get_id()
return self.provider.backuper.get_snapshots(repo_name) return self.provider.backuper.get_snapshots(repo_name)
def restore_service_from_snapshot(self, service: Service, snapshot_id: str):
repo_name = service.get_id()
folder = service.get_location()
self.provider.backuper.restore_from_backup(repo_name, snapshot_id, folder)
# Our dummy service is not yet globally registered so this is not testable yet
def restore_snapshot(self, snapshot: Snapshot):
self.restore_service_from_snapshot(
get_service_by_id(snapshot.service_name), snapshot.id
)

View file

@ -20,3 +20,8 @@ class AbstractBackuper(ABC):
@abstractmethod @abstractmethod
def init(self, repo_name): def init(self, repo_name):
raise NotImplementedError raise NotImplementedError
@abstractmethod
def restore_from_backup(self, repo_name: str, snapshot_id: str, folder: str):
"""Restore a target folder using a snapshot"""
raise NotImplementedError

View file

@ -98,7 +98,13 @@ class ResticBackuper(AbstractBackuper):
repo_name, "restore", snapshot_id, "--target", folder repo_name, "restore", snapshot_id, "--target", folder
) )
subprocess.run(restore_command, shell=False) with subprocess.Popen(
restore_command, stdout=subprocess.PIPE, shell=False
) as handle:
output = handle.communicate()[0].decode("utf-8")
if "restored" not in output:
raise ValueError("cannot restore a snapshot: " + output)
def _load_snapshots(self, repo_name) -> object: def _load_snapshots(self, repo_name) -> object:
""" """