MCPcopy
hub / github.com/Dokploy/dokploy / runWebServerBackup

Function runWebServerBackup

packages/server/src/utils/backups/web-server.ts:25–135  ·  view source on GitHub ↗
(backup: BackupSchedule)

Source from the content-addressed store, hash-verified

23}
24
25export const runWebServerBackup = async (backup: BackupSchedule) => {
26 if (IS_CLOUD) {
27 return;
28 }
29
30 const deployment = await createDeploymentBackup({
31 backupId: backup.backupId,
32 title: "Web Server Backup",
33 description: "Web Server Backup",
34 });
35 const writeStream = createWriteStream(deployment.logPath, { flags: "a" });
36 let computedBackupSize: number | undefined;
37 try {
38 const destination = await findDestinationById(backup.destinationId);
39 const rcloneFlags = getS3Credentials(destination);
40 const timestamp = getBackupTimestamp();
41 const { BASE_PATH } = paths();
42 const tempDir = await mkdtemp(join(tmpdir(), "dokploy-backup-"));
43 const backupFileName = `webserver-backup-${timestamp}.zip`;
44 const s3Path = `:s3:${destination.bucket}/${backup.appName}/${normalizeS3Path(backup.prefix)}${backupFileName}`;
45
46 try {
47 await execAsync(`mkdir -p ${tempDir}/filesystem`);
48
49 // First get the container ID
50 const { stdout: containerId } = await execAsync(
51 `docker ps --filter "name=dokploy-postgres" --filter "status=running" -q | head -n 1`,
52 );
53
54 if (!containerId) {
55 writeStream.write("Dokploy postgres container not found❌\n");
56 writeStream.end();
57 throw new Error("Dokploy postgres container not found");
58 }
59
60 writeStream.write(`Dokploy postgres container ID: ${containerId}\n`);
61
62 const postgresContainerId = containerId.trim();
63
64 // First dump the database inside the container
65 const dumpCommand = `docker exec ${postgresContainerId} pg_dump -v -Fc -U dokploy -d dokploy -f /tmp/database.sql`;
66 writeStream.write(`Running dump command: ${dumpCommand}\n`);
67 await execAsync(dumpCommand);
68
69 // Then copy the file from the container to host
70 const copyCommand = `docker cp ${postgresContainerId}:/tmp/database.sql ${tempDir}/database.sql`;
71 writeStream.write(`Copying database dump: ${copyCommand}\n`);
72 await execAsync(copyCommand);
73
74 // Clean up the temp file in the container
75 const cleanupCommand = `docker exec ${postgresContainerId} rm -f /tmp/database.sql`;
76 writeStream.write(`Cleaning up temp file: ${cleanupCommand}\n`);
77 await execAsync(cleanupCommand);
78
79 await execAsync(
80 `rsync -a --ignore-errors --no-specials --no-devices --exclude='volume-backups/' ${BASE_PATH}/ ${tempDir}/filesystem/`,
81 );
82

Callers 2

scheduleBackupFunction · 0.90
backup.tsFile · 0.90

Calls 10

createDeploymentBackupFunction · 0.90
findDestinationByIdFunction · 0.90
getS3CredentialsFunction · 0.90
getBackupTimestampFunction · 0.90
pathsFunction · 0.90
normalizeS3PathFunction · 0.90
execAsyncFunction · 0.90
updateDeploymentStatusFunction · 0.90
formatBytesFunction · 0.70

Tested by

no test coverage detected