From 875c88c3f48f1f45c63ffe745b4332762ff962d8 Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Sun, 8 Mar 2026 13:19:07 +0100 Subject: [PATCH] fix: single item file restore --- app/client/api-client/types.gen.ts | 1 + app/client/components/restore-form.tsx | 2 ++ .../modules/repositories/repositories.dto.ts | 1 + .../restic/commands/__tests__/restore.test.ts | 14 ++++++++++++++ app/server/utils/restic/commands/restore.ts | 9 +++++++-- 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/client/api-client/types.gen.ts b/app/client/api-client/types.gen.ts index b88a5980..74b30211 100644 --- a/app/client/api-client/types.gen.ts +++ b/app/client/api-client/types.gen.ts @@ -2240,6 +2240,7 @@ export type RestoreSnapshotData = { excludeXattr?: Array; include?: Array; overwrite?: 'always' | 'if-changed' | 'if-newer' | 'never'; + selectedItemKind?: 'dir' | 'file'; targetPath?: string; }; path: { diff --git a/app/client/components/restore-form.tsx b/app/client/components/restore-form.tsx index 40573ebd..c7c48615 100644 --- a/app/client/components/restore-form.tsx +++ b/app/client/components/restore-form.tsx @@ -133,6 +133,7 @@ export function RestoreForm({ repository, snapshotId, returnPath, basePath }: Re body: { snapshotId, include: includePaths.length > 0 ? includePaths : undefined, + selectedItemKind: includePaths.length === 1 ? (selectedPathKind ?? undefined) : undefined, excludeXattr: excludeXattrArray && excludeXattrArray.length > 0 ? excludeXattrArray : undefined, targetPath, overwrite: overwriteMode, @@ -145,6 +146,7 @@ export function RestoreForm({ repository, snapshotId, returnPath, basePath }: Re restoreLocation, customTargetPath, selectedPaths, + selectedPathKind, overwriteMode, restoreSnapshot, ]); diff --git a/app/server/modules/repositories/repositories.dto.ts b/app/server/modules/repositories/repositories.dto.ts index e5958882..7d3a2d9b 100644 --- a/app/server/modules/repositories/repositories.dto.ts +++ b/app/server/modules/repositories/repositories.dto.ts @@ -375,6 +375,7 @@ export const overwriteModeSchema = type.valueOf(OVERWRITE_MODES); export const restoreSnapshotBody = type({ snapshotId: "string", include: "string[]?", + selectedItemKind: dumpPathKindSchema.optional(), exclude: "string[]?", excludeXattr: "string[]?", delete: "boolean?", diff --git a/app/server/utils/restic/commands/__tests__/restore.test.ts b/app/server/utils/restic/commands/__tests__/restore.test.ts index c73537a5..a4167649 100644 --- a/app/server/utils/restic/commands/__tests__/restore.test.ts +++ b/app/server/utils/restic/commands/__tests__/restore.test.ts @@ -100,6 +100,20 @@ describe("restore command", () => { expect(getOptionValues("--include")).toEqual([]); }); + test("restores a single selected file from its parent directory for non-root targets", async () => { + const { getRestoreArg, getOptionValues } = setup(); + const options: Parameters[3] & { selectedItemKind: "file" } = { + organizationId: "org-1", + include: ["/var/lib/zerobyte/volumes/vol123/_data/archive/backup.20260301-233001.7z"], + selectedItemKind: "file", + }; + + await restore(config, "snapshot-single-file", "/tmp/restore-target", options); + + expect(getRestoreArg()).toBe("snapshot-single-file:/var/lib/zerobyte/volumes/vol123/_data/archive"); + expect(getOptionValues("--include")).toEqual(["backup.20260301-233001.7z"]); + }); + test("does not pass an empty include when include equals restore root", async () => { const { getArgs, getRestoreArg, getOptionValues } = setup(); await restore(config, "snapshot-7202d8cc", "/Users/nicolas/Documents/restore", { diff --git a/app/server/utils/restic/commands/restore.ts b/app/server/utils/restic/commands/restore.ts index 0e283539..29f1da9e 100644 --- a/app/server/utils/restic/commands/restore.ts +++ b/app/server/utils/restic/commands/restore.ts @@ -33,6 +33,7 @@ export const restore = async ( basePath?: string; organizationId: string; include?: string[]; + selectedItemKind?: "file" | "dir"; exclude?: string[]; excludeXattr?: string[]; delete?: boolean; @@ -47,7 +48,11 @@ export const restore = async ( let restoreArg = snapshotId; const includes = options.include?.length ? options.include : [options.basePath ?? "/"]; - const commonAncestor = findCommonAncestor(includes); + const commonAncestor = + options.selectedItemKind === "file" && includes.length === 1 + ? path.posix.dirname(includes[0] ?? "/") + : findCommonAncestor(includes); + if (target !== "/") { restoreArg = `${snapshotId}:${commonAncestor}`; } @@ -64,7 +69,7 @@ export const restore = async ( args.push("--include", pattern); } } else { - const strippedIncludes = options.include.map((pattern) => path.relative(commonAncestor, pattern)); + const strippedIncludes = options.include.map((pattern) => path.posix.relative(commonAncestor, pattern)); const includesCoverRestoreRoot = strippedIncludes.some((pattern) => pattern === "" || pattern === "."); if (!includesCoverRestoreRoot) {