refactor: centralize restic backup schemas

This commit is contained in:
Nicolas Meienberger 2026-02-12 17:53:31 +01:00
parent 25bb351aaf
commit 7f1874a571
11 changed files with 167 additions and 245 deletions

View file

@ -1,26 +1,10 @@
import { Card, CardContent } from "~/client/components/ui/card"; import { Card, CardContent } from "~/client/components/ui/card";
import { ByteSize } from "~/client/components/bytes-size"; import { ByteSize } from "~/client/components/bytes-size";
import type { ResticSnapshotSummaryDto } from "~/schemas/restic-dto";
import { formatDuration } from "~/utils/utils"; import { formatDuration } from "~/utils/utils";
type BackupSummary = {
backup_start: string;
backup_end: string;
files_new: number;
files_changed: number;
files_unmodified: number;
dirs_new: number;
dirs_changed: number;
dirs_unmodified: number;
data_blobs: number;
tree_blobs: number;
data_added: number;
data_added_packed?: number | null;
total_files_processed: number;
total_bytes_processed: number;
};
type Props = { type Props = {
summary?: BackupSummary | null; summary?: ResticSnapshotSummaryDto | null;
}; };
const formatCount = (value: number) => value.toLocaleString(); const formatCount = (value: number) => value.toLocaleString();

View file

@ -1,5 +1,10 @@
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
import { useQueryClient } from "@tanstack/react-query"; import { useQueryClient } from "@tanstack/react-query";
import type {
BackupCompletedEventDto,
BackupProgressEventDto,
BackupStartedEventDto,
} from "~/schemas/events-dto";
type ServerEventType = type ServerEventType =
| "connected" | "connected"
@ -16,42 +21,6 @@ type ServerEventType =
| "doctor:completed" | "doctor:completed"
| "doctor:cancelled"; | "doctor:cancelled";
export interface BackupEvent {
scheduleId: number;
volumeName: string;
repositoryName: string;
status?: "success" | "error";
summary?: {
files_new: number;
files_changed: number;
files_unmodified: number;
dirs_new: number;
dirs_changed: number;
dirs_unmodified: number;
data_blobs: number;
tree_blobs: number;
data_added: number;
data_added_packed?: number;
total_files_processed: number;
total_bytes_processed: number;
total_duration: number;
snapshot_id: string;
};
}
export interface BackupProgressEvent {
scheduleId: number;
volumeName: string;
repositoryName: string;
seconds_elapsed: number;
percent_done: number;
total_files: number;
files_done: number;
total_bytes: number;
bytes_done: number;
current_files: string[];
}
export interface VolumeEvent { export interface VolumeEvent {
volumeName: string; volumeName: string;
} }
@ -103,7 +72,7 @@ export function useServerEvents() {
eventSource.addEventListener("heartbeat", () => {}); eventSource.addEventListener("heartbeat", () => {});
eventSource.addEventListener("backup:started", (e) => { eventSource.addEventListener("backup:started", (e) => {
const data = JSON.parse(e.data) as BackupEvent; const data = JSON.parse(e.data) as BackupStartedEventDto;
console.info("[SSE] Backup started:", data); console.info("[SSE] Backup started:", data);
handlersRef.current.get("backup:started")?.forEach((handler) => { handlersRef.current.get("backup:started")?.forEach((handler) => {
@ -112,7 +81,7 @@ export function useServerEvents() {
}); });
eventSource.addEventListener("backup:progress", (e) => { eventSource.addEventListener("backup:progress", (e) => {
const data = JSON.parse(e.data) as BackupProgressEvent; const data = JSON.parse(e.data) as BackupProgressEventDto;
handlersRef.current.get("backup:progress")?.forEach((handler) => { handlersRef.current.get("backup:progress")?.forEach((handler) => {
handler(data); handler(data);
@ -120,7 +89,7 @@ export function useServerEvents() {
}); });
eventSource.addEventListener("backup:completed", (e) => { eventSource.addEventListener("backup:completed", (e) => {
const data = JSON.parse(e.data) as BackupEvent; const data = JSON.parse(e.data) as BackupCompletedEventDto;
console.info("[SSE] Backup completed:", data); console.info("[SSE] Backup completed:", data);
void queryClient.invalidateQueries(); void queryClient.invalidateQueries();

View file

@ -2,7 +2,8 @@ import { useEffect, useState } from "react";
import { ByteSize } from "~/client/components/bytes-size"; import { ByteSize } from "~/client/components/bytes-size";
import { Card } from "~/client/components/ui/card"; import { Card } from "~/client/components/ui/card";
import { Progress } from "~/client/components/ui/progress"; import { Progress } from "~/client/components/ui/progress";
import { type BackupProgressEvent, useServerEvents } from "~/client/hooks/use-server-events"; import { useServerEvents } from "~/client/hooks/use-server-events";
import type { BackupProgressEventDto } from "~/schemas/events-dto";
import { formatDuration } from "~/utils/utils"; import { formatDuration } from "~/utils/utils";
import { formatBytes } from "../../../../utils/format-bytes"; import { formatBytes } from "../../../../utils/format-bytes";
@ -12,11 +13,11 @@ type Props = {
export const BackupProgressCard = ({ scheduleId }: Props) => { export const BackupProgressCard = ({ scheduleId }: Props) => {
const { addEventListener } = useServerEvents(); const { addEventListener } = useServerEvents();
const [progress, setProgress] = useState<BackupProgressEvent | null>(null); const [progress, setProgress] = useState<BackupProgressEventDto | null>(null);
useEffect(() => { useEffect(() => {
const unsubscribe = addEventListener("backup:progress", (data) => { const unsubscribe = addEventListener("backup:progress", (data) => {
const progressData = data as BackupProgressEvent; const progressData = data as BackupProgressEventDto;
if (progressData.scheduleId === scheduleId) { if (progressData.scheduleId === scheduleId) {
setProgress(progressData); setProgress(progressData);
} }

39
app/schemas/events-dto.ts Normal file
View file

@ -0,0 +1,39 @@
import { type } from "arktype";
import { resticBackupProgressMetricsSchema, resticBackupRunSummarySchema } from "~/schemas/restic-dto";
export const backupEventStatusSchema = type("'success' | 'error' | 'stopped' | 'warning'");
const backupEventBaseSchema = type({
scheduleId: "number",
volumeName: "string",
repositoryName: "string",
});
const organizationScopedSchema = type({
organizationId: "string",
});
export const backupStartedEventSchema = backupEventBaseSchema;
export const backupProgressEventSchema = backupEventBaseSchema.and(resticBackupProgressMetricsSchema);
export const backupCompletedEventSchema = backupEventBaseSchema.and(
type({
status: backupEventStatusSchema,
summary: resticBackupRunSummarySchema.optional(),
}),
);
export const serverBackupStartedEventSchema = organizationScopedSchema.and(backupStartedEventSchema);
export const serverBackupProgressEventSchema = organizationScopedSchema.and(backupProgressEventSchema);
export const serverBackupCompletedEventSchema = organizationScopedSchema.and(backupCompletedEventSchema);
export type BackupEventStatusDto = typeof backupEventStatusSchema.infer;
export type BackupStartedEventDto = typeof backupStartedEventSchema.infer;
export type BackupProgressEventDto = typeof backupProgressEventSchema.infer;
export type BackupCompletedEventDto = typeof backupCompletedEventSchema.infer;
export type ServerBackupStartedEventDto = typeof serverBackupStartedEventSchema.infer;
export type ServerBackupProgressEventDto = typeof serverBackupProgressEventSchema.infer;
export type ServerBackupCompletedEventDto = typeof serverBackupCompletedEventSchema.infer;

70
app/schemas/restic-dto.ts Normal file
View file

@ -0,0 +1,70 @@
import { type } from "arktype";
export const resticSummaryBaseSchema = type({
files_new: "number",
files_changed: "number",
files_unmodified: "number",
dirs_new: "number",
dirs_changed: "number",
dirs_unmodified: "number",
data_blobs: "number",
tree_blobs: "number",
data_added: "number",
data_added_packed: "number?",
total_files_processed: "number",
total_bytes_processed: "number",
});
export const resticSnapshotSummarySchema = resticSummaryBaseSchema.and(
type({
backup_start: "string",
backup_end: "string",
}),
);
export const resticBackupRunSummarySchema = resticSummaryBaseSchema.and(
type({
total_duration: "number",
snapshot_id: "string",
}),
);
export const resticBackupOutputSchema = resticBackupRunSummarySchema.and(
type({
message_type: "'summary'",
}),
);
export const resticBackupProgressMetricsSchema = type({
seconds_elapsed: "number",
percent_done: "number",
total_files: "number",
files_done: "number",
total_bytes: "number",
bytes_done: "number",
current_files: "string[]",
});
export const resticBackupProgressSchema = resticBackupProgressMetricsSchema.and(
type({
message_type: "'status'",
}),
);
export const resticRestoreOutputSchema = type({
message_type: "'summary'",
total_files: "number?",
files_restored: "number",
files_skipped: "number",
total_bytes: "number?",
bytes_restored: "number?",
bytes_skipped: "number",
});
export type ResticSnapshotSummaryDto = typeof resticSnapshotSummarySchema.infer;
export type ResticBackupRunSummaryDto = typeof resticBackupRunSummarySchema.infer;
export type ResticBackupOutputDto = typeof resticBackupOutputSchema.infer;
export type ResticBackupProgressMetricsDto = typeof resticBackupProgressMetricsSchema.infer;
export type ResticBackupProgressDto = typeof resticBackupProgressSchema.infer;
export type ResticRestoreOutputDto = typeof resticRestoreOutputSchema.infer;

View file

@ -1,48 +1,19 @@
import { EventEmitter } from "node:events"; import { EventEmitter } from "node:events";
import type { TypedEmitter } from "tiny-typed-emitter"; import type { TypedEmitter } from "tiny-typed-emitter";
import type { DoctorResult } from "~/schemas/restic"; import type { DoctorResult } from "~/schemas/restic";
import type {
ServerBackupCompletedEventDto,
ServerBackupProgressEventDto,
ServerBackupStartedEventDto,
} from "~/schemas/events-dto";
/** /**
* Event payloads for the SSE system * Event payloads for the SSE system
*/ */
interface ServerEvents { interface ServerEvents {
"backup:started": (data: { organizationId: string; scheduleId: number; volumeName: string; repositoryName: string }) => void; "backup:started": (data: ServerBackupStartedEventDto) => void;
"backup:progress": (data: { "backup:progress": (data: ServerBackupProgressEventDto) => void;
organizationId: string; "backup:completed": (data: ServerBackupCompletedEventDto) => void;
scheduleId: number;
volumeName: string;
repositoryName: string;
seconds_elapsed: number;
percent_done: number;
total_files: number;
files_done: number;
total_bytes: number;
bytes_done: number;
current_files: string[];
}) => void;
"backup:completed": (data: {
organizationId: string;
scheduleId: number;
volumeName: string;
repositoryName: string;
status: "success" | "error" | "stopped" | "warning";
summary?: {
files_new: number;
files_changed: number;
files_unmodified: number;
dirs_new: number;
dirs_changed: number;
dirs_unmodified: number;
data_blobs: number;
tree_blobs: number;
data_added: number;
data_added_packed?: number;
total_files_processed: number;
total_bytes_processed: number;
total_duration: number;
snapshot_id: string;
};
}) => void;
"mirror:started": (data: { "mirror:started": (data: {
organizationId: string; organizationId: string;
scheduleId: number; scheduleId: number;

View file

@ -1,6 +1,6 @@
import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced"; import { NotFoundError, BadRequestError, ConflictError } from "http-errors-enhanced";
import type { BackupSchedule, Volume, Repository } from "../../db/schema"; import type { BackupSchedule, Volume, Repository } from "../../db/schema";
import { restic, type BackupOutput } from "../../utils/restic"; import { restic } from "../../utils/restic";
import { logger } from "../../utils/logger"; import { logger } from "../../utils/logger";
import { cache } from "../../utils/cache"; import { cache } from "../../utils/cache";
import { getVolumePath } from "../volumes/helpers"; import { getVolumePath } from "../volumes/helpers";
@ -11,6 +11,7 @@ import { repoMutex } from "../../core/repository-mutex";
import { getOrganizationId } from "~/server/core/request-context"; import { getOrganizationId } from "~/server/core/request-context";
import { scheduleQueries, mirrorQueries, repositoryQueries } from "./backups.queries"; import { scheduleQueries, mirrorQueries, repositoryQueries } from "./backups.queries";
import { calculateNextRun, createBackupOptions } from "./backup.helpers"; import { calculateNextRun, createBackupOptions } from "./backup.helpers";
import type { ResticBackupOutputDto } from "~/schemas/restic-dto";
const runningBackups = new Map<number, AbortController>(); const runningBackups = new Map<number, AbortController>();
@ -131,7 +132,7 @@ const runBackupOperation = async (ctx: BackupContext, signal: AbortSignal) => {
} }
}; };
const buildBackupSummary = (result: BackupOutput | null | undefined) => { const buildBackupSummary = (result: ResticBackupOutputDto | null | undefined) => {
if (!result) return undefined; if (!result) return undefined;
return result; return result;
}; };
@ -140,7 +141,7 @@ const finalizeSuccessfulBackup = async (
ctx: BackupContext, ctx: BackupContext,
scheduleId: number, scheduleId: number,
exitCode: number, exitCode: number,
result: BackupOutput | null, result: ResticBackupOutputDto | null,
) => { ) => {
const finalStatus = exitCode === 0 ? "success" : "warning"; const finalStatus = exitCode === 0 ? "success" : "warning";
const summary = buildBackupSummary(result); const summary = buildBackupSummary(result);

View file

@ -4,6 +4,11 @@ import { logger } from "../../utils/logger";
import { serverEvents } from "../../core/events"; import { serverEvents } from "../../core/events";
import { requireAuth } from "../auth/auth.middleware"; import { requireAuth } from "../auth/auth.middleware";
import type { DoctorResult } from "~/schemas/restic"; import type { DoctorResult } from "~/schemas/restic";
import type {
ServerBackupCompletedEventDto,
ServerBackupProgressEventDto,
ServerBackupStartedEventDto,
} from "~/schemas/events-dto";
export const eventsController = new Hono().use(requireAuth).get("/", (c) => { export const eventsController = new Hono().use(requireAuth).get("/", (c) => {
logger.info("Client connected to SSE endpoint"); logger.info("Client connected to SSE endpoint");
@ -15,12 +20,7 @@ export const eventsController = new Hono().use(requireAuth).get("/", (c) => {
event: "connected", event: "connected",
}); });
const onBackupStarted = async (data: { const onBackupStarted = async (data: ServerBackupStartedEventDto) => {
organizationId: string;
scheduleId: number;
volumeName: string;
repositoryName: string;
}) => {
if (data.organizationId !== organizationId) return; if (data.organizationId !== organizationId) return;
await stream.writeSSE({ await stream.writeSSE({
data: JSON.stringify(data), data: JSON.stringify(data),
@ -28,19 +28,7 @@ export const eventsController = new Hono().use(requireAuth).get("/", (c) => {
}); });
}; };
const onBackupProgress = async (data: { const onBackupProgress = async (data: ServerBackupProgressEventDto) => {
organizationId: string;
scheduleId: number;
volumeName: string;
repositoryName: string;
seconds_elapsed: number;
percent_done: number;
total_files: number;
files_done: number;
total_bytes: number;
bytes_done: number;
current_files: string[];
}) => {
if (data.organizationId !== organizationId) return; if (data.organizationId !== organizationId) return;
await stream.writeSSE({ await stream.writeSSE({
data: JSON.stringify(data), data: JSON.stringify(data),
@ -48,29 +36,7 @@ export const eventsController = new Hono().use(requireAuth).get("/", (c) => {
}); });
}; };
const onBackupCompleted = async (data: { const onBackupCompleted = async (data: ServerBackupCompletedEventDto) => {
organizationId: string;
scheduleId: number;
volumeName: string;
repositoryName: string;
status: "success" | "error" | "stopped" | "warning";
summary?: {
files_new: number;
files_changed: number;
files_unmodified: number;
dirs_new: number;
dirs_changed: number;
dirs_unmodified: number;
data_blobs: number;
tree_blobs: number;
data_added: number;
data_added_packed?: number;
total_files_processed: number;
total_bytes_processed: number;
total_duration: number;
snapshot_id: string;
};
}) => {
if (data.organizationId !== organizationId) return; if (data.organizationId !== organizationId) return;
await stream.writeSSE({ await stream.writeSSE({
data: JSON.stringify(data), data: JSON.stringify(data),

View file

@ -9,10 +9,10 @@ import {
import { cryptoUtils } from "../../utils/crypto"; import { cryptoUtils } from "../../utils/crypto";
import { logger } from "../../utils/logger"; import { logger } from "../../utils/logger";
import { sendNotification } from "../../utils/shoutrrr"; import { sendNotification } from "../../utils/shoutrrr";
import type { BackupOutput } from "../../utils/restic";
import { formatDuration } from "~/utils/utils"; import { formatDuration } from "~/utils/utils";
import { buildShoutrrrUrl } from "./builders"; import { buildShoutrrrUrl } from "./builders";
import { notificationConfigSchema, type NotificationConfig, type NotificationEvent } from "~/schemas/notifications"; import { notificationConfigSchema, type NotificationConfig, type NotificationEvent } from "~/schemas/notifications";
import type { ResticBackupRunSummaryDto } from "~/schemas/restic-dto";
import { toMessage } from "../../utils/errors"; import { toMessage } from "../../utils/errors";
import { type } from "arktype"; import { type } from "arktype";
import { getOrganizationId } from "~/server/core/request-context"; import { getOrganizationId } from "~/server/core/request-context";
@ -318,8 +318,6 @@ const updateScheduleNotifications = async (
return getScheduleNotifications(scheduleId); return getScheduleNotifications(scheduleId);
}; };
type BackupSummary = Omit<BackupOutput, "message_type">;
const formatBytesText = (bytes: number) => { const formatBytesText = (bytes: number) => {
const { text, unit } = formatBytes(bytes, { const { text, unit } = formatBytes(bytes, {
base: 1024, base: 1024,
@ -330,7 +328,7 @@ const formatBytesText = (bytes: number) => {
return unit ? `${text} ${unit}` : text; return unit ? `${text} ${unit}` : text;
}; };
const buildBackupSummaryLines = (summary?: BackupSummary) => { const buildBackupSummaryLines = (summary?: ResticBackupRunSummaryDto) => {
if (!summary) return []; if (!summary) return [];
const safeNumber = (value: number | undefined) => (typeof value === "number" && Number.isFinite(value) ? value : 0); const safeNumber = (value: number | undefined) => (typeof value === "number" && Number.isFinite(value) ? value : 0);
@ -374,7 +372,7 @@ const sendBackupNotification = async (
filesProcessed?: number; filesProcessed?: number;
bytesProcessed?: string; bytesProcessed?: string;
snapshotId?: string; snapshotId?: string;
summary?: BackupSummary; summary?: ResticBackupRunSummaryDto;
}, },
) => { ) => {
try { try {
@ -454,7 +452,7 @@ function buildNotificationMessage(
filesProcessed?: number; filesProcessed?: number;
bytesProcessed?: string; bytesProcessed?: string;
snapshotId?: string; snapshotId?: string;
summary?: BackupSummary; summary?: ResticBackupRunSummaryDto;
}, },
) { ) {
const backupName = context.scheduleName ?? "backup"; const backupName = context.scheduleName ?? "backup";

View file

@ -8,6 +8,7 @@ import {
repositoryConfigSchema, repositoryConfigSchema,
doctorResultSchema, doctorResultSchema,
} from "~/schemas/restic"; } from "~/schemas/restic";
import { resticSnapshotSummarySchema } from "~/schemas/restic-dto";
export const repositorySchema = type({ export const repositorySchema = type({
id: "string", id: "string",
@ -180,22 +181,7 @@ export const snapshotSchema = type({
tags: "string[]", tags: "string[]",
retentionCategories: "string[]", retentionCategories: "string[]",
hostname: "string?", hostname: "string?",
summary: type({ summary: resticSnapshotSummarySchema.optional(),
backup_start: "string",
backup_end: "string",
files_new: "number",
files_changed: "number",
files_unmodified: "number",
dirs_new: "number",
dirs_changed: "number",
dirs_unmodified: "number",
data_blobs: "number",
tree_blobs: "number",
data_added: "number",
data_added_packed: "number?",
total_files_processed: "number",
total_bytes_processed: "number",
}).optional(),
}); });
const listSnapshotsResponse = snapshotSchema.array(); const listSnapshotsResponse = snapshotSchema.array();

View file

@ -11,29 +11,19 @@ import { cryptoUtils } from "./crypto";
import type { RetentionPolicy } from "../modules/backups/backups.dto"; import type { RetentionPolicy } from "../modules/backups/backups.dto";
import { safeSpawn, exec } from "./spawn"; import { safeSpawn, exec } from "./spawn";
import type { CompressionMode, RepositoryConfig, OverwriteMode, BandwidthLimit } from "~/schemas/restic"; import type { CompressionMode, RepositoryConfig, OverwriteMode, BandwidthLimit } from "~/schemas/restic";
import {
resticBackupOutputSchema,
resticBackupProgressSchema,
resticRestoreOutputSchema,
resticSnapshotSummarySchema,
type ResticBackupProgressDto,
type ResticRestoreOutputDto,
type ResticSnapshotSummaryDto,
} from "~/schemas/restic-dto";
import { ResticError } from "./errors"; import { ResticError } from "./errors";
import { db } from "../db/db"; import { db } from "../db/db";
import { safeJsonParse } from "./json"; import { safeJsonParse } from "./json";
const backupOutputSchema = type({
message_type: "'summary'",
files_new: "number",
files_changed: "number",
files_unmodified: "number",
dirs_new: "number",
dirs_changed: "number",
dirs_unmodified: "number",
data_blobs: "number",
tree_blobs: "number",
data_added: "number",
data_added_packed: "number?",
total_files_processed: "number",
total_bytes_processed: "number",
total_duration: "number",
snapshot_id: "string",
});
export type BackupOutput = typeof backupOutputSchema.infer;
const snapshotInfoSchema = type({ const snapshotInfoSchema = type({
gid: "number?", gid: "number?",
hostname: "string", hostname: "string",
@ -46,22 +36,7 @@ const snapshotInfoSchema = type({
uid: "number?", uid: "number?",
username: "string?", username: "string?",
tags: "string[]?", tags: "string[]?",
summary: type({ summary: resticSnapshotSummarySchema.optional(),
backup_end: "string",
backup_start: "string",
data_added: "number",
data_added_packed: "number",
data_blobs: "number",
dirs_changed: "number",
dirs_new: "number",
dirs_unmodified: "number",
files_changed: "number",
files_new: "number",
files_unmodified: "number",
total_bytes_processed: "number",
total_files_processed: "number",
tree_blobs: "number",
}).optional(),
}); });
export const buildRepoUrl = (config: RepositoryConfig): string => { export const buildRepoUrl = (config: RepositoryConfig): string => {
@ -268,19 +243,6 @@ const init = async (config: RepositoryConfig, organizationId: string, options?:
return { success: true, error: null }; return { success: true, error: null };
}; };
const backupProgressSchema = type({
message_type: "'status'",
seconds_elapsed: "number",
percent_done: "number",
total_files: "number",
files_done: "number",
total_bytes: "number",
bytes_done: "number",
current_files: "string[]",
});
export type BackupProgress = typeof backupProgressSchema.infer;
const backup = async ( const backup = async (
config: RepositoryConfig, config: RepositoryConfig,
source: string, source: string,
@ -293,7 +255,7 @@ const backup = async (
oneFileSystem?: boolean; oneFileSystem?: boolean;
compressionMode?: CompressionMode; compressionMode?: CompressionMode;
signal?: AbortSignal; signal?: AbortSignal;
onProgress?: (progress: BackupProgress) => void; onProgress?: (progress: ResticBackupProgressDto) => void;
}, },
) => { ) => {
const repoUrl = buildRepoUrl(config); const repoUrl = buildRepoUrl(config);
@ -357,7 +319,7 @@ const backup = async (
if (options?.onProgress) { if (options?.onProgress) {
try { try {
const jsonData = JSON.parse(data); const jsonData = JSON.parse(data);
const progress = backupProgressSchema(jsonData); const progress = resticBackupProgressSchema(jsonData);
if (!(progress instanceof type.errors)) { if (!(progress instanceof type.errors)) {
options.onProgress(progress); options.onProgress(progress);
} }
@ -412,7 +374,7 @@ const backup = async (
} }
logger.debug(`Restic backup output last line: ${summaryLine}`); logger.debug(`Restic backup output last line: ${summaryLine}`);
const result = backupOutputSchema(summaryLine); const result = resticBackupOutputSchema(summaryLine);
if (result instanceof type.errors) { if (result instanceof type.errors) {
logger.error(`Restic backup output validation failed: ${result.summary}`); logger.error(`Restic backup output validation failed: ${result.summary}`);
@ -422,16 +384,6 @@ const backup = async (
return { result, exitCode: res.exitCode }; return { result, exitCode: res.exitCode };
}; };
const restoreOutputSchema = type({
message_type: "'summary'",
total_files: "number?",
files_restored: "number",
files_skipped: "number",
total_bytes: "number?",
bytes_restored: "number?",
bytes_skipped: "number",
});
const restore = async ( const restore = async (
config: RepositoryConfig, config: RepositoryConfig,
snapshotId: string, snapshotId: string,
@ -499,18 +451,20 @@ const restore = async (
} }
logger.debug(`Restic restore output last line: ${summaryLine}`); logger.debug(`Restic restore output last line: ${summaryLine}`);
const result = restoreOutputSchema(summaryLine); const result = resticRestoreOutputSchema(summaryLine);
if (result instanceof type.errors) { if (result instanceof type.errors) {
logger.warn(`Restic restore output validation failed: ${result.summary}`); logger.warn(`Restic restore output validation failed: ${result.summary}`);
logger.info(`Restic restore completed for snapshot ${snapshotId} to target ${target}`); logger.info(`Restic restore completed for snapshot ${snapshotId} to target ${target}`);
return { const fallback: ResticRestoreOutputDto = {
message_type: "summary" as const, message_type: "summary" as const,
total_files: 0, total_files: 0,
files_restored: 0, files_restored: 0,
files_skipped: 0, files_skipped: 0,
bytes_skipped: 0, bytes_skipped: 0,
}; };
return fallback;
} }
logger.info( logger.info(
@ -577,28 +531,11 @@ export interface Snapshot {
excludes?: string[] | null; excludes?: string[] | null;
tags?: string[] | null; tags?: string[] | null;
program_version?: string; program_version?: string;
summary?: SnapshotSummary; summary?: ResticSnapshotSummaryDto;
id: string; id: string;
short_id: string; short_id: string;
} }
export interface SnapshotSummary {
backup_start: string;
backup_end: string;
files_new: number;
files_changed: number;
files_unmodified: number;
dirs_new: number;
dirs_changed: number;
dirs_unmodified: number;
data_blobs: number;
tree_blobs: number;
data_added: number;
data_added_packed: number;
total_files_processed: number;
total_bytes_processed: number;
}
export interface ForgetReason { export interface ForgetReason {
snapshot: Snapshot; snapshot: Snapshot;
matches: string[]; matches: string[];