refactor: centralize sse event types
This commit is contained in:
parent
a91f994966
commit
00ddfe7f30
11 changed files with 284 additions and 746 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useRef, useState } from "react";
|
||||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { ChevronDown, FileIcon, FolderOpen, RotateCcw } from "lucide-react";
|
import { ChevronDown, FileIcon, FolderOpen, RotateCcw } from "lucide-react";
|
||||||
import { Button } from "~/client/components/ui/button";
|
import { Button } from "~/client/components/ui/button";
|
||||||
|
|
@ -20,9 +20,8 @@ import { FileTree } from "~/client/components/file-tree";
|
||||||
import { RestoreProgress } from "~/client/components/restore-progress";
|
import { RestoreProgress } from "~/client/components/restore-progress";
|
||||||
import { listSnapshotFilesOptions, restoreSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen";
|
import { listSnapshotFilesOptions, restoreSnapshotMutation } from "~/client/api-client/@tanstack/react-query.gen";
|
||||||
import { useFileBrowser } from "~/client/hooks/use-file-browser";
|
import { useFileBrowser } from "~/client/hooks/use-file-browser";
|
||||||
import { useServerEvents } from "~/client/hooks/use-server-events";
|
import { type RestoreCompletedEvent, useServerEvents } from "~/client/hooks/use-server-events";
|
||||||
import { OVERWRITE_MODES, type OverwriteMode } from "~/schemas/restic";
|
import { OVERWRITE_MODES, type OverwriteMode } from "~/schemas/restic";
|
||||||
import type { RestoreCompletedEventDto, RestoreProgressEventDto } from "~/schemas/events-dto";
|
|
||||||
import type { Repository, Snapshot } from "~/client/lib/types";
|
import type { Repository, Snapshot } from "~/client/lib/types";
|
||||||
import { handleRepositoryError } from "~/client/lib/errors";
|
import { handleRepositoryError } from "~/client/lib/errors";
|
||||||
import { useNavigate } from "@tanstack/react-router";
|
import { useNavigate } from "@tanstack/react-router";
|
||||||
|
|
@ -49,34 +48,34 @@ export function RestoreForm({ snapshot, repository, snapshotId, returnPath }: Re
|
||||||
const [showAdvanced, setShowAdvanced] = useState(false);
|
const [showAdvanced, setShowAdvanced] = useState(false);
|
||||||
const [excludeXattr, setExcludeXattr] = useState("");
|
const [excludeXattr, setExcludeXattr] = useState("");
|
||||||
const [isRestoreActive, setIsRestoreActive] = useState(false);
|
const [isRestoreActive, setIsRestoreActive] = useState(false);
|
||||||
const [restoreResult, setRestoreResult] = useState<RestoreCompletedEventDto | null>(null);
|
const [restoreResult, setRestoreResult] = useState<RestoreCompletedEvent | null>(null);
|
||||||
const [showRestoreResultAlert, setShowRestoreResultAlert] = useState(false);
|
const [showRestoreResultAlert, setShowRestoreResultAlert] = useState(false);
|
||||||
|
const restoreCompletedRef = useRef(false);
|
||||||
|
|
||||||
const [selectedPaths, setSelectedPaths] = useState<Set<string>>(new Set());
|
const [selectedPaths, setSelectedPaths] = useState<Set<string>>(new Set());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribeStarted = addEventListener("restore:started", (data) => {
|
const unsubscribeStarted = addEventListener("restore:started", (startedData) => {
|
||||||
const startedData = data as {
|
|
||||||
repositoryId: string;
|
|
||||||
snapshotId: string;
|
|
||||||
};
|
|
||||||
if (startedData.repositoryId === repository.id && startedData.snapshotId === snapshotId) {
|
if (startedData.repositoryId === repository.id && startedData.snapshotId === snapshotId) {
|
||||||
|
restoreCompletedRef.current = false;
|
||||||
setIsRestoreActive(true);
|
setIsRestoreActive(true);
|
||||||
setRestoreResult(null);
|
setRestoreResult(null);
|
||||||
setShowRestoreResultAlert(false);
|
setShowRestoreResultAlert(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsubscribeProgress = addEventListener("restore:progress", (data) => {
|
const unsubscribeProgress = addEventListener("restore:progress", (progressData) => {
|
||||||
const progressData = data as RestoreProgressEventDto;
|
|
||||||
if (progressData.repositoryId === repository.id && progressData.snapshotId === snapshotId) {
|
if (progressData.repositoryId === repository.id && progressData.snapshotId === snapshotId) {
|
||||||
|
if (restoreCompletedRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
setIsRestoreActive(true);
|
setIsRestoreActive(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsubscribeCompleted = addEventListener("restore:completed", (data) => {
|
const unsubscribeCompleted = addEventListener("restore:completed", (completedData) => {
|
||||||
const completedData = data as RestoreCompletedEventDto;
|
|
||||||
if (completedData.repositoryId === repository.id && completedData.snapshotId === snapshotId) {
|
if (completedData.repositoryId === repository.id && completedData.snapshotId === snapshotId) {
|
||||||
|
restoreCompletedRef.current = true;
|
||||||
setIsRestoreActive(false);
|
setIsRestoreActive(false);
|
||||||
setRestoreResult(completedData);
|
setRestoreResult(completedData);
|
||||||
setShowRestoreResultAlert(true);
|
setShowRestoreResultAlert(true);
|
||||||
|
|
@ -149,6 +148,7 @@ export function RestoreForm({ snapshot, repository, snapshotId, returnPath }: Re
|
||||||
const { mutate: restoreSnapshot, isPending: isRestoring } = useMutation({
|
const { mutate: restoreSnapshot, isPending: isRestoring } = useMutation({
|
||||||
...restoreSnapshotMutation(),
|
...restoreSnapshotMutation(),
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
restoreCompletedRef.current = true;
|
||||||
setIsRestoreActive(false);
|
setIsRestoreActive(false);
|
||||||
handleRepositoryError("Restore failed", error, repository.id);
|
handleRepositoryError("Restore failed", error, repository.id);
|
||||||
},
|
},
|
||||||
|
|
@ -166,6 +166,7 @@ export function RestoreForm({ snapshot, repository, snapshotId, returnPath }: Re
|
||||||
const pathsArray = Array.from(selectedPaths);
|
const pathsArray = Array.from(selectedPaths);
|
||||||
const includePaths = pathsArray.map((path) => addBasePath(path));
|
const includePaths = pathsArray.map((path) => addBasePath(path));
|
||||||
|
|
||||||
|
restoreCompletedRef.current = false;
|
||||||
setIsRestoreActive(true);
|
setIsRestoreActive(true);
|
||||||
setRestoreResult(null);
|
setRestoreResult(null);
|
||||||
setShowRestoreResultAlert(false);
|
setShowRestoreResultAlert(false);
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,7 @@ 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 { useServerEvents } from "~/client/hooks/use-server-events";
|
import { type RestoreProgressEvent, useServerEvents } from "~/client/hooks/use-server-events";
|
||||||
import type { RestoreCompletedEventDto, RestoreProgressEventDto } from "~/schemas/events-dto";
|
|
||||||
import { formatBytes } from "~/utils/format-bytes";
|
import { formatBytes } from "~/utils/format-bytes";
|
||||||
import { formatDuration } from "~/utils/utils";
|
import { formatDuration } from "~/utils/utils";
|
||||||
|
|
||||||
|
|
@ -14,18 +13,16 @@ type Props = {
|
||||||
|
|
||||||
export const RestoreProgress = ({ repositoryId, snapshotId }: Props) => {
|
export const RestoreProgress = ({ repositoryId, snapshotId }: Props) => {
|
||||||
const { addEventListener } = useServerEvents();
|
const { addEventListener } = useServerEvents();
|
||||||
const [progress, setProgress] = useState<RestoreProgressEventDto | null>(null);
|
const [progress, setProgress] = useState<RestoreProgressEvent | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = addEventListener("restore:progress", (data) => {
|
const unsubscribe = addEventListener("restore:progress", (progressData) => {
|
||||||
const progressData = data as RestoreProgressEventDto;
|
|
||||||
if (progressData.repositoryId === repositoryId && progressData.snapshotId === snapshotId) {
|
if (progressData.repositoryId === repositoryId && progressData.snapshotId === snapshotId) {
|
||||||
setProgress(progressData);
|
setProgress(progressData);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsubscribeComplete = addEventListener("restore:completed", (data) => {
|
const unsubscribeComplete = addEventListener("restore:completed", (completedData) => {
|
||||||
const completedData = data as RestoreCompletedEventDto;
|
|
||||||
if (completedData.repositoryId === repositoryId && completedData.snapshotId === snapshotId) {
|
if (completedData.repositoryId === repositoryId && completedData.snapshotId === snapshotId) {
|
||||||
setProgress(null);
|
setProgress(null);
|
||||||
}
|
}
|
||||||
|
|
@ -49,7 +46,7 @@ export const RestoreProgress = ({ repositoryId, snapshotId }: Props) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const percentDone = Math.round(progress.percent_done * 100);
|
const percentDone = Math.round(progress.percent_done * 100);
|
||||||
const speed = progress.seconds_elapsed > 0 ? formatBytes(progress.bytes_done / progress.seconds_elapsed) : null;
|
const speed = progress.seconds_elapsed > 0 ? formatBytes(progress.bytes_restored / progress.seconds_elapsed) : null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card className="p-4">
|
<Card className="p-4">
|
||||||
|
|
@ -67,13 +64,17 @@ export const RestoreProgress = ({ repositoryId, snapshotId }: Props) => {
|
||||||
<div>
|
<div>
|
||||||
<p className="text-xs uppercase text-muted-foreground">Files</p>
|
<p className="text-xs uppercase text-muted-foreground">Files</p>
|
||||||
<p className="font-medium">
|
<p className="font-medium">
|
||||||
{progress.files_done.toLocaleString()} / {progress.total_files.toLocaleString()}
|
{progress.files_restored.toLocaleString()}
|
||||||
|
/
|
||||||
|
{progress.total_files.toLocaleString()}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<p className="text-xs uppercase text-muted-foreground">Data</p>
|
<p className="text-xs uppercase text-muted-foreground">Data</p>
|
||||||
<p className="font-medium">
|
<p className="font-medium">
|
||||||
<ByteSize bytes={progress.bytes_done} /> / <ByteSize bytes={progress.total_bytes} />
|
<ByteSize bytes={progress.bytes_restored} base={1024} />
|
||||||
|
/
|
||||||
|
<ByteSize bytes={progress.total_bytes} base={1024} />
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
|
|
|
||||||
|
|
@ -1,62 +1,38 @@
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
import { useCallback, useEffect, useRef } from "react";
|
import { useCallback, useEffect, useRef } from "react";
|
||||||
import type {
|
import { serverEventNames, type ServerEventPayloadMap } from "~/schemas/server-events";
|
||||||
BackupCompletedEventDto,
|
|
||||||
BackupProgressEventDto,
|
|
||||||
BackupStartedEventDto,
|
|
||||||
RestoreCompletedEventDto,
|
|
||||||
RestoreProgressEventDto,
|
|
||||||
RestoreStartedEventDto,
|
|
||||||
} from "~/schemas/events-dto";
|
|
||||||
|
|
||||||
type ServerEventType =
|
type LifecycleEventPayloadMap = {
|
||||||
| "connected"
|
connected: { type: "connected"; timestamp: number };
|
||||||
| "heartbeat"
|
heartbeat: { timestamp: number };
|
||||||
| "backup:started"
|
};
|
||||||
| "backup:progress"
|
|
||||||
| "backup:completed"
|
|
||||||
| "volume:mounted"
|
|
||||||
| "volume:unmounted"
|
|
||||||
| "volume:updated"
|
|
||||||
| "mirror:started"
|
|
||||||
| "mirror:completed"
|
|
||||||
| "restore:started"
|
|
||||||
| "restore:progress"
|
|
||||||
| "restore:completed"
|
|
||||||
| "doctor:started"
|
|
||||||
| "doctor:completed"
|
|
||||||
| "doctor:cancelled";
|
|
||||||
|
|
||||||
export interface VolumeEvent {
|
type ServerEventsPayloadMap = LifecycleEventPayloadMap & ServerEventPayloadMap;
|
||||||
volumeName: string;
|
type ServerEventType = keyof ServerEventsPayloadMap;
|
||||||
}
|
|
||||||
|
|
||||||
export interface MirrorEvent {
|
type EventHandler<T extends ServerEventType> = (data: ServerEventsPayloadMap[T]) => void;
|
||||||
scheduleId: number;
|
type EventHandlerSet<T extends ServerEventType> = Set<EventHandler<T>>;
|
||||||
repositoryId: string;
|
type EventHandlerMap = {
|
||||||
repositoryName: string;
|
[K in ServerEventType]?: EventHandlerSet<K>;
|
||||||
status?: "success" | "error" | "in_progress";
|
};
|
||||||
error?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface DoctorEvent {
|
const invalidatingEvents = new Set<ServerEventType>([
|
||||||
repositoryId: string;
|
"backup:completed",
|
||||||
repositoryName: string;
|
"volume:updated",
|
||||||
error?: string;
|
"volume:status_changed",
|
||||||
}
|
"mirror:completed",
|
||||||
|
"doctor:started",
|
||||||
|
"doctor:completed",
|
||||||
|
"doctor:cancelled",
|
||||||
|
]);
|
||||||
|
|
||||||
export interface DoctorCompletedEvent extends DoctorEvent {
|
export type RestoreEvent = ServerEventsPayloadMap["restore:started"] | ServerEventsPayloadMap["restore:completed"];
|
||||||
success: boolean;
|
export type RestoreProgressEvent = ServerEventsPayloadMap["restore:progress"];
|
||||||
completedAt: number;
|
export type RestoreCompletedEvent = ServerEventsPayloadMap["restore:completed"];
|
||||||
steps: Array<{
|
export type BackupProgressEvent = ServerEventsPayloadMap["backup:progress"];
|
||||||
step: string;
|
|
||||||
success: boolean;
|
|
||||||
output: string | null;
|
|
||||||
error: string | null;
|
|
||||||
}>;
|
|
||||||
}
|
|
||||||
|
|
||||||
type EventHandler = (data: unknown) => void;
|
const parseEventData = <T extends ServerEventType>(event: Event): ServerEventsPayloadMap[T] =>
|
||||||
|
JSON.parse((event as MessageEvent<string>).data) as ServerEventsPayloadMap[T];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to listen to Server-Sent Events (SSE) from the backend
|
* Hook to listen to Server-Sent Events (SSE) from the backend
|
||||||
|
|
@ -65,166 +41,51 @@ type EventHandler = (data: unknown) => void;
|
||||||
export function useServerEvents() {
|
export function useServerEvents() {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const eventSourceRef = useRef<EventSource | null>(null);
|
const eventSourceRef = useRef<EventSource | null>(null);
|
||||||
const handlersRef = useRef<Map<ServerEventType, Set<EventHandler>>>(new Map());
|
const handlersRef = useRef<EventHandlerMap>({});
|
||||||
|
const emit = useCallback(<T extends ServerEventType>(eventName: T, data: ServerEventsPayloadMap[T]) => {
|
||||||
|
const handlers = handlersRef.current[eventName] as EventHandlerSet<T> | undefined;
|
||||||
|
handlers?.forEach((handler) => {
|
||||||
|
handler(data);
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const eventSource = new EventSource("/api/v1/events");
|
const eventSource = new EventSource("/api/v1/events");
|
||||||
eventSourceRef.current = eventSource;
|
eventSourceRef.current = eventSource;
|
||||||
|
|
||||||
eventSource.addEventListener("connected", () => {
|
eventSource.addEventListener("connected", (event) => {
|
||||||
|
const data = parseEventData<"connected">(event);
|
||||||
console.info("[SSE] Connected to server events");
|
console.info("[SSE] Connected to server events");
|
||||||
|
emit("connected", data);
|
||||||
});
|
});
|
||||||
|
|
||||||
eventSource.addEventListener("heartbeat", () => {});
|
eventSource.addEventListener("heartbeat", (event) => {
|
||||||
|
emit("heartbeat", parseEventData<"heartbeat">(event));
|
||||||
|
});
|
||||||
|
|
||||||
eventSource.addEventListener("backup:started", (e) => {
|
for (const eventName of serverEventNames) {
|
||||||
const data = JSON.parse(e.data) as BackupStartedEventDto;
|
eventSource.addEventListener(eventName, (event) => {
|
||||||
console.info("[SSE] Backup started:", data);
|
const data = parseEventData<typeof eventName>(event);
|
||||||
|
console.info(`[SSE] ${eventName}:`, data);
|
||||||
|
|
||||||
handlersRef.current.get("backup:started")?.forEach((handler) => {
|
if (invalidatingEvents.has(eventName)) {
|
||||||
handler(data);
|
void queryClient.invalidateQueries();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eventName === "backup:completed") {
|
||||||
|
void queryClient.refetchQueries();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (eventName === "volume:status_changed") {
|
||||||
|
const statusData = data as ServerEventsPayloadMap["volume:status_changed"];
|
||||||
|
emit("volume:status_changed", statusData);
|
||||||
|
emit("volume:updated", statusData);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(eventName, data);
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
eventSource.addEventListener("backup:progress", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as BackupProgressEventDto;
|
|
||||||
|
|
||||||
handlersRef.current.get("backup:progress")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("backup:completed", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as BackupCompletedEventDto;
|
|
||||||
console.info("[SSE] Backup completed:", data);
|
|
||||||
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
void queryClient.refetchQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("backup:completed")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("volume:mounted", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as VolumeEvent;
|
|
||||||
console.info("[SSE] Volume mounted:", data);
|
|
||||||
|
|
||||||
handlersRef.current.get("volume:mounted")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("volume:unmounted", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as VolumeEvent;
|
|
||||||
console.info("[SSE] Volume unmounted:", data);
|
|
||||||
|
|
||||||
handlersRef.current.get("volume:unmounted")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("volume:updated", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as VolumeEvent;
|
|
||||||
console.info("[SSE] Volume updated:", data);
|
|
||||||
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("volume:updated")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("volume:status_changed", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as VolumeEvent;
|
|
||||||
console.info("[SSE] Volume status updated:", data);
|
|
||||||
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("volume:updated")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("mirror:started", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as MirrorEvent;
|
|
||||||
console.info("[SSE] Mirror copy started:", data);
|
|
||||||
|
|
||||||
handlersRef.current.get("mirror:started")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("mirror:completed", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as MirrorEvent;
|
|
||||||
console.info("[SSE] Mirror copy completed:", data);
|
|
||||||
|
|
||||||
// Invalidate queries to refresh mirror status in the UI
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("mirror:completed")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("restore:started", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as RestoreStartedEventDto;
|
|
||||||
console.info("[SSE] Restore started:", data);
|
|
||||||
|
|
||||||
handlersRef.current.get("restore:started")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("restore:progress", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as RestoreProgressEventDto;
|
|
||||||
|
|
||||||
handlersRef.current.get("restore:progress")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("restore:completed", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as RestoreCompletedEventDto;
|
|
||||||
console.info("[SSE] Restore completed:", data);
|
|
||||||
|
|
||||||
handlersRef.current.get("restore:completed")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("doctor:started", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as DoctorEvent;
|
|
||||||
console.info("[SSE] Doctor started:", data);
|
|
||||||
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("doctor:started")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("doctor:completed", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as DoctorCompletedEvent;
|
|
||||||
console.info("[SSE] Doctor completed:", data);
|
|
||||||
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("doctor:completed")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.addEventListener("doctor:cancelled", (e) => {
|
|
||||||
const data = JSON.parse(e.data) as DoctorEvent;
|
|
||||||
console.info("[SSE] Doctor cancelled:", data);
|
|
||||||
|
|
||||||
void queryClient.invalidateQueries();
|
|
||||||
|
|
||||||
handlersRef.current.get("doctor:cancelled")?.forEach((handler) => {
|
|
||||||
handler(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
eventSource.onerror = (error) => {
|
eventSource.onerror = (error) => {
|
||||||
console.error("[SSE] Connection error:", error);
|
console.error("[SSE] Connection error:", error);
|
||||||
|
|
@ -235,16 +96,20 @@ export function useServerEvents() {
|
||||||
eventSource.close();
|
eventSource.close();
|
||||||
eventSourceRef.current = null;
|
eventSourceRef.current = null;
|
||||||
};
|
};
|
||||||
}, [queryClient]);
|
}, [emit, queryClient]);
|
||||||
|
|
||||||
const addEventListener = useCallback((event: ServerEventType, handler: EventHandler) => {
|
const addEventListener = useCallback(<T extends ServerEventType>(eventName: T, handler: EventHandler<T>) => {
|
||||||
if (!handlersRef.current.has(event)) {
|
const existingHandlers = handlersRef.current[eventName] as EventHandlerSet<T> | undefined;
|
||||||
handlersRef.current.set(event, new Set());
|
const eventHandlers = existingHandlers ?? new Set<EventHandler<T>>();
|
||||||
}
|
eventHandlers.add(handler);
|
||||||
handlersRef.current.get(event)?.add(handler);
|
handlersRef.current[eventName] = eventHandlers as EventHandlerMap[T];
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
handlersRef.current.get(event)?.delete(handler);
|
const handlers = handlersRef.current[eventName] as EventHandlerSet<T> | undefined;
|
||||||
|
handlers?.delete(handler);
|
||||||
|
if (handlers && handlers.size === 0) {
|
||||||
|
delete handlersRef.current[eventName];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,7 @@ 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 { useServerEvents } from "~/client/hooks/use-server-events";
|
import { type BackupProgressEvent, 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";
|
||||||
|
|
||||||
|
|
@ -13,18 +12,16 @@ type Props = {
|
||||||
|
|
||||||
export const BackupProgressCard = ({ scheduleId }: Props) => {
|
export const BackupProgressCard = ({ scheduleId }: Props) => {
|
||||||
const { addEventListener } = useServerEvents();
|
const { addEventListener } = useServerEvents();
|
||||||
const [progress, setProgress] = useState<BackupProgressEventDto | null>(null);
|
const [progress, setProgress] = useState<BackupProgressEvent | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = addEventListener("backup:progress", (data) => {
|
const unsubscribe = addEventListener("backup:progress", (progressData) => {
|
||||||
const progressData = data as BackupProgressEventDto;
|
|
||||||
if (progressData.scheduleId === scheduleId) {
|
if (progressData.scheduleId === scheduleId) {
|
||||||
setProgress(progressData);
|
setProgress(progressData);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsubscribeComplete = addEventListener("backup:completed", (data) => {
|
const unsubscribeComplete = addEventListener("backup:completed", (completedData) => {
|
||||||
const completedData = data as { scheduleId: number };
|
|
||||||
if (completedData.scheduleId === scheduleId) {
|
if (completedData.scheduleId === scheduleId) {
|
||||||
setProgress(null);
|
setProgress(null);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -99,8 +99,7 @@ export const ScheduleMirrorsConfig = ({ scheduleId, primaryRepositoryId, reposit
|
||||||
}, [compatibility]);
|
}, [compatibility]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribeStarted = addEventListener("mirror:started", (data) => {
|
const unsubscribeStarted = addEventListener("mirror:started", (event) => {
|
||||||
const event = data as { scheduleId: number; repositoryId: string };
|
|
||||||
if (event.scheduleId !== scheduleId) return;
|
if (event.scheduleId !== scheduleId) return;
|
||||||
setAssignments((prev) => {
|
setAssignments((prev) => {
|
||||||
const next = new Map(prev);
|
const next = new Map(prev);
|
||||||
|
|
@ -111,8 +110,7 @@ export const ScheduleMirrorsConfig = ({ scheduleId, primaryRepositoryId, reposit
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const unsubscribeCompleted = addEventListener("mirror:completed", (data) => {
|
const unsubscribeCompleted = addEventListener("mirror:completed", (event) => {
|
||||||
const event = data as { scheduleId: number; repositoryId: string; status?: "success" | "error"; error?: string };
|
|
||||||
if (event.scheduleId !== scheduleId) return;
|
if (event.scheduleId !== scheduleId) return;
|
||||||
setAssignments((prev) => {
|
setAssignments((prev) => {
|
||||||
const next = new Map(prev);
|
const next = new Map(prev);
|
||||||
|
|
|
||||||
|
|
@ -23,9 +23,9 @@ const restoreProgressMetricsSchema = type({
|
||||||
seconds_elapsed: "number",
|
seconds_elapsed: "number",
|
||||||
percent_done: "number",
|
percent_done: "number",
|
||||||
total_files: "number",
|
total_files: "number",
|
||||||
files_done: "number",
|
files_restored: "number",
|
||||||
total_bytes: "number",
|
total_bytes: "number",
|
||||||
bytes_done: "number",
|
bytes_restored: "number",
|
||||||
});
|
});
|
||||||
|
|
||||||
export const backupStartedEventSchema = backupEventBaseSchema;
|
export const backupStartedEventSchema = backupEventBaseSchema;
|
||||||
|
|
@ -44,10 +44,7 @@ export const restoreStartedEventSchema = restoreEventBaseSchema;
|
||||||
export const restoreProgressEventSchema = restoreEventBaseSchema.and(restoreProgressMetricsSchema);
|
export const restoreProgressEventSchema = restoreEventBaseSchema.and(restoreProgressMetricsSchema);
|
||||||
|
|
||||||
export const restoreCompletedEventSchema = restoreEventBaseSchema.and(
|
export const restoreCompletedEventSchema = restoreEventBaseSchema.and(
|
||||||
type({
|
type({ status: restoreEventStatusSchema, error: "string?" }),
|
||||||
status: restoreEventStatusSchema,
|
|
||||||
error: "string?",
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
export const serverBackupStartedEventSchema = organizationScopedSchema.and(backupStartedEventSchema);
|
export const serverBackupStartedEventSchema = organizationScopedSchema.and(backupStartedEventSchema);
|
||||||
|
|
|
||||||
64
app/schemas/server-events.ts
Normal file
64
app/schemas/server-events.ts
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
import type {
|
||||||
|
ServerBackupCompletedEventDto,
|
||||||
|
ServerBackupProgressEventDto,
|
||||||
|
ServerBackupStartedEventDto,
|
||||||
|
ServerRestoreCompletedEventDto,
|
||||||
|
ServerRestoreProgressEventDto,
|
||||||
|
ServerRestoreStartedEventDto,
|
||||||
|
} from "~/schemas/events-dto";
|
||||||
|
import type { DoctorResult } from "~/schemas/restic";
|
||||||
|
|
||||||
|
const payload = <T>() => undefined as unknown as T;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single runtime registry for all broadcastable server events.
|
||||||
|
* Used as source-of-truth for both event names and payload typing.
|
||||||
|
*/
|
||||||
|
export const serverEventPayloads = {
|
||||||
|
"backup:started": payload<ServerBackupStartedEventDto>(),
|
||||||
|
"backup:progress": payload<ServerBackupProgressEventDto>(),
|
||||||
|
"backup:completed": payload<ServerBackupCompletedEventDto>(),
|
||||||
|
"restore:started": payload<ServerRestoreStartedEventDto>(),
|
||||||
|
"restore:progress": payload<ServerRestoreProgressEventDto>(),
|
||||||
|
"restore:completed": payload<ServerRestoreCompletedEventDto>(),
|
||||||
|
"mirror:started": payload<{
|
||||||
|
organizationId: string;
|
||||||
|
scheduleId: number;
|
||||||
|
repositoryId: string;
|
||||||
|
repositoryName: string;
|
||||||
|
}>(),
|
||||||
|
"mirror:completed": payload<{
|
||||||
|
organizationId: string;
|
||||||
|
scheduleId: number;
|
||||||
|
repositoryId: string;
|
||||||
|
repositoryName: string;
|
||||||
|
status: "success" | "error";
|
||||||
|
error?: string;
|
||||||
|
}>(),
|
||||||
|
"volume:mounted": payload<{ organizationId: string; volumeName: string }>(),
|
||||||
|
"volume:unmounted": payload<{ organizationId: string; volumeName: string }>(),
|
||||||
|
"volume:updated": payload<{ organizationId: string; volumeName: string }>(),
|
||||||
|
"volume:status_changed": payload<{ organizationId: string; volumeName: string; status: string }>(),
|
||||||
|
"doctor:started": payload<{ organizationId: string; repositoryId: string; repositoryName: string }>(),
|
||||||
|
"doctor:completed": payload<
|
||||||
|
{
|
||||||
|
organizationId: string;
|
||||||
|
repositoryId: string;
|
||||||
|
repositoryName: string;
|
||||||
|
} & DoctorResult
|
||||||
|
>(),
|
||||||
|
"doctor:cancelled": payload<{
|
||||||
|
organizationId: string;
|
||||||
|
repositoryId: string;
|
||||||
|
repositoryName: string;
|
||||||
|
error?: string;
|
||||||
|
}>(),
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export type ServerEventPayloadMap = typeof serverEventPayloads;
|
||||||
|
|
||||||
|
export type ServerEventHandlers = {
|
||||||
|
[K in keyof ServerEventPayloadMap]: (data: ServerEventPayloadMap[K]) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const serverEventNames = Object.keys(serverEventPayloads) as Array<keyof ServerEventPayloadMap>;
|
||||||
|
|
@ -1,61 +1,10 @@
|
||||||
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 {
|
import type { ServerEventHandlers } from "~/schemas/server-events";
|
||||||
ServerBackupCompletedEventDto,
|
export type { ServerEventHandlers, ServerEventPayloadMap } from "~/schemas/server-events";
|
||||||
ServerBackupProgressEventDto,
|
|
||||||
ServerBackupStartedEventDto,
|
|
||||||
ServerRestoreCompletedEventDto,
|
|
||||||
ServerRestoreProgressEventDto,
|
|
||||||
ServerRestoreStartedEventDto,
|
|
||||||
} from "~/schemas/events-dto";
|
|
||||||
import type { DoctorResult } from "~/schemas/restic";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event payloads for the SSE system
|
|
||||||
*/
|
|
||||||
interface ServerEvents {
|
|
||||||
"backup:started": (data: ServerBackupStartedEventDto) => void;
|
|
||||||
"backup:progress": (data: ServerBackupProgressEventDto) => void;
|
|
||||||
"backup:completed": (data: ServerBackupCompletedEventDto) => void;
|
|
||||||
"restore:started": (data: ServerRestoreStartedEventDto) => void;
|
|
||||||
"restore:progress": (data: ServerRestoreProgressEventDto) => void;
|
|
||||||
"restore:completed": (data: ServerRestoreCompletedEventDto) => void;
|
|
||||||
"mirror:started": (data: {
|
|
||||||
organizationId: string;
|
|
||||||
scheduleId: number;
|
|
||||||
repositoryId: string;
|
|
||||||
repositoryName: string;
|
|
||||||
}) => void;
|
|
||||||
"mirror:completed": (data: {
|
|
||||||
organizationId: string;
|
|
||||||
scheduleId: number;
|
|
||||||
repositoryId: string;
|
|
||||||
repositoryName: string;
|
|
||||||
status: "success" | "error";
|
|
||||||
error?: string;
|
|
||||||
}) => void;
|
|
||||||
"volume:mounted": (data: { organizationId: string; volumeName: string }) => void;
|
|
||||||
"volume:unmounted": (data: { organizationId: string; volumeName: string }) => void;
|
|
||||||
"volume:updated": (data: { organizationId: string; volumeName: string }) => void;
|
|
||||||
"volume:status_changed": (data: { organizationId: string; volumeName: string; status: string }) => void;
|
|
||||||
"doctor:started": (data: { organizationId: string; repositoryId: string; repositoryName: string }) => void;
|
|
||||||
"doctor:completed": (
|
|
||||||
data: {
|
|
||||||
organizationId: string;
|
|
||||||
repositoryId: string;
|
|
||||||
repositoryName: string;
|
|
||||||
} & DoctorResult,
|
|
||||||
) => void;
|
|
||||||
"doctor:cancelled": (data: {
|
|
||||||
organizationId: string;
|
|
||||||
repositoryId: string;
|
|
||||||
repositoryName: string;
|
|
||||||
error?: string;
|
|
||||||
}) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Global event emitter for server-side events
|
* Global event emitter for server-side events
|
||||||
* Use this to emit events that should be broadcasted to connected clients via SSE
|
* Use this to emit events that should be broadcasted to connected clients via SSE
|
||||||
*/
|
*/
|
||||||
export const serverEvents = new EventEmitter() as TypedEmitter<ServerEvents>;
|
export const serverEvents = new EventEmitter() as TypedEmitter<ServerEventHandlers>;
|
||||||
|
|
|
||||||
|
|
@ -143,8 +143,6 @@ export const repositoriesController = new Hono()
|
||||||
summary: snapshot.summary,
|
summary: snapshot.summary,
|
||||||
};
|
};
|
||||||
|
|
||||||
c.header("Cache-Control", "max-age=300, stale-while-revalidate=600");
|
|
||||||
|
|
||||||
return c.json<GetSnapshotDetailsDto>(response, 200);
|
return c.json<GetSnapshotDetailsDto>(response, 200);
|
||||||
})
|
})
|
||||||
.get(
|
.get(
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,7 @@ import {
|
||||||
import { serverEvents } from "~/server/core/events";
|
import { serverEvents } from "~/server/core/events";
|
||||||
import { getOrganizationId } from "~/server/core/request-context";
|
import { getOrganizationId } from "~/server/core/request-context";
|
||||||
import { logger } from "~/server/utils/logger";
|
import { logger } from "~/server/utils/logger";
|
||||||
import {
|
import { parseRetentionCategories, type RetentionCategory } from "~/server/utils/retention-categories";
|
||||||
parseRetentionCategories,
|
|
||||||
type RetentionCategory,
|
|
||||||
} from "~/server/utils/retention-categories";
|
|
||||||
import { repoMutex } from "../../core/repository-mutex";
|
import { repoMutex } from "../../core/repository-mutex";
|
||||||
import { db } from "../../db/db";
|
import { db } from "../../db/db";
|
||||||
import { repositoriesTable } from "../../db/schema";
|
import { repositoriesTable } from "../../db/schema";
|
||||||
|
|
@ -27,13 +24,7 @@ import { cache } from "../../utils/cache";
|
||||||
import { cryptoUtils } from "../../utils/crypto";
|
import { cryptoUtils } from "../../utils/crypto";
|
||||||
import { toMessage } from "../../utils/errors";
|
import { toMessage } from "../../utils/errors";
|
||||||
import { generateShortId } from "../../utils/id";
|
import { generateShortId } from "../../utils/id";
|
||||||
import {
|
import { addCommonArgs, buildEnv, buildRepoUrl, cleanupTemporaryKeys, restic } from "../../utils/restic";
|
||||||
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 { UpdateRepositoryBody } from "./repositories.dto";
|
||||||
|
|
@ -45,31 +36,22 @@ const findRepository = async (idOrShortId: string) => {
|
||||||
const organizationId = getOrganizationId();
|
const organizationId = getOrganizationId();
|
||||||
return await db.query.repositoriesTable.findFirst({
|
return await db.query.repositoriesTable.findFirst({
|
||||||
where: {
|
where: {
|
||||||
AND: [
|
AND: [{ OR: [{ id: idOrShortId }, { shortId: idOrShortId }] }, { organizationId }],
|
||||||
{ OR: [{ id: idOrShortId }, { shortId: idOrShortId }] },
|
|
||||||
{ organizationId },
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const listRepositories = async () => {
|
const listRepositories = async () => {
|
||||||
const organizationId = getOrganizationId();
|
const organizationId = getOrganizationId();
|
||||||
const repositories = await db.query.repositoriesTable.findMany({
|
const repositories = await db.query.repositoriesTable.findMany({ where: { organizationId } });
|
||||||
where: { organizationId },
|
|
||||||
});
|
|
||||||
return repositories;
|
return repositories;
|
||||||
};
|
};
|
||||||
|
|
||||||
const encryptConfig = async (
|
const encryptConfig = async (config: RepositoryConfig): Promise<RepositoryConfig> => {
|
||||||
config: RepositoryConfig,
|
|
||||||
): Promise<RepositoryConfig> => {
|
|
||||||
const encryptedConfig: Record<string, unknown> = { ...config };
|
const encryptedConfig: Record<string, unknown> = { ...config };
|
||||||
|
|
||||||
if (config.customPassword) {
|
if (config.customPassword) {
|
||||||
encryptedConfig.customPassword = await cryptoUtils.sealSecret(
|
encryptedConfig.customPassword = await cryptoUtils.sealSecret(config.customPassword);
|
||||||
config.customPassword,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.cacert) {
|
if (config.cacert) {
|
||||||
|
|
@ -79,39 +61,25 @@ const encryptConfig = async (
|
||||||
switch (config.backend) {
|
switch (config.backend) {
|
||||||
case "s3":
|
case "s3":
|
||||||
case "r2":
|
case "r2":
|
||||||
encryptedConfig.accessKeyId = await cryptoUtils.sealSecret(
|
encryptedConfig.accessKeyId = await cryptoUtils.sealSecret(config.accessKeyId);
|
||||||
config.accessKeyId,
|
encryptedConfig.secretAccessKey = await cryptoUtils.sealSecret(config.secretAccessKey);
|
||||||
);
|
|
||||||
encryptedConfig.secretAccessKey = await cryptoUtils.sealSecret(
|
|
||||||
config.secretAccessKey,
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case "gcs":
|
case "gcs":
|
||||||
encryptedConfig.credentialsJson = await cryptoUtils.sealSecret(
|
encryptedConfig.credentialsJson = await cryptoUtils.sealSecret(config.credentialsJson);
|
||||||
config.credentialsJson,
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case "azure":
|
case "azure":
|
||||||
encryptedConfig.accountKey = await cryptoUtils.sealSecret(
|
encryptedConfig.accountKey = await cryptoUtils.sealSecret(config.accountKey);
|
||||||
config.accountKey,
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
case "rest":
|
case "rest":
|
||||||
if (config.username) {
|
if (config.username) {
|
||||||
encryptedConfig.username = await cryptoUtils.sealSecret(
|
encryptedConfig.username = await cryptoUtils.sealSecret(config.username);
|
||||||
config.username,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (config.password) {
|
if (config.password) {
|
||||||
encryptedConfig.password = await cryptoUtils.sealSecret(
|
encryptedConfig.password = await cryptoUtils.sealSecret(config.password);
|
||||||
config.password,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "sftp":
|
case "sftp":
|
||||||
encryptedConfig.privateKey = await cryptoUtils.sealSecret(
|
encryptedConfig.privateKey = await cryptoUtils.sealSecret(config.privateKey);
|
||||||
config.privateKey,
|
|
||||||
);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -193,9 +161,7 @@ const createRepository = async (name: string, config: RepositoryConfig, compress
|
||||||
|
|
||||||
error = result.error;
|
error = result.error;
|
||||||
} else {
|
} else {
|
||||||
const initResult = await restic.init(encryptedConfig, organizationId, {
|
const initResult = await restic.init(encryptedConfig, organizationId, { timeoutMs: 20000 });
|
||||||
timeoutMs: 20000,
|
|
||||||
});
|
|
||||||
error = initResult.error;
|
error = initResult.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,12 +169,7 @@ const createRepository = async (name: string, config: RepositoryConfig, compress
|
||||||
await db
|
await db
|
||||||
.update(repositoriesTable)
|
.update(repositoriesTable)
|
||||||
.set({ status: "healthy", lastChecked: Date.now(), lastError: null })
|
.set({ status: "healthy", lastChecked: Date.now(), lastError: null })
|
||||||
.where(
|
.where(and(eq(repositoriesTable.id, id), eq(repositoriesTable.organizationId, organizationId)));
|
||||||
and(
|
|
||||||
eq(repositoriesTable.id, id),
|
|
||||||
eq(repositoriesTable.organizationId, organizationId),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return { repository: created, status: 201 };
|
return { repository: created, status: 201 };
|
||||||
}
|
}
|
||||||
|
|
@ -216,16 +177,9 @@ const createRepository = async (name: string, config: RepositoryConfig, compress
|
||||||
const errorMessage = toMessage(error);
|
const errorMessage = toMessage(error);
|
||||||
await db
|
await db
|
||||||
.delete(repositoriesTable)
|
.delete(repositoriesTable)
|
||||||
.where(
|
.where(and(eq(repositoriesTable.id, id), eq(repositoriesTable.organizationId, organizationId)));
|
||||||
and(
|
|
||||||
eq(repositoriesTable.id, id),
|
|
||||||
eq(repositoriesTable.organizationId, organizationId),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
throw new InternalServerError(
|
throw new InternalServerError(`Failed to initialize repository: ${errorMessage}`);
|
||||||
`Failed to initialize repository: ${errorMessage}`,
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRepository = async (id: string) => {
|
const getRepository = async (id: string) => {
|
||||||
|
|
@ -250,10 +204,7 @@ const deleteRepository = async (id: string) => {
|
||||||
await db
|
await db
|
||||||
.delete(repositoriesTable)
|
.delete(repositoriesTable)
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(eq(repositoriesTable.id, repository.id), eq(repositoriesTable.organizationId, repository.organizationId)),
|
||||||
eq(repositoriesTable.id, repository.id),
|
|
||||||
eq(repositoriesTable.organizationId, repository.organizationId),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
cache.delByPrefix(`snapshots:${repository.id}:`);
|
cache.delByPrefix(`snapshots:${repository.id}:`);
|
||||||
|
|
@ -277,8 +228,7 @@ const listSnapshots = async (id: string, backupId?: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheKey = `snapshots:${repository.id}:${backupId || "all"}`;
|
const cacheKey = `snapshots:${repository.id}:${backupId || "all"}`;
|
||||||
const cached =
|
const cached = cache.get<Awaited<ReturnType<typeof restic.snapshots>>>(cacheKey);
|
||||||
cache.get<Awaited<ReturnType<typeof restic.snapshots>>>(cacheKey);
|
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
@ -288,10 +238,7 @@ const listSnapshots = async (id: string, backupId?: string) => {
|
||||||
let snapshots = [];
|
let snapshots = [];
|
||||||
|
|
||||||
if (backupId) {
|
if (backupId) {
|
||||||
snapshots = await restic.snapshots(repository.config, {
|
snapshots = await restic.snapshots(repository.config, { tags: [backupId], organizationId });
|
||||||
tags: [backupId],
|
|
||||||
organizationId,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
snapshots = await restic.snapshots(repository.config, { organizationId });
|
snapshots = await restic.snapshots(repository.config, { organizationId });
|
||||||
}
|
}
|
||||||
|
|
@ -322,26 +269,9 @@ const listSnapshotFiles = async (
|
||||||
|
|
||||||
const cacheKey = `ls:${repository.id}:${snapshotId}:${path || "root"}:${offset}:${limit}`;
|
const cacheKey = `ls:${repository.id}:${snapshotId}:${path || "root"}:${offset}:${limit}`;
|
||||||
type LsResult = {
|
type LsResult = {
|
||||||
snapshot: {
|
snapshot: { id: string; short_id: string; time: string; hostname: string; paths: string[] } | null;
|
||||||
id: string;
|
nodes: { name: string; type: string; path: string; size?: number; mode?: number }[];
|
||||||
short_id: string;
|
pagination: { offset: number; limit: number; total: number; hasMore: boolean };
|
||||||
time: string;
|
|
||||||
hostname: string;
|
|
||||||
paths: string[];
|
|
||||||
} | null;
|
|
||||||
nodes: {
|
|
||||||
name: string;
|
|
||||||
type: string;
|
|
||||||
path: string;
|
|
||||||
size?: number;
|
|
||||||
mode?: number;
|
|
||||||
}[];
|
|
||||||
pagination: {
|
|
||||||
offset: number;
|
|
||||||
limit: number;
|
|
||||||
total: number;
|
|
||||||
hasMore: boolean;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
const cached = cache.get<LsResult>(cacheKey);
|
const cached = cache.get<LsResult>(cacheKey);
|
||||||
if (cached?.snapshot) {
|
if (cached?.snapshot) {
|
||||||
|
|
@ -355,18 +285,9 @@ const listSnapshotFiles = async (
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireShared(
|
const releaseLock = await repoMutex.acquireShared(repository.id, `ls:${snapshotId}`);
|
||||||
repository.id,
|
|
||||||
`ls:${snapshotId}`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
const result = await restic.ls(
|
const result = await restic.ls(repository.config, snapshotId, organizationId, path, { offset, limit });
|
||||||
repository.config,
|
|
||||||
snapshotId,
|
|
||||||
organizationId,
|
|
||||||
path,
|
|
||||||
{ offset, limit },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!result.snapshot) {
|
if (!result.snapshot) {
|
||||||
throw new NotFoundError("Snapshot not found or empty");
|
throw new NotFoundError("Snapshot not found or empty");
|
||||||
|
|
@ -416,10 +337,7 @@ const restoreSnapshot = async (
|
||||||
|
|
||||||
const target = options?.targetPath || "/";
|
const target = options?.targetPath || "/";
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireShared(
|
const releaseLock = await repoMutex.acquireShared(repository.id, `restore:${snapshotId}`);
|
||||||
repository.id,
|
|
||||||
`restore:${snapshotId}`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
serverEvents.emit("restore:started", {
|
serverEvents.emit("restore:started", {
|
||||||
organizationId,
|
organizationId,
|
||||||
|
|
@ -431,25 +349,11 @@ const restoreSnapshot = async (
|
||||||
...options,
|
...options,
|
||||||
organizationId,
|
organizationId,
|
||||||
onProgress: (progress) => {
|
onProgress: (progress) => {
|
||||||
const {
|
|
||||||
seconds_elapsed,
|
|
||||||
percent_done,
|
|
||||||
total_files,
|
|
||||||
files_done,
|
|
||||||
total_bytes,
|
|
||||||
bytes_done,
|
|
||||||
} = progress;
|
|
||||||
|
|
||||||
serverEvents.emit("restore:progress", {
|
serverEvents.emit("restore:progress", {
|
||||||
organizationId,
|
organizationId,
|
||||||
repositoryId: repository.id,
|
repositoryId: repository.id,
|
||||||
snapshotId,
|
snapshotId,
|
||||||
seconds_elapsed,
|
...progress,
|
||||||
percent_done,
|
|
||||||
total_files,
|
|
||||||
files_done,
|
|
||||||
total_bytes,
|
|
||||||
bytes_done,
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
@ -490,14 +394,10 @@ const getSnapshotDetails = async (id: string, snapshotId: string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheKey = `snapshots:${repository.id}:all`;
|
const cacheKey = `snapshots:${repository.id}:all`;
|
||||||
let snapshots =
|
let snapshots = cache.get<Awaited<ReturnType<typeof restic.snapshots>>>(cacheKey);
|
||||||
cache.get<Awaited<ReturnType<typeof restic.snapshots>>>(cacheKey);
|
|
||||||
|
|
||||||
if (!snapshots) {
|
if (!snapshots) {
|
||||||
const releaseLock = await repoMutex.acquireShared(
|
const releaseLock = await repoMutex.acquireShared(repository.id, `snapshot_details:${snapshotId}`);
|
||||||
repository.id,
|
|
||||||
`snapshot_details:${snapshotId}`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
snapshots = await restic.snapshots(repository.config, { organizationId });
|
snapshots = await restic.snapshots(repository.config, { organizationId });
|
||||||
cache.set(cacheKey, snapshots);
|
cache.set(cacheKey, snapshots);
|
||||||
|
|
@ -506,9 +406,7 @@ const getSnapshotDetails = async (id: string, snapshotId: string) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const snapshot = snapshots.find(
|
const snapshot = snapshots.find((snap) => snap.id === snapshotId || snap.short_id === snapshotId);
|
||||||
(snap) => snap.id === snapshotId || snap.short_id === snapshotId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!snapshot) {
|
if (!snapshot) {
|
||||||
void refreshSnapshots(id).catch(() => {});
|
void refreshSnapshots(id).catch(() => {});
|
||||||
|
|
@ -529,9 +427,7 @@ const checkHealth = async (repositoryId: string) => {
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireExclusive(repository.id, "check");
|
const releaseLock = await repoMutex.acquireExclusive(repository.id, "check");
|
||||||
try {
|
try {
|
||||||
const { hasErrors, error } = await restic.check(repository.config, {
|
const { hasErrors, error } = await restic.check(repository.config, { organizationId });
|
||||||
organizationId,
|
|
||||||
});
|
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.update(repositoriesTable)
|
.update(repositoriesTable)
|
||||||
|
|
@ -541,10 +437,7 @@ const checkHealth = async (repositoryId: string) => {
|
||||||
lastError: error,
|
lastError: error,
|
||||||
})
|
})
|
||||||
.where(
|
.where(
|
||||||
and(
|
and(eq(repositoriesTable.id, repository.id), eq(repositoriesTable.organizationId, repository.organizationId)),
|
||||||
eq(repositoriesTable.id, repository.id),
|
|
||||||
eq(repositoriesTable.organizationId, repository.organizationId),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return { lastError: error };
|
return { lastError: error };
|
||||||
|
|
@ -567,10 +460,7 @@ const startDoctor = async (id: string) => {
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db
|
await db.update(repositoriesTable).set({ status: "doctor" }).where(eq(repositoriesTable.id, repository.id));
|
||||||
.update(repositoriesTable)
|
|
||||||
.set({ status: "doctor" })
|
|
||||||
.where(eq(repositoriesTable.id, repository.id));
|
|
||||||
|
|
||||||
serverEvents.emit("doctor:started", {
|
serverEvents.emit("doctor:started", {
|
||||||
organizationId: repository.organizationId,
|
organizationId: repository.organizationId,
|
||||||
|
|
@ -584,12 +474,7 @@ const startDoctor = async (id: string) => {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
executeDoctor(
|
executeDoctor(repository.id, repository.config, repository.name, abortController.signal)
|
||||||
repository.id,
|
|
||||||
repository.config,
|
|
||||||
repository.name,
|
|
||||||
abortController.signal,
|
|
||||||
)
|
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
logger.error(`Doctor background task failed: ${toMessage(error)}`);
|
logger.error(`Doctor background task failed: ${toMessage(error)}`);
|
||||||
})
|
})
|
||||||
|
|
@ -609,20 +494,14 @@ const cancelDoctor = async (id: string) => {
|
||||||
|
|
||||||
const abortController = runningDoctors.get(repository.id);
|
const abortController = runningDoctors.get(repository.id);
|
||||||
if (!abortController) {
|
if (!abortController) {
|
||||||
await db
|
await db.update(repositoriesTable).set({ status: "unknown" }).where(eq(repositoriesTable.id, repository.id));
|
||||||
.update(repositoriesTable)
|
|
||||||
.set({ status: "unknown" })
|
|
||||||
.where(eq(repositoriesTable.id, repository.id));
|
|
||||||
throw new ConflictError("No doctor operation is currently running");
|
throw new ConflictError("No doctor operation is currently running");
|
||||||
}
|
}
|
||||||
|
|
||||||
abortController.abort();
|
abortController.abort();
|
||||||
runningDoctors.delete(repository.id);
|
runningDoctors.delete(repository.id);
|
||||||
|
|
||||||
await db
|
await db.update(repositoriesTable).set({ status: "unknown" }).where(eq(repositoriesTable.id, repository.id));
|
||||||
.update(repositoriesTable)
|
|
||||||
.set({ status: "unknown" })
|
|
||||||
.where(eq(repositoriesTable.id, repository.id));
|
|
||||||
|
|
||||||
serverEvents.emit("doctor:cancelled", {
|
serverEvents.emit("doctor:cancelled", {
|
||||||
organizationId: repository.organizationId,
|
organizationId: repository.organizationId,
|
||||||
|
|
@ -641,10 +520,7 @@ const deleteSnapshot = async (id: string, snapshotId: string) => {
|
||||||
throw new NotFoundError("Repository not found");
|
throw new NotFoundError("Repository not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireExclusive(
|
const releaseLock = await repoMutex.acquireExclusive(repository.id, `delete:${snapshotId}`);
|
||||||
repository.id,
|
|
||||||
`delete:${snapshotId}`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
await restic.deleteSnapshot(repository.config, snapshotId, organizationId);
|
await restic.deleteSnapshot(repository.config, snapshotId, organizationId);
|
||||||
cache.delByPrefix(`snapshots:${repository.id}:`);
|
cache.delByPrefix(`snapshots:${repository.id}:`);
|
||||||
|
|
@ -662,16 +538,9 @@ const deleteSnapshots = async (id: string, snapshotIds: string[]) => {
|
||||||
throw new NotFoundError("Repository not found");
|
throw new NotFoundError("Repository not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireExclusive(
|
const releaseLock = await repoMutex.acquireExclusive(repository.id, `delete:bulk`);
|
||||||
repository.id,
|
|
||||||
`delete:bulk`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
await restic.deleteSnapshots(
|
await restic.deleteSnapshots(repository.config, snapshotIds, organizationId);
|
||||||
repository.config,
|
|
||||||
snapshotIds,
|
|
||||||
organizationId,
|
|
||||||
);
|
|
||||||
cache.delByPrefix(`snapshots:${repository.id}:`);
|
cache.delByPrefix(`snapshots:${repository.id}:`);
|
||||||
for (const snapshotId of snapshotIds) {
|
for (const snapshotId of snapshotIds) {
|
||||||
cache.delByPrefix(`ls:${repository.id}:${snapshotId}:`);
|
cache.delByPrefix(`ls:${repository.id}:${snapshotId}:`);
|
||||||
|
|
@ -693,17 +562,9 @@ const tagSnapshots = async (
|
||||||
throw new NotFoundError("Repository not found");
|
throw new NotFoundError("Repository not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireExclusive(
|
const releaseLock = await repoMutex.acquireExclusive(repository.id, `tag:bulk`);
|
||||||
repository.id,
|
|
||||||
`tag:bulk`,
|
|
||||||
);
|
|
||||||
try {
|
try {
|
||||||
await restic.tagSnapshots(
|
await restic.tagSnapshots(repository.config, snapshotIds, tags, organizationId);
|
||||||
repository.config,
|
|
||||||
snapshotIds,
|
|
||||||
tags,
|
|
||||||
organizationId,
|
|
||||||
);
|
|
||||||
cache.delByPrefix(`snapshots:${repository.id}:`);
|
cache.delByPrefix(`snapshots:${repository.id}:`);
|
||||||
for (const snapshotId of snapshotIds) {
|
for (const snapshotId of snapshotIds) {
|
||||||
cache.delByPrefix(`ls:${repository.id}:${snapshotId}:`);
|
cache.delByPrefix(`ls:${repository.id}:${snapshotId}:`);
|
||||||
|
|
@ -726,9 +587,7 @@ const refreshSnapshots = async (id: string) => {
|
||||||
|
|
||||||
const releaseLock = await repoMutex.acquireShared(repository.id, "refresh");
|
const releaseLock = await repoMutex.acquireShared(repository.id, "refresh");
|
||||||
try {
|
try {
|
||||||
const snapshots = await restic.snapshots(repository.config, {
|
const snapshots = await restic.snapshots(repository.config, { organizationId });
|
||||||
organizationId,
|
|
||||||
});
|
|
||||||
const cacheKey = `snapshots:${repository.id}:all`;
|
const cacheKey = `snapshots:${repository.id}:all`;
|
||||||
cache.set(cacheKey, snapshots);
|
cache.set(cacheKey, snapshots);
|
||||||
|
|
||||||
|
|
@ -855,24 +714,14 @@ const execResticCommand = async (
|
||||||
}
|
}
|
||||||
addCommonArgs(resticArgs, env, repository.config);
|
addCommonArgs(resticArgs, env, repository.config);
|
||||||
|
|
||||||
const result = await safeSpawn({
|
const result = await safeSpawn({ command: "restic", args: resticArgs, env, signal, onStdout, onStderr });
|
||||||
command: "restic",
|
|
||||||
args: resticArgs,
|
|
||||||
env,
|
|
||||||
signal,
|
|
||||||
onStdout,
|
|
||||||
onStderr,
|
|
||||||
});
|
|
||||||
|
|
||||||
await cleanupTemporaryKeys(env);
|
await cleanupTemporaryKeys(env);
|
||||||
|
|
||||||
return { exitCode: result.exitCode };
|
return { exitCode: result.exitCode };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRetentionCategories = async (
|
const getRetentionCategories = async (repositoryId: string, scheduleId?: string) => {
|
||||||
repositoryId: string,
|
|
||||||
scheduleId?: string,
|
|
||||||
) => {
|
|
||||||
if (!scheduleId) {
|
if (!scheduleId) {
|
||||||
return new Map<string, RetentionCategory[]>();
|
return new Map<string, RetentionCategory[]>();
|
||||||
}
|
}
|
||||||
|
|
@ -896,15 +745,11 @@ const getRetentionCategories = async (
|
||||||
|
|
||||||
const { repository } = repositoryResult;
|
const { repository } = repositoryResult;
|
||||||
|
|
||||||
const dryRunResults = await restic.forget(
|
const dryRunResults = await restic.forget(repository.config, schedule.retentionPolicy, {
|
||||||
repository.config,
|
tag: scheduleId,
|
||||||
schedule.retentionPolicy,
|
organizationId: getOrganizationId(),
|
||||||
{
|
dryRun: true,
|
||||||
tag: scheduleId,
|
});
|
||||||
organizationId: getOrganizationId(),
|
|
||||||
dryRun: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!dryRunResults.data) {
|
if (!dryRunResults.data) {
|
||||||
return new Map<string, RetentionCategory[]>();
|
return new Map<string, RetentionCategory[]>();
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,7 @@ import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { type } from "arktype";
|
import { type } from "arktype";
|
||||||
import { throttle } from "es-toolkit";
|
import { throttle } from "es-toolkit";
|
||||||
import type {
|
import type { BandwidthLimit, CompressionMode, OverwriteMode, RepositoryConfig } from "~/schemas/restic";
|
||||||
BandwidthLimit,
|
|
||||||
CompressionMode,
|
|
||||||
OverwriteMode,
|
|
||||||
RepositoryConfig,
|
|
||||||
} from "~/schemas/restic";
|
|
||||||
import {
|
import {
|
||||||
type ResticBackupProgressDto,
|
type ResticBackupProgressDto,
|
||||||
type ResticRestoreOutputDto,
|
type ResticRestoreOutputDto,
|
||||||
|
|
@ -20,12 +15,7 @@ import {
|
||||||
resticSnapshotSummarySchema,
|
resticSnapshotSummarySchema,
|
||||||
} from "~/schemas/restic-dto";
|
} from "~/schemas/restic-dto";
|
||||||
import { config as appConfig } from "../core/config";
|
import { config as appConfig } from "../core/config";
|
||||||
import {
|
import { DEFAULT_EXCLUDES, RESTIC_CACHE_DIR, RESTIC_PASS_FILE } from "../core/constants";
|
||||||
DEFAULT_EXCLUDES,
|
|
||||||
REPOSITORY_BASE,
|
|
||||||
RESTIC_CACHE_DIR,
|
|
||||||
RESTIC_PASS_FILE,
|
|
||||||
} from "../core/constants";
|
|
||||||
import { db } from "../db/db";
|
import { db } from "../db/db";
|
||||||
import type { RetentionPolicy } from "../modules/backups/backups.dto";
|
import type { RetentionPolicy } from "../modules/backups/backups.dto";
|
||||||
import { cryptoUtils } from "./crypto";
|
import { cryptoUtils } from "./crypto";
|
||||||
|
|
@ -72,37 +62,25 @@ export const buildRepoUrl = (config: RepositoryConfig): string => {
|
||||||
case "sftp":
|
case "sftp":
|
||||||
return `sftp:${config.user}@${config.host}:${config.path}`;
|
return `sftp:${config.user}@${config.host}:${config.path}`;
|
||||||
default: {
|
default: {
|
||||||
throw new Error(
|
throw new Error(`Unsupported repository backend: ${JSON.stringify(config)}`);
|
||||||
`Unsupported repository backend: ${JSON.stringify(config)}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const buildEnv = async (
|
export const buildEnv = async (config: RepositoryConfig, organizationId: string) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
organizationId: string,
|
|
||||||
) => {
|
|
||||||
const env: Record<string, string> = {
|
const env: Record<string, string> = {
|
||||||
RESTIC_CACHE_DIR,
|
RESTIC_CACHE_DIR,
|
||||||
PATH: process.env.PATH || "/usr/local/bin:/usr/bin:/bin",
|
PATH: process.env.PATH || "/usr/local/bin:/usr/bin:/bin",
|
||||||
};
|
};
|
||||||
|
|
||||||
if (config.isExistingRepository && config.customPassword) {
|
if (config.isExistingRepository && config.customPassword) {
|
||||||
const decryptedPassword = await cryptoUtils.resolveSecret(
|
const decryptedPassword = await cryptoUtils.resolveSecret(config.customPassword);
|
||||||
config.customPassword,
|
const passwordFilePath = path.join("/tmp", `zerobyte-pass-${crypto.randomBytes(8).toString("hex")}.txt`);
|
||||||
);
|
|
||||||
const passwordFilePath = path.join(
|
|
||||||
"/tmp",
|
|
||||||
`zerobyte-pass-${crypto.randomBytes(8).toString("hex")}.txt`,
|
|
||||||
);
|
|
||||||
|
|
||||||
await fs.writeFile(passwordFilePath, decryptedPassword, { mode: 0o600 });
|
await fs.writeFile(passwordFilePath, decryptedPassword, { mode: 0o600 });
|
||||||
env.RESTIC_PASSWORD_FILE = passwordFilePath;
|
env.RESTIC_PASSWORD_FILE = passwordFilePath;
|
||||||
} else {
|
} else {
|
||||||
const org = await db.query.organization.findFirst({
|
const org = await db.query.organization.findFirst({ where: { id: organizationId } });
|
||||||
where: { id: organizationId },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!org) {
|
if (!org) {
|
||||||
throw new Error(`Organization ${organizationId} not found`);
|
throw new Error(`Organization ${organizationId} not found`);
|
||||||
|
|
@ -110,17 +88,10 @@ export const buildEnv = async (
|
||||||
|
|
||||||
const metadata = org.metadata;
|
const metadata = org.metadata;
|
||||||
if (!metadata?.resticPassword) {
|
if (!metadata?.resticPassword) {
|
||||||
throw new Error(
|
throw new Error(`Restic password not configured for organization ${organizationId}`);
|
||||||
`Restic password not configured for organization ${organizationId}`,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
const decryptedPassword = await cryptoUtils.resolveSecret(
|
const decryptedPassword = await cryptoUtils.resolveSecret(metadata.resticPassword);
|
||||||
metadata.resticPassword,
|
const passwordFilePath = path.join("/tmp", `zerobyte-pass-${crypto.randomBytes(8).toString("hex")}.txt`);
|
||||||
);
|
|
||||||
const passwordFilePath = path.join(
|
|
||||||
"/tmp",
|
|
||||||
`zerobyte-pass-${crypto.randomBytes(8).toString("hex")}.txt`,
|
|
||||||
);
|
|
||||||
await fs.writeFile(passwordFilePath, decryptedPassword, { mode: 0o600 });
|
await fs.writeFile(passwordFilePath, decryptedPassword, { mode: 0o600 });
|
||||||
env.RESTIC_PASSWORD_FILE = passwordFilePath;
|
env.RESTIC_PASSWORD_FILE = passwordFilePath;
|
||||||
}
|
}
|
||||||
|
|
@ -128,12 +99,8 @@ export const buildEnv = async (
|
||||||
|
|
||||||
switch (config.backend) {
|
switch (config.backend) {
|
||||||
case "s3":
|
case "s3":
|
||||||
env.AWS_ACCESS_KEY_ID = await cryptoUtils.resolveSecret(
|
env.AWS_ACCESS_KEY_ID = await cryptoUtils.resolveSecret(config.accessKeyId);
|
||||||
config.accessKeyId,
|
env.AWS_SECRET_ACCESS_KEY = await cryptoUtils.resolveSecret(config.secretAccessKey);
|
||||||
);
|
|
||||||
env.AWS_SECRET_ACCESS_KEY = await cryptoUtils.resolveSecret(
|
|
||||||
config.secretAccessKey,
|
|
||||||
);
|
|
||||||
|
|
||||||
// Huawei OBS requires virtual-hosted style access
|
// Huawei OBS requires virtual-hosted style access
|
||||||
if (config.endpoint.includes("myhuaweicloud")) {
|
if (config.endpoint.includes("myhuaweicloud")) {
|
||||||
|
|
@ -141,35 +108,22 @@ export const buildEnv = async (
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "r2":
|
case "r2":
|
||||||
env.AWS_ACCESS_KEY_ID = await cryptoUtils.resolveSecret(
|
env.AWS_ACCESS_KEY_ID = await cryptoUtils.resolveSecret(config.accessKeyId);
|
||||||
config.accessKeyId,
|
env.AWS_SECRET_ACCESS_KEY = await cryptoUtils.resolveSecret(config.secretAccessKey);
|
||||||
);
|
|
||||||
env.AWS_SECRET_ACCESS_KEY = await cryptoUtils.resolveSecret(
|
|
||||||
config.secretAccessKey,
|
|
||||||
);
|
|
||||||
env.AWS_REGION = "auto";
|
env.AWS_REGION = "auto";
|
||||||
env.AWS_S3_FORCE_PATH_STYLE = "true";
|
env.AWS_S3_FORCE_PATH_STYLE = "true";
|
||||||
break;
|
break;
|
||||||
case "gcs": {
|
case "gcs": {
|
||||||
const decryptedCredentials = await cryptoUtils.resolveSecret(
|
const decryptedCredentials = await cryptoUtils.resolveSecret(config.credentialsJson);
|
||||||
config.credentialsJson,
|
const credentialsPath = path.join("/tmp", `zerobyte-gcs-${crypto.randomBytes(8).toString("hex")}.json`);
|
||||||
);
|
await fs.writeFile(credentialsPath, decryptedCredentials, { mode: 0o600 });
|
||||||
const credentialsPath = path.join(
|
|
||||||
"/tmp",
|
|
||||||
`zerobyte-gcs-${crypto.randomBytes(8).toString("hex")}.json`,
|
|
||||||
);
|
|
||||||
await fs.writeFile(credentialsPath, decryptedCredentials, {
|
|
||||||
mode: 0o600,
|
|
||||||
});
|
|
||||||
env.GOOGLE_PROJECT_ID = config.projectId;
|
env.GOOGLE_PROJECT_ID = config.projectId;
|
||||||
env.GOOGLE_APPLICATION_CREDENTIALS = credentialsPath;
|
env.GOOGLE_APPLICATION_CREDENTIALS = credentialsPath;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "azure": {
|
case "azure": {
|
||||||
env.AZURE_ACCOUNT_NAME = config.accountName;
|
env.AZURE_ACCOUNT_NAME = config.accountName;
|
||||||
env.AZURE_ACCOUNT_KEY = await cryptoUtils.resolveSecret(
|
env.AZURE_ACCOUNT_KEY = await cryptoUtils.resolveSecret(config.accountKey);
|
||||||
config.accountKey,
|
|
||||||
);
|
|
||||||
if (config.endpointSuffix) {
|
if (config.endpointSuffix) {
|
||||||
env.AZURE_ENDPOINT_SUFFIX = config.endpointSuffix;
|
env.AZURE_ENDPOINT_SUFFIX = config.endpointSuffix;
|
||||||
}
|
}
|
||||||
|
|
@ -177,23 +131,16 @@ export const buildEnv = async (
|
||||||
}
|
}
|
||||||
case "rest": {
|
case "rest": {
|
||||||
if (config.username) {
|
if (config.username) {
|
||||||
env.RESTIC_REST_USERNAME = await cryptoUtils.resolveSecret(
|
env.RESTIC_REST_USERNAME = await cryptoUtils.resolveSecret(config.username);
|
||||||
config.username,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (config.password) {
|
if (config.password) {
|
||||||
env.RESTIC_REST_PASSWORD = await cryptoUtils.resolveSecret(
|
env.RESTIC_REST_PASSWORD = await cryptoUtils.resolveSecret(config.password);
|
||||||
config.password,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "sftp": {
|
case "sftp": {
|
||||||
const decryptedKey = await cryptoUtils.resolveSecret(config.privateKey);
|
const decryptedKey = await cryptoUtils.resolveSecret(config.privateKey);
|
||||||
const keyPath = path.join(
|
const keyPath = path.join("/tmp", `zerobyte-ssh-${crypto.randomBytes(8).toString("hex")}`);
|
||||||
"/tmp",
|
|
||||||
`zerobyte-ssh-${crypto.randomBytes(8).toString("hex")}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
let normalizedKey = decryptedKey.replace(/\r\n/g, "\n");
|
let normalizedKey = decryptedKey.replace(/\r\n/g, "\n");
|
||||||
if (!normalizedKey.endsWith("\n")) {
|
if (!normalizedKey.endsWith("\n")) {
|
||||||
|
|
@ -201,12 +148,8 @@ export const buildEnv = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (normalizedKey.includes("ENCRYPTED")) {
|
if (normalizedKey.includes("ENCRYPTED")) {
|
||||||
logger.error(
|
logger.error("SFTP: Private key appears to be passphrase-protected. Please use an unencrypted key.");
|
||||||
"SFTP: Private key appears to be passphrase-protected. Please use an unencrypted key.",
|
throw new Error("Passphrase-protected SSH keys are not supported. Please provide an unencrypted private key.");
|
||||||
);
|
|
||||||
throw new Error(
|
|
||||||
"Passphrase-protected SSH keys are not supported. Please provide an unencrypted private key.",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await fs.writeFile(keyPath, normalizedKey, { mode: 0o600 });
|
await fs.writeFile(keyPath, normalizedKey, { mode: 0o600 });
|
||||||
|
|
@ -241,25 +184,12 @@ export const buildEnv = async (
|
||||||
];
|
];
|
||||||
|
|
||||||
if (config.skipHostKeyCheck || !config.knownHosts) {
|
if (config.skipHostKeyCheck || !config.knownHosts) {
|
||||||
sshArgs.push(
|
sshArgs.push("-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null");
|
||||||
"-o",
|
|
||||||
"StrictHostKeyChecking=no",
|
|
||||||
"-o",
|
|
||||||
"UserKnownHostsFile=/dev/null",
|
|
||||||
);
|
|
||||||
} else if (config.knownHosts) {
|
} else if (config.knownHosts) {
|
||||||
const knownHostsPath = path.join(
|
const knownHostsPath = path.join("/tmp", `zerobyte-known-hosts-${crypto.randomBytes(8).toString("hex")}`);
|
||||||
"/tmp",
|
|
||||||
`zerobyte-known-hosts-${crypto.randomBytes(8).toString("hex")}`,
|
|
||||||
);
|
|
||||||
await fs.writeFile(knownHostsPath, config.knownHosts, { mode: 0o600 });
|
await fs.writeFile(knownHostsPath, config.knownHosts, { mode: 0o600 });
|
||||||
env._SFTP_KNOWN_HOSTS_PATH = knownHostsPath;
|
env._SFTP_KNOWN_HOSTS_PATH = knownHostsPath;
|
||||||
sshArgs.push(
|
sshArgs.push("-o", "StrictHostKeyChecking=yes", "-o", `UserKnownHostsFile=${knownHostsPath}`);
|
||||||
"-o",
|
|
||||||
"StrictHostKeyChecking=yes",
|
|
||||||
"-o",
|
|
||||||
`UserKnownHostsFile=${knownHostsPath}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.port && config.port !== 22) {
|
if (config.port && config.port !== 22) {
|
||||||
|
|
@ -274,10 +204,7 @@ export const buildEnv = async (
|
||||||
|
|
||||||
if (config.cacert) {
|
if (config.cacert) {
|
||||||
const decryptedCert = await cryptoUtils.resolveSecret(config.cacert);
|
const decryptedCert = await cryptoUtils.resolveSecret(config.cacert);
|
||||||
const certPath = path.join(
|
const certPath = path.join("/tmp", `zerobyte-cacert-${crypto.randomBytes(8).toString("hex")}.pem`);
|
||||||
"/tmp",
|
|
||||||
`zerobyte-cacert-${crypto.randomBytes(8).toString("hex")}.pem`,
|
|
||||||
);
|
|
||||||
await fs.writeFile(certPath, decryptedCert, { mode: 0o600 });
|
await fs.writeFile(certPath, decryptedCert, { mode: 0o600 });
|
||||||
env.RESTIC_CACERT = certPath;
|
env.RESTIC_CACERT = certPath;
|
||||||
}
|
}
|
||||||
|
|
@ -289,11 +216,7 @@ export const buildEnv = async (
|
||||||
return env;
|
return env;
|
||||||
};
|
};
|
||||||
|
|
||||||
const init = async (
|
const init = async (config: RepositoryConfig, organizationId: string, options?: { timeoutMs?: number }) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
organizationId: string,
|
|
||||||
options?: { timeoutMs?: number },
|
|
||||||
) => {
|
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
|
|
||||||
logger.info(`Initializing restic repository at ${repoUrl}...`);
|
logger.info(`Initializing restic repository at ${repoUrl}...`);
|
||||||
|
|
@ -303,12 +226,7 @@ const init = async (
|
||||||
const args = ["init", "--repo", repoUrl];
|
const args = ["init", "--repo", repoUrl];
|
||||||
addCommonArgs(args, env, config);
|
addCommonArgs(args, env, config);
|
||||||
|
|
||||||
const res = await exec({
|
const res = await exec({ command: "restic", args, env, timeout: options?.timeoutMs ?? 20000 });
|
||||||
command: "restic",
|
|
||||||
args,
|
|
||||||
env,
|
|
||||||
timeout: options?.timeoutMs ?? 20000,
|
|
||||||
});
|
|
||||||
await cleanupTemporaryKeys(env);
|
await cleanupTemporaryKeys(env);
|
||||||
|
|
||||||
if (res.exitCode !== 0) {
|
if (res.exitCode !== 0) {
|
||||||
|
|
@ -338,13 +256,7 @@ const backup = async (
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
const env = await buildEnv(config, options?.organizationId);
|
const env = await buildEnv(config, options?.organizationId);
|
||||||
|
|
||||||
const args: string[] = [
|
const args: string[] = ["--repo", repoUrl, "backup", "--compression", options?.compressionMode ?? "auto"];
|
||||||
"--repo",
|
|
||||||
repoUrl,
|
|
||||||
"backup",
|
|
||||||
"--compression",
|
|
||||||
options?.compressionMode ?? "auto",
|
|
||||||
];
|
|
||||||
|
|
||||||
if (options?.oneFileSystem) {
|
if (options?.oneFileSystem) {
|
||||||
args.push("--one-file-system");
|
args.push("--one-file-system");
|
||||||
|
|
@ -362,9 +274,7 @@ const backup = async (
|
||||||
|
|
||||||
let includeFile: string | null = null;
|
let includeFile: string | null = null;
|
||||||
if (options?.include && options.include.length > 0) {
|
if (options?.include && options.include.length > 0) {
|
||||||
const tmp = await fs.mkdtemp(
|
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "zerobyte-restic-include-"));
|
||||||
path.join(os.tmpdir(), "zerobyte-restic-include-"),
|
|
||||||
);
|
|
||||||
includeFile = path.join(tmp, `include.txt`);
|
includeFile = path.join(tmp, `include.txt`);
|
||||||
|
|
||||||
await fs.writeFile(includeFile, options.include.join("\n"), "utf-8");
|
await fs.writeFile(includeFile, options.include.join("\n"), "utf-8");
|
||||||
|
|
@ -380,9 +290,7 @@ const backup = async (
|
||||||
|
|
||||||
let excludeFile: string | null = null;
|
let excludeFile: string | null = null;
|
||||||
if (options?.exclude && options.exclude.length > 0) {
|
if (options?.exclude && options.exclude.length > 0) {
|
||||||
const tmp = await fs.mkdtemp(
|
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "zerobyte-restic-exclude-"));
|
||||||
path.join(os.tmpdir(), "zerobyte-restic-exclude-"),
|
|
||||||
);
|
|
||||||
excludeFile = path.join(tmp, `exclude.txt`);
|
excludeFile = path.join(tmp, `exclude.txt`);
|
||||||
|
|
||||||
await fs.writeFile(excludeFile, options.exclude.join("\n"), "utf-8");
|
await fs.writeFile(excludeFile, options.exclude.join("\n"), "utf-8");
|
||||||
|
|
@ -409,6 +317,8 @@ const backup = async (
|
||||||
const progress = resticBackupProgressSchema(jsonData);
|
const progress = resticBackupProgressSchema(jsonData);
|
||||||
if (!(progress instanceof type.errors)) {
|
if (!(progress instanceof type.errors)) {
|
||||||
options.onProgress(progress);
|
options.onProgress(progress);
|
||||||
|
} else {
|
||||||
|
logger.error(progress.summary);
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// Ignore JSON parse errors for non-JSON lines
|
// Ignore JSON parse errors for non-JSON lines
|
||||||
|
|
@ -472,13 +382,13 @@ const backup = async (
|
||||||
};
|
};
|
||||||
|
|
||||||
const restoreProgressSchema = type({
|
const restoreProgressSchema = type({
|
||||||
message_type: "'status'",
|
message_type: "'status' | 'summary'",
|
||||||
seconds_elapsed: "number",
|
seconds_elapsed: "number",
|
||||||
percent_done: "number",
|
percent_done: "number = 0",
|
||||||
total_files: "number",
|
total_files: "number",
|
||||||
files_done: "number",
|
files_restored: "number = 0",
|
||||||
total_bytes: "number",
|
total_bytes: "number = 0",
|
||||||
bytes_done: "number",
|
bytes_restored: "number = 0",
|
||||||
});
|
});
|
||||||
|
|
||||||
export type RestoreProgress = typeof restoreProgressSchema.infer;
|
export type RestoreProgress = typeof restoreProgressSchema.infer;
|
||||||
|
|
@ -500,23 +410,12 @@ const restore = async (
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
const env = await buildEnv(config, options.organizationId);
|
const env = await buildEnv(config, options.organizationId);
|
||||||
|
|
||||||
const args: string[] = [
|
const args: string[] = ["--repo", repoUrl, "restore", snapshotId, "--target", target];
|
||||||
"--repo",
|
|
||||||
repoUrl,
|
|
||||||
"restore",
|
|
||||||
snapshotId,
|
|
||||||
"--target",
|
|
||||||
target,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (options?.overwrite) {
|
if (options?.overwrite) {
|
||||||
args.push("--overwrite", options.overwrite);
|
args.push("--overwrite", options.overwrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options?.delete) {
|
|
||||||
args.push("--delete");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.include?.length) {
|
if (options?.include?.length) {
|
||||||
for (const pattern of options.include) {
|
for (const pattern of options.include) {
|
||||||
args.push("--include", pattern);
|
args.push("--include", pattern);
|
||||||
|
|
@ -544,6 +443,8 @@ const restore = async (
|
||||||
const progress = restoreProgressSchema(jsonData);
|
const progress = restoreProgressSchema(jsonData);
|
||||||
if (!(progress instanceof type.errors)) {
|
if (!(progress instanceof type.errors)) {
|
||||||
options.onProgress(progress);
|
options.onProgress(progress);
|
||||||
|
} else {
|
||||||
|
logger.error(progress.summary);
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
// Ignore JSON parse errors for non-JSON lines
|
// Ignore JSON parse errors for non-JSON lines
|
||||||
|
|
@ -576,23 +477,16 @@ const restore = async (
|
||||||
try {
|
try {
|
||||||
summaryLine = JSON.parse(lastLine ?? "{}");
|
summaryLine = JSON.parse(lastLine ?? "{}");
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
logger.warn(
|
logger.warn("Failed to parse restic restore output JSON summary.", lastLine);
|
||||||
"Failed to parse restic restore output JSON summary.",
|
|
||||||
lastLine,
|
|
||||||
);
|
|
||||||
summaryLine = {};
|
summaryLine = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(`Restic restore output last line: ${JSON.stringify(summaryLine)}`);
|
||||||
`Restic restore output last line: ${JSON.stringify(summaryLine)}`,
|
|
||||||
);
|
|
||||||
const result = resticRestoreOutputSchema(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(
|
logger.info(`Restic restore completed for snapshot ${snapshotId} to target ${target}`);
|
||||||
`Restic restore completed for snapshot ${snapshotId} to target ${target}`,
|
|
||||||
);
|
|
||||||
const fallback: ResticRestoreOutputDto = {
|
const fallback: ResticRestoreOutputDto = {
|
||||||
message_type: "summary" as const,
|
message_type: "summary" as const,
|
||||||
total_files: 0,
|
total_files: 0,
|
||||||
|
|
@ -611,10 +505,7 @@ const restore = async (
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
const snapshots = async (
|
const snapshots = async (config: RepositoryConfig, options: { tags?: string[]; organizationId: string }) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
options: { tags?: string[]; organizationId: string },
|
|
||||||
) => {
|
|
||||||
const { tags, organizationId } = options;
|
const { tags, organizationId } = options;
|
||||||
|
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
|
|
@ -641,12 +532,8 @@ const snapshots = async (
|
||||||
const result = snapshotInfoSchema.array()(JSON.parse(res.stdout));
|
const result = snapshotInfoSchema.array()(JSON.parse(res.stdout));
|
||||||
|
|
||||||
if (result instanceof type.errors) {
|
if (result instanceof type.errors) {
|
||||||
logger.error(
|
logger.error(`Restic snapshots output validation failed: ${result.summary}`);
|
||||||
`Restic snapshots output validation failed: ${result.summary}`,
|
throw new Error(`Restic snapshots output validation failed: ${result.summary}`);
|
||||||
);
|
|
||||||
throw new Error(
|
|
||||||
`Restic snapshots output validation failed: ${result.summary}`,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -693,15 +580,7 @@ const forget = async (
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
const env = await buildEnv(config, extra.organizationId);
|
const env = await buildEnv(config, extra.organizationId);
|
||||||
|
|
||||||
const args: string[] = [
|
const args: string[] = ["--repo", repoUrl, "forget", "--group-by", "tags", "--tag", extra.tag];
|
||||||
"--repo",
|
|
||||||
repoUrl,
|
|
||||||
"forget",
|
|
||||||
"--group-by",
|
|
||||||
"tags",
|
|
||||||
"--tag",
|
|
||||||
extra.tag,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (extra.dryRun) {
|
if (extra.dryRun) {
|
||||||
args.push("--dry-run", "--no-lock");
|
args.push("--dry-run", "--no-lock");
|
||||||
|
|
@ -744,18 +623,12 @@ const forget = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
const lines = res.stdout.split("\n").filter((line) => line.trim());
|
const lines = res.stdout.split("\n").filter((line) => line.trim());
|
||||||
const result = extra.dryRun
|
const result = extra.dryRun ? safeJsonParse<ResticForgetResponse>(lines.at(-1) ?? "[]") : null;
|
||||||
? safeJsonParse<ResticForgetResponse>(lines.at(-1) ?? "[]")
|
|
||||||
: null;
|
|
||||||
|
|
||||||
return { success: true, data: result };
|
return { success: true, data: result };
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteSnapshots = async (
|
const deleteSnapshots = async (config: RepositoryConfig, snapshotIds: string[], organizationId: string) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
snapshotIds: string[],
|
|
||||||
organizationId: string,
|
|
||||||
) => {
|
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
const env = await buildEnv(config, organizationId);
|
const env = await buildEnv(config, organizationId);
|
||||||
|
|
||||||
|
|
@ -763,13 +636,7 @@ const deleteSnapshots = async (
|
||||||
throw new Error("No snapshot IDs provided for deletion.");
|
throw new Error("No snapshot IDs provided for deletion.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const args: string[] = [
|
const args: string[] = ["--repo", repoUrl, "forget", ...snapshotIds, "--prune"];
|
||||||
"--repo",
|
|
||||||
repoUrl,
|
|
||||||
"forget",
|
|
||||||
...snapshotIds,
|
|
||||||
"--prune",
|
|
||||||
];
|
|
||||||
addCommonArgs(args, env, config);
|
addCommonArgs(args, env, config);
|
||||||
|
|
||||||
const res = await exec({ command: "restic", args, env });
|
const res = await exec({ command: "restic", args, env });
|
||||||
|
|
@ -783,11 +650,7 @@ const deleteSnapshots = async (
|
||||||
return { success: true };
|
return { success: true };
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteSnapshot = async (
|
const deleteSnapshot = async (config: RepositoryConfig, snapshotId: string, organizationId: string) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
snapshotId: string,
|
|
||||||
organizationId: string,
|
|
||||||
) => {
|
|
||||||
return deleteSnapshots(config, [snapshotId], organizationId);
|
return deleteSnapshots(config, [snapshotId], organizationId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -954,22 +817,14 @@ const ls = async (
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const unlock = async (
|
const unlock = async (config: RepositoryConfig, options: { signal?: AbortSignal; organizationId: string }) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
options: { signal?: AbortSignal; organizationId: string },
|
|
||||||
) => {
|
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
const env = await buildEnv(config, options.organizationId);
|
const env = await buildEnv(config, options.organizationId);
|
||||||
|
|
||||||
const args = ["unlock", "--repo", repoUrl, "--remove-all"];
|
const args = ["unlock", "--repo", repoUrl, "--remove-all"];
|
||||||
addCommonArgs(args, env, config);
|
addCommonArgs(args, env, config);
|
||||||
|
|
||||||
const res = await exec({
|
const res = await exec({ command: "restic", args, env, signal: options?.signal });
|
||||||
command: "restic",
|
|
||||||
args,
|
|
||||||
env,
|
|
||||||
signal: options?.signal,
|
|
||||||
});
|
|
||||||
await cleanupTemporaryKeys(env);
|
await cleanupTemporaryKeys(env);
|
||||||
|
|
||||||
if (options?.signal?.aborted) {
|
if (options?.signal?.aborted) {
|
||||||
|
|
@ -1001,22 +856,12 @@ const check = async (
|
||||||
|
|
||||||
addCommonArgs(args, env, config);
|
addCommonArgs(args, env, config);
|
||||||
|
|
||||||
const res = await exec({
|
const res = await exec({ command: "restic", args, env, signal: options?.signal });
|
||||||
command: "restic",
|
|
||||||
args,
|
|
||||||
env,
|
|
||||||
signal: options?.signal,
|
|
||||||
});
|
|
||||||
await cleanupTemporaryKeys(env);
|
await cleanupTemporaryKeys(env);
|
||||||
|
|
||||||
if (options?.signal?.aborted) {
|
if (options?.signal?.aborted) {
|
||||||
logger.warn("Restic check was aborted by signal.");
|
logger.warn("Restic check was aborted by signal.");
|
||||||
return {
|
return { success: false, hasErrors: true, output: "", error: "Operation aborted" };
|
||||||
success: false,
|
|
||||||
hasErrors: true,
|
|
||||||
output: "",
|
|
||||||
error: "Operation aborted",
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { stdout, stderr } = res;
|
const { stdout, stderr } = res;
|
||||||
|
|
@ -1042,22 +887,14 @@ const check = async (
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const repairIndex = async (
|
const repairIndex = async (config: RepositoryConfig, options: { signal?: AbortSignal; organizationId: string }) => {
|
||||||
config: RepositoryConfig,
|
|
||||||
options: { signal?: AbortSignal; organizationId: string },
|
|
||||||
) => {
|
|
||||||
const repoUrl = buildRepoUrl(config);
|
const repoUrl = buildRepoUrl(config);
|
||||||
const env = await buildEnv(config, options.organizationId);
|
const env = await buildEnv(config, options.organizationId);
|
||||||
|
|
||||||
const args = ["repair", "index", "--repo", repoUrl];
|
const args = ["repair", "index", "--repo", repoUrl];
|
||||||
addCommonArgs(args, env, config);
|
addCommonArgs(args, env, config);
|
||||||
|
|
||||||
const res = await exec({
|
const res = await exec({ command: "restic", args, env, signal: options?.signal });
|
||||||
command: "restic",
|
|
||||||
args,
|
|
||||||
env,
|
|
||||||
signal: options?.signal,
|
|
||||||
});
|
|
||||||
await cleanupTemporaryKeys(env);
|
await cleanupTemporaryKeys(env);
|
||||||
|
|
||||||
if (options?.signal?.aborted) {
|
if (options?.signal?.aborted) {
|
||||||
|
|
@ -1097,13 +934,7 @@ const copy = async (
|
||||||
RESTIC_FROM_PASSWORD_FILE: sourceEnv.RESTIC_PASSWORD_FILE,
|
RESTIC_FROM_PASSWORD_FILE: sourceEnv.RESTIC_PASSWORD_FILE,
|
||||||
};
|
};
|
||||||
|
|
||||||
const args: string[] = [
|
const args: string[] = ["--repo", destRepoUrl, "copy", "--from-repo", sourceRepoUrl];
|
||||||
"--repo",
|
|
||||||
destRepoUrl,
|
|
||||||
"copy",
|
|
||||||
"--from-repo",
|
|
||||||
sourceRepoUrl,
|
|
||||||
];
|
|
||||||
|
|
||||||
if (options.tag) {
|
if (options.tag) {
|
||||||
args.push("--tag", options.tag);
|
args.push("--tag", options.tag);
|
||||||
|
|
@ -1232,12 +1063,7 @@ export const restic = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cleanupTemporaryKeys = async (env: Record<string, string>) => {
|
export const cleanupTemporaryKeys = async (env: Record<string, string>) => {
|
||||||
const keysToClean = [
|
const keysToClean = ["_SFTP_KEY_PATH", "_SFTP_KNOWN_HOSTS_PATH", "RESTIC_CACERT", "GOOGLE_APPLICATION_CREDENTIALS"];
|
||||||
"_SFTP_KEY_PATH",
|
|
||||||
"_SFTP_KNOWN_HOSTS_PATH",
|
|
||||||
"RESTIC_CACERT",
|
|
||||||
"GOOGLE_APPLICATION_CREDENTIALS",
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const key of keysToClean) {
|
for (const key of keysToClean) {
|
||||||
if (env[key]) {
|
if (env[key]) {
|
||||||
|
|
@ -1246,10 +1072,7 @@ export const cleanupTemporaryKeys = async (env: Record<string, string>) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up custom password files
|
// Clean up custom password files
|
||||||
if (
|
if (env.RESTIC_PASSWORD_FILE && env.RESTIC_PASSWORD_FILE !== RESTIC_PASS_FILE) {
|
||||||
env.RESTIC_PASSWORD_FILE &&
|
|
||||||
env.RESTIC_PASSWORD_FILE !== RESTIC_PASS_FILE
|
|
||||||
) {
|
|
||||||
await fs.unlink(env.RESTIC_PASSWORD_FILE).catch(() => {});
|
await fs.unlink(env.RESTIC_PASSWORD_FILE).catch(() => {});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue