( postgres: Postgres, destination: Destination, backupInput: z.infer<typeof apiRestoreBackup>, emit: (log: string) => void, )
| 7 | import { getRestoreCommand } from "./utils"; |
| 8 | |
| 9 | export const restorePostgresBackup = async ( |
| 10 | postgres: Postgres, |
| 11 | destination: Destination, |
| 12 | backupInput: z.infer<typeof apiRestoreBackup>, |
| 13 | emit: (log: string) => void, |
| 14 | ) => { |
| 15 | try { |
| 16 | const { appName, databaseUser, serverId } = postgres; |
| 17 | |
| 18 | const rcloneFlags = getS3Credentials(destination); |
| 19 | const bucketPath = `:s3:${destination.bucket}`; |
| 20 | |
| 21 | const backupPath = `${bucketPath}/${backupInput.backupFile}`; |
| 22 | |
| 23 | const rcloneCommand = `rclone cat ${rcloneFlags.join(" ")} "${backupPath}" | gunzip`; |
| 24 | |
| 25 | const command = getRestoreCommand({ |
| 26 | appName, |
| 27 | credentials: { |
| 28 | database: backupInput.databaseName, |
| 29 | databaseUser, |
| 30 | }, |
| 31 | type: "postgres", |
| 32 | rcloneCommand, |
| 33 | restoreType: "database", |
| 34 | }); |
| 35 | |
| 36 | emit("Starting restore..."); |
| 37 | emit( |
| 38 | `Restoring database: ${backupInput.databaseName} from ${backupInput.backupFile}`, |
| 39 | ); |
| 40 | |
| 41 | if (serverId) { |
| 42 | await execAsyncRemote(serverId, command); |
| 43 | } else { |
| 44 | await execAsync(command); |
| 45 | } |
| 46 | |
| 47 | emit("Restore completed successfully!"); |
| 48 | } catch (error) { |
| 49 | emit( |
| 50 | `Error: ${ |
| 51 | error instanceof Error |
| 52 | ? error.message |
| 53 | : "Error restoring postgres backup" |
| 54 | }`, |
| 55 | ); |
| 56 | throw error; |
| 57 | } |
| 58 | }; |
no test coverage detected