1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(vmbackups): fourth audit: missing incrementals, VM row changes

- Retention counts keep in ready backups only and also deletes the
  missing ones older than the oldest kept: an incremental whose files
  were lost no longer pins its chain on the target forever.
- When another row takes a VM's place (a migration the destination's
  VM list recorded first, or a move found by a sync), its backups,
  schedule membership and today's schedule runs move with its owners:
  owners keep their backups, retention and restore over still see
  them, and the VM is not backed up twice that night.
This commit is contained in:
catborise 2026-11-18 10:06:52 +03:00
parent bd5d42e65a
commit 57d4b61c6e
5 changed files with 71 additions and 17 deletions

View file

@ -1,3 +1,4 @@
import datetime
from uuid import UUID
from django.db import IntegrityError
@ -56,3 +57,23 @@ class BackupModelTests(TestCase):
def test_claim_keys(self):
self.assertEqual(backup_target_key(3), "backup_target:3")
self.assertEqual(backup_key("00000000-0000-4000-8000-000000000001"), "backup:00000000-0000-4000-8000-000000000001")
def test_a_vm_whose_row_changes_keeps_its_backups_and_schedules(self):
# a migration the destination's VM list recorded first: that row takes the VM's place
from computes.utils import move_ownership
from vmbackups.models import BackupPolicy, BackupRun
uuid = "11111111-1111-1111-1111-111111111111"
old = Instance.objects.create(compute=self.compute, name="vm1", uuid=uuid)
other = Compute.objects.create(name="h2", hostname="192.0.2.11", login="u", password="p", type=1)
new = Instance.objects.create(compute=other, name="vm1", uuid=uuid)
backup = self.backup(old)
policy = BackupPolicy.objects.create(name="nightly", target=self.target, time=datetime.time(2), keep=2)
policy.instances.add(old)
BackupRun.objects.create(policy=policy, instance=old, day=datetime.date(2026, 11, 18), state=BackupRun.SUBMITTED)
move_ownership(old, new)
old.delete()
backup.refresh_from_db()
self.assertEqual(backup.instance, new)
self.assertEqual(list(policy.instances.all()), [new])
self.assertEqual(list(policy.runs.values_list("instance", flat=True)), [new.pk]) # not backed up twice tonight

View file

@ -17,7 +17,7 @@ from operations import jobs
from operations.jobs import Context
from operations.models import Operation, acquire
from vmbackups import schedule
from vmbackups.jobs import CheckTarget, DeleteBackup
from vmbackups.jobs import CheckTarget, DeleteBackup, _beyond_keep
from vmbackups.models import Backup, BackupFile, BackupPolicy, BackupRun, BackupTarget, BackupTargetPool
from vrtManager.util import OperationError
@ -397,6 +397,24 @@ class ScheduleTests(TransactionTestCase):
self.assertEqual(len(self.deletes()), 2)
self.assertEqual(self.deletes()[-1].params["backup"], str(oldest.uuid))
def test_a_missing_incremental_goes_with_its_chain(self):
# keep 2: a full and an incremental on it whose files are lost, then two new fulls
full, inc, new, newest = self.backups(Backup.READY, Backup.MISSING, Backup.READY, Backup.READY)
self.chained(full, inc)
schedule.retain()
(op,) = self.deletes()
self.assertEqual(op.params["backup"], str(inc.uuid))
self.assertTrue(_beyond_keep(inc, self.policy.pk))
self.finish_all()
Backup.objects.filter(pk=inc.pk).delete()
schedule.retain()
self.assertEqual(self.deletes()[-1].params["backup"], str(full.uuid))
def test_a_missing_backup_does_not_count_in_keep(self):
oldest, _missing, _newest = self.backups(Backup.READY, Backup.MISSING, Backup.READY)
schedule.retain()
self.assertEqual(self.deletes(), [])
def test_retention_keeps_keep_ready_backups_when_the_newest_failed(self):
self.backups(Backup.READY, Backup.READY, Backup.FAILED)
schedule.retain()