( destination: Destination, backupFile: string, emit: (log: string) => void, )
| 7 | import { execAsync } from "../process/execAsync"; |
| 8 | |
| 9 | export const restoreWebServerBackup = async ( |
| 10 | destination: Destination, |
| 11 | backupFile: string, |
| 12 | emit: (log: string) => void, |
| 13 | ) => { |
| 14 | if (IS_CLOUD) { |
| 15 | return; |
| 16 | } |
| 17 | try { |
| 18 | const rcloneFlags = getS3Credentials(destination); |
| 19 | const bucketPath = `:s3:${destination.bucket}`; |
| 20 | const backupPath = `${bucketPath}/${backupFile}`; |
| 21 | const { BASE_PATH } = paths(); |
| 22 | |
| 23 | // Create a temporary directory outside of BASE_PATH |
| 24 | const tempDir = await mkdtemp(join(tmpdir(), "dokploy-restore-")); |
| 25 | |
| 26 | try { |
| 27 | emit("Starting restore..."); |
| 28 | emit(`Backup path: ${backupPath}`); |
| 29 | emit(`Temp directory: ${tempDir}`); |
| 30 | |
| 31 | // Create temp directory |
| 32 | emit("Creating temporary directory..."); |
| 33 | await execAsync(`mkdir -p ${tempDir}`); |
| 34 | |
| 35 | // Download backup from S3 |
| 36 | emit("Downloading backup from S3..."); |
| 37 | await execAsync( |
| 38 | `rclone copyto ${rcloneFlags.join(" ")} "${backupPath}" "${tempDir}/${backupFile}"`, |
| 39 | ); |
| 40 | |
| 41 | // List files before extraction |
| 42 | emit("Listing files before extraction..."); |
| 43 | const { stdout: beforeFiles } = await execAsync(`ls -la ${tempDir}`); |
| 44 | emit(`Files before extraction: ${beforeFiles}`); |
| 45 | |
| 46 | // Extract backup |
| 47 | emit("Extracting backup..."); |
| 48 | await execAsync(`cd ${tempDir} && unzip ${backupFile} > /dev/null 2>&1`); |
| 49 | |
| 50 | // Restore filesystem first |
| 51 | emit("Restoring filesystem..."); |
| 52 | emit(`Copying from ${tempDir}/filesystem/* to ${BASE_PATH}/`); |
| 53 | |
| 54 | // First clean the target directory |
| 55 | emit("Cleaning target directory..."); |
| 56 | await execAsync(`rm -rf "${BASE_PATH}/"*`); |
| 57 | |
| 58 | // Ensure the target directory exists |
| 59 | emit("Setting up target directory..."); |
| 60 | await execAsync(`mkdir -p "${BASE_PATH}"`); |
| 61 | |
| 62 | // Copy files preserving permissions |
| 63 | emit("Copying files..."); |
| 64 | await execAsync(`cp -rp "${tempDir}/filesystem/"* "${BASE_PATH}/"`); |
| 65 | |
| 66 | // Now handle database restore |
no test coverage detected