feature(backups): allow non-autobackup slices for full restoration

This commit is contained in:
Houkime 2024-09-06 08:40:32 +00:00
parent 1a9a381753
commit ee06d68047
3 changed files with 11 additions and 13 deletions
selfprivacy_api/backup
tests

View file

@ -760,9 +760,7 @@ class Backups:
@staticmethod @staticmethod
def is_same_slice(snap1: Snapshot, snap2: Snapshot) -> bool: def is_same_slice(snap1: Snapshot, snap2: Snapshot) -> bool:
# Protologic for slicing. Determines if the snaps were made during the same # Determines if the snaps were made roughly in the same time period
# autobackup operation
# TODO: Replace with slice id tag comparison
period_minutes = Backups.autobackup_period_minutes() period_minutes = Backups.autobackup_period_minutes()
# Autobackups are not guaranteed to be enabled during restore. # Autobackups are not guaranteed to be enabled during restore.
@ -779,27 +777,27 @@ class Backups:
return True return True
@staticmethod @staticmethod
def last_autobackup_slice() -> List[Snapshot]: def last_backup_slice() -> List[Snapshot]:
""" """
Guarantees that the slice is valid, ie, it has an api snapshot too Guarantees that the slice is valid, ie, it has an api snapshot too
Or empty Or empty
""" """
slice: List[Snapshot] = [] slice: List[Snapshot] = []
# We need snapshots that were made in the same autobackup session. # We need snapshots that were made around the same time.
# And we need to be sure that api snap is in there # And we need to be sure that api snap is in there
# That's why we form the slice around api snap # That's why we form the slice around api snap
api_autosnaps = Backups._auto_snaps(ServiceManager()) api_snaps = Backups.get_snapshots(ServiceManager())
if api_autosnaps == []: if api_snaps == []:
return [] return []
api_autosnaps.sort(key=lambda x: x.created_at, reverse=True) api_snaps.sort(key=lambda x: x.created_at, reverse=True)
api_snap = api_autosnaps[0] # pick the latest one api_snap = api_snaps[0] # pick the latest one
for service in ServiceManager.get_all_services(): for service in ServiceManager.get_all_services():
if isinstance(service, ServiceManager): if isinstance(service, ServiceManager):
continue continue
snaps = Backups._auto_snaps(service) snaps = Backups.get_snapshots(service)
snaps.sort(key=lambda x: x.created_at, reverse=True) snaps.sort(key=lambda x: x.created_at, reverse=True)
for snap in snaps: for snap in snaps:
if Backups.is_same_slice(snap, api_snap): if Backups.is_same_slice(snap, api_snap):

View file

@ -127,7 +127,7 @@ def eligible_for_full_restoration(snap: Snapshot):
def which_snapshots_to_full_restore() -> list[Snapshot]: def which_snapshots_to_full_restore() -> list[Snapshot]:
autoslice = Backups.last_autobackup_slice() autoslice = Backups.last_backup_slice()
api_snapshot = None api_snapshot = None
for snap in autoslice: for snap in autoslice:
if snap.service_name == "api": if snap.service_name == "api":

View file

@ -217,7 +217,7 @@ def test_slices_with_autobackups_disabled(backups, only_dummy_service_and_api):
snaps = Backups.get_all_snapshots() snaps = Backups.get_all_snapshots()
assert len(snaps) == 2 assert len(snaps) == 2
slice = Backups.last_autobackup_slice() slice = Backups.last_backup_slice()
assert len(slice) == 2 assert len(slice) == 2
assert set([snap.id for snap in slice]) == set([snap.id for snap in snaps]) assert set([snap.id for snap in slice]) == set([snap.id for snap in snaps])
@ -239,7 +239,7 @@ def test_slices_minimal(backups, only_dummy_service_and_api):
snaps = Backups.get_all_snapshots() snaps = Backups.get_all_snapshots()
assert len(snaps) == 2 assert len(snaps) == 2
slice = Backups.last_autobackup_slice() slice = Backups.last_backup_slice()
assert len(slice) == 2 assert len(slice) == 2
assert set([snap.id for snap in slice]) == set([snap.id for snap in snaps]) assert set([snap.id for snap in slice]) == set([snap.id for snap in snaps])