feat(backup): support cross-platform background database backups

This commit is contained in:
CSXFanMeng authored and GitHub committed 2026-09-23 12:29:56 +08:00
1 parent 250e0c0a10
commit 2f960f104c
56 files changed
+4195 -962

No files matched your search

@@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";
import {
databaseBackupFileNamePatternIsValid,
databaseBackupFilePath,
databaseBackupRunDirectory,
databaseBackupTimestamp,
sanitizeDatabaseBackupFileSegment,
databaseBackupRunsToPrune,
databaseBackupTableMatchesPattern,
@@ -28,6 +30,13 @@ function run(overrides: Record<string, unknown> = {}) {
}
describe("database backup run persistence", () => {
it("renders preview names in the saved zone rather than the browser zone", () => {
const instant = "2026-09-12T18:00:00Z";
expect(databaseBackupTimestamp(instant, "Asia/Shanghai")).toBe("20260913-020000");
expect(databaseBackupTimestamp(instant, "America/New_York")).toBe("20260912-140000");
expect(databaseBackupRunDirectory("/backups", "{date}/{timestamp}", "Daily", instant, "12345678", "Asia/Shanghai")).toBe("/backups/20260913/20260913020000");
expect(databaseBackupFilePath("/backups", "Daily", "app", instant, "12345678", "gzip", "{timestamp}", "Asia/Shanghai")).toBe("/backups/20260913-020000__app.sql.gz");
});
it("uses a gzip extension when the backup output is compressed", () => {
expect(databaseBackupFilePath("/backups", "Nightly", "app", startedAt, "run-12345678", "gzip")).toMatch(/\.sql\.gz$/);
});
@@ -0,0 +1,28 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { prepareDatabaseBackupRestore, previewSqlFile, releaseSqlFilePreview } from "@/lib/backend/http";
afterEach(() => vi.unstubAllGlobals());
describe("Web backup restore transport", () => {
it("returns the server preview from the record-authorized restore endpoint", async () => {
const preview = { fileName: "backup.sql", filePath: "/server/tmp/sql_file/restore-token/backup.sql", preview: "SELECT 1;", cleanupToken: "restore-token" };
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => preview });
vi.stubGlobal("fetch", fetchMock);
await expect(prepareDatabaseBackupRestore("run/1", 2)).resolves.toEqual(preview);
expect(fetchMock).toHaveBeenCalledWith("/api/database-backups/run%2F1/files/2/restore", expect.objectContaining({ method: "POST", body: "{}" }));
});
it("continues rejecting arbitrary server paths without making a request", async () => {
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
await expect(previewSqlFile("/etc/passwd")).rejects.toThrow("requires a File object");
expect(fetchMock).not.toHaveBeenCalled();
});
it("releases only by the opaque cleanup token", async () => {
const fetchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => null });
vi.stubGlobal("fetch", fetchMock);
await releaseSqlFilePreview("restore-token");
expect(fetchMock).toHaveBeenCalledWith("/api/sql-file/preview/release", expect.objectContaining({ method: "POST", body: '{"cleanupToken":"restore-token"}' }));
});
});
+5
View File
@@ -177,6 +177,10 @@ export const savedSqlStorageDir = forward("savedSqlStorageDir");
export const openSavedSqlStorageDir = forward("openSavedSqlStorageDir");
export const revealPathInFileManager = forward("revealPathInFileManager");
export const deleteDatabaseBackupFiles = forward("deleteDatabaseBackupFiles");
export const databaseBackupCommand = forward("databaseBackupCommand");
export const databaseBackupBackground = forward("databaseBackupBackground");
export const downloadDatabaseBackupFile = forward("downloadDatabaseBackupFile");
export const prepareDatabaseBackupRestore = forward("prepareDatabaseBackupRestore");
export const isSqliteDatabaseFile = forward("isSqliteDatabaseFile");
export const backupSqliteDatabase = forward("backupSqliteDatabase");
export const restoreSqliteDatabase = forward("restoreSqliteDatabase");
@@ -431,6 +435,7 @@ export const listLocalSshKeys = forward("listLocalSshKeys");
// SQL File Execution
export const previewSqlFile = forward("previewSqlFile");
export const releaseSqlFilePreview = forward("releaseSqlFilePreview");
export const executeSqlFile = forward("executeSqlFile");
export const executeSqlFiles = forward("executeSqlFiles");
export const cancelSqlFileExecution = forward("cancelSqlFileExecution");
+24
View File
@@ -1,6 +1,26 @@
import type { MongoDumpFormat, MongoDumpSourceInput, MongoDumpCatalog, MongoRestoreSourcePreview, MongoDatabaseDumpRequest, MongoDatabaseRestoreRequest, MongoDatabaseDumpProgress } from "./mongodbDumpTypes";
import type { MongoRestoreUpload, MongoSourceReadOptions } from "./mongodbDumpTypes";
import type { UserSkillRootSettings, UserSkillsListResult, UserSkillsReadResult } from "@/types/userSkills";
import type { DatabaseBackupCommand, DatabaseBackupBackgroundStatus } from "@/lib/backup/backgroundDatabaseBackup";
export function databaseBackupCommand<T = unknown>(command: DatabaseBackupCommand): Promise<T> {
return post("/api/database-backups", command);
}
export async function databaseBackupBackground(enabled?: boolean): Promise<DatabaseBackupBackgroundStatus> {
if (enabled !== undefined) throw new Error("The server manages background backups");
return { enabled: true, platform: "server" };
}
export async function downloadDatabaseBackupFile(runId: string, index: number): Promise<void> {
const anchor = document.createElement("a");
anchor.href = apiUrl(`/api/database-backups/${encodeURIComponent(runId)}/files/${index}`);
anchor.click();
}
export function prepareDatabaseBackupRestore(runId: string, index: number): Promise<SqlFilePreview> {
return post(`/api/database-backups/${encodeURIComponent(runId)}/files/${index}/restore`, {});
}
import type {
ConnectionConfig,
ConnectionTestResult,
@@ -2528,6 +2548,10 @@ export async function previewSqlFile(fileOrPath: string | File): Promise<SqlFile
return res.json();
}
export async function releaseSqlFilePreview(cleanupToken: string): Promise<void> {
return post("/api/sql-file/preview/release", { cleanupToken });
}
export async function executeSqlFile(request: SqlFileRequest): Promise<void> {
return post("/api/sql-file/execute", { request });
}
+20
View File
@@ -2,6 +2,23 @@ import { invoke as tauriInvoke } from "@tauri-apps/api/core";
import type { MongoDumpFormat, MongoDumpSourceInput, MongoDumpCatalog, MongoRestoreSourcePreview, MongoDatabaseDumpRequest, MongoDatabaseRestoreRequest, MongoDatabaseDumpProgress } from "./mongodbDumpTypes";
import type { MongoRestoreUpload, MongoSourceReadOptions } from "./mongodbDumpTypes";
import type { UserSkillRootSettings, UserSkillsListResult, UserSkillsReadResult } from "@/types/userSkills";
import type { DatabaseBackupCommand, DatabaseBackupBackgroundStatus } from "@/lib/backup/backgroundDatabaseBackup";
export function databaseBackupCommand<T = unknown>(command: DatabaseBackupCommand): Promise<T> {
return invoke("database_backup_command", { command });
}
export function databaseBackupBackground(enabled?: boolean): Promise<DatabaseBackupBackgroundStatus> {
return invoke("database_backup_background", { enabled });
}
export async function downloadDatabaseBackupFile(_runId: string, _index: number): Promise<void> {
throw new Error("Use the file manager to access desktop backup files");
}
export function prepareDatabaseBackupRestore(id: string, index: number): Promise<string | SqlFilePreview> {
return invoke("database_backup_command", { command: { action: "file", id, index } });
}
import { assertUpdateAllowsCommand } from "@/lib/app/updatePreparation";
import { collectBrowserSupportInfo } from "@/lib/app/supportInfo";
// Re-exported below so the HTTP transport shares one definition; imported here
@@ -5018,8 +5035,11 @@ export interface SqlFilePreview {
establishesDatabaseContext?: boolean;
packageFilePaths?: string[];
packagePartCount?: number;
cleanupToken?: string;
}
export async function releaseSqlFilePreview(_cleanupToken: string): Promise<void> {}
export interface SqlFileProgress {
executionId: string;
status: SqlFileStatus;
@@ -0,0 +1,25 @@
import type { DatabaseBackupExecutionConfig, DatabaseBackupRun, DatabaseBackupSchedule } from "./scheduledDatabaseBackup";
export interface DatabaseBackupSnapshot {
schedules: DatabaseBackupSchedule[];
runs: DatabaseBackupRun[];
migrated: boolean;
heartbeat: string | null;
destinationRoot: string | null;
}
export type DatabaseBackupCommand =
| { action: "snapshot" }
| { action: "preview"; schedule: DatabaseBackupSchedule }
| { action: "save"; schedule: DatabaseBackupSchedule }
| { action: "deleteSchedule"; id: string }
| { action: "migrate"; migration: { schedules: DatabaseBackupSchedule[]; runs: DatabaseBackupRun[] } }
| { action: "run"; request: { scheduleId?: string; config?: DatabaseBackupExecutionConfig; displayName?: string; timeZone?: string } }
| { action: "cancel"; id: string }
| { action: "rename"; id: string; name: string }
| { action: "deleteRuns"; ids: string[] };
export interface DatabaseBackupBackgroundStatus {
enabled: boolean;
platform: string;
}
@@ -126,6 +126,7 @@ export interface DatabaseBackupExecutionConfig {
}
export interface DatabaseBackupSchedule extends DatabaseBackupExecutionConfig {
timeZone?: string;
id: string;
name: string;
enabled: boolean;
@@ -251,6 +252,7 @@ export function normalizeDatabaseBackupSchedule(value: unknown, now = new Date()
frequency,
intervalHours: boundedInteger(input.intervalHours, 6, 1, 168),
timeOfDay: normalizeDatabaseBackupTime(input.timeOfDay),
timeZone: typeof input.timeZone === "string" ? input.timeZone : undefined,
weekday: boundedInteger(input.weekday, 1, 0, 6),
includeStructure: booleanValue(input.includeStructure, true),
includeData: booleanValue(input.includeData, true),
@@ -411,15 +413,15 @@ export function normalizeDatabaseBackupRunDirectoryPattern(value: unknown): stri
return stringValue(value).trim() || DEFAULT_DATABASE_BACKUP_RUN_DIRECTORY_PATTERN;
}
function renderDatabaseBackupRunDirectoryPattern(pattern: string, scheduleName: string, startedAt: Date | string, runId: string): string {
const timestamp = databaseBackupTimestamp(startedAt);
function renderDatabaseBackupRunDirectoryPattern(pattern: string, scheduleName: string, startedAt: Date | string, runId: string, timeZone?: string): string {
const timestamp = databaseBackupTimestamp(startedAt, timeZone);
const compactTimestamp = timestamp.replace("-", "");
return normalizeDatabaseBackupRunDirectoryPattern(pattern).replaceAll("{schedule}", sanitizeDatabaseBackupFileSegment(scheduleName)).replaceAll("{date}", timestamp.slice(0, 8)).replaceAll("{timestamp}", compactTimestamp).replaceAll("{runId}", sanitizeDatabaseBackupFileSegment(runId).slice(0, 8));
}
export function databaseBackupRunDirectory(directory: string, pattern: string, scheduleName: string, startedAt: Date | string, runId: string): string {
export function databaseBackupRunDirectory(directory: string, pattern: string, scheduleName: string, startedAt: Date | string, runId: string, timeZone?: string): string {
const normalizedPattern = normalizeDatabaseBackupRunDirectoryPattern(pattern);
const rendered = renderDatabaseBackupRunDirectoryPattern(normalizedPattern, scheduleName, startedAt, runId);
const rendered = renderDatabaseBackupRunDirectoryPattern(normalizedPattern, scheduleName, startedAt, runId, timeZone);
if (!rendered || /^[\\/]|^[a-zA-Z]:/.test(rendered) || /[\\/]$/.test(rendered)) {
throw new Error("Backup run directory template must be a relative path.");
}
@@ -455,9 +457,9 @@ export function databaseBackupFileNamePatternIsValid(pattern: string): boolean {
return raw === raw.trim() && !/[\\/:*?"<>|]/.test(raw) && !/[. ]$/.test(raw);
}
function renderDatabaseBackupFileNamePattern(pattern: string, scheduleName: string, fileStem: string, startedAt: Date | string, runId: string): string {
function renderDatabaseBackupFileNamePattern(pattern: string, scheduleName: string, fileStem: string, startedAt: Date | string, runId: string, timeZone?: string): string {
const normalized = normalizeDatabaseBackupFileNamePattern(pattern);
const timestamp = databaseBackupTimestamp(startedAt);
const timestamp = databaseBackupTimestamp(startedAt, timeZone);
const variables = {
"{schedule}": sanitizeDatabaseBackupFileSegment(scheduleName),
"{date}": timestamp.slice(0, 8),
@@ -470,8 +472,13 @@ function renderDatabaseBackupFileNamePattern(pattern: string, scheduleName: stri
return [sanitizeDatabaseBackupFileSegment(rendered), ...requiredSuffixes].join("__");
}
export function databaseBackupTimestamp(value: Date | string): string {
export function databaseBackupTimestamp(value: Date | string, timeZone?: string): string {
const date = value instanceof Date ? value : new Date(value);
if (timeZone) {
const parts = new Intl.DateTimeFormat("en-US", { timeZone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23" }).formatToParts(date);
const part = (name: string) => parts.find((item) => item.type === name)?.value || "";
return `${part("year")}${part("month")}${part("day")}-${part("hour")}${part("minute")}${part("second")}`;
}
const part = (number: number) => String(number).padStart(2, "0");
return `${date.getFullYear()}${part(date.getMonth() + 1)}${part(date.getDate())}-${part(date.getHours())}${part(date.getMinutes())}${part(date.getSeconds())}`;
}
@@ -481,8 +488,8 @@ export function joinDatabaseBackupPath(directory: string, fileName: string): str
return `${directory.replace(/[\\/]+$/, "")}${separator}${fileName}`;
}
export function databaseBackupFilePath(directory: string, scheduleName: string, fileStem: string, startedAt: Date | string, runId: string, outputCompression: DatabaseBackupOutputCompression = "none", fileNamePattern?: string): string {
export function databaseBackupFilePath(directory: string, scheduleName: string, fileStem: string, startedAt: Date | string, runId: string, outputCompression: DatabaseBackupOutputCompression = "none", fileNamePattern?: string, timeZone?: string): string {
const suffix = outputCompression === "gzip" ? ".sql.gz" : ".sql";
const fileName = `${renderDatabaseBackupFileNamePattern(fileNamePattern ?? DEFAULT_DATABASE_BACKUP_FILE_NAME_PATTERN, scheduleName, fileStem, startedAt, runId)}${suffix}`;
const fileName = `${renderDatabaseBackupFileNamePattern(fileNamePattern ?? DEFAULT_DATABASE_BACKUP_FILE_NAME_PATTERN, scheduleName, fileStem, startedAt, runId, timeZone)}${suffix}`;
return joinDatabaseBackupPath(directory, fileName);
}