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

feat(vmbackups): restore, delete, retention, check and import know chains

An incremental backup is restored by copying its chain flat: every base's
disk file is checked first, as the backup's own, and a chain whose base's
row is gone is refused. A backup that a living backup builds on is not
deleted, by hand or by retention: retention deletes a chain only once all
of it is beyond keep, newest first; a failed backup holds no base. The
target check marks a backup missing when a base's file is gone. Reading a
target links each incremental backup to its base, read first, and leaves
one without its base unread. Restoring over the VM forgets its
checkpoints: its disks are replaced, and the next backup is full.
This commit is contained in:
catborise 2026-11-17 17:03:45 +03:00
parent 72e270a87e
commit d856ccca68
12 changed files with 555 additions and 391 deletions

View file

@ -85,6 +85,23 @@ class DeleteBackupTests(TransactionTestCase):
with self.assertRaisesMessage(OperationError, "being made"):
self.submit()
def test_a_backup_others_build_on_is_refused_until_they_go(self):
child = Backup.objects.create(target=self.target, instance=self.vm, vm_uuid=self.vm.uuid, vm_name="vm1",
compute_name="h1", state=Backup.READY, parent=self.backup)
with self.assertRaisesMessage(OperationError, "depend on this one"):
self.submit()
Backup.objects.filter(pk=child.pk).update(state=Backup.FAILED) # a failed one holds nothing
self.run_job(self.submit())
self.assertFalse(Backup.objects.filter(pk=self.backup.pk).exists())
def test_a_base_made_after_the_deletion_was_queued_is_kept(self):
op = self.submit()
Backup.objects.create(target=self.target, instance=self.vm, vm_uuid=self.vm.uuid, vm_name="vm1",
compute_name="h1", state=Backup.READY, parent=self.backup)
with self.assertRaisesMessage(OperationError, "depend on this one"):
self.run_job(op)
self.assertEqual(self.removed, [])
def test_a_target_no_compute_reaches_is_refused(self):
op = self.submit()
with patch("vmbackups.jobs.target_pool", side_effect=OperationError("Storage pool %(pool)s is gone", pool="b1")):

View file

@ -100,6 +100,34 @@ class ImportTests(TransactionTestCase):
self.assertEqual(self.read(), {"added": 0})
self.assertEqual(Backup.objects.count(), 2)
def test_a_chain_is_read_base_first_and_linked(self):
base, top = sorted((self.a, self.b), reverse=True) # the base's manifest is listed last
older = manifest(base)
older["created_at"] = "2026-10-31T10:00:00+00:00"
older["checkpoint"] = f"wvc-{base}"
newer = manifest(top)
newer["parent"] = base
self.files[f"wvc-{base}-manifest.json"] = json.dumps(older).encode()
self.files[f"wvc-{top}-manifest.json"] = json.dumps(newer).encode()
self.assertEqual(self.read(), {"added": 2})
self.assertEqual(Backup.objects.get(uuid=top).parent, Backup.objects.get(uuid=base))
self.assertEqual(Backup.objects.get(uuid=base).checkpoint, f"wvc-{base}")
def test_a_time_without_a_zone_does_not_stop_the_others(self):
odd = manifest(self.a)
odd["created_at"] = "2026-11-01T10:00:00"
self.files[f"wvc-{self.a}-manifest.json"] = json.dumps(odd).encode()
self.assertGreaterEqual(self.read()["added"], 1)
self.assertTrue(Backup.objects.filter(uuid=self.b).exists())
def test_an_incremental_backup_without_its_base_is_incomplete(self):
lonely = manifest(self.a)
lonely["parent"] = str(uuid4())
self.files[f"wvc-{self.a}-manifest.json"] = json.dumps(lonely).encode()
result = self.read()
self.assertEqual(result["added"], 1)
self.assertIn(f"wvc-{self.a}-manifest.json", result["warning"][0]["values"]["names"])
def test_a_manifest_with_a_missing_file_is_incomplete(self):
del self.files[f"wvc-{self.b}-vda.qcow2"]
self.files[f"wvc-{self.a}-domain.xml"] = b"<domain/>" # changed since it was written

View file

@ -264,6 +264,25 @@ class RestoreTests(TransactionTestCase):
op.refresh_from_db()
self.assertEqual(RestoreBackup().reconcile(op).state, Operation.FAILED)
def test_an_incremental_backup_needs_its_whole_chain(self):
base = Backup.objects.create(target=self.target, vm_uuid=self.backup.vm_uuid, vm_name="vm1", compute_name="h1",
state=Backup.READY)
BackupFile.objects.create(backup=base, role=BackupFile.DISK, dev="vda", volume=f"{base.prefix}-vda.qcow2",
format="qcow2", capacity=GIB)
Backup.objects.filter(pk=self.backup.pk).update(parent=base)
seen = []
self.tpool.storageVolLookupByName.side_effect = lambda name: (
seen.append(name), (_ for _ in ()).throw(error(libvirt.VIR_ERR_NO_STORAGE_VOL))
if name == f"{base.prefix}-vda.qcow2" else MagicMock(info=MagicMock(return_value=[0, GIB, GIB])))[1]
with self.assertRaisesMessage(OperationError, f"{base.prefix}-vda.qcow2"):
self.run_job(self.submit())
self.storage.clone_volume.assert_not_called()
def test_a_chain_whose_base_is_gone_is_refused(self):
Backup.objects.filter(pk=self.backup.pk).update(manifest={"parent": str(uuid4())})
with self.assertRaisesMessage(OperationError, "builds on a backup that is gone"):
self.run_job(self.submit())
def test_the_nvram_file_is_qemus(self):
self.run_job(self.submit())
self.assertEqual(self.conn.upload_volume.call_args.kwargs, {"owner": _owner(), "mode": "0600"})

View file

@ -165,6 +165,11 @@ class RestoreOverTests(TransactionTestCase):
self.assertTrue(op.intent["switching"])
self.assertNotIn("/pool/vm1.qcow2", self.removed) # the old volume stays
def test_the_chain_ends(self):
self.run_job(self.submit())
# its disks are replaced: their bitmaps are not the VM's any more
self.proxy.drop_checkpoint_metadata.assert_called_once_with()
def test_the_tpm_state_is_replaced(self):
self.with_tpm()
self.run_job(self.submit())

View file

@ -363,6 +363,28 @@ class ScheduleTests(TransactionTestCase):
schedule.retain()
self.assertEqual(self.deletes()[-1].params["backup"], str(older.uuid))
def chained(self, *backups):
for parent, child in zip(backups, backups[1:]):
Backup.objects.filter(pk=child.pk).update(parent=parent)
def test_retention_deletes_a_chain_only_when_all_of_it_is_beyond_keep(self):
# keep 2: a full, two incrementals on it, then a new full
full, inc1, inc2, _new = self.backups(*[Backup.READY] * 4)
self.chained(full, inc1, inc2)
schedule.retain() # inc2 is kept, and inc1 and full are its base
self.assertEqual(self.deletes(), [])
(newest,) = self.backups(Backup.READY) # one more: inc2 is beyond keep too, and a leaf
Backup.objects.filter(pk=newest.pk).update(created_at=at(2026, 11, 20))
schedule.retain()
self.assertEqual(self.deletes()[-1].params["backup"], str(inc2.uuid)) # newest first: each one a leaf
def test_a_failed_incremental_does_not_pin_its_base(self):
oldest, _a, _b = self.backups(*[Backup.READY] * 3)
(failed,) = self.backups(Backup.FAILED)
Backup.objects.filter(pk=failed.pk).update(parent=oldest)
schedule.retain()
self.assertEqual(self.deletes()[-1].params["backup"], str(oldest.uuid))
def test_a_failed_deletion_is_tried_again_an_hour_later(self):
oldest, *_rest = self.backups(*[Backup.READY] * 3)
schedule.retain()
@ -451,6 +473,14 @@ class ScheduleTests(TransactionTestCase):
self.run_check([*present, f"{gone.prefix}-manifest.json"])
self.assertEqual(Backup.objects.get(pk=gone.pk).state, Backup.READY)
def test_an_incremental_backup_whose_base_file_is_gone_is_missing(self):
base, top = self.backups(Backup.READY, Backup.READY)
Backup.objects.filter(pk=top.pk).update(parent=base)
for b in (base, top):
BackupFile.objects.create(backup=b, role=BackupFile.DISK, dev="vda", volume=f"{b.prefix}-vda.qcow2")
self.run_check([f"{base.prefix}-manifest.json", f"{top.prefix}-vda.qcow2", f"{top.prefix}-manifest.json"])
self.assertEqual(Backup.objects.get(pk=top.pk).state, Backup.MISSING)
def test_a_check_that_reaches_no_pool_marks_nothing(self):
(backup,) = self.backups(Backup.READY)
op = jobs.submit("backup.check", self.admin, {"target": self.target.pk}, "key-check-0002")