feat(backend): backup service with retention policy

This commit is contained in:
Nicolas Meienberger
2025-10-25 19:43:36 +02:00
parent 2202ad3247
commit 43e31596f1
16 changed files with 1688 additions and 3 deletions

View File

@@ -23,6 +23,12 @@ import {
deleteRepository,
getRepository,
listSnapshots,
listBackupSchedules,
createBackupSchedule,
deleteBackupSchedule,
getBackupSchedule,
updateBackupSchedule,
runBackupNow,
} from "../sdk.gen";
import { queryOptions, type UseMutationOptions, type DefaultError } from "@tanstack/react-query";
import type {
@@ -59,6 +65,16 @@ import type {
DeleteRepositoryResponse,
GetRepositoryData,
ListSnapshotsData,
ListBackupSchedulesData,
CreateBackupScheduleData,
CreateBackupScheduleResponse,
DeleteBackupScheduleData,
DeleteBackupScheduleResponse,
GetBackupScheduleData,
UpdateBackupScheduleData,
UpdateBackupScheduleResponse,
RunBackupNowData,
RunBackupNowResponse,
} from "../types.gen";
import { client as _heyApiClient } from "../client.gen";
@@ -693,3 +709,174 @@ export const listSnapshotsOptions = (options: Options<ListSnapshotsData>) => {
queryKey: listSnapshotsQueryKey(options),
});
};
export const listBackupSchedulesQueryKey = (options?: Options<ListBackupSchedulesData>) =>
createQueryKey("listBackupSchedules", options);
/**
* List all backup schedules
*/
export const listBackupSchedulesOptions = (options?: Options<ListBackupSchedulesData>) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await listBackupSchedules({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: listBackupSchedulesQueryKey(options),
});
};
export const createBackupScheduleQueryKey = (options?: Options<CreateBackupScheduleData>) =>
createQueryKey("createBackupSchedule", options);
/**
* Create a new backup schedule for a volume
*/
export const createBackupScheduleOptions = (options?: Options<CreateBackupScheduleData>) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await createBackupSchedule({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: createBackupScheduleQueryKey(options),
});
};
/**
* Create a new backup schedule for a volume
*/
export const createBackupScheduleMutation = (
options?: Partial<Options<CreateBackupScheduleData>>,
): UseMutationOptions<CreateBackupScheduleResponse, DefaultError, Options<CreateBackupScheduleData>> => {
const mutationOptions: UseMutationOptions<
CreateBackupScheduleResponse,
DefaultError,
Options<CreateBackupScheduleData>
> = {
mutationFn: async (localOptions) => {
const { data } = await createBackupSchedule({
...options,
...localOptions,
throwOnError: true,
});
return data;
},
};
return mutationOptions;
};
/**
* Delete a backup schedule
*/
export const deleteBackupScheduleMutation = (
options?: Partial<Options<DeleteBackupScheduleData>>,
): UseMutationOptions<DeleteBackupScheduleResponse, DefaultError, Options<DeleteBackupScheduleData>> => {
const mutationOptions: UseMutationOptions<
DeleteBackupScheduleResponse,
DefaultError,
Options<DeleteBackupScheduleData>
> = {
mutationFn: async (localOptions) => {
const { data } = await deleteBackupSchedule({
...options,
...localOptions,
throwOnError: true,
});
return data;
},
};
return mutationOptions;
};
export const getBackupScheduleQueryKey = (options: Options<GetBackupScheduleData>) =>
createQueryKey("getBackupSchedule", options);
/**
* Get a backup schedule by ID
*/
export const getBackupScheduleOptions = (options: Options<GetBackupScheduleData>) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await getBackupSchedule({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: getBackupScheduleQueryKey(options),
});
};
/**
* Update a backup schedule
*/
export const updateBackupScheduleMutation = (
options?: Partial<Options<UpdateBackupScheduleData>>,
): UseMutationOptions<UpdateBackupScheduleResponse, DefaultError, Options<UpdateBackupScheduleData>> => {
const mutationOptions: UseMutationOptions<
UpdateBackupScheduleResponse,
DefaultError,
Options<UpdateBackupScheduleData>
> = {
mutationFn: async (localOptions) => {
const { data } = await updateBackupSchedule({
...options,
...localOptions,
throwOnError: true,
});
return data;
},
};
return mutationOptions;
};
export const runBackupNowQueryKey = (options: Options<RunBackupNowData>) => createQueryKey("runBackupNow", options);
/**
* Trigger a backup immediately for a schedule
*/
export const runBackupNowOptions = (options: Options<RunBackupNowData>) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await runBackupNow({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: runBackupNowQueryKey(options),
});
};
/**
* Trigger a backup immediately for a schedule
*/
export const runBackupNowMutation = (
options?: Partial<Options<RunBackupNowData>>,
): UseMutationOptions<RunBackupNowResponse, DefaultError, Options<RunBackupNowData>> => {
const mutationOptions: UseMutationOptions<RunBackupNowResponse, DefaultError, Options<RunBackupNowData>> = {
mutationFn: async (localOptions) => {
const { data } = await runBackupNow({
...options,
...localOptions,
throwOnError: true,
});
return data;
},
};
return mutationOptions;
};