feat: tägliche datenbanksicherung

Konsistente Momentaufnahme per SQLite-Backup-API nach data/backups,
geprüft mit PRAGMA integrity_check und komprimiert; erst eine geprüfte
Datei bekommt den endgültigen Namen. Aufbewahrung 7 täglich + 4
wöchentlich, verpasste Sicherungen werden nach dem Start nachgeholt.
lc-admin backup für manuelle Sicherungen, Status für Admins unter
/api/admin/backup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dbildhauerandClaude Opus 5.5 committed 2026-10-25 15:32:13 +01:00
1 parent 4aebac5336
commit 7fd699c6af
6 files changed
+444 -2

No files matched your search

+25 -2
View File
@@ -1,4 +1,5 @@
import asyncio
import contextlib
import logging
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
@@ -17,6 +18,7 @@ from app.middleware import (
OriginCheckMiddleware,
SecurityHeadersMiddleware,
)
from app.services import backup as backup_service
from app.services import files as file_service
from app.services.access import ProjectNotFound, ReadOnly
from app.services.auth import delete_old_login_attempts
@@ -27,6 +29,8 @@ from app.ws.hub import hub
logger = logging.getLogger(__name__)
CLEANUP_INTERVAL_SECONDS = 3600
# Give the app time to start before a missed backup is caught up
BACKUP_CATCH_UP_DELAY_SECONDS = 60
def _cleanup() -> list[tuple[int, set[int]]]:
@@ -79,14 +83,33 @@ def _bootstrap_admin() -> None:
logger.info("INITIAL_ADMIN_* ignored: users already exist; remove them from .env.")
async def _run_backup() -> None:
# Failures are logged with traceback and shown in the admin area
with contextlib.suppress(backup_service.BackupError):
await run_in_threadpool(backup_service.create_backup)
async def _backup_loop() -> None:
"""Daily at BACKUP_HOUR; a backup missed while the container was down is caught up."""
await asyncio.sleep(BACKUP_CATCH_UP_DELAY_SECONDS)
if await run_in_threadpool(backup_service.needs_catch_up):
await _run_backup()
while True:
await asyncio.sleep(backup_service.seconds_until_next_run())
await _run_backup()
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
await run_in_threadpool(_bootstrap_admin)
task = asyncio.create_task(_cleanup_loop())
tasks = [asyncio.create_task(_cleanup_loop())]
if get_settings().backup_enabled:
tasks.append(asyncio.create_task(_backup_loop()))
try:
yield
finally:
task.cancel()
for task in tasks:
task.cancel()
# Hashed build output never changes; everything else must be revalidated so updates arrive