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

feat(vmbackups): a backup's file names begin with its VM's name

Someone looking at the target's directory could not tell whose backup a
file was without opening its manifest. A new backup's files are named
<vm>-<backup uuid>-<file> (the VM's name made safe for a file name, at most
64 characters). The prefix is kept on the row: backups made before keep
their wvc-<uuid> names (migration 0007), and an import takes it from the
manifest's file name. Manifests of either naming are found by their
UUID suffix.
This commit is contained in:
catborise 2026-11-18 14:57:14 +03:00
parent 8e0a84a3bd
commit 5b05e76ebc
8 changed files with 81 additions and 33 deletions

View file

@ -2,6 +2,7 @@
import hashlib
import os
import re
import libvirt
from django.utils.translation import gettext_noop
@ -213,9 +214,15 @@ class wvmBackup(wvmInstance):
for name in sorted(pool.listVolumes()) if not name.startswith(".")}
def backup_file_names(uuid, disks, nvram):
"""The names of a backup's files in the target pool, from its UUID only."""
prefix = f"wvc-{uuid}"
def backup_prefix(vm_name, uuid):
"""The start of a backup's file names in the target pool: its VM's name,
so the directory shows whose it is, and the backup's UUID."""
name = re.sub(r"[^A-Za-z0-9._-]", "_", vm_name)[:64].lstrip(".-") or "vm"
return f"{name}-{uuid}"
def backup_file_names(prefix, disks, nvram):
"""The names of a backup's files in the target pool."""
return {
"disks": {d["dev"]: f"{prefix}-{d['dev']}.{'qcow2' if d['format'] == 'qcow2' else 'raw'}" for d in disks},
"nvram": f"{prefix}-nvram.fd" if nvram else None,
@ -267,9 +274,12 @@ def volume_exists(pool, name):
return wvmConnect._find_volume(pool, name) is not None
MANIFEST_NAME = re.compile(r"-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}-manifest\.json$")
def manifest_names(pool):
pool.refresh(0)
return sorted(name for name in pool.listVolumes() if name.startswith("wvc-") and name.endswith("-manifest.json"))
return sorted(name for name in pool.listVolumes() if MANIFEST_NAME.search(name))
def set_disk_sources(tree, disks):

View file

@ -9,7 +9,7 @@ import libvirt
from lxml import etree
from vrtManager import util
from vrtManager.backup import backup_file_names, check_target_space, put_file, wvmBackup
from vrtManager.backup import backup_file_names, backup_prefix, check_target_space, manifest_names, put_file, wvmBackup
from vrtManager.instance import PERSISTENT_XML
GIB = 1024**3
@ -208,18 +208,33 @@ class BackupPlanTestCase(unittest.TestCase):
class FilesTestCase(unittest.TestCase):
def test_file_names(self):
uuid = "00000000-0000-4000-8000-000000000001"
prefix = backup_prefix("web-01", uuid)
self.assertEqual(prefix, f"web-01-{uuid}") # whose backup it is, seen in the target's directory
disks = [{"dev": "vda", "format": "qcow2"}, {"dev": "vdb", "format": "raw"}]
self.assertEqual(
backup_file_names(uuid, disks, nvram=True),
backup_file_names(prefix, disks, nvram=True),
{
"disks": {"vda": f"wvc-{uuid}-vda.qcow2", "vdb": f"wvc-{uuid}-vdb.raw"},
"nvram": f"wvc-{uuid}-nvram.fd",
"tpm": f"wvc-{uuid}-tpm-",
"domain": f"wvc-{uuid}-domain.xml",
"manifest": f"wvc-{uuid}-manifest.json",
"disks": {"vda": f"{prefix}-vda.qcow2", "vdb": f"{prefix}-vdb.raw"},
"nvram": f"{prefix}-nvram.fd",
"tpm": f"{prefix}-tpm-",
"domain": f"{prefix}-domain.xml",
"manifest": f"{prefix}-manifest.json",
},
)
self.assertIsNone(backup_file_names(uuid, disks, nvram=False)["nvram"])
self.assertIsNone(backup_file_names(prefix, disks, nvram=False)["nvram"])
def test_a_vm_name_is_made_safe_for_a_file_name(self):
uuid = "00000000-0000-4000-8000-000000000001"
self.assertEqual(backup_prefix("my vm/ç", uuid), f"my_vm__-{uuid}")
self.assertEqual(backup_prefix("..", uuid), f"vm-{uuid}")
self.assertEqual(len(backup_prefix("x" * 200, uuid)), 64 + 1 + 36)
def test_manifests_of_either_naming_are_found(self):
pool = MagicMock()
uuid = "00000000-0000-4000-8000-000000000001"
pool.listVolumes.return_value = [f"wvc-{uuid}-manifest.json", f"web-01-{uuid}-manifest.json",
f"web-01-{uuid}-vda.qcow2", "notes-manifest.json"]
self.assertEqual(manifest_names(pool), [f"web-01-{uuid}-manifest.json", f"wvc-{uuid}-manifest.json"])
def test_space_counts_virtual_capacity_and_the_margin(self):
# 2 x 10 GiB plus max(10 % of it, 1 GiB) = 22 GiB