fix(restore): streamline container ID retrieval for database operations

Refactor the database restore process to consistently use a single container ID for the PostgreSQL container. This change enhances reliability by ensuring that commands are executed against the correct container, preventing potential errors from multiple matches.

Co-authored-by: Merloss 54235902+Merloss@users.noreply.github.com
This commit is contained in:
Mauricio Siu
2025-04-26 16:07:50 -06:00
parent 8d28a50a17
commit 461d7c530a
2 changed files with 23 additions and 18 deletions

View File

@@ -24,28 +24,23 @@ export const runWebServerBackup = async (backup: BackupSchedule) => {
await execAsync(`mkdir -p ${tempDir}/filesystem`);
// First get the container ID
// Returns: ID\nID\nID...
const { stdout: containerId } = await execAsync(
"docker ps --filter 'name=dokploy-postgres' -q",
`docker ps --filter "name=dokploy-postgres" --filter "status=running" -q | head -n 1`,
);
if (!containerId) {
throw new Error("PostgreSQL container not found");
}
// ID\nID\nID... => [ "ID", "ID", ... ]
const containers = containerId.trim().split("\n").filter(Boolean);
const postgresContainerId = containerId.trim();
// Then run pg_dump with the container ID
for (const containerId of containers) {
// Maybe we can find a better identification for this part vvv
const postgresCommand = `docker exec ${containerId.trim()} pg_dump -v -Fc -U dokploy -d dokploy > '${tempDir}/database-${containerId}.sql'`;
await execAsync(postgresCommand);
}
const postgresCommand = `docker exec ${postgresContainerId} pg_dump -v -Fc -U dokploy -d dokploy > '${tempDir}/database.sql'`;
await execAsync(postgresCommand);
await execAsync(`cp -r ${BASE_PATH}/* ${tempDir}/filesystem/`);
await execAsync( // Zip all .sql files since we created more than one
await execAsync(
// Zip all .sql files since we created more than one
`cd ${tempDir} && zip -r ${backupFileName} *.sql filesystem/ > /dev/null 2>&1`,
);