Enhance backup logging and error handling in web server backup utility

- Added detailed logging for PostgreSQL container retrieval and command execution, improving traceability during backup processes.
- Updated success and error messages to provide clearer feedback on backup operations, including specific command outputs and error details.
- Ensured proper stream handling for logging messages, enhancing the overall robustness of the backup utility.
This commit is contained in:
Mauricio Siu 2025-05-03 13:41:19 -06:00
parent b3e2af3b40
commit 557c89ac6d

View File

@ -42,24 +42,35 @@ export const runWebServerBackup = async (backup: BackupSchedule) => {
); );
if (!containerId) { if (!containerId) {
writeStream.write("PostgreSQL container not found❌");
writeStream.end();
throw new Error("PostgreSQL container not found"); throw new Error("PostgreSQL container not found");
} }
writeStream.write(`PostgreSQL container ID: ${containerId}`);
const postgresContainerId = containerId.trim(); const postgresContainerId = containerId.trim();
const postgresCommand = `docker exec ${postgresContainerId} pg_dump -v -Fc -U dokploy -d dokploy > '${tempDir}/database.sql'`; const postgresCommand = `docker exec ${postgresContainerId} pg_dump -v -Fc -U dokploy -d dokploy > '${tempDir}/database.sql'`;
writeStream.write(`Running command: ${postgresCommand}`);
await execAsync(postgresCommand); await execAsync(postgresCommand);
await execAsync(`cp -r ${BASE_PATH}/* ${tempDir}/filesystem/`); await execAsync(`cp -r ${BASE_PATH}/* ${tempDir}/filesystem/`);
writeStream.write("Copied filesystem to temp directory");
await execAsync( await execAsync(
// Zip all .sql files since we created more than one // Zip all .sql files since we created more than one
`cd ${tempDir} && zip -r ${backupFileName} *.sql filesystem/ > /dev/null 2>&1`, `cd ${tempDir} && zip -r ${backupFileName} *.sql filesystem/ > /dev/null 2>&1`,
); );
writeStream.write("Zipped database and filesystem");
const uploadCommand = `rclone copyto ${rcloneFlags.join(" ")} "${tempDir}/${backupFileName}" "${s3Path}"`; const uploadCommand = `rclone copyto ${rcloneFlags.join(" ")} "${tempDir}/${backupFileName}" "${s3Path}"`;
writeStream.write(`Running command: ${uploadCommand}`);
await execAsync(uploadCommand); await execAsync(uploadCommand);
writeStream.write("Backup done✅"); writeStream.write("Uploaded backup to S3 ✅");
writeStream.end(); writeStream.end();
await updateDeploymentStatus(deployment.deploymentId, "done"); await updateDeploymentStatus(deployment.deploymentId, "done");
return true; return true;
@ -68,7 +79,7 @@ export const runWebServerBackup = async (backup: BackupSchedule) => {
} }
} catch (error) { } catch (error) {
console.error("Backup error:", error); console.error("Backup error:", error);
writeStream.write("Backup error❌"); writeStream.write("Backup error❌\n");
writeStream.write(error instanceof Error ? error.message : "Unknown error"); writeStream.write(error instanceof Error ? error.message : "Unknown error");
writeStream.end(); writeStream.end();
await updateDeploymentStatus(deployment.deploymentId, "error"); await updateDeploymentStatus(deployment.deploymentId, "error");