CI & Build / Plugin hooks (push) Successful in 10s
CI & Build / Python lint (push) Successful in 1s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 1m42s
CI & Build / integration (push) Successful in 1m48s
CI & Build / Build & push image (push) Canceled after 22s
The web restore no longer copies a backup in beside the live data. - GET /api/admin/restore/sources lists the backups at the backup destination, newest first. - POST /api/admin/restore/preview says what a restore would do, changing nothing: fresh or rollback, when the backup was taken, and counts now and after per kind. A backup that cannot apply here gets a 409 with the reason. - POST /api/admin/restore?confirm=rollback runs backup_job.rollback_to_backup: safety backup, replace, then search rebuilt in the background. Each takes ?source=<name> (a backup at the destination) or an upload. The destination path exists because uploads are held to the request-size limit, and a real instance's backup is far over it. backup_s3.get_file downloads from a bucket. backup_destination.list_backups/open_backup only accept names the scheduled backup writes, so a request cannot reach any other file. Settings -> Data -> Restore from Backup opens RestoreBackupDialog. The dialog: 1. Pick a backup at the destination or a file. 2. Read the preview. 3. Type "roll back". 4. Run it. The dialog then says where the replaced state was saved, and sends the user to sign in again. Closes #6840. Milestone 582, step 4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
4.7 KiB
TypeScript
143 lines
4.7 KiB
TypeScript
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<BackupConfig> & {
|
||
s3_secret?: string;
|
||
clear_s3_secret?: boolean;
|
||
};
|
||
|
||
export function getBackups(): Promise<BackupsOverview> {
|
||
return apiGet<BackupsOverview>("/api/admin/backups");
|
||
}
|
||
|
||
export function saveBackups(changes: BackupChanges): Promise<BackupsOverview> {
|
||
return apiPut<BackupsOverview>("/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<string, number>;
|
||
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<T>(path: string, from: RestoreFrom, query: Record<string, string> = {}): Promise<T> {
|
||
const params = new URLSearchParams(query);
|
||
if ("source" in from) {
|
||
params.set("source", from.source);
|
||
return apiPostRaw<T>(`${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<T>(`${path}${qs}`, body, isTar ? "application/x-tar" : "application/json",
|
||
{ timeoutMs: RESTORE_TIMEOUT_MS });
|
||
}
|
||
|
||
export function previewRestore(from: RestoreFrom): Promise<RestorePreview> {
|
||
return restoreCall<RestorePreview>("/api/admin/restore/preview", from);
|
||
}
|
||
|
||
export function restoreBackup(from: RestoreFrom): Promise<RestoreResult> {
|
||
return restoreCall<RestoreResult>("/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]));
|
||
}
|