2023-04-03 18:18:23 +00:00
|
|
|
from typing import List, Optional
|
2023-04-03 23:23:47 +00:00
|
|
|
from datetime import datetime, timezone, timedelta
|
2023-02-17 15:55:19 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.models.backup.snapshot import Snapshot
|
2023-03-13 19:03:41 +00:00
|
|
|
from selfprivacy_api.models.backup.provider import BackupProviderModel
|
2023-02-17 15:55:19 +00:00
|
|
|
|
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-03-13 19:03:41 +00:00
|
|
|
from selfprivacy_api.utils.redis_pool import RedisPool
|
|
|
|
from selfprivacy_api.utils.redis_model_storage import store_model_as_hash, hash_as_model
|
|
|
|
|
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
|
2023-03-13 19:03:41 +00:00
|
|
|
from selfprivacy_api.backup.providers import get_provider, get_kind
|
2023-02-08 14:57:34 +00:00
|
|
|
from selfprivacy_api.graphql.queries.providers import BackupProvider
|
2023-01-23 13:43:18 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
# a hack to store file path.
|
2023-04-03 18:18:23 +00:00
|
|
|
REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS = 24 * 60 * 60 # one day
|
|
|
|
|
2023-04-03 18:37:12 +00:00
|
|
|
REDIS_AUTOBACKUP_ENABLED_PREFIX = "backup:autobackup:services:"
|
2023-04-03 18:18:23 +00:00
|
|
|
REDIS_SNAPSHOTS_PREFIX = "backups:snapshots:"
|
|
|
|
REDIS_LAST_BACKUP_PREFIX = "backups:last-backed-up:"
|
2023-04-03 18:54:27 +00:00
|
|
|
REDIS_INITTED_CACHE_PREFIX = "backups:initted_services:"
|
2023-03-29 11:15:38 +00:00
|
|
|
|
2023-04-03 18:54:27 +00:00
|
|
|
REDIS_REPO_PATH_KEY = "backups:test_repo_path"
|
2023-03-13 19:03:41 +00:00
|
|
|
REDIS_PROVIDER_KEY = "backups:provider"
|
2023-04-03 18:54:27 +00:00
|
|
|
REDIS_AUTOBACKUP_PERIOD_KEY = "backups:autobackup_period"
|
|
|
|
|
2023-03-13 19:03:41 +00:00
|
|
|
|
|
|
|
redis = RedisPool().get_connection()
|
|
|
|
|
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-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_localfile_repo(file_path: str):
|
2023-02-20 13:51:06 +00:00
|
|
|
ProviderClass = get_provider(BackupProvider.FILE)
|
|
|
|
provider = ProviderClass(file_path)
|
2023-03-29 11:15:38 +00:00
|
|
|
redis.set(REDIS_REPO_PATH_KEY, file_path)
|
|
|
|
Backups.store_provider_redis(provider)
|
|
|
|
|
2023-04-03 18:18:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def _redis_last_backup_key(service_id):
|
|
|
|
return REDIS_LAST_BACKUP_PREFIX + service_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _redis_snapshot_key(snapshot: Snapshot):
|
|
|
|
return REDIS_SNAPSHOTS_PREFIX + snapshot.id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_last_backed_up(service: Service) -> Optional[datetime]:
|
2023-04-07 15:18:54 +00:00
|
|
|
"""Get a timezone-aware time of the last backup of a service"""
|
2023-04-03 18:18:23 +00:00
|
|
|
return Backups._get_last_backup_time_redis(service.get_id())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_last_backup_time_redis(service_id: str) -> Optional[datetime]:
|
|
|
|
key = Backups._redis_last_backup_key(service_id)
|
|
|
|
if not redis.exists(key):
|
|
|
|
return None
|
|
|
|
|
2023-04-07 15:18:54 +00:00
|
|
|
snapshot = hash_as_model(redis, key, Snapshot)
|
2023-04-03 18:18:23 +00:00
|
|
|
return snapshot.created_at
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _store_last_snapshot(service_id: str, snapshot: Snapshot):
|
2023-04-07 15:41:02 +00:00
|
|
|
# non-expiring timestamp of the last
|
2023-04-03 18:18:23 +00:00
|
|
|
store_model_as_hash(redis, Backups._redis_last_backup_key(service_id), snapshot)
|
2023-04-07 15:41:02 +00:00
|
|
|
# expiring cache entry
|
|
|
|
Backups.cache_snapshot(snapshot)
|
2023-04-03 18:18:23 +00:00
|
|
|
|
2023-04-07 15:41:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def cache_snapshot(snapshot: Snapshot):
|
2023-04-03 18:18:23 +00:00
|
|
|
snapshot_key = Backups._redis_snapshot_key(snapshot)
|
|
|
|
store_model_as_hash(redis, snapshot_key, snapshot)
|
|
|
|
redis.expire(snapshot_key, REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS)
|
|
|
|
|
2023-04-07 15:41:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def delete_cached_snapshot(snapshot: Snapshot):
|
|
|
|
snapshot_key = Backups._redis_snapshot_key(snapshot)
|
|
|
|
redis.delete(snapshot_key)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_cached_snapshots() -> List[Snapshot]:
|
|
|
|
keys = redis.keys(REDIS_SNAPSHOTS_PREFIX + "*")
|
|
|
|
result = []
|
|
|
|
|
|
|
|
for key in keys:
|
|
|
|
snapshot = hash_as_model(redis, key, Snapshot)
|
|
|
|
result.append(snapshot)
|
|
|
|
return result
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_cached_snapshots_service(service_id: str) -> List[Snapshot]:
|
|
|
|
snapshots = Backups.get_cached_snapshots()
|
|
|
|
return [snap for snap in snapshots if snap.service_name == service_id]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def sync_service_snapshots(service_id: str, snapshots: List[Snapshot]):
|
|
|
|
for snapshot in snapshots:
|
|
|
|
if snapshot.service_name == service_id:
|
|
|
|
Backups.cache_snapshot(snapshot)
|
|
|
|
for snapshot in Backups.get_cached_snapshots_service(service_id):
|
|
|
|
if snapshot.id not in [snap.id for snap in snapshots]:
|
|
|
|
Backups.delete_cached_snapshot(snapshot)
|
|
|
|
|
2023-04-03 18:37:12 +00:00
|
|
|
@staticmethod
|
2023-04-03 23:23:47 +00:00
|
|
|
def _redis_autobackup_key(service_name: str) -> str:
|
|
|
|
return REDIS_AUTOBACKUP_ENABLED_PREFIX + service_name
|
2023-04-03 18:37:12 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def enable_autobackup(service: Service):
|
2023-04-03 23:23:47 +00:00
|
|
|
redis.set(Backups._redis_autobackup_key(service.get_id()), 1)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_time_to_backup(time: datetime) -> bool:
|
|
|
|
"""
|
|
|
|
Intended as a time validator for huey cron scheduler of automatic backups
|
|
|
|
"""
|
|
|
|
for key in redis.keys(REDIS_AUTOBACKUP_ENABLED_PREFIX + "*"):
|
|
|
|
service_id = key.split(":")[-1]
|
|
|
|
if Backups.is_time_to_backup_service(service_id, time):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_time_to_backup_service(service_id: str, time: datetime):
|
|
|
|
period = Backups.autobackup_period_minutes()
|
|
|
|
if period is None:
|
|
|
|
return False
|
|
|
|
if not Backups._is_autobackup_enabled_by_name(service_id) is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
last_backup = Backups._get_last_backup_time_redis(service_id)
|
|
|
|
if last_backup is None:
|
|
|
|
return True # queue a backup immediately if there are no previous backups
|
|
|
|
|
|
|
|
if time > last_backup + timedelta(minutes=period):
|
|
|
|
return True
|
|
|
|
return False
|
2023-04-03 18:37:12 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def disable_autobackup(service: Service):
|
2023-04-03 18:54:27 +00:00
|
|
|
"""also see disable_all_autobackup()"""
|
2023-04-03 23:23:47 +00:00
|
|
|
redis.delete(Backups._redis_autobackup_key(service.get_id()))
|
2023-04-03 18:37:12 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_autobackup_enabled(service: Service) -> bool:
|
2023-04-03 23:23:47 +00:00
|
|
|
return Backups._is_autobackup_enabled_by_name(service.get_id())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _is_autobackup_enabled_by_name(service_name: str):
|
|
|
|
return redis.exists(Backups._redis_autobackup_key(service_name))
|
2023-04-03 18:37:12 +00:00
|
|
|
|
2023-04-03 18:54:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def autobackup_period_minutes() -> Optional[int]:
|
|
|
|
"""None means autobackup is disabled"""
|
|
|
|
if not redis.exists(REDIS_AUTOBACKUP_PERIOD_KEY):
|
|
|
|
return None
|
2023-04-03 22:39:04 +00:00
|
|
|
return int(redis.get(REDIS_AUTOBACKUP_PERIOD_KEY))
|
2023-04-03 18:54:27 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_autobackup_period_minutes(minutes: int):
|
2023-04-03 22:39:04 +00:00
|
|
|
"""
|
|
|
|
0 and negative numbers are equivalent to disable.
|
|
|
|
Setting to a positive number may result in a backup very soon if some services are not backed up.
|
|
|
|
"""
|
|
|
|
if minutes <= 0:
|
|
|
|
Backups.disable_all_autobackup()
|
|
|
|
return
|
2023-04-03 18:54:27 +00:00
|
|
|
redis.set(REDIS_AUTOBACKUP_PERIOD_KEY, minutes)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def disable_all_autobackup():
|
|
|
|
"""disables all automatic backing up, but does not change per-service settings"""
|
|
|
|
redis.delete(REDIS_AUTOBACKUP_PERIOD_KEY)
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def provider():
|
|
|
|
return Backups.lookup_provider()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_provider(kind: str, login: str, key: str):
|
|
|
|
provider = Backups.construct_provider(kind, login, key)
|
|
|
|
Backups.store_provider_redis(provider)
|
2023-02-20 13:51:06 +00:00
|
|
|
|
2023-03-10 14:14:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def construct_provider(kind: str, login: str, key: str):
|
|
|
|
provider_class = get_provider(BackupProvider[kind])
|
2023-03-29 11:15:38 +00:00
|
|
|
|
|
|
|
if kind == "FILE":
|
|
|
|
path = redis.get(REDIS_REPO_PATH_KEY)
|
|
|
|
return provider_class(path)
|
|
|
|
|
2023-03-10 14:14:41 +00:00
|
|
|
return provider_class(login=login, key=key)
|
|
|
|
|
2023-03-13 19:03:41 +00:00
|
|
|
@staticmethod
|
|
|
|
def store_provider_redis(provider: AbstractBackupProvider):
|
|
|
|
store_model_as_hash(
|
|
|
|
redis,
|
|
|
|
REDIS_PROVIDER_KEY,
|
|
|
|
BackupProviderModel(
|
|
|
|
kind=get_kind(provider), login=provider.login, key=provider.key
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def load_provider_redis() -> AbstractBackupProvider:
|
|
|
|
provider_model = hash_as_model(redis, REDIS_PROVIDER_KEY, BackupProviderModel)
|
|
|
|
if provider_model is None:
|
|
|
|
return None
|
|
|
|
return Backups.construct_provider(
|
|
|
|
provider_model.kind, provider_model.login, provider_model.key
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def reset():
|
|
|
|
redis.delete(REDIS_PROVIDER_KEY)
|
2023-03-29 11:15:38 +00:00
|
|
|
redis.delete(REDIS_REPO_PATH_KEY)
|
2023-04-03 18:54:27 +00:00
|
|
|
redis.delete(REDIS_AUTOBACKUP_PERIOD_KEY)
|
2023-04-03 18:18:23 +00:00
|
|
|
|
2023-04-03 18:37:12 +00:00
|
|
|
prefixes_to_clean = [
|
|
|
|
REDIS_INITTED_CACHE_PREFIX,
|
|
|
|
REDIS_SNAPSHOTS_PREFIX,
|
|
|
|
REDIS_LAST_BACKUP_PREFIX,
|
2023-04-03 21:59:14 +00:00
|
|
|
REDIS_AUTOBACKUP_ENABLED_PREFIX,
|
2023-04-03 18:37:12 +00:00
|
|
|
]
|
2023-04-03 18:18:23 +00:00
|
|
|
|
2023-04-03 18:37:12 +00:00
|
|
|
for prefix in prefixes_to_clean:
|
|
|
|
for key in redis.keys(prefix + "*"):
|
|
|
|
redis.delete(key)
|
2023-04-03 18:18:23 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def lookup_provider() -> 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-29 11:15:38 +00:00
|
|
|
Backups.store_provider_redis(json_provider)
|
2023-03-10 14:14:41 +00:00
|
|
|
return json_provider
|
2023-02-08 14:57:34 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
memory_provider = Backups.construct_provider("MEMORY", login="", key="")
|
|
|
|
Backups.store_provider_redis(memory_provider)
|
|
|
|
return memory_provider
|
2023-02-08 14:57:34 +00:00
|
|
|
|
|
|
|
@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-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def back_up(service: Service):
|
2023-04-07 15:41:02 +00:00
|
|
|
"""The top-level function to back up a service"""
|
2023-02-08 14:57:34 +00:00
|
|
|
folder = service.get_location()
|
|
|
|
repo_name = service.get_id()
|
2023-02-08 15:27:49 +00:00
|
|
|
|
|
|
|
service.pre_backup()
|
2023-04-03 18:18:23 +00:00
|
|
|
snapshot = Backups.provider().backuper.start_backup(folder, repo_name)
|
|
|
|
Backups._store_last_snapshot(repo_name, snapshot)
|
|
|
|
|
2023-02-08 15:27:49 +00:00
|
|
|
service.post_restore()
|
2023-02-17 15:55:19 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def init_repo(service: Service):
|
2023-02-17 16:11:17 +00:00
|
|
|
repo_name = service.get_id()
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.provider().backuper.init(repo_name)
|
|
|
|
Backups._redis_mark_as_init(service)
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def _has_redis_init_mark(service: Service) -> bool:
|
2023-03-14 00:39:15 +00:00
|
|
|
repo_name = service.get_id()
|
|
|
|
if redis.exists(REDIS_INITTED_CACHE_PREFIX + repo_name):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def _redis_mark_as_init(service: Service):
|
2023-03-14 00:39:15 +00:00
|
|
|
repo_name = service.get_id()
|
|
|
|
redis.set(REDIS_INITTED_CACHE_PREFIX + repo_name, 1)
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_initted(service: Service) -> bool:
|
2023-03-14 00:39:15 +00:00
|
|
|
repo_name = service.get_id()
|
2023-03-29 11:15:38 +00:00
|
|
|
if Backups._has_redis_init_mark(service):
|
2023-03-14 00:39:15 +00:00
|
|
|
return True
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
initted = Backups.provider().backuper.is_initted(repo_name)
|
2023-03-14 00:39:15 +00:00
|
|
|
if initted:
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups._redis_mark_as_init(service)
|
2023-03-14 00:39:15 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2023-02-17 16:11:17 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_snapshots(service: Service) -> List[Snapshot]:
|
2023-04-07 17:24:53 +00:00
|
|
|
service_id = service.get_id()
|
|
|
|
cached_snapshots = Backups.get_cached_snapshots_service(service_id)
|
|
|
|
if cached_snapshots != []:
|
|
|
|
return cached_snapshots
|
|
|
|
# TODO: the oldest snapshots will get expired faster than the new ones.
|
|
|
|
# How to detect that the end is missing?
|
|
|
|
|
|
|
|
upstream_snapshots = Backups.provider().backuper.get_snapshots(service_id)
|
|
|
|
Backups.sync_service_snapshots(service_id, upstream_snapshots)
|
|
|
|
return upstream_snapshots
|
2023-02-22 14:45:11 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def restore_service_from_snapshot(service: Service, snapshot_id: str):
|
2023-02-22 14:45:11 +00:00
|
|
|
repo_name = service.get_id()
|
|
|
|
folder = service.get_location()
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.provider().backuper.restore_from_backup(repo_name, snapshot_id, folder)
|
2023-02-22 14:45:11 +00:00
|
|
|
|
|
|
|
# Our dummy service is not yet globally registered so this is not testable yet
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def restore_snapshot(snapshot: Snapshot):
|
|
|
|
Backups.restore_service_from_snapshot(
|
2023-02-22 14:45:11 +00:00
|
|
|
get_service_by_id(snapshot.service_name), snapshot.id
|
|
|
|
)
|
2023-02-22 18:48:08 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def service_snapshot_size(service: Service, snapshot_id: str) -> float:
|
2023-02-22 18:48:08 +00:00
|
|
|
repo_name = service.get_id()
|
2023-03-29 11:15:38 +00:00
|
|
|
return Backups.provider().backuper.restored_size(repo_name, snapshot_id)
|
2023-02-22 18:48:08 +00:00
|
|
|
|
|
|
|
# Our dummy service is not yet globally registered so this is not testable yet
|
2023-03-29 11:15:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def snapshot_restored_size(snapshot: Snapshot) -> float:
|
2023-04-03 18:39:55 +00:00
|
|
|
return Backups.service_snapshot_size(
|
2023-02-22 18:48:08 +00:00
|
|
|
get_service_by_id(snapshot.service_name), snapshot.id
|
|
|
|
)
|