From 58086909a4d0ee30e867ea2289021c140a9e524b Mon Sep 17 00:00:00 2001 From: Houkime <> Date: Mon, 3 Apr 2023 18:37:12 +0000 Subject: [PATCH] feature(backups): check, set and unset service autobackup status --- selfprivacy_api/backup/__init__.py | 33 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/selfprivacy_api/backup/__init__.py b/selfprivacy_api/backup/__init__.py index 9bf87a0..79844a3 100644 --- a/selfprivacy_api/backup/__init__.py +++ b/selfprivacy_api/backup/__init__.py @@ -20,6 +20,8 @@ from selfprivacy_api.graphql.queries.providers import BackupProvider # a hack to store file path. REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS = 24 * 60 * 60 # one day +REDIS_AUTOBACKUP_ENABLED_PREFIX = "backup:autobackup:services:" + REDIS_SNAPSHOTS_PREFIX = "backups:snapshots:" REDIS_LAST_BACKUP_PREFIX = "backups:last-backed-up:" REDIS_REPO_PATH_KEY = "backups:test_repo_path" @@ -74,6 +76,22 @@ class Backups: store_model_as_hash(redis, snapshot_key, snapshot) redis.expire(snapshot_key, REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS) + @staticmethod + def _redis_autobackup_key(service: Service): + return REDIS_AUTOBACKUP_ENABLED_PREFIX + service.get_id() + + @staticmethod + def enable_autobackup(service: Service): + redis.set(Backups._redis_autobackup_key(service), True) + + @staticmethod + def disable_autobackup(service: Service): + redis.delete(Backups._redis_autobackup_key(service)) + + @staticmethod + def is_autobackup_enabled(service: Service) -> bool: + return redis.exists(Backups._redis_autobackup_key(service)) + @staticmethod def provider(): return Backups.lookup_provider() @@ -117,14 +135,15 @@ class Backups: redis.delete(REDIS_PROVIDER_KEY) redis.delete(REDIS_REPO_PATH_KEY) - for key in redis.keys(REDIS_INITTED_CACHE_PREFIX + "*"): - redis.delete(key) + prefixes_to_clean = [ + REDIS_INITTED_CACHE_PREFIX, + REDIS_SNAPSHOTS_PREFIX, + REDIS_LAST_BACKUP_PREFIX, + ] - for key in redis.keys(REDIS_SNAPSHOTS_PREFIX + "*"): - redis.delete(key) - - for key in redis.keys(REDIS_LAST_BACKUP_PREFIX + "*"): - redis.delete(key) + for prefix in prefixes_to_clean: + for key in redis.keys(prefix + "*"): + redis.delete(key) @staticmethod def lookup_provider() -> AbstractBackupProvider: