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