1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00
webvirtcloud/vmbackups/api.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

32 lines
1.5 KiB
Python

from django.urls import reverse
from drf_spectacular.utils import extend_schema
from rest_framework import mixins, status, viewsets
from rest_framework.response import Response
from admin.permissions import IsSuperUser
from operations import jobs
from operations.api.serializers import OperationSerializer
from webvirtcloud.serializers import ErrorSerializer
from .models import Backup
from .serializers import BackupSerializer, CreateBackupSerializer
class BackupViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""VM backups: list, read, and back a VM up (a job, answered 202)."""
permission_classes = [IsSuperUser]
serializer_class = BackupSerializer
queryset = Backup.objects.select_related("target").prefetch_related("files")
lookup_field = "uuid"
@extend_schema(request=CreateBackupSerializer, responses={202: OperationSerializer, 400: dict, 409: ErrorSerializer})
def create(self, request):
serializer = CreateBackupSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
data = serializer.validated_data
raw = {"instance": data["instance"].pk, "target": data["target"].pk, "frozen": data["frozen"]}
op = jobs.submit("backup.create", request.user, raw, data["submission_key"])
return Response(OperationSerializer(op, context={"request": request}).data, status=status.HTTP_202_ACCEPTED,
headers={"Location": reverse("operation-detail", args=[op.pk])})