1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vmbackups/utils.py
catborise f4559acbc7 feat(vmbackups): back up a running or shut-off VM into a target
A superuser backs a VM up from its new Backups tab, or with POST
/api/v1/backups/ (202 with the job); GET lists and reads backups. The
backup.create job holds the VM, its compute (shared) and the target, so
migrations, snapshots and power changes of the VM wait meanwhile.

- A shut-off VM's disks are copied one at a time into the target pool,
  flat, in their format (createXMLFrom); a cancel stops before the next.
- A running or paused VM's disks go by libvirt's push backup into
  volumes made first, as the live clone: the file systems frozen for its
  start when the guest agent answers (frozen), crash-consistent
  otherwise, or refused when frozen file systems are required. A guest
  that shuts down meanwhile does not end the backup.
- A running VM's NVRAM file is kept only when it reads the same after
  the copy as before it (three reads), so it is the capture point's.
- Then the NVRAM file, the definition and the manifest (written last)
  are uploaded and read back, and the row becomes ready with its files.
- Recovery thaws a frozen guest first, watches a push backup still
  running, finishes a copied backup, removes the files of a failed or
  cancelled one, and leaves a copy whose end libvirt cannot tell
  uncertain with its files named. The target pool is checked by UUID and
  its free space before anything is made.
2026-11-17 09:13:59 +03:00

56 lines
2.2 KiB
Python

from django.utils.translation import gettext, gettext_noop
from libvirt import libvirtError
from lxml import etree
from storages.forms import inside
from vrtManager.backup import wvmBackup
from vrtManager.util import InUse
from .models import Backup, BackupTarget, BackupTargetPool
def inside_vm_pool(conn, pool):
"""The name of another pool of the host whose directory holds the target
pool's: its backups would share that storage's failures."""
path = etree.fromstring(pool.XMLDesc(0)).findtext("target/path").rstrip("/")
for name, (_pool, directory) in conn.file_pools().items():
if name != pool.name() and inside(path, directory):
return name
return None
def refuse_backup_volume(compute, pool, volume):
"""A volume of a backup is deleted with its backup, on the Backups pages."""
for backup in Backup.objects.filter(target__pools__compute=compute, target__pools__pool=pool):
if volume.startswith(backup.prefix + "-"):
raise InUse(
gettext_noop("Volume %(volume)s belongs to a backup of %(vm)s; delete the backup on the Backups page"),
volume=volume,
vm=backup.vm_name,
)
def refuse_target_pool(compute, pool):
row = BackupTargetPool.objects.filter(compute=compute, pool=pool).select_related("target").first()
if row:
raise InUse(
gettext_noop("Storage pool %(pool)s is backup target %(target)s; remove it from the target first"),
pool=pool,
target=row.target.name,
)
def vm_tab(instance):
"""The VM page's Backups tab: the VM's backups, the targets of its host,
and why it cannot be backed up, if it cannot."""
targets = list(BackupTarget.objects.filter(pools__compute=instance.compute).order_by("name"))
problem = None
if not targets:
problem = gettext("No backup target has a storage pool on this VM's host.")
else:
c = instance.compute
try:
wvmBackup(c.hostname, c.login, c.password, c.type, instance.name, uuid=instance.uuid).backup_plan()
except libvirtError as err:
problem = str(err)
return {"rows": instance.backups.select_related("target", "operation"), "targets": targets, "problem": problem}