feat: add Django backend foundation and Docker runtime
Add a Django admin-backed management layer for pobsync configs, runs, snapshots, and schedules. Keep the existing CLI engine as the execution source of truth, add import/run management commands, and provide SQLite default plus optional MariaDB Docker Compose support.
This commit is contained in:
1
src/pobsync_backend/__init__.py
Normal file
1
src/pobsync_backend/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
40
src/pobsync_backend/admin.py
Normal file
40
src/pobsync_backend/admin.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import BackupRun, GlobalConfig, HostConfig, ScheduleConfig, SnapshotRecord
|
||||
|
||||
|
||||
@admin.register(GlobalConfig)
|
||||
class GlobalConfigAdmin(admin.ModelAdmin):
|
||||
list_display = ("name", "backup_root", "updated_at")
|
||||
readonly_fields = ("created_at", "updated_at")
|
||||
|
||||
|
||||
@admin.register(HostConfig)
|
||||
class HostConfigAdmin(admin.ModelAdmin):
|
||||
list_display = ("host", "address", "enabled", "updated_at")
|
||||
list_filter = ("enabled",)
|
||||
search_fields = ("host", "address")
|
||||
readonly_fields = ("created_at", "updated_at")
|
||||
|
||||
|
||||
@admin.register(BackupRun)
|
||||
class BackupRunAdmin(admin.ModelAdmin):
|
||||
list_display = ("host", "run_type", "status", "started_at", "ended_at", "snapshot_path")
|
||||
list_filter = ("run_type", "status", "started_at")
|
||||
search_fields = ("host__host", "snapshot_path")
|
||||
|
||||
|
||||
@admin.register(SnapshotRecord)
|
||||
class SnapshotRecordAdmin(admin.ModelAdmin):
|
||||
list_display = ("host", "kind", "dirname", "status", "started_at")
|
||||
list_filter = ("kind", "status", "started_at")
|
||||
search_fields = ("host__host", "dirname", "path")
|
||||
|
||||
|
||||
@admin.register(ScheduleConfig)
|
||||
class ScheduleConfigAdmin(admin.ModelAdmin):
|
||||
list_display = ("host", "cron_expr", "enabled", "prune", "updated_at")
|
||||
list_filter = ("enabled", "prune")
|
||||
search_fields = ("host__host", "cron_expr")
|
||||
9
src/pobsync_backend/apps.py
Normal file
9
src/pobsync_backend/apps.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PobsyncBackendConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "pobsync_backend"
|
||||
verbose_name = "Pobsync backend"
|
||||
1
src/pobsync_backend/management/__init__.py
Normal file
1
src/pobsync_backend/management/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
1
src/pobsync_backend/management/commands/__init__.py
Normal file
1
src/pobsync_backend/management/commands/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from pobsync.config.load import load_global_config, load_host_config
|
||||
from pobsync.paths import PobsyncPaths
|
||||
from pobsync_backend.models import GlobalConfig, HostConfig
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Import pobsync YAML configs into the Django database."
|
||||
|
||||
def add_arguments(self, parser) -> None:
|
||||
parser.add_argument("--prefix", default=settings.POBSYNC_HOME, help="Pobsync home directory")
|
||||
|
||||
def handle(self, *args: Any, **options: Any) -> None:
|
||||
paths = PobsyncPaths(home=Path(options["prefix"]))
|
||||
if not paths.global_config_path.exists():
|
||||
raise CommandError(f"Missing global config: {paths.global_config_path}")
|
||||
|
||||
global_cfg = load_global_config(paths.global_config_path)
|
||||
GlobalConfig.objects.update_or_create(
|
||||
name="default",
|
||||
defaults={
|
||||
"backup_root": global_cfg["backup_root"],
|
||||
"pobsync_home": global_cfg.get("pobsync_home", str(paths.home)),
|
||||
"data": global_cfg,
|
||||
},
|
||||
)
|
||||
|
||||
count = 0
|
||||
for host_path in sorted(paths.hosts_dir.glob("*.yaml")):
|
||||
host_cfg = load_host_config(host_path)
|
||||
HostConfig.objects.update_or_create(
|
||||
host=host_cfg["host"],
|
||||
defaults={
|
||||
"address": host_cfg["address"],
|
||||
"config": host_cfg,
|
||||
"enabled": True,
|
||||
},
|
||||
)
|
||||
count += 1
|
||||
|
||||
self.stdout.write(self.style.SUCCESS(f"Imported global config and {count} host config(s)."))
|
||||
@@ -0,0 +1,89 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import timezone
|
||||
|
||||
from pobsync.commands.run_scheduled import run_scheduled
|
||||
from pobsync.config.load import load_host_config
|
||||
from pobsync.paths import PobsyncPaths
|
||||
from pobsync_backend.models import BackupRun, HostConfig
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Run a scheduled pobsync backup and record the result in Django."
|
||||
|
||||
def add_arguments(self, parser) -> None:
|
||||
parser.add_argument("host", help="Host to back up")
|
||||
parser.add_argument("--prefix", default=settings.POBSYNC_HOME, help="Pobsync home directory")
|
||||
parser.add_argument("--dry-run", action="store_true", help="Run rsync --dry-run")
|
||||
parser.add_argument("--prune", action="store_true", help="Apply retention after a successful run")
|
||||
parser.add_argument("--prune-max-delete", type=int, default=10)
|
||||
parser.add_argument("--prune-protect-bases", action="store_true")
|
||||
|
||||
def handle(self, *args: Any, **options: Any) -> None:
|
||||
host_name = options["host"]
|
||||
paths = PobsyncPaths(home=Path(options["prefix"]))
|
||||
host_path = paths.hosts_dir / f"{host_name}.yaml"
|
||||
if not host_path.exists():
|
||||
raise CommandError(f"Missing host config: {host_path}")
|
||||
|
||||
host_cfg = load_host_config(host_path)
|
||||
host, _created = HostConfig.objects.update_or_create(
|
||||
host=host_cfg["host"],
|
||||
defaults={
|
||||
"address": host_cfg["address"],
|
||||
"config": host_cfg,
|
||||
"enabled": True,
|
||||
},
|
||||
)
|
||||
|
||||
run = BackupRun.objects.create(
|
||||
host=host,
|
||||
run_type=BackupRun.RunType.SCHEDULED,
|
||||
status=BackupRun.Status.RUNNING,
|
||||
started_at=timezone.now(),
|
||||
)
|
||||
|
||||
try:
|
||||
result = run_scheduled(
|
||||
prefix=paths.home,
|
||||
host=host.host,
|
||||
dry_run=bool(options["dry_run"]),
|
||||
prune=bool(options["prune"]),
|
||||
prune_max_delete=int(options["prune_max_delete"]),
|
||||
prune_protect_bases=bool(options["prune_protect_bases"]),
|
||||
)
|
||||
except Exception as exc:
|
||||
run.status = BackupRun.Status.FAILED
|
||||
run.ended_at = timezone.now()
|
||||
run.result = {"ok": False, "error": str(exc), "type": type(exc).__name__}
|
||||
run.save(update_fields=["status", "ended_at", "result"])
|
||||
raise
|
||||
|
||||
run.status = BackupRun.Status.SUCCESS if result.get("ok") else BackupRun.Status.FAILED
|
||||
run.ended_at = timezone.now()
|
||||
run.snapshot_path = str(result.get("snapshot") or "")
|
||||
run.base_path = str(result.get("base") or "")
|
||||
rsync = result.get("rsync") if isinstance(result.get("rsync"), dict) else {}
|
||||
run.rsync_exit_code = rsync.get("exit_code")
|
||||
run.result = result
|
||||
run.save(
|
||||
update_fields=[
|
||||
"status",
|
||||
"ended_at",
|
||||
"snapshot_path",
|
||||
"base_path",
|
||||
"rsync_exit_code",
|
||||
"result",
|
||||
],
|
||||
)
|
||||
|
||||
if result.get("ok"):
|
||||
self.stdout.write(self.style.SUCCESS(f"Backup completed for {host.host}."))
|
||||
return
|
||||
|
||||
raise CommandError(f"Backup failed for {host.host}; run id={run.id}")
|
||||
103
src/pobsync_backend/migrations/0001_initial.py
Normal file
103
src/pobsync_backend/migrations/0001_initial.py
Normal file
@@ -0,0 +1,103 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="GlobalConfig",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
("name", models.CharField(default="default", max_length=64, unique=True)),
|
||||
("backup_root", models.CharField(max_length=512)),
|
||||
("pobsync_home", models.CharField(default="/opt/pobsync", max_length=512)),
|
||||
("data", models.JSONField(blank=True, default=dict)),
|
||||
],
|
||||
options={
|
||||
"verbose_name": "global config",
|
||||
"verbose_name_plural": "global configs",
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="HostConfig",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
("host", models.CharField(max_length=255, unique=True)),
|
||||
("address", models.CharField(max_length=255)),
|
||||
("enabled", models.BooleanField(default=True)),
|
||||
("config", models.JSONField(blank=True, default=dict)),
|
||||
],
|
||||
options={
|
||||
"ordering": ["host"],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="BackupRun",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("run_type", models.CharField(choices=[("scheduled", "Scheduled"), ("manual", "Manual")], default="scheduled", max_length=16)),
|
||||
("status", models.CharField(choices=[("queued", "Queued"), ("running", "Running"), ("success", "Success"), ("failed", "Failed"), ("cancelled", "Cancelled")], default="queued", max_length=16)),
|
||||
("started_at", models.DateTimeField(blank=True, null=True)),
|
||||
("ended_at", models.DateTimeField(blank=True, null=True)),
|
||||
("snapshot_path", models.CharField(blank=True, max_length=1024)),
|
||||
("base_path", models.CharField(blank=True, max_length=1024)),
|
||||
("rsync_exit_code", models.IntegerField(blank=True, null=True)),
|
||||
("result", models.JSONField(blank=True, default=dict)),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("host", models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name="runs", to="pobsync_backend.hostconfig")),
|
||||
],
|
||||
options={
|
||||
"ordering": ["-created_at"],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="ScheduleConfig",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
("cron_expr", models.CharField(max_length=128)),
|
||||
("user", models.CharField(default="root", max_length=64)),
|
||||
("enabled", models.BooleanField(default=True)),
|
||||
("prune", models.BooleanField(default=False)),
|
||||
("prune_max_delete", models.PositiveIntegerField(default=10)),
|
||||
("prune_protect_bases", models.BooleanField(default=False)),
|
||||
("host", models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name="schedule", to="pobsync_backend.hostconfig")),
|
||||
],
|
||||
options={
|
||||
"ordering": ["host__host"],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="SnapshotRecord",
|
||||
fields=[
|
||||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
|
||||
("kind", models.CharField(choices=[("scheduled", "Scheduled"), ("manual", "Manual"), ("incomplete", "Incomplete")], max_length=16)),
|
||||
("dirname", models.CharField(max_length=255)),
|
||||
("path", models.CharField(max_length=1024)),
|
||||
("status", models.CharField(blank=True, max_length=32)),
|
||||
("started_at", models.DateTimeField(blank=True, null=True)),
|
||||
("ended_at", models.DateTimeField(blank=True, null=True)),
|
||||
("metadata", models.JSONField(blank=True, default=dict)),
|
||||
("discovered_at", models.DateTimeField(auto_now_add=True)),
|
||||
("host", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name="snapshots", to="pobsync_backend.hostconfig")),
|
||||
],
|
||||
options={
|
||||
"ordering": ["host__host", "-started_at", "dirname"],
|
||||
},
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name="snapshotrecord",
|
||||
constraint=models.UniqueConstraint(fields=("host", "kind", "dirname"), name="unique_snapshot_per_host_kind"),
|
||||
),
|
||||
]
|
||||
1
src/pobsync_backend/migrations/__init__.py
Normal file
1
src/pobsync_backend/migrations/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
110
src/pobsync_backend/models.py
Normal file
110
src/pobsync_backend/models.py
Normal file
@@ -0,0 +1,110 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from django.db import models
|
||||
|
||||
|
||||
class TimestampedModel(models.Model):
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
class GlobalConfig(TimestampedModel):
|
||||
name = models.CharField(max_length=64, default="default", unique=True)
|
||||
backup_root = models.CharField(max_length=512)
|
||||
pobsync_home = models.CharField(max_length=512, default="/opt/pobsync")
|
||||
data = models.JSONField(default=dict, blank=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "global config"
|
||||
verbose_name_plural = "global configs"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class HostConfig(TimestampedModel):
|
||||
host = models.CharField(max_length=255, unique=True)
|
||||
address = models.CharField(max_length=255)
|
||||
enabled = models.BooleanField(default=True)
|
||||
config = models.JSONField(default=dict, blank=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["host"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.host
|
||||
|
||||
|
||||
class BackupRun(models.Model):
|
||||
class RunType(models.TextChoices):
|
||||
SCHEDULED = "scheduled", "Scheduled"
|
||||
MANUAL = "manual", "Manual"
|
||||
|
||||
class Status(models.TextChoices):
|
||||
QUEUED = "queued", "Queued"
|
||||
RUNNING = "running", "Running"
|
||||
SUCCESS = "success", "Success"
|
||||
FAILED = "failed", "Failed"
|
||||
CANCELLED = "cancelled", "Cancelled"
|
||||
|
||||
host = models.ForeignKey(HostConfig, on_delete=models.PROTECT, related_name="runs")
|
||||
run_type = models.CharField(max_length=16, choices=RunType.choices, default=RunType.SCHEDULED)
|
||||
status = models.CharField(max_length=16, choices=Status.choices, default=Status.QUEUED)
|
||||
started_at = models.DateTimeField(null=True, blank=True)
|
||||
ended_at = models.DateTimeField(null=True, blank=True)
|
||||
snapshot_path = models.CharField(max_length=1024, blank=True)
|
||||
base_path = models.CharField(max_length=1024, blank=True)
|
||||
rsync_exit_code = models.IntegerField(null=True, blank=True)
|
||||
result = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.host} {self.run_type} {self.status}"
|
||||
|
||||
|
||||
class SnapshotRecord(models.Model):
|
||||
class Kind(models.TextChoices):
|
||||
SCHEDULED = "scheduled", "Scheduled"
|
||||
MANUAL = "manual", "Manual"
|
||||
INCOMPLETE = "incomplete", "Incomplete"
|
||||
|
||||
host = models.ForeignKey(HostConfig, on_delete=models.CASCADE, related_name="snapshots")
|
||||
kind = models.CharField(max_length=16, choices=Kind.choices)
|
||||
dirname = models.CharField(max_length=255)
|
||||
path = models.CharField(max_length=1024)
|
||||
status = models.CharField(max_length=32, blank=True)
|
||||
started_at = models.DateTimeField(null=True, blank=True)
|
||||
ended_at = models.DateTimeField(null=True, blank=True)
|
||||
metadata = models.JSONField(default=dict, blank=True)
|
||||
discovered_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["host", "kind", "dirname"], name="unique_snapshot_per_host_kind"),
|
||||
]
|
||||
ordering = ["host__host", "-started_at", "dirname"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.host}/{self.kind}/{self.dirname}"
|
||||
|
||||
|
||||
class ScheduleConfig(TimestampedModel):
|
||||
host = models.OneToOneField(HostConfig, on_delete=models.CASCADE, related_name="schedule")
|
||||
cron_expr = models.CharField(max_length=128)
|
||||
user = models.CharField(max_length=64, default="root")
|
||||
enabled = models.BooleanField(default=True)
|
||||
prune = models.BooleanField(default=False)
|
||||
prune_max_delete = models.PositiveIntegerField(default=10)
|
||||
prune_protect_bases = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
ordering = ["host__host"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.host} {self.cron_expr}"
|
||||
Reference in New Issue
Block a user