2023-02-17 15:55:19 +00:00
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from selfprivacy_api.models.backup.snapshot import Snapshot
|
|
|
|
|
2023-02-08 14:57:34 +00:00
|
|
|
from selfprivacy_api.utils.singleton_metaclass import SingletonMetaclass
|
2023-03-10 14:14:41 +00:00
|
|
|
from selfprivacy_api.utils import ReadUserData
|
2023-02-08 14:05:25 +00:00
|
|
|
|
2023-02-22 14:45:11 +00:00
|
|
|
from selfprivacy_api.services import get_service_by_id
|
2023-02-08 14:57:34 +00:00
|
|
|
from selfprivacy_api.services.service import Service
|
2023-02-22 14:45:11 +00:00
|
|
|
|
2023-02-08 14:57:34 +00:00
|
|
|
from selfprivacy_api.backup.providers.provider import AbstractBackupProvider
|
|
|
|
from selfprivacy_api.backup.providers import get_provider
|
|
|
|
from selfprivacy_api.graphql.queries.providers import BackupProvider
|
2023-01-23 13:43:18 +00:00
|
|
|
|
2023-03-10 14:14:41 +00:00
|
|
|
|
|
|
|
# Singleton has a property of being persistent between tests.
|
|
|
|
# I don't know what to do with this yet
|
2023-02-20 16:09:01 +00:00
|
|
|
# class Backups(metaclass=SingletonMetaclass):
|
|
|
|
class Backups:
|
2023-02-08 14:18:45 +00:00
|
|
|
"""A singleton controller for backups"""
|
|
|
|
|
2023-02-08 14:57:34 +00:00
|
|
|
provider: AbstractBackupProvider
|
2023-02-08 14:18:45 +00:00
|
|
|
|
2023-02-20 13:51:06 +00:00
|
|
|
def __init__(self, test_repo_file: str = ""):
|
|
|
|
if test_repo_file != "":
|
|
|
|
self.set_localfile_repo(test_repo_file)
|
2023-02-20 16:09:01 +00:00
|
|
|
else:
|
2023-03-10 14:14:41 +00:00
|
|
|
self.provider = self.lookup_provider()
|
2023-02-08 14:57:34 +00:00
|
|
|
|
2023-02-20 13:51:06 +00:00
|
|
|
def set_localfile_repo(self, file_path: str):
|
|
|
|
ProviderClass = get_provider(BackupProvider.FILE)
|
|
|
|
provider = ProviderClass(file_path)
|
|
|
|
self.provider = provider
|
|
|
|
|
2023-03-10 14:14:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def construct_provider(kind: str, login: str, key: str):
|
|
|
|
provider_class = get_provider(BackupProvider[kind])
|
|
|
|
return provider_class(login=login, key=key)
|
|
|
|
|
|
|
|
def lookup_provider(self) -> AbstractBackupProvider:
|
2023-02-08 14:57:34 +00:00
|
|
|
redis_provider = Backups.load_provider_redis()
|
|
|
|
if redis_provider is not None:
|
2023-03-10 14:14:41 +00:00
|
|
|
return redis_provider
|
2023-02-08 14:57:34 +00:00
|
|
|
|
|
|
|
json_provider = Backups.load_provider_json()
|
|
|
|
if json_provider is not None:
|
2023-03-10 14:14:41 +00:00
|
|
|
return json_provider
|
2023-02-08 14:57:34 +00:00
|
|
|
|
2023-03-10 14:14:41 +00:00
|
|
|
return Backups.construct_provider("MEMORY", login="", key="")
|
2023-02-08 14:57:34 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_provider_redis() -> AbstractBackupProvider:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_provider_json() -> AbstractBackupProvider:
|
2023-03-10 14:14:41 +00:00
|
|
|
with ReadUserData() as user_data:
|
|
|
|
account = ""
|
|
|
|
key = ""
|
|
|
|
|
|
|
|
if "backup" not in user_data.keys():
|
|
|
|
if "backblaze" in user_data.keys():
|
|
|
|
account = user_data["backblaze"]["accountId"]
|
|
|
|
key = user_data["backblaze"]["accountKey"]
|
|
|
|
provider_string = "BACKBLAZE"
|
|
|
|
return Backups.construct_provider(
|
|
|
|
kind=provider_string, login=account, key=key
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
account = user_data["backup"]["accountId"]
|
|
|
|
key = user_data["backup"]["accountKey"]
|
|
|
|
provider_string = user_data["backup"]["provider"]
|
|
|
|
return Backups.construct_provider(
|
|
|
|
kind=provider_string, login=account, key=key
|
|
|
|
)
|
2023-02-08 14:05:25 +00:00
|
|
|
|
2023-02-08 14:57:34 +00:00
|
|
|
def back_up(self, service: Service):
|
|
|
|
folder = service.get_location()
|
|
|
|
repo_name = service.get_id()
|
2023-02-08 15:27:49 +00:00
|
|
|
|
|
|
|
service.pre_backup()
|
2023-02-08 14:57:34 +00:00
|
|
|
self.provider.backuper.start_backup(folder, repo_name)
|
2023-02-08 15:27:49 +00:00
|
|
|
service.post_restore()
|
2023-02-17 15:55:19 +00:00
|
|
|
|
2023-02-17 16:11:17 +00:00
|
|
|
def init_repo(self, service: Service):
|
|
|
|
repo_name = service.get_id()
|
|
|
|
self.provider.backuper.init(repo_name)
|
|
|
|
|
2023-02-17 15:55:19 +00:00
|
|
|
def get_snapshots(self, service: Service) -> List[Snapshot]:
|
|
|
|
repo_name = service.get_id()
|
|
|
|
|
|
|
|
return self.provider.backuper.get_snapshots(repo_name)
|
2023-02-22 14:45:11 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2023-02-22 18:48:08 +00:00
|
|
|
|
|
|
|
def service_snapshot_size(self, service: Service, snapshot_id: str) -> float:
|
|
|
|
repo_name = service.get_id()
|
|
|
|
return self.provider.backuper.restored_size(repo_name, snapshot_id)
|
|
|
|
|
|
|
|
# Our dummy service is not yet globally registered so this is not testable yet
|
|
|
|
def snapshot_restored_size(self, snapshot: Snapshot) -> float:
|
|
|
|
return self.service_snapshot_size(
|
|
|
|
get_service_by_id(snapshot.service_name), snapshot.id
|
|
|
|
)
|