feature(backups): check, set and unset service autobackup status

This commit is contained in:
Houkime 2023-04-03 18:37:12 +00:00
parent 18d5cd2b83
commit 45f33e2d31

View file

@ -20,6 +20,8 @@ from selfprivacy_api.graphql.queries.providers import BackupProvider
# a hack to store file path. # a hack to store file path.
REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS = 24 * 60 * 60 # one day REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS = 24 * 60 * 60 # one day
REDIS_AUTOBACKUP_ENABLED_PREFIX = "backup:autobackup:services:"
REDIS_SNAPSHOTS_PREFIX = "backups:snapshots:" REDIS_SNAPSHOTS_PREFIX = "backups:snapshots:"
REDIS_LAST_BACKUP_PREFIX = "backups:last-backed-up:" REDIS_LAST_BACKUP_PREFIX = "backups:last-backed-up:"
REDIS_REPO_PATH_KEY = "backups:test_repo_path" REDIS_REPO_PATH_KEY = "backups:test_repo_path"
@ -74,6 +76,22 @@ class Backups:
store_model_as_hash(redis, snapshot_key, snapshot) store_model_as_hash(redis, snapshot_key, snapshot)
redis.expire(snapshot_key, REDIS_SNAPSHOT_CACHE_EXPIRE_SECONDS) 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 @staticmethod
def provider(): def provider():
return Backups.lookup_provider() return Backups.lookup_provider()
@ -117,13 +135,14 @@ class Backups:
redis.delete(REDIS_PROVIDER_KEY) redis.delete(REDIS_PROVIDER_KEY)
redis.delete(REDIS_REPO_PATH_KEY) redis.delete(REDIS_REPO_PATH_KEY)
for key in redis.keys(REDIS_INITTED_CACHE_PREFIX + "*"): prefixes_to_clean = [
redis.delete(key) REDIS_INITTED_CACHE_PREFIX,
REDIS_SNAPSHOTS_PREFIX,
REDIS_LAST_BACKUP_PREFIX,
]
for key in redis.keys(REDIS_SNAPSHOTS_PREFIX + "*"): for prefix in prefixes_to_clean:
redis.delete(key) for key in redis.keys(prefix + "*"):
for key in redis.keys(REDIS_LAST_BACKUP_PREFIX + "*"):
redis.delete(key) redis.delete(key)
@staticmethod @staticmethod