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

@@ -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;