fix: dump single file no tar

This commit is contained in:
Nicolas Meienberger 2026-02-21 09:51:21 +01:00
parent d650c53c69
commit 5c67f2e665
11 changed files with 249 additions and 30 deletions

View file

@ -25,6 +25,7 @@ import {
deleteVolume, deleteVolume,
devPanelExec, devPanelExec,
downloadResticPassword, downloadResticPassword,
dumpSnapshot,
getBackupSchedule, getBackupSchedule,
getBackupScheduleForVolume, getBackupScheduleForVolume,
getDevPanel, getDevPanel,
@ -100,6 +101,8 @@ import type {
DevPanelExecResponse, DevPanelExecResponse,
DownloadResticPasswordData, DownloadResticPasswordData,
DownloadResticPasswordResponse, DownloadResticPasswordResponse,
DumpSnapshotData,
DumpSnapshotResponse,
GetBackupScheduleData, GetBackupScheduleData,
GetBackupScheduleForVolumeData, GetBackupScheduleForVolumeData,
GetBackupScheduleForVolumeResponse, GetBackupScheduleForVolumeResponse,
@ -851,6 +854,25 @@ export const listSnapshotFilesInfiniteOptions = (options: Options<ListSnapshotFi
}, },
); );
export const dumpSnapshotQueryKey = (options: Options<DumpSnapshotData>) => createQueryKey("dumpSnapshot", options);
/**
* Download a snapshot path as a tar archive (folders) or raw file stream (single files)
*/
export const dumpSnapshotOptions = (options: Options<DumpSnapshotData>) =>
queryOptions<DumpSnapshotResponse, DefaultError, DumpSnapshotResponse, ReturnType<typeof dumpSnapshotQueryKey>>({
queryFn: async ({ queryKey, signal }) => {
const { data } = await dumpSnapshot({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: dumpSnapshotQueryKey(options),
});
/** /**
* Restore a snapshot to a target path on the filesystem * Restore a snapshot to a target path on the filesystem
*/ */

View file

@ -16,6 +16,7 @@ export {
deleteVolume, deleteVolume,
devPanelExec, devPanelExec,
downloadResticPassword, downloadResticPassword,
dumpSnapshot,
getBackupSchedule, getBackupSchedule,
getBackupScheduleForVolume, getBackupScheduleForVolume,
getDevPanel, getDevPanel,
@ -109,6 +110,9 @@ export type {
DownloadResticPasswordData, DownloadResticPasswordData,
DownloadResticPasswordResponse, DownloadResticPasswordResponse,
DownloadResticPasswordResponses, DownloadResticPasswordResponses,
DumpSnapshotData,
DumpSnapshotResponse,
DumpSnapshotResponses,
GetBackupScheduleData, GetBackupScheduleData,
GetBackupScheduleForVolumeData, GetBackupScheduleForVolumeData,
GetBackupScheduleForVolumeResponse, GetBackupScheduleForVolumeResponse,

View file

@ -35,6 +35,8 @@ import type {
DevPanelExecResponses, DevPanelExecResponses,
DownloadResticPasswordData, DownloadResticPasswordData,
DownloadResticPasswordResponses, DownloadResticPasswordResponses,
DumpSnapshotData,
DumpSnapshotResponses,
GetBackupScheduleData, GetBackupScheduleData,
GetBackupScheduleForVolumeData, GetBackupScheduleForVolumeData,
GetBackupScheduleForVolumeResponses, GetBackupScheduleForVolumeResponses,
@ -431,6 +433,15 @@ export const listSnapshotFiles = <ThrowOnError extends boolean = false>(
...options, ...options,
}); });
/**
* Download a snapshot path as a tar archive (folders) or raw file stream (single files)
*/
export const dumpSnapshot = <ThrowOnError extends boolean = false>(options: Options<DumpSnapshotData, ThrowOnError>) =>
(options.client ?? client).get<DumpSnapshotResponses, unknown, ThrowOnError>({
url: "/api/v1/repositories/{shortId}/snapshots/{snapshotId}/dump",
...options,
});
/** /**
* Restore a snapshot to a target path on the filesystem * Restore a snapshot to a target path on the filesystem
*/ */

View file

@ -1970,6 +1970,28 @@ export type ListSnapshotFilesResponses = {
export type ListSnapshotFilesResponse = ListSnapshotFilesResponses[keyof ListSnapshotFilesResponses]; export type ListSnapshotFilesResponse = ListSnapshotFilesResponses[keyof ListSnapshotFilesResponses];
export type DumpSnapshotData = {
body?: never;
path: {
shortId: string;
snapshotId: string;
};
query?: {
kind?: "dir" | "file";
path?: string;
};
url: "/api/v1/repositories/{shortId}/snapshots/{snapshotId}/dump";
};
export type DumpSnapshotResponses = {
/**
* Snapshot content stream
*/
200: Blob | File;
};
export type DumpSnapshotResponse = DumpSnapshotResponses[keyof DumpSnapshotResponses];
export type RestoreSnapshotData = { export type RestoreSnapshotData = {
body?: { body?: {
snapshotId: string; snapshotId: string;

View file

@ -12,6 +12,7 @@ type SnapshotTreeBrowserProps = FileBrowserUiProps & {
basePath?: string; basePath?: string;
pageSize?: number; pageSize?: number;
enabled?: boolean; enabled?: boolean;
onSingleSelectionKindChange?: (kind: "file" | "dir" | null) => void;
}; };
export const SnapshotTreeBrowser = ({ export const SnapshotTreeBrowser = ({
@ -22,7 +23,7 @@ export const SnapshotTreeBrowser = ({
enabled = true, enabled = true,
...uiProps ...uiProps
}: SnapshotTreeBrowserProps) => { }: SnapshotTreeBrowserProps) => {
const { selectedPaths, onSelectionChange, ...fileBrowserUiProps } = uiProps; const { selectedPaths, onSelectionChange, onSingleSelectionKindChange, ...fileBrowserUiProps } = uiProps;
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const normalizedBasePath = normalizeAbsolutePath(basePath); const normalizedBasePath = normalizeAbsolutePath(basePath);
@ -66,20 +67,6 @@ export const SnapshotTreeBrowser = ({
return displayPaths; return displayPaths;
}, [selectedPaths, stripBasePath]); }, [selectedPaths, stripBasePath]);
const handleSelectionChange = useCallback(
(nextDisplayPaths: Set<string>) => {
if (!onSelectionChange) return;
const nextFullPaths = new Set<string>();
for (const displayPath of nextDisplayPaths) {
nextFullPaths.add(addBasePath(displayPath));
}
onSelectionChange(nextFullPaths);
},
[onSelectionChange, addBasePath],
);
const fileBrowser = useFileBrowser({ const fileBrowser = useFileBrowser({
initialData: data, initialData: data,
isLoading, isLoading,
@ -113,6 +100,41 @@ export const SnapshotTreeBrowser = ({
}, },
}); });
const displayPathKinds = useMemo(() => {
const kinds = new Map<string, "file" | "dir">();
for (const entry of fileBrowser.fileArray) {
kinds.set(entry.path, entry.type === "file" ? "file" : "dir");
}
return kinds;
}, [fileBrowser.fileArray]);
const handleSelectionChange = useCallback(
(nextDisplayPaths: Set<string>) => {
if (!onSelectionChange) return;
const nextFullPaths = new Set<string>();
for (const displayPath of nextDisplayPaths) {
nextFullPaths.add(addBasePath(displayPath));
}
if (onSingleSelectionKindChange) {
if (nextDisplayPaths.size === 1) {
const [selectedDisplayPath] = nextDisplayPaths;
if (selectedDisplayPath) {
onSingleSelectionKindChange(displayPathKinds.get(selectedDisplayPath) ?? null);
} else {
onSingleSelectionKindChange(null);
}
} else {
onSingleSelectionKindChange(null);
}
}
onSelectionChange(nextFullPaths);
},
[onSelectionChange, addBasePath, onSingleSelectionKindChange, displayPathKinds],
);
const errorDetails = parseError(error)?.message; const errorDetails = parseError(error)?.message;
const errorMessage = errorDetails const errorMessage = errorDetails
? `Failed to load files: ${errorDetails}` ? `Failed to load files: ${errorDetails}`

View file

@ -54,6 +54,7 @@ export function RestoreForm({ repository, snapshotId, returnPath, basePath }: Re
const restoreCompletedRef = useRef(false); const restoreCompletedRef = useRef(false);
const [selectedPaths, setSelectedPaths] = useState<Set<string>>(new Set()); const [selectedPaths, setSelectedPaths] = useState<Set<string>>(new Set());
const [selectedPathKind, setSelectedPathKind] = useState<"file" | "dir" | null>(null);
useEffect(() => { useEffect(() => {
const abortController = new AbortController(); const abortController = new AbortController();
@ -165,6 +166,9 @@ export function RestoreForm({ repository, snapshotId, returnPath, basePath }: Re
const [selectedPath] = selectedPaths; const [selectedPath] = selectedPaths;
if (selectedPath) { if (selectedPath) {
dumpUrl.searchParams.set("path", selectedPath); dumpUrl.searchParams.set("path", selectedPath);
if (selectedPathKind) {
dumpUrl.searchParams.set("kind", selectedPathKind);
}
} }
} }
@ -174,7 +178,7 @@ export function RestoreForm({ repository, snapshotId, returnPath, basePath }: Re
link.click(); link.click();
document.body.removeChild(link); document.body.removeChild(link);
setWaitingForDownload(false); setWaitingForDownload(false);
}, [repository.shortId, snapshotId, selectedPaths]); }, [repository.shortId, snapshotId, selectedPathKind, selectedPaths]);
const acknowledgeRestoreResult = useCallback(() => { const acknowledgeRestoreResult = useCallback(() => {
setShowRestoreResultAlert(false); setShowRestoreResultAlert(false);
@ -362,6 +366,7 @@ export function RestoreForm({ repository, snapshotId, returnPath, basePath }: Re
withCheckboxes withCheckboxes
selectedPaths={selectedPaths} selectedPaths={selectedPaths}
onSelectionChange={setSelectedPaths} onSelectionChange={setSelectedPaths}
onSingleSelectionKindChange={setSelectedPathKind}
stateClassName="flex-1 min-h-0" stateClassName="flex-1 min-h-0"
/> />
</CardContent> </CardContent>

View file

@ -131,7 +131,7 @@ describe("repositoriesService.dumpSnapshot", () => {
const emitSpy = spyOn(serverEvents, "emit"); const emitSpy = spyOn(serverEvents, "emit");
await withContext({ organizationId, userId: user.id }, () => await withContext({ organizationId, userId: user.id }, () =>
repositoriesService.dumpSnapshot(shortId, "snapshot-123", `${basePath}/documents`), repositoriesService.dumpSnapshot(shortId, "snapshot-123", `${basePath}/documents`, "dir"),
); );
expect(dumpMock).toHaveBeenCalledTimes(1); expect(dumpMock).toHaveBeenCalledTimes(1);
@ -156,6 +156,101 @@ describe("repositoriesService.dumpSnapshot", () => {
); );
}); });
test("streams a single file directly when selected path is a file", async () => {
const { organizationId, user } = await createTestSession();
const shortId = generateShortId();
const basePath = "/var/lib/zerobyte/volumes/vol123/_data";
await db.insert(repositoriesTable).values({
id: randomUUID(),
shortId,
name: `Repository-${randomUUID()}`,
type: "local",
config: {
backend: "local",
path: `/tmp/repository-${randomUUID()}`,
isExistingRepository: true,
},
compressionMode: "off",
organizationId,
});
const snapshotsMock = mock(() =>
Promise.resolve([
{
id: "snapshot-file",
short_id: "snapshot-file",
time: new Date().toISOString(),
tree: "tree-file",
paths: [basePath],
hostname: "host",
},
]),
);
spyOn(restic, "snapshots").mockImplementation(snapshotsMock as typeof restic.snapshots);
const dumpMock = mock(() =>
Promise.resolve({
stream: Readable.from([]),
completion: Promise.resolve(),
abort: () => {},
}),
);
spyOn(restic, "dump").mockImplementation(dumpMock);
const result = await withContext({ organizationId, userId: user.id }, () =>
repositoriesService.dumpSnapshot(shortId, "snapshot-file", `${basePath}/documents/report.txt`, "file"),
);
expect(dumpMock).toHaveBeenCalledWith(expect.anything(), `snapshot-file:${basePath}`, {
organizationId,
path: "/documents/report.txt",
archive: false,
});
expect(result.filename).toBe("report.txt");
expect(result.contentType).toBe("application/octet-stream");
});
test("rejects path downloads without a kind", async () => {
const { organizationId, user } = await createTestSession();
const shortId = generateShortId();
const basePath = "/var/lib/zerobyte/volumes/vol123/_data";
await db.insert(repositoriesTable).values({
id: randomUUID(),
shortId,
name: `Repository-${randomUUID()}`,
type: "local",
config: {
backend: "local",
path: `/tmp/repository-${randomUUID()}`,
isExistingRepository: true,
},
compressionMode: "off",
organizationId,
});
const snapshotsMock = mock(() =>
Promise.resolve([
{
id: "snapshot-no-kind",
short_id: "snapshot-no-kind",
time: new Date().toISOString(),
tree: "tree-no-kind",
paths: [basePath],
hostname: "host",
},
]),
);
spyOn(restic, "snapshots").mockImplementation(snapshotsMock as typeof restic.snapshots);
await expect(
withContext({ organizationId, userId: user.id }, () =>
repositoriesService.dumpSnapshot(shortId, "snapshot-no-kind", `${basePath}/documents/report.txt`),
),
).rejects.toThrow("Path kind is required when downloading a specific snapshot path");
});
test("downloads full snapshot relative to common ancestor when path is omitted", async () => { test("downloads full snapshot relative to common ancestor when path is omitted", async () => {
const { organizationId, user } = await createTestSession(); const { organizationId, user } = await createTestSession();
const shortId = generateShortId(); const shortId = generateShortId();

View file

@ -171,9 +171,9 @@ export const repositoriesController = new Hono()
) )
.get("/:shortId/snapshots/:snapshotId/dump", dumpSnapshotDto, validator("query", dumpSnapshotQuery), async (c) => { .get("/:shortId/snapshots/:snapshotId/dump", dumpSnapshotDto, validator("query", dumpSnapshotQuery), async (c) => {
const { shortId, snapshotId } = c.req.param(); const { shortId, snapshotId } = c.req.param();
const { path } = c.req.valid("query"); const { path, kind } = c.req.valid("query");
const dumpStream = await repositoriesService.dumpSnapshot(shortId, snapshotId, path); const dumpStream = await repositoriesService.dumpSnapshot(shortId, snapshotId, path, kind);
const signal = c.req.raw.signal; const signal = c.req.raw.signal;
if (signal.aborted) { if (signal.aborted) {
@ -187,7 +187,7 @@ export const repositoriesController = new Hono()
return new Response(webStream, { return new Response(webStream, {
status: 200, status: 200,
headers: { headers: {
"Content-Type": "application/x-tar", "Content-Type": dumpStream.contentType,
"Content-Disposition": contentDisposition(dumpStream.filename || "snapshot.tar"), "Content-Disposition": contentDisposition(dumpStream.filename || "snapshot.tar"),
"X-Content-Type-Options": "nosniff", "X-Content-Type-Options": "nosniff",
}, },

View file

@ -290,24 +290,36 @@ export const listSnapshotFilesDto = describeRoute({
}, },
}); });
const DUMP_PATH_KINDS = {
file: "file",
dir: "dir",
} as const;
export const dumpPathKindSchema = type.valueOf(DUMP_PATH_KINDS);
export type DumpPathKind = typeof dumpPathKindSchema.infer;
/** /**
* Download snapshot or a specific path as tar archive * Download snapshot paths as tar archives (folders) or raw file streams (single files)
*/ */
export const dumpSnapshotQuery = type({ export const dumpSnapshotQuery = type({
path: "string?", path: "string?",
kind: dumpPathKindSchema.optional(),
}); });
export const dumpSnapshotDto = describeRoute({ export const dumpSnapshotDto = describeRoute({
description: "Download a full snapshot or a single file/folder path as a tar archive", description: "Download a snapshot path as a tar archive (folders) or raw file stream (single files)",
tags: ["Repositories"], tags: ["Repositories"],
operationId: "dumpSnapshot", operationId: "dumpSnapshot",
responses: { responses: {
200: { 200: {
description: "Snapshot archive stream", description: "Snapshot content stream",
content: { content: {
"application/x-tar": { "application/x-tar": {
schema: { type: "string", format: "binary" }, schema: { type: "string", format: "binary" },
}, },
"application/octet-stream": {
schema: { type: "string", format: "binary" },
},
}, },
}, },
}, },

View file

@ -1,4 +1,5 @@
import crypto from "node:crypto"; import crypto from "node:crypto";
import nodePath from "node:path";
import { type } from "arktype"; import { type } from "arktype";
import { and, eq } from "drizzle-orm"; import { and, eq } from "drizzle-orm";
import { BadRequestError, ConflictError, InternalServerError, NotFoundError } from "http-errors-enhanced"; import { BadRequestError, ConflictError, InternalServerError, NotFoundError } from "http-errors-enhanced";
@ -22,7 +23,7 @@ import { generateShortId } from "../../utils/id";
import { addCommonArgs, buildEnv, buildRepoUrl, cleanupTemporaryKeys, restic } from "../../utils/restic"; import { addCommonArgs, buildEnv, buildRepoUrl, cleanupTemporaryKeys, restic } from "../../utils/restic";
import { safeSpawn } from "../../utils/spawn"; import { safeSpawn } from "../../utils/spawn";
import { backupsService } from "../backups/backups.service"; import { backupsService } from "../backups/backups.service";
import type { UpdateRepositoryBody } from "./repositories.dto"; import type { DumpPathKind, UpdateRepositoryBody } from "./repositories.dto";
import { REPOSITORY_BASE } from "~/server/core/constants"; import { REPOSITORY_BASE } from "~/server/core/constants";
import { findCommonAncestor } from "~/utils/common-ancestor"; import { findCommonAncestor } from "~/utils/common-ancestor";
import { prepareSnapshotDump } from "./helpers/dump"; import { prepareSnapshotDump } from "./helpers/dump";
@ -390,7 +391,7 @@ const restoreSnapshot = async (
} }
}; };
const dumpSnapshot = async (shortId: string, snapshotId: string, path?: string) => { const dumpSnapshot = async (shortId: string, snapshotId: string, path?: string, kind?: DumpPathKind) => {
const organizationId = getOrganizationId(); const organizationId = getOrganizationId();
const repository = await findRepository(shortId); const repository = await findRepository(shortId);
@ -404,23 +405,43 @@ const dumpSnapshot = async (shortId: string, snapshotId: string, path?: string)
try { try {
const snapshot = await getSnapshotDetails(repository.shortId, snapshotId); const snapshot = await getSnapshotDetails(repository.shortId, snapshotId);
const preparedDump = prepareSnapshotDump({ snapshotId, snapshotPaths: snapshot.paths, requestedPath: path }); const preparedDump = prepareSnapshotDump({ snapshotId, snapshotPaths: snapshot.paths, requestedPath: path });
dumpStream = await restic.dump(repository.config, preparedDump.snapshotRef, { const dumpOptions: Parameters<typeof restic.dump>[2] = {
organizationId, organizationId,
path: preparedDump.path, path: preparedDump.path,
}); };
let filename = preparedDump.filename;
let contentType = "application/x-tar";
if (path && preparedDump.path !== "/") {
if (!kind) {
throw new BadRequestError("Path kind is required when downloading a specific snapshot path");
}
if (kind === "file") {
dumpOptions.archive = false;
contentType = "application/octet-stream";
const fileName = nodePath.posix.basename(preparedDump.path);
if (fileName) {
filename = fileName;
}
}
}
dumpStream = await restic.dump(repository.config, preparedDump.snapshotRef, dumpOptions);
serverEvents.emit("dump:started", { serverEvents.emit("dump:started", {
organizationId, organizationId,
repositoryId: repository.shortId, repositoryId: repository.shortId,
snapshotId, snapshotId,
path: preparedDump.path, path: preparedDump.path,
filename: preparedDump.filename, filename,
}); });
const completion = dumpStream.completion.finally(releaseLock); const completion = dumpStream.completion.finally(releaseLock);
void completion.catch(() => {}); void completion.catch(() => {});
return { ...dumpStream, completion, filename: preparedDump.filename }; return { ...dumpStream, completion, filename, contentType };
} catch (error) { } catch (error) {
if (dumpStream) { if (dumpStream) {
dumpStream.abort(); dumpStream.abort();

View file

@ -580,13 +580,18 @@ const dump = async (
options: { options: {
organizationId: string; organizationId: string;
path?: string; path?: string;
archive?: false;
}, },
): Promise<ResticDumpStream> => { ): Promise<ResticDumpStream> => {
const repoUrl = buildRepoUrl(config); const repoUrl = buildRepoUrl(config);
const env = await buildEnv(config, options.organizationId); const env = await buildEnv(config, options.organizationId);
const pathToDump = normalizeDumpPath(options.path); const pathToDump = normalizeDumpPath(options.path);
const args: string[] = ["--repo", repoUrl, "dump", snapshotRef, pathToDump, "--archive", "tar"]; const args: string[] = ["--repo", repoUrl, "dump", snapshotRef, pathToDump];
if (options.archive !== false) {
args.push("--archive", "tar");
}
addCommonArgs(args, env, config, { includeJson: false }); addCommonArgs(args, env, config, { includeJson: false });