| 1 | import { randomUUID } from "crypto"; |
| 2 | import { and, eq |
| 3 | import { z } |
| 4 | import { ORPCError } |
| 5 | import { db } |
| 6 | import { backups, billingSubscriptions |
| 7 | import { createWingsClient } |
| 8 | import { safeDecrypt } |
| 9 | import { signBackupDownloadToken } |
| 10 | import { requireServerAccess } |
| 11 | import { adapterStringForType, resolveDestinationForNode |
| 12 | import { deleteBackupObject, presignDownloadUrl |
| 13 | import { |
| 14 | ensureGDriveFolders, |
| 15 | getAppUrl, |
| 16 | getGDriveConnection, |
| 17 | getOperatorGDriveConfig, |
| 18 | } from "../services/google-drive"; |
| 19 | import { recordActivity } |
| 20 | import { protectedProcedure } |
| 21 | |
| 22 | export const backupsRouter = |
| 23 | list: protectedProcedure |
| 24 | .input(z.object |
| 25 | .handler(async ( |
| 26 | await requireServerAccess(context |
| 27 | return db.query |
| 28 | where: eq(backups |
| 29 | }); |
| 30 | }), |
| 31 | |
| 32 | create: protectedProcedure |
| 33 | .input( |
| 34 | z.object({ |
| 35 | serverId: z.string |
| 36 | name: z.string |
| 37 | ignoredFiles: z.string |
| 38 | destination: z.enum |
| 39 | }), |
| 40 | ) |
| 41 | .handler(async ( |
| 42 | const server = |
| 43 | context.session.user |
| 44 | input.serverId, |
| 45 | context.session.user |
| 46 | ); |
| 47 | |
| 48 | let backupLimit = |
| 49 | if (server.subscriptionId |
| 50 | const subscription = |
| 51 | where: and( |
| 52 | eq(billingSubscriptions.id |
| 53 | or( |
| 54 | eq(billingSubscriptions.status |
| 55 | eq(billingSubscriptions.status |
| 56 | ), |
| 57 | ), |
| 58 | with: { plan: { |
| 59 | }); |
| 60 | backupLimit = |
| 61 | (subscription?.plan?.resourceLimits |
| 62 | } |
| 63 | const id = |
| 64 | const uuid = |
| 65 | const node = |
| 66 | |
| 67 | let adapter: |
| 68 | let driveUserId: |
| 69 | if (input.destination |
| 70 | const config = |
| 71 | if (!config) |
| 72 | throw new ORPCError( |
| 73 | message: "Google Drive is not configured by the administrator", |
| 74 | }); |
| 75 | } |
| 76 | const connection = |
| 77 | if (!connection) |
| 78 | throw new ORPCError( |
| 79 | message: "Connect your Google Drive in account settings", |
| 80 | }); |
| 81 | } |
| 82 | await ensureGDriveFolders(connection |
| 83 | adapter = "google-drive" |
| 84 | driveUserId = context. |
| 85 | } else { |
| 86 | const destination = |
| 87 | adapter = destination |
| 88 | } |
| 89 | |
| 90 | await db.transaction |
| 91 | if (backupLimit > |
| 92 | await tx |
| 93 | .select({ id: |
| 94 | .from(servers) |
| 95 | .where(eq(servers |
| 96 | .for("update") |
| 97 | const used = |
| 98 | backups, |
| 99 | and( |
| 100 | eq(backups.serverId |
| 101 | eq(backups.isSuccessful |
| 102 | ne(backups.disk |
| 103 | ), |
| 104 | ); |
| 105 | if (used >= |
| 106 | throw new ORPCError( |
| 107 | message: "Backup limit reached for this server", |
| 108 | }); |
| 109 | } |
| 110 | } |
| 111 | await tx.insert |
| 112 | id, |
| 113 | uuid, |
| 114 | serverId: input.serverId |
| 115 | name: input.name |
| 116 | ignoredFiles: input.ignoredFiles |
| 117 | disk: adapter, |
| 118 | driveUserId, |
| 119 | }); |
| 120 | }); |
| 121 | |
| 122 | const client = |
| 123 | await client.createBackup |
| 124 | uuid, |
| 125 | ignore: input.ignoredFiles |
| 126 | adapter, |
| 127 | }); |
| 128 | |
| 129 | recordActivity({ |
| 130 | eventType: "server:backup.start", |
| 131 | userId: context.session |
| 132 | serverId: input.serverId |
| 133 | ip: context.ip |
| 134 | properties: { name: input |
| 135 | }); |
| 136 | |
| 137 | return db.query |
| 138 | }), |
| 139 | |
| 140 | delete: protectedProcedure |
| 141 | .input(z.object |
| 142 | .handler(async ( |
| 143 | const server = |
| 144 | context.session.user |
| 145 | input.serverId, |
| 146 | context.session.user |
| 147 | ); |
| 148 | |
| 149 | const backup = |
| 150 | where: and(eq |
| 151 | }); |
| 152 | if (!backup) |
| 153 | if (backup.isLocked |
| 154 | |
| 155 | const node = |
| 156 | const client = |
| 157 | await client.deleteBackup |
| 158 | |
| 159 | if (backup.disk |
| 160 | const destination = |
| 161 | if (destination?.type |
| 162 | await deleteBackupObject(destination |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | await db.delete |
| 167 | |
| 168 | recordActivity({ |
| 169 | eventType: "server:backup.delete", |
| 170 | userId: context.session |
| 171 | serverId: input.serverId |
| 172 | ip: context.ip |
| 173 | properties: { name: backup |
| 174 | }); |
| 175 | }), |
| 176 | |
| 177 | getDownloadUrl: protectedProcedure |
| 178 | .input(z.object |
| 179 | .handler(async ( |
| 180 | const server = |
| 181 | context.session.user |
| 182 | input.serverId, |
| 183 | context.session.user |
| 184 | ); |
| 185 | |
| 186 | const backup = |
| 187 | where: and(eq |
| 188 | }); |
| 189 | if (!backup || |
| 190 | |
| 191 | const node = |
| 192 | |
| 193 | if (backup.disk |
| 194 | const destination = |
| 195 | if (!destination || |
| 196 | throw new ORPCError( |
| 197 | message: "The S3 backup destination for this node is no longer configured", |
| 198 | }); |
| 199 | } |
| 200 | if (!destination.allowPublicDownload |
| 201 | throw new ORPCError( |
| 202 | message: "Backup downloads are disabled for this storage destination", |
| 203 | }); |
| 204 | } |
| 205 | return { url: |
| 206 | } |
| 207 | |
| 208 | if (backup.disk |
| 209 | const connection = |
| 210 | ? await getGDriveConnection( |
| 211 | : null; |
| 212 | if (!connection) |
| 213 | throw new ORPCError( |
| 214 | message: "Google Drive connection removed", |
| 215 | }); |
| 216 | } |
| 217 | return { url: |
| 218 | } |
| 219 | |
| 220 | const token = |
| 221 | context.session.user |
| 222 | server.uuid, |
| 223 | backup.uuid, |
| 224 | safeDecrypt(node.token |
| 225 | ); |
| 226 | const url = |
| 227 | return { url |
| 228 | }), |
| 229 | |
| 230 | getDownloadAvailability: protectedProcedure |
| 231 | .input(z.object |
| 232 | .handler(async ( |
| 233 | const server = |
| 234 | context.session.user |
| 235 | input.serverId, |
| 236 | context.session.user |
| 237 | ); |
| 238 | const node = |
| 239 | const [destination, |
| 240 | resolveDestinationForNode(node.id |
| 241 | getOperatorGDriveConfig(), |
| 242 | getGDriveConnection(context.session |
| 243 | ]); |
| 244 | const s3PublicDownloads = |
| 245 | destination?.type === |
| 246 | return { |
| 247 | s3PublicDownloads, |
| 248 | googleDriveConfigured: !!gdriveConfig, |
| 249 | googleDriveConnected: !!gdriveConnection, |
| 250 | }; |
| 251 | }), |
| 252 | |
| 253 | restore: protectedProcedure |
| 254 | .input(z.object |
| 255 | .handler(async ( |
| 256 | const server = |
| 257 | context.session.user |
| 258 | input.serverId, |
| 259 | context.session.user |
| 260 | ); |
| 261 | |
| 262 | const backup = |
| 263 | where: and(eq |
| 264 | }); |
| 265 | if (!backup || |
| 266 | |
| 267 | const node = |
| 268 | const adapter = |
| 269 | backup.disk && |
| 270 | |
| 271 | let downloadUrl: |
| 272 | if (adapter === |
| 273 | const destination = |
| 274 | if (!destination || |
| 275 | throw new ORPCError( |
| 276 | message: "The S3 backup destination for this node is no longer configured", |
| 277 | }); |
| 278 | } |
| 279 | downloadUrl = await |
| 280 | } |
| 281 | |
| 282 | const client = |
| 283 | await client.restoreBackup |
| 284 | adapter, |
| 285 | download_url: downloadUrl, |
| 286 | }); |
| 287 | |
| 288 | recordActivity({ |
| 289 | eventType: "server:backup.restore", |
| 290 | userId: context.session |
| 291 | serverId: input.serverId |
| 292 | ip: context.ip |
| 293 | properties: { name: backup |
| 294 | }); |
| 295 | }), |
| 296 | }; |