Compare commits

...

1 Commits

Author SHA1 Message Date
Nicolas Meienberger
5f620b4c45 feat: allow stopping an ongoing backup 2025-11-08 23:26:53 +01:00
11 changed files with 258 additions and 97 deletions

View File

@@ -36,6 +36,7 @@ import {
updateBackupSchedule, updateBackupSchedule,
getBackupScheduleForVolume, getBackupScheduleForVolume,
runBackupNow, runBackupNow,
stopBackup,
getSystemInfo, getSystemInfo,
downloadResticPassword, downloadResticPassword,
} from "../sdk.gen"; } from "../sdk.gen";
@@ -94,9 +95,10 @@ import type {
GetBackupScheduleForVolumeData, GetBackupScheduleForVolumeData,
RunBackupNowData, RunBackupNowData,
RunBackupNowResponse, RunBackupNowResponse,
StopBackupData,
StopBackupResponse,
GetSystemInfoData, GetSystemInfoData,
DownloadResticPasswordData, DownloadResticPasswordData,
DownloadResticPasswordError,
DownloadResticPasswordResponse, DownloadResticPasswordResponse,
} from "../types.gen"; } from "../types.gen";
import { client as _heyApiClient } from "../client.gen"; import { client as _heyApiClient } from "../client.gen";
@@ -1108,6 +1110,45 @@ export const runBackupNowMutation = (
return mutationOptions; return mutationOptions;
}; };
export const stopBackupQueryKey = (options: Options<StopBackupData>) => createQueryKey("stopBackup", options);
/**
* Stop a backup that is currently in progress
*/
export const stopBackupOptions = (options: Options<StopBackupData>) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await stopBackup({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: stopBackupQueryKey(options),
});
};
/**
* Stop a backup that is currently in progress
*/
export const stopBackupMutation = (
options?: Partial<Options<StopBackupData>>,
): UseMutationOptions<StopBackupResponse, DefaultError, Options<StopBackupData>> => {
const mutationOptions: UseMutationOptions<StopBackupResponse, DefaultError, Options<StopBackupData>> = {
mutationFn: async (localOptions) => {
const { data } = await stopBackup({
...options,
...localOptions,
throwOnError: true,
});
return data;
},
};
return mutationOptions;
};
export const getSystemInfoQueryKey = (options?: Options<GetSystemInfoData>) => createQueryKey("getSystemInfo", options); export const getSystemInfoQueryKey = (options?: Options<GetSystemInfoData>) => createQueryKey("getSystemInfo", options);
/** /**
@@ -1154,14 +1195,10 @@ export const downloadResticPasswordOptions = (options?: Options<DownloadResticPa
*/ */
export const downloadResticPasswordMutation = ( export const downloadResticPasswordMutation = (
options?: Partial<Options<DownloadResticPasswordData>>, options?: Partial<Options<DownloadResticPasswordData>>,
): UseMutationOptions< ): UseMutationOptions<DownloadResticPasswordResponse, DefaultError, Options<DownloadResticPasswordData>> => {
DownloadResticPasswordResponse,
DownloadResticPasswordError,
Options<DownloadResticPasswordData>
> => {
const mutationOptions: UseMutationOptions< const mutationOptions: UseMutationOptions<
DownloadResticPasswordResponse, DownloadResticPasswordResponse,
DownloadResticPasswordError, DefaultError,
Options<DownloadResticPasswordData> Options<DownloadResticPasswordData>
> = { > = {
mutationFn: async (localOptions) => { mutationFn: async (localOptions) => {

View File

@@ -74,11 +74,13 @@ import type {
GetBackupScheduleForVolumeResponses, GetBackupScheduleForVolumeResponses,
RunBackupNowData, RunBackupNowData,
RunBackupNowResponses, RunBackupNowResponses,
StopBackupData,
StopBackupResponses,
StopBackupErrors,
GetSystemInfoData, GetSystemInfoData,
GetSystemInfoResponses, GetSystemInfoResponses,
DownloadResticPasswordData, DownloadResticPasswordData,
DownloadResticPasswordResponses, DownloadResticPasswordResponses,
DownloadResticPasswordErrors,
} from "./types.gen"; } from "./types.gen";
import { client as _heyApiClient } from "./client.gen"; import { client as _heyApiClient } from "./client.gen";
@@ -533,6 +535,16 @@ export const runBackupNow = <ThrowOnError extends boolean = false>(
}); });
}; };
/**
* Stop a backup that is currently in progress
*/
export const stopBackup = <ThrowOnError extends boolean = false>(options: Options<StopBackupData, ThrowOnError>) => {
return (options.client ?? _heyApiClient).post<StopBackupResponses, StopBackupErrors, ThrowOnError>({
url: "/api/v1/backups/{scheduleId}/stop",
...options,
});
};
/** /**
* Get system information including available capabilities * Get system information including available capabilities
*/ */
@@ -551,11 +563,7 @@ export const getSystemInfo = <ThrowOnError extends boolean = false>(
export const downloadResticPassword = <ThrowOnError extends boolean = false>( export const downloadResticPassword = <ThrowOnError extends boolean = false>(
options?: Options<DownloadResticPasswordData, ThrowOnError>, options?: Options<DownloadResticPasswordData, ThrowOnError>,
) => { ) => {
return (options?.client ?? _heyApiClient).post< return (options?.client ?? _heyApiClient).post<DownloadResticPasswordResponses, unknown, ThrowOnError>({
DownloadResticPasswordResponses,
DownloadResticPasswordErrors,
ThrowOnError
>({
url: "/api/v1/system/restic-password", url: "/api/v1/system/restic-password",
...options, ...options,
headers: { headers: {

View File

@@ -1480,6 +1480,33 @@ export type RunBackupNowResponses = {
export type RunBackupNowResponse = RunBackupNowResponses[keyof RunBackupNowResponses]; export type RunBackupNowResponse = RunBackupNowResponses[keyof RunBackupNowResponses];
export type StopBackupData = {
body?: never;
path: {
scheduleId: string;
};
query?: never;
url: "/api/v1/backups/{scheduleId}/stop";
};
export type StopBackupErrors = {
/**
* No backup is currently running for this schedule
*/
409: unknown;
};
export type StopBackupResponses = {
/**
* Backup stopped successfully
*/
200: {
success: boolean;
};
};
export type StopBackupResponse = StopBackupResponses[keyof StopBackupResponses];
export type GetSystemInfoData = { export type GetSystemInfoData = {
body?: never; body?: never;
path?: never; path?: never;
@@ -1509,17 +1536,6 @@ export type DownloadResticPasswordData = {
url: "/api/v1/system/restic-password"; url: "/api/v1/system/restic-password";
}; };
export type DownloadResticPasswordErrors = {
/**
* Authentication required or incorrect password
*/
401: {
message?: string;
};
};
export type DownloadResticPasswordError = DownloadResticPasswordErrors[keyof DownloadResticPasswordErrors];
export type DownloadResticPasswordResponses = { export type DownloadResticPasswordResponses = {
/** /**
* Restic password file content * Restic password file content

View File

@@ -1,4 +1,4 @@
import { Pencil, Play, Trash2 } from "lucide-react"; import { Pencil, Play, Square, Trash2 } from "lucide-react";
import { useMemo, useState } from "react"; import { useMemo, useState } from "react";
import { OnOff } from "~/components/onoff"; import { OnOff } from "~/components/onoff";
import { Button } from "~/components/ui/button"; import { Button } from "~/components/ui/button";
@@ -18,12 +18,14 @@ type Props = {
schedule: BackupSchedule; schedule: BackupSchedule;
handleToggleEnabled: (enabled: boolean) => void; handleToggleEnabled: (enabled: boolean) => void;
handleRunBackupNow: () => void; handleRunBackupNow: () => void;
handleStopBackup: () => void;
handleDeleteSchedule: () => void; handleDeleteSchedule: () => void;
setIsEditMode: (isEdit: boolean) => void; setIsEditMode: (isEdit: boolean) => void;
}; };
export const ScheduleSummary = (props: Props) => { export const ScheduleSummary = (props: Props) => {
const { schedule, handleToggleEnabled, handleRunBackupNow, handleDeleteSchedule, setIsEditMode } = props; const { schedule, handleToggleEnabled, handleRunBackupNow, handleStopBackup, handleDeleteSchedule, setIsEditMode } =
props;
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
const summary = useMemo(() => { const summary = useMemo(() => {
@@ -75,16 +77,17 @@ export const ScheduleSummary = (props: Props) => {
</div> </div>
</div> </div>
<div className="flex flex-col sm:flex-row gap-2"> <div className="flex flex-col sm:flex-row gap-2">
<Button {schedule.lastBackupStatus === "in_progress" ? (
variant="default" <Button variant="destructive" size="sm" onClick={handleStopBackup} className="w-full sm:w-auto">
size="sm" <Square className="h-4 w-4 mr-2" />
onClick={handleRunBackupNow} <span className="sm:inline">Stop backup</span>
disabled={schedule.lastBackupStatus === "in_progress"} </Button>
className="w-full sm:w-auto" ) : (
> <Button variant="default" size="sm" onClick={handleRunBackupNow} className="w-full sm:w-auto">
<Play className="h-4 w-4 mr-2" /> <Play className="h-4 w-4 mr-2" />
<span className="sm:inline">Backup now</span> <span className="sm:inline">Backup now</span>
</Button> </Button>
)}
<Button variant="outline" size="sm" onClick={() => setIsEditMode(true)} className="w-full sm:w-auto"> <Button variant="outline" size="sm" onClick={() => setIsEditMode(true)} className="w-full sm:w-auto">
<Pencil className="h-4 w-4 mr-2" /> <Pencil className="h-4 w-4 mr-2" />
<span className="sm:inline">Edit schedule</span> <span className="sm:inline">Edit schedule</span>

View File

@@ -9,6 +9,7 @@ import {
deleteBackupScheduleMutation, deleteBackupScheduleMutation,
listSnapshotsOptions, listSnapshotsOptions,
updateBackupScheduleMutation, updateBackupScheduleMutation,
stopBackupMutation,
} from "~/api-client/@tanstack/react-query.gen"; } from "~/api-client/@tanstack/react-query.gen";
import { parseError } from "~/lib/errors"; import { parseError } from "~/lib/errors";
import { getCronExpression } from "~/utils/utils"; import { getCronExpression } from "~/utils/utils";
@@ -44,9 +45,7 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
const [selectedSnapshotId, setSelectedSnapshotId] = useState<string>(); const [selectedSnapshotId, setSelectedSnapshotId] = useState<string>();
const { data: schedule } = useQuery({ const { data: schedule } = useQuery({
...getBackupScheduleOptions({ ...getBackupScheduleOptions({ path: { scheduleId: params.id } }),
path: { scheduleId: params.id },
}),
initialData: loaderData, initialData: loaderData,
refetchInterval: 10000, refetchInterval: 10000,
refetchOnWindowFocus: true, refetchOnWindowFocus: true,
@@ -57,13 +56,10 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
isLoading, isLoading,
failureReason, failureReason,
} = useQuery({ } = useQuery({
...listSnapshotsOptions({ ...listSnapshotsOptions({ path: { name: schedule.repository.name }, query: { backupId: schedule.id.toString() } }),
path: { name: schedule.repository.name },
query: { backupId: schedule.id.toString() },
}),
}); });
const upsertSchedule = useMutation({ const updateSchedule = useMutation({
...updateBackupScheduleMutation(), ...updateBackupScheduleMutation(),
onSuccess: () => { onSuccess: () => {
toast.success("Backup schedule saved successfully"); toast.success("Backup schedule saved successfully");
@@ -82,9 +78,17 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
toast.success("Backup started successfully"); toast.success("Backup started successfully");
}, },
onError: (error) => { onError: (error) => {
toast.error("Failed to start backup", { toast.error("Failed to start backup", { description: parseError(error)?.message });
description: parseError(error)?.message, },
}); });
const stopBackup = useMutation({
...stopBackupMutation(),
onSuccess: () => {
toast.success("Backup stopped successfully");
},
onError: (error) => {
toast.error("Failed to stop backup", { description: parseError(error)?.message });
}, },
}); });
@@ -95,9 +99,7 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
navigate("/backups"); navigate("/backups");
}, },
onError: (error) => { onError: (error) => {
toast.error("Failed to delete backup schedule", { toast.error("Failed to delete backup schedule", { description: parseError(error)?.message });
description: parseError(error)?.message,
});
}, },
}); });
@@ -114,7 +116,7 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
if (formValues.keepMonthly) retentionPolicy.keepMonthly = formValues.keepMonthly; if (formValues.keepMonthly) retentionPolicy.keepMonthly = formValues.keepMonthly;
if (formValues.keepYearly) retentionPolicy.keepYearly = formValues.keepYearly; if (formValues.keepYearly) retentionPolicy.keepYearly = formValues.keepYearly;
upsertSchedule.mutate({ updateSchedule.mutate({
path: { scheduleId: schedule.id.toString() }, path: { scheduleId: schedule.id.toString() },
body: { body: {
repositoryId: formValues.repositoryId, repositoryId: formValues.repositoryId,
@@ -128,9 +130,7 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
}; };
const handleToggleEnabled = (enabled: boolean) => { const handleToggleEnabled = (enabled: boolean) => {
if (!schedule) return; updateSchedule.mutate({
upsertSchedule.mutate({
path: { scheduleId: schedule.id.toString() }, path: { scheduleId: schedule.id.toString() },
body: { body: {
repositoryId: schedule.repositoryId, repositoryId: schedule.repositoryId,
@@ -143,28 +143,12 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
}); });
}; };
const handleRunBackupNow = () => {
if (!schedule) return;
runBackupNow.mutate({
path: {
scheduleId: schedule.id.toString(),
},
});
};
const handleDeleteSchedule = () => {
if (!schedule) return;
deleteSchedule.mutate({ path: { scheduleId: schedule.id.toString() } });
};
if (isEditMode) { if (isEditMode) {
return ( return (
<div> <div>
<CreateScheduleForm volume={schedule.volume} initialValues={schedule} onSubmit={handleSubmit} formId={formId} /> <CreateScheduleForm volume={schedule.volume} initialValues={schedule} onSubmit={handleSubmit} formId={formId} />
<div className="flex justify-end mt-4 gap-2"> <div className="flex justify-end mt-4 gap-2">
<Button type="submit" className="ml-auto" variant="primary" form={formId} loading={upsertSchedule.isPending}> <Button type="submit" className="ml-auto" variant="primary" form={formId} loading={updateSchedule.isPending}>
Update schedule Update schedule
</Button> </Button>
<Button variant="outline" onClick={() => setIsEditMode(false)}> <Button variant="outline" onClick={() => setIsEditMode(false)}>
@@ -181,8 +165,9 @@ export default function ScheduleDetailsPage({ params, loaderData }: Route.Compon
<div className="flex flex-col gap-6"> <div className="flex flex-col gap-6">
<ScheduleSummary <ScheduleSummary
handleToggleEnabled={handleToggleEnabled} handleToggleEnabled={handleToggleEnabled}
handleRunBackupNow={handleRunBackupNow} handleRunBackupNow={() => runBackupNow.mutate({ path: { scheduleId: schedule.id.toString() } })}
handleDeleteSchedule={handleDeleteSchedule} handleStopBackup={() => stopBackup.mutate({ path: { scheduleId: schedule.id.toString() } })}
handleDeleteSchedule={() => deleteSchedule.mutate({ path: { scheduleId: schedule.id.toString() } })}
setIsEditMode={setIsEditMode} setIsEditMode={setIsEditMode}
schedule={schedule} schedule={schedule}
/> />

View File

@@ -10,7 +10,7 @@ interface ServerEvents {
scheduleId: number; scheduleId: number;
volumeName: string; volumeName: string;
repositoryName: string; repositoryName: string;
status: "success" | "error"; status: "success" | "error" | "stopped";
}) => void; }) => void;
"volume:mounted": (data: { volumeName: string }) => void; "volume:mounted": (data: { volumeName: string }) => void;
"volume:unmounted": (data: { volumeName: string }) => void; "volume:unmounted": (data: { volumeName: string }) => void;

View File

@@ -8,6 +8,7 @@ import {
getBackupScheduleForVolumeDto, getBackupScheduleForVolumeDto,
listBackupSchedulesDto, listBackupSchedulesDto,
runBackupNowDto, runBackupNowDto,
stopBackupDto,
updateBackupScheduleDto, updateBackupScheduleDto,
updateBackupScheduleBody, updateBackupScheduleBody,
type CreateBackupScheduleDto, type CreateBackupScheduleDto,
@@ -16,6 +17,7 @@ import {
type GetBackupScheduleForVolumeResponseDto, type GetBackupScheduleForVolumeResponseDto,
type ListBackupSchedulesResponseDto, type ListBackupSchedulesResponseDto,
type RunBackupNowDto, type RunBackupNowDto,
type StopBackupDto,
type UpdateBackupScheduleDto, type UpdateBackupScheduleDto,
} from "./backups.dto"; } from "./backups.dto";
import { backupsService } from "./backups.service"; import { backupsService } from "./backups.service";
@@ -69,4 +71,11 @@ export const backupScheduleController = new Hono()
}); });
return c.json<RunBackupNowDto>({ success: true }, 200); return c.json<RunBackupNowDto>({ success: true }, 200);
})
.post("/:scheduleId/stop", stopBackupDto, async (c) => {
const scheduleId = c.req.param("scheduleId");
await backupsService.stopBackup(Number(scheduleId));
return c.json<StopBackupDto>({ success: true }, 200);
}); });

View File

@@ -223,3 +223,31 @@ export const runBackupNowDto = describeRoute({
}, },
}, },
}); });
/**
* Stop a running backup
*/
export const stopBackupResponse = type({
success: "boolean",
});
export type StopBackupDto = typeof stopBackupResponse.infer;
export const stopBackupDto = describeRoute({
description: "Stop a backup that is currently in progress",
operationId: "stopBackup",
tags: ["Backups"],
responses: {
200: {
description: "Backup stopped successfully",
content: {
"application/json": {
schema: resolver(stopBackupResponse),
},
},
},
409: {
description: "No backup is currently running for this schedule",
},
},
});

View File

@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import cron from "node-cron"; import cron from "node-cron";
import { CronExpressionParser } from "cron-parser"; import { CronExpressionParser } from "cron-parser";
import { NotFoundError, BadRequestError } from "http-errors-enhanced"; import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
import { db } from "../../db/db"; import { db } from "../../db/db";
import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/schema"; import { backupSchedulesTable, repositoriesTable, volumesTable } from "../../db/schema";
import { restic } from "../../utils/restic"; import { restic } from "../../utils/restic";
@@ -11,6 +11,8 @@ import type { CreateBackupScheduleBody, UpdateBackupScheduleBody } from "./backu
import { toMessage } from "../../utils/errors"; import { toMessage } from "../../utils/errors";
import { serverEvents } from "../../core/events"; import { serverEvents } from "../../core/events";
const runningBackups = new Map<number, AbortController>();
const calculateNextRun = (cronExpression: string): number => { const calculateNextRun = (cronExpression: string): number => {
try { try {
const interval = CronExpressionParser.parse(cronExpression, { const interval = CronExpressionParser.parse(cronExpression, {
@@ -198,6 +200,9 @@ const executeBackup = async (scheduleId: number, manual = false) => {
.set({ lastBackupStatus: "in_progress", updatedAt: Date.now() }) .set({ lastBackupStatus: "in_progress", updatedAt: Date.now() })
.where(eq(backupSchedulesTable.id, scheduleId)); .where(eq(backupSchedulesTable.id, scheduleId));
const abortController = new AbortController();
runningBackups.set(scheduleId, abortController);
try { try {
const volumePath = getVolumePath(volume); const volumePath = getVolumePath(volume);
@@ -205,8 +210,10 @@ const executeBackup = async (scheduleId: number, manual = false) => {
exclude?: string[]; exclude?: string[];
include?: string[]; include?: string[];
tags?: string[]; tags?: string[];
signal?: AbortSignal;
} = { } = {
tags: [schedule.id.toString()], tags: [schedule.id.toString()],
signal: abortController.signal,
}; };
if (schedule.excludePatterns && schedule.excludePatterns.length > 0) { if (schedule.excludePatterns && schedule.excludePatterns.length > 0) {
@@ -264,6 +271,8 @@ const executeBackup = async (scheduleId: number, manual = false) => {
}); });
throw error; throw error;
} finally {
runningBackups.delete(scheduleId);
} }
}; };
@@ -293,6 +302,34 @@ const getScheduleForVolume = async (volumeId: number) => {
return schedule ?? null; return schedule ?? null;
}; };
const stopBackup = async (scheduleId: number) => {
const schedule = await db.query.backupSchedulesTable.findFirst({
where: eq(backupSchedulesTable.id, scheduleId),
});
if (!schedule) {
throw new NotFoundError("Backup schedule not found");
}
await db
.update(backupSchedulesTable)
.set({
lastBackupStatus: "error",
lastBackupError: "Backup was stopped by user",
updatedAt: Date.now(),
})
.where(eq(backupSchedulesTable.id, scheduleId));
const abortController = runningBackups.get(scheduleId);
if (!abortController) {
throw new ConflictError("No backup is currently running for this schedule");
}
logger.info(`Stopping backup for schedule ${scheduleId}`);
abortController.abort();
};
export const backupsService = { export const backupsService = {
listSchedules, listSchedules,
getSchedule, getSchedule,
@@ -302,4 +339,5 @@ export const backupsService = {
executeBackup, executeBackup,
getSchedulesToExecute, getSchedulesToExecute,
getScheduleForVolume, getScheduleForVolume,
stopBackup,
}; };

View File

@@ -23,7 +23,7 @@ export const eventsController = new Hono().get("/", (c) => {
scheduleId: number; scheduleId: number;
volumeName: string; volumeName: string;
repositoryName: string; repositoryName: string;
status: "success" | "error"; status: "success" | "error" | "stopped";
}) => { }) => {
stream.writeSSE({ stream.writeSSE({
data: JSON.stringify(data), data: JSON.stringify(data),

View File

@@ -1,6 +1,7 @@
import crypto from "node:crypto"; import crypto from "node:crypto";
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { spawn } from "node:child_process";
import type { RepositoryConfig } from "@ironmount/schemas/restic"; import type { RepositoryConfig } from "@ironmount/schemas/restic";
import { type } from "arktype"; import { type } from "arktype";
import { $ } from "bun"; import { $ } from "bun";
@@ -114,7 +115,7 @@ const init = async (config: RepositoryConfig) => {
const backup = async ( const backup = async (
config: RepositoryConfig, config: RepositoryConfig,
source: string, source: string,
options?: { exclude?: string[]; include?: string[]; tags?: string[] }, options?: { exclude?: string[]; include?: string[]; tags?: string[]; signal?: AbortSignal },
) => { ) => {
const repoUrl = buildRepoUrl(config); const repoUrl = buildRepoUrl(config);
const env = await buildEnv(config); const env = await buildEnv(config);
@@ -148,32 +149,68 @@ const backup = async (
args.push("--json"); args.push("--json");
// await $`restic unlock --repo ${repoUrl}`.env(env).nothrow(); return new Promise((resolve, reject) => {
const res = await $`restic ${args}`.env(env).nothrow(); const child = spawn("restic", args, {
env: { ...process.env, ...env },
signal: options?.signal,
});
if (includeFile) { let stdout = "";
await fs.unlink(includeFile).catch(() => {}); let stderr = "";
}
if (res.exitCode !== 0) { child.stdout.on("data", (data) => {
logger.error(`Restic backup failed: ${res.stderr}`); stdout += data.toString();
throw new Error(`Restic backup failed: ${res.stderr}`); });
}
// res is a succession of JSON objects, we need to parse the last one which contains the summary child.stderr.on("data", (data) => {
const stdout = res.text(); stderr += data.toString();
const outputLines = stdout.trim().split("\n"); });
const lastLine = outputLines[outputLines.length - 1];
const resSummary = JSON.parse(lastLine ?? "{}");
const result = backupOutputSchema(resSummary); child.on("error", async (error) => {
if (includeFile) {
await fs.unlink(includeFile).catch(() => {});
}
if (result instanceof type.errors) { if (error.name === "AbortError") {
logger.error(`Restic backup output validation failed: ${result}`); logger.info("Restic backup process was aborted");
throw new Error(`Restic backup output validation failed: ${result}`); reject(error);
} } else {
logger.error(`Restic backup process error: ${error.message}`);
reject(new Error(`Restic backup process error: ${error.message}`));
}
});
return result; child.on("close", async (code) => {
if (includeFile) {
await fs.unlink(includeFile).catch(() => {});
}
if (code !== 0) {
logger.error(`Restic backup failed with exit code ${code}: ${stderr}`);
reject(new Error(`Restic backup failed: ${stderr}`));
return;
}
try {
const outputLines = stdout.trim().split("\n");
const lastLine = outputLines[outputLines.length - 1];
const resSummary = JSON.parse(lastLine ?? "{}");
const result = backupOutputSchema(resSummary);
if (result instanceof type.errors) {
logger.error(`Restic backup output validation failed: ${result}`);
reject(new Error(`Restic backup output validation failed: ${result}`));
return;
}
resolve(result);
} catch (error) {
logger.error(`Failed to parse restic backup output: ${error}`);
reject(new Error(`Failed to parse restic backup output: ${error}`));
}
});
});
}; };
const restoreOutputSchema = type({ const restoreOutputSchema = type({