| 1 | import { eq, isNull } from "drizzle-orm"; |
| 2 | import { z } |
| 3 | import { db } |
| 4 | import { backupDestinations } |
| 5 | import { decrypt } |
| 6 | |
| 7 | export const BACKUP_DESTINATION_TYPES = |
| 8 | "wings", |
| 9 | "ddup_bak", |
| 10 | "s3", |
| 11 | "restic", |
| 12 | "pbs", |
| 13 | "kopia", |
| 14 | ] as const; |
| 15 | |
| 16 | export type BackupDestinationType |
| 17 | |
| 18 | const s3ConfigSchema = z |
| 19 | endpoint: z.string |
| 20 | region: z.string |
| 21 | bucket: z.string |
| 22 | accessKey: z.string |
| 23 | secretKey: z.string |
| 24 | usePathStyle: z.boolean |
| 25 | allowPublicDownload: z.boolean |
| 26 | }); |
| 27 | |
| 28 | const resticConfigSchema = z |
| 29 | repository: z.string |
| 30 | password: z.string |
| 31 | retryLockSeconds: z.number |
| 32 | environment: z.record |
| 33 | }); |
| 34 | |
| 35 | const pbsConfigSchema = z |
| 36 | url: z.string |
| 37 | datastore: z.string |
| 38 | namespace: z.string |
| 39 | tokenId: z.string |
| 40 | tokenSecret: z.string |
| 41 | fingerprint: z.string |
| 42 | backupIdPrefix: z.string |
| 43 | }); |
| 44 | |
| 45 | const kopiaConfigSchema = z |
| 46 | url: z.string |
| 47 | username: z.string |
| 48 | password: z.string |
| 49 | fingerprint: z.string |
| 50 | tags: z.record |
| 51 | }); |
| 52 | |
| 53 | export const backupDestinationInputSchema = |
| 54 | z.object({ |
| 55 | z.object({ |
| 56 | z.object({ |
| 57 | z.object({ |
| 58 | z.object({ |
| 59 | z.object({ |
| 60 | ]); |
| 61 | |
| 62 | export type BackupDestinationInput |
| 63 | |
| 64 | export type S3DestinationConfig |
| 65 | export type ResticDestinationConfig |
| 66 | export type PbsDestinationConfig |
| 67 | export type KopiaDestinationConfig |
| 68 | |
| 69 | export function adapterStringForType(type |
| 70 | switch (type) { |
| 71 | case "wings": |
| 72 | return "wings"; |
| 73 | case "ddup_bak": |
| 74 | return "ddup-bak"; |
| 75 | case "s3": |
| 76 | return "s3"; |
| 77 | case "restic": |
| 78 | return "restic"; |
| 79 | case "pbs": |
| 80 | return "proxmox-backup-server"; |
| 81 | case "kopia": |
| 82 | return "kopia"; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | function parseDestinationRow( |
| 87 | row: typeof |
| 88 | ): BackupDestinationInput | |
| 89 | try { |
| 90 | const config = |
| 91 | return backupDestinationInputSchema.parse |
| 92 | } catch { |
| 93 | return null; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | export async function getDestinationForNode |
| 98 | nodeId: string |
| 99 | ): Promise<BackupDestinationInput |
| 100 | const row = |
| 101 | where: nodeId |
| 102 | ? eq(backupDestinations. |
| 103 | : isNull(backupDestinations. |
| 104 | }); |
| 105 | return row ? |
| 106 | } |
| 107 | |
| 108 | export async function resolveDestinationForNode |
| 109 | nodeId: string, |
| 110 | ): Promise<BackupDestinationInput |
| 111 | const nodeDestination = |
| 112 | if (nodeDestination) return |
| 113 | return getDestinationForNode(null) |
| 114 | } |