import { apiGet, apiPost, apiPostRaw, apiPut } from "@/api/client"; /** * Scheduled backups — `/api/admin/backups`. One dated, * uncompressed tar per run, delivered to a folder or an S3 bucket. */ export interface BackupConfig { enabled: boolean; /** UTC hour the daily backup runs at, 0–23. */ hour: number; destination: "folder" | "s3"; folder_path: string; s3_endpoint: string; s3_bucket: string; s3_access_key: string; s3_region: string; s3_verify_tls: boolean; } export interface BackupRun { started_at: string; finished_at: string; trigger: "scheduled" | "manual"; status: "ok" | "failed"; destination?: string; file?: string; size_bytes?: number; where?: string; error?: string; duration_s: number; } export interface BackupsOverview { config: BackupConfig; /** Never the secret itself: whether one is kept, and whether this * instance can still open it. */ secret: { set: boolean; readable: boolean }; /** What stops the saved config from sending a backup, in words. */ problems: string[]; running: boolean; /** Newest first, at most 30. */ runs: BackupRun[]; last_run: BackupRun | null; last_success: BackupRun | null; overdue: boolean; } /** What the form sends: config fields, plus the secret when one was typed. */ export type BackupChanges = Partial & { s3_secret?: string; clear_s3_secret?: boolean; }; export function getBackups(): Promise { return apiGet("/api/admin/backups"); } export function saveBackups(changes: BackupChanges): Promise { return apiPut("/api/admin/backups", changes); } /** Ask the destination whether it would take a backup, with the form's * unsaved values. The server gives the destination 45s; this waits a little * longer so its own answer, not the client's deadline, is what arrives. */ export function checkBackupDestination( changes: BackupChanges, ): Promise<{ ok: boolean; message: string }> { return apiPost<{ ok: boolean; message: string }>("/api/admin/backups/check", changes, { timeoutMs: 60_000 }); } export function runBackupNow(): Promise<{ status: string }> { return apiPost<{ status: string }>("/api/admin/backups/run", {}); } // ── Restore: put the whole instance back to a full backup ───────────────── export interface StoredBackup { name: string; size_bytes: number; } export interface RestoreCount { kind: string; now: number; after: number; } export interface RestorePreview { /** "fresh": an empty instance takes the backup. "rollback": this instance * goes back to its own earlier backup. */ mode: "fresh" | "rollback"; exported_at: string | null; counts: RestoreCount[]; } export interface RestoreResult extends RestorePreview { status: "ok"; restored: Record; safety_backup: { file: string; size_bytes: number; where: string; note?: string }; } /** A backup at the destination, by name, or a file picked from this computer. */ export type RestoreFrom = { source: string } | { file: File }; // Reading, checking and writing the whole store is slow by design; bounded, // but in minutes. const RESTORE_TIMEOUT_MS = 10 * 60 * 1000; export function listRestoreSources(): Promise<{ backups: StoredBackup[]; error?: string }> { return apiGet("/api/admin/restore/sources", { timeoutMs: 60_000 }); } async function restoreCall(path: string, from: RestoreFrom, query: Record = {}): Promise { const params = new URLSearchParams(query); if ("source" in from) { params.set("source", from.source); return apiPostRaw(`${path}?${params}`, null, null, { timeoutMs: RESTORE_TIMEOUT_MS }); } const qs = params.toString() ? `?${params}` : ""; const isTar = from.file.name.toLowerCase().endsWith(".tar"); // The JSON download is parsed first, so a damaged file fails here, before // the upload. const body = isTar ? from.file : JSON.stringify(JSON.parse(await from.file.text())); return apiPostRaw(`${path}${qs}`, body, isTar ? "application/x-tar" : "application/json", { timeoutMs: RESTORE_TIMEOUT_MS }); } export function previewRestore(from: RestoreFrom): Promise { return restoreCall("/api/admin/restore/preview", from); } export function restoreBackup(from: RestoreFrom): Promise { return restoreCall("/api/admin/restore", from, { confirm: "rollback" }); } /** When a backup named `grimoire-20261015T020000Z.tar` was taken. */ export function backupTakenAt(name: string): Date | null { const m = /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/.exec(name); if (!m) return null; return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6])); }