refactor(backups): group operations together

This commit is contained in:
Houkime 2023-06-26 18:30:31 +00:00
parent e6efd1b42d
commit 2f2c4f14af

View file

@ -160,6 +160,24 @@ class Backups:
user_data["backup"] = DEFAULT_JSON_PROVIDER user_data["backup"] = DEFAULT_JSON_PROVIDER
### Init
@staticmethod
def init_repo():
Backups.provider().backupper.init()
Storage.mark_as_init()
@staticmethod
def is_initted() -> bool:
if Storage.has_init_mark():
return True
initted = Backups.provider().backupper.is_initted()
if initted:
Storage.mark_as_init()
return True
return False
### Backup ### Backup
@ -189,24 +207,69 @@ class Backups:
Jobs.update(job, status=JobStatus.FINISHED) Jobs.update(job, status=JobStatus.FINISHED)
return snapshot return snapshot
### Init ### Restoring
# to be deprecated/internalized in favor of restore_snapshot()
@staticmethod
def restore_service_from_snapshot(service: Service, snapshot_id: str):
folders = service.get_folders()
Backups.provider().backupper.restore_from_backup(
snapshot_id,
folders,
)
@staticmethod @staticmethod
def init_repo(): def assert_restorable(snapshot: Snapshot):
Backups.provider().backupper.init() service = get_service_by_id(snapshot.service_name)
Storage.mark_as_init() if service is None:
raise ValueError(
f"snapshot has a nonexistent service: {snapshot.service_name}"
)
needed_space = Backups.service_snapshot_size(snapshot.id)
available_space = Backups.space_usable_for_service(service)
if needed_space > available_space:
raise ValueError(
f"we only have {available_space} bytes "
f"but snapshot needs {needed_space}"
)
@staticmethod @staticmethod
def is_initted() -> bool: def restore_snapshot(snapshot: Snapshot):
if Storage.has_init_mark(): service = get_service_by_id(snapshot.service_name)
return True
initted = Backups.provider().backupper.is_initted() if service is None:
if initted: raise ValueError(
Storage.mark_as_init() f"snapshot has a nonexistent service: {snapshot.service_name}"
return True )
return False job = get_restore_job(service)
if job is None:
job = add_restore_job(snapshot)
Jobs.update(
job,
status=JobStatus.RUNNING,
)
try:
Backups.assert_restorable(snapshot)
Backups.restore_service_from_snapshot(
service,
snapshot.id,
)
service.post_restore()
except Exception as e:
Jobs.update(
job,
status=JobStatus.ERROR,
)
raise e
Jobs.update(
job,
status=JobStatus.FINISHED,
)
### Snapshots ### Snapshots
@ -284,71 +347,8 @@ class Backups:
if snapshot.id not in [snap.id for snap in snapshots]: if snapshot.id not in [snap.id for snap in snapshots]:
Storage.delete_cached_snapshot(snapshot) Storage.delete_cached_snapshot(snapshot)
### Restoring
# to be deprecated/internalized in favor of restore_snapshot()
@staticmethod
def restore_service_from_snapshot(service: Service, snapshot_id: str):
folders = service.get_folders()
Backups.provider().backupper.restore_from_backup(
snapshot_id,
folders,
)
@staticmethod
def assert_restorable(snapshot: Snapshot):
service = get_service_by_id(snapshot.service_name)
if service is None:
raise ValueError(
f"snapshot has a nonexistent service: {snapshot.service_name}"
)
needed_space = Backups.service_snapshot_size(snapshot.id)
available_space = Backups.space_usable_for_service(service)
if needed_space > available_space:
raise ValueError(
f"we only have {available_space} bytes "
f"but snapshot needs {needed_space}"
)
@staticmethod
def restore_snapshot(snapshot: Snapshot):
service = get_service_by_id(snapshot.service_name)
if service is None:
raise ValueError(
f"snapshot has a nonexistent service: {snapshot.service_name}"
)
job = get_restore_job(service)
if job is None:
job = add_restore_job(snapshot)
Jobs.update(
job,
status=JobStatus.RUNNING,
)
try:
Backups.assert_restorable(snapshot)
Backups.restore_service_from_snapshot(
service,
snapshot.id,
)
service.post_restore()
except Exception as e:
Jobs.update(
job,
status=JobStatus.ERROR,
)
raise e
Jobs.update(
job,
status=JobStatus.FINISHED,
)
### Autobackup ### Autobackup
@staticmethod @staticmethod
def is_autobackup_enabled(service: Service) -> bool: def is_autobackup_enabled(service: Service) -> bool:
return Storage.is_autobackup_set(service.get_id()) return Storage.is_autobackup_set(service.get_id())