( compose: Compose, backup: BackupSchedule, )
| 17 | } from "./utils"; |
| 18 | |
| 19 | export const runComposeBackup = async ( |
| 20 | compose: Compose, |
| 21 | backup: BackupSchedule, |
| 22 | ) => { |
| 23 | const { environmentId, name, appName } = compose; |
| 24 | const environment = await findEnvironmentById(environmentId); |
| 25 | const project = await findProjectById(environment.projectId); |
| 26 | const { prefix, databaseType, serviceName } = backup; |
| 27 | const destination = await findDestinationById(backup.destinationId); |
| 28 | const backupFileName = `${getBackupTimestamp()}.${databaseType === "mongo" ? "bson" : "sql"}.gz`; |
| 29 | const s3AppName = serviceName ? `${appName}_${serviceName}` : appName; |
| 30 | const bucketDestination = `${s3AppName}/${normalizeS3Path(prefix)}${backupFileName}`; |
| 31 | const deployment = await createDeploymentBackup({ |
| 32 | backupId: backup.backupId, |
| 33 | title: "Compose Backup", |
| 34 | description: "Compose Backup", |
| 35 | }); |
| 36 | |
| 37 | try { |
| 38 | const rcloneFlags = getS3Credentials(destination); |
| 39 | const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`; |
| 40 | const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`; |
| 41 | |
| 42 | const backupCommand = getBackupCommand( |
| 43 | backup, |
| 44 | rcloneCommand, |
| 45 | deployment.logPath, |
| 46 | ); |
| 47 | if (compose.serverId) { |
| 48 | await execAsyncRemote(compose.serverId, backupCommand); |
| 49 | } else { |
| 50 | await execAsync(backupCommand, { |
| 51 | shell: "/bin/bash", |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | await sendDatabaseBackupNotifications({ |
| 56 | applicationName: name, |
| 57 | projectName: project.name, |
| 58 | databaseType: getDatabaseType(databaseType), |
| 59 | type: "success", |
| 60 | organizationId: project.organizationId, |
| 61 | databaseName: backup.database, |
| 62 | }); |
| 63 | |
| 64 | await updateDeploymentStatus(deployment.deploymentId, "done"); |
| 65 | } catch (error) { |
| 66 | console.log(error); |
| 67 | await sendDatabaseBackupNotifications({ |
| 68 | applicationName: name, |
| 69 | projectName: project.name, |
| 70 | databaseType: getDatabaseType(databaseType), |
| 71 | type: "error", |
| 72 | // @ts-ignore |
| 73 | errorMessage: error?.message || "Error message not provided", |
| 74 | organizationId: project.organizationId, |
| 75 | databaseName: backup.database, |
| 76 | }); |
no test coverage detected