1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/vmbackups/models.py
catborise 1219a37cc4 feat(vmbackups): backup schedules' records; the worker runs the apps' periodic tasks
A backup schedule (BackupPolicy) names its VMs, chosen or every VM of a
compute at the time it runs (templates left out), a target, a daily time
in the panel's time zone, how many backups of each VM to keep (one at
least), whether frozen file systems are required, and the superuser its
jobs run as. A run (BackupRun) records one VM's backup of one day, keyed
by the VM's row, not its UUID, which two VM rows may share. A backup
keeps the schedule that made it; deleting the schedule keeps its
backups. A backup whose files are gone can be marked missing, and a
target keeps when it was last checked.

The worker runs the tasks apps register with register_periodic about
once a minute, on a thread of their own, one pass at a time, each
guarded: a pass stuck on a host takes no thread recovery or a cancel
needs, and one failing task stops none of the others. Nothing registers
one yet.

backup.create takes the schedule that asked for it; one deleted while
the job was queued leaves the backup without a schedule.
2026-11-17 10:18:04 +03:00

128 lines
5.8 KiB
Python

import uuid
from django.conf import settings
from django.db import models
from computes.models import Compute
from instances.models import Instance
from operations.models import Operation
class BackupTarget(models.Model):
"""Storage the backups go to: a pool on each compute that sees it."""
name = models.CharField(max_length=64, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
checked_at = models.DateTimeField(null=True) # its last target check that ended
def __str__(self):
return self.name
def pool_on(self, compute):
return self.pools.filter(compute=compute).first()
class BackupTargetPool(models.Model):
"""The target's pool on one compute; its UUID is checked before each use,
so a pool replaced by another of the same name is refused. A removed
compute takes its pool row with it: the backups stay in the target."""
target = models.ForeignKey(BackupTarget, on_delete=models.CASCADE, related_name="pools")
compute = models.ForeignKey(Compute, on_delete=models.CASCADE)
pool = models.CharField(max_length=128)
pool_uuid = models.CharField(max_length=36)
class Meta:
constraints = [models.UniqueConstraint(fields=["target", "compute"], name="one_pool_per_target_and_compute")]
class BackupPolicy(models.Model):
"""A schedule: its VMs (those chosen, or every VM of a compute) backed
up into its target once a day at its time, the newest keep of each VM
kept. Its jobs run as its owner, a superuser."""
name = models.CharField(max_length=64, unique=True)
target = models.ForeignKey(BackupTarget, on_delete=models.PROTECT, related_name="policies")
compute = models.ForeignKey(Compute, on_delete=models.CASCADE, null=True, blank=True)
instances = models.ManyToManyField(Instance, blank=True, related_name="backup_policies")
time = models.TimeField() # in the panel's time zone
keep = models.PositiveSmallIntegerField()
frozen = models.BooleanField(default=False)
enabled = models.BooleanField(default=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
constraints = [models.CheckConstraint(condition=models.Q(keep__gte=1), name="policy_keeps_one")]
def __str__(self):
return self.name
def vms(self):
"""Its VMs now: a compute's are those it has at this moment, templates left out."""
if self.compute_id:
return Instance.objects.filter(compute_id=self.compute_id, is_template=False).order_by("name")
return self.instances.select_related("compute").order_by("name")
class BackupRun(models.Model):
"""A schedule's backup of one VM on one day: what it submitted, or why not."""
SUBMITTED, SKIPPED, FAILED = "submitted", "skipped", "failed"
STATES = [(SUBMITTED, "submitted"), (SKIPPED, "skipped"), (FAILED, "failed")]
policy = models.ForeignKey(BackupPolicy, on_delete=models.CASCADE, related_name="runs")
instance = models.ForeignKey(Instance, on_delete=models.CASCADE, related_name="backup_runs")
day = models.DateField() # the local day of the schedule's time
state = models.CharField(max_length=16, choices=STATES)
operation = models.ForeignKey(Operation, on_delete=models.SET_NULL, null=True)
note = models.CharField(max_length=255, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
class Meta:
constraints = [models.UniqueConstraint(fields=["policy", "instance", "day"], name="one_run_per_day")]
class Backup(models.Model):
CREATING, READY, FAILED, DELETING, MISSING = "creating", "ready", "failed", "deleting", "missing"
STATES = [(CREATING, "creating"), (READY, "ready"), (FAILED, "failed"), (DELETING, "deleting"), (MISSING, "missing")]
OFFLINE, FROZEN, CRASH = "offline", "frozen", "crash"
CONSISTENCIES = [(OFFLINE, "offline"), (FROZEN, "frozen"), (CRASH, "crash-consistent")]
uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
target = models.ForeignKey(BackupTarget, on_delete=models.PROTECT, related_name="backups")
instance = models.ForeignKey(Instance, on_delete=models.SET_NULL, null=True, related_name="backups")
# kept when the VM row is gone
vm_uuid = models.CharField(max_length=36)
vm_name = models.CharField(max_length=255)
compute_name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
finished_at = models.DateTimeField(null=True)
state = models.CharField(max_length=16, choices=STATES, default=CREATING)
consistency = models.CharField(max_length=16, choices=CONSISTENCIES, blank=True)
size = models.BigIntegerField(default=0) # bytes in the target
manifest = models.JSONField(default=dict)
operation = models.ForeignKey(Operation, on_delete=models.SET_NULL, null=True)
policy = models.ForeignKey(BackupPolicy, on_delete=models.SET_NULL, null=True, related_name="backups")
class Meta:
ordering = ["-created_at"]
@property
def prefix(self):
"""Every file of the backup in the target is named from its UUID only."""
return f"wvc-{self.uuid}"
class BackupFile(models.Model):
DISK, NVRAM, DOMAIN, MANIFEST = "disk", "nvram", "domain", "manifest"
ROLES = [(DISK, "disk"), (NVRAM, "nvram"), (DOMAIN, "domain"), (MANIFEST, "manifest")]
backup = models.ForeignKey(Backup, on_delete=models.CASCADE, related_name="files")
role = models.CharField(max_length=16, choices=ROLES)
dev = models.CharField(max_length=32, blank=True)
volume = models.CharField(max_length=255)
format = models.CharField(max_length=16, blank=True)
capacity = models.BigIntegerField(default=0)
size = models.BigIntegerField(default=0)
sha256 = models.CharField(max_length=64, blank=True)