( deployment: Omit< z.infer<typeof apiCreateDeploymentBackup>, "deploymentId" | "createdAt" | "status" | "logPath" >, )
| 351 | }; |
| 352 | |
| 353 | export const createDeploymentBackup = async ( |
| 354 | deployment: Omit< |
| 355 | z.infer<typeof apiCreateDeploymentBackup>, |
| 356 | "deploymentId" | "createdAt" | "status" | "logPath" |
| 357 | >, |
| 358 | ) => { |
| 359 | const backup = await findBackupById(deployment.backupId); |
| 360 | |
| 361 | let serverId: string | null | undefined; |
| 362 | if (backup.backupType === "database") { |
| 363 | serverId = |
| 364 | backup.postgres?.serverId || |
| 365 | backup.mariadb?.serverId || |
| 366 | backup.mysql?.serverId || |
| 367 | backup.mongo?.serverId; |
| 368 | } else if (backup.backupType === "compose") { |
| 369 | serverId = backup.compose?.serverId; |
| 370 | } |
| 371 | await removeLastTenDeployments(deployment.backupId, "backup", serverId); |
| 372 | try { |
| 373 | const { LOGS_PATH } = paths(!!serverId); |
| 374 | const formattedDateTime = format(new Date(), "yyyy-MM-dd:HH:mm:ss"); |
| 375 | const fileName = `${backup.appName}-${formattedDateTime}.log`; |
| 376 | const logFilePath = path.join(LOGS_PATH, backup.appName, fileName); |
| 377 | |
| 378 | if (serverId) { |
| 379 | const server = await findServerById(serverId); |
| 380 | |
| 381 | const command = ` |
| 382 | mkdir -p ${LOGS_PATH}/${backup.appName}; |
| 383 | echo "Initializing backup\n" >> ${logFilePath}; |
| 384 | `; |
| 385 | |
| 386 | await execAsyncRemote(server.serverId, command); |
| 387 | } else { |
| 388 | await fsPromises.mkdir(path.join(LOGS_PATH, backup.appName), { |
| 389 | recursive: true, |
| 390 | }); |
| 391 | await fsPromises.writeFile(logFilePath, "Initializing backup\n"); |
| 392 | } |
| 393 | |
| 394 | const deploymentCreate = await db |
| 395 | .insert(deployments) |
| 396 | .values({ |
| 397 | backupId: deployment.backupId, |
| 398 | title: deployment.title || "Backup", |
| 399 | description: deployment.description || "", |
| 400 | status: "running", |
| 401 | logPath: logFilePath, |
| 402 | startedAt: new Date().toISOString(), |
| 403 | }) |
| 404 | .returning(); |
| 405 | if (deploymentCreate.length === 0 || !deploymentCreate[0]) { |
| 406 | throw new TRPCError({ |
| 407 | code: "BAD_REQUEST", |
| 408 | message: "Error creating the backup", |
| 409 | }); |
| 410 | } |
no test coverage detected