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