Skip to content

Commit f633d4b

Browse files
committed
fix: run database dump once per backup and clean up partial uploads on failure
1 parent b7db284 commit f633d4b

7 files changed

Lines changed: 23 additions & 38 deletions

File tree

‎packages/server/src/utils/backups/compose.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ export const runComposeBackup = async (
3737
try {
3838
const rcloneFlags = getS3Credentials(destination);
3939
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
40-
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
41-
4240
const backupCommand = getBackupCommand(
4341
backup,
44-
rcloneCommand,
42+
rcloneFlags,
43+
rcloneDestination,
4544
deployment.logPath,
4645
);
4746
if (compose.serverId) {

‎packages/server/src/utils/backups/libsql.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ export const runLibsqlBackup = async (
3636
try {
3737
const rcloneFlags = getS3Credentials(destination);
3838
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
39-
40-
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
41-
4239
const backupCommand = getBackupCommand(
4340
backup,
44-
rcloneCommand,
41+
rcloneFlags,
42+
rcloneDestination,
4543
deployment.logPath,
4644
);
4745
if (libsql.serverId) {

‎packages/server/src/utils/backups/mariadb.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ export const runMariadbBackup = async (
3535
try {
3636
const rcloneFlags = getS3Credentials(destination);
3737
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
38-
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
39-
4038
const backupCommand = getBackupCommand(
4139
backup,
42-
rcloneCommand,
40+
rcloneFlags,
41+
rcloneDestination,
4342
deployment.logPath,
4443
);
4544
if (mariadb.serverId) {

‎packages/server/src/utils/backups/mongo.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ export const runMongoBackup = async (mongo: Mongo, backup: BackupSchedule) => {
3232
try {
3333
const rcloneFlags = getS3Credentials(destination);
3434
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
35-
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
36-
3735
const backupCommand = getBackupCommand(
3836
backup,
39-
rcloneCommand,
37+
rcloneFlags,
38+
rcloneDestination,
4039
deployment.logPath,
4140
);
4241

‎packages/server/src/utils/backups/mysql.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ export const runMySqlBackup = async (mysql: MySql, backup: BackupSchedule) => {
3333
try {
3434
const rcloneFlags = getS3Credentials(destination);
3535
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
36-
37-
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
38-
3936
const backupCommand = getBackupCommand(
4037
backup,
41-
rcloneCommand,
38+
rcloneFlags,
39+
rcloneDestination,
4240
deployment.logPath,
4341
);
4442

‎packages/server/src/utils/backups/postgres.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ export const runPostgresBackup = async (
3636
try {
3737
const rcloneFlags = getS3Credentials(destination);
3838
const rcloneDestination = `:s3:${destination.bucket}/${bucketDestination}`;
39-
40-
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
41-
4239
const backupCommand = getBackupCommand(
4340
backup,
44-
rcloneCommand,
41+
rcloneFlags,
42+
rcloneDestination,
4543
deployment.logPath,
4644
);
4745
if (postgres.serverId) {

‎packages/server/src/utils/backups/utils.ts‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,14 @@ export const generateBackupCommand = (backup: BackupSchedule) => {
259259

260260
export const getBackupCommand = (
261261
backup: BackupSchedule,
262-
rcloneCommand: string,
262+
rcloneFlags: string[],
263+
rcloneDestination: string,
263264
logPath: string,
264265
) => {
265266
const containerSearch = getContainerSearchCommand(backup);
266267
const backupCommand = generateBackupCommand(backup);
268+
const rcloneCommand = `rclone rcat ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
269+
const rcloneDeleteCommand = `rclone deletefile ${rcloneFlags.join(" ")} "${rcloneDestination}"`;
267270

268271
logger.info(
269272
{
@@ -279,33 +282,24 @@ export const getBackupCommand = (
279282
set -eo pipefail;
280283
echo "[$(date)] Starting backup process..." >> ${logPath};
281284
echo "[$(date)] Executing backup command..." >> ${logPath};
282-
CONTAINER_ID=$(${containerSearch})
285+
CONTAINER_ID=$(${containerSearch});
283286
284287
if [ -z "$CONTAINER_ID" ]; then
285288
echo "[$(date)] ❌ Error: Container not found" >> ${logPath};
286289
exit 1;
287-
fi
290+
fi;
288291
289292
echo "[$(date)] Container Up: $CONTAINER_ID" >> ${logPath};
293+
echo "[$(date)] Starting backup and upload to S3..." >> ${logPath};
290294
291-
# Run the backup command and capture the exit status
292-
BACKUP_OUTPUT=$(${backupCommand} 2>&1 >/dev/null) || {
295+
UPLOAD_OUTPUT=$({ ${backupCommand} | ${rcloneCommand}; } 2>&1 >/dev/null) || {
293296
echo "[$(date)] ❌ Error: Backup failed" >> ${logPath};
294-
echo "Error: $BACKUP_OUTPUT" >> ${logPath};
295-
exit 1;
296-
}
297-
298-
echo "[$(date)] ✅ backup completed successfully" >> ${logPath};
299-
echo "[$(date)] Starting upload to S3..." >> ${logPath};
300-
301-
# Run the upload command and capture the exit status
302-
UPLOAD_OUTPUT=$(${backupCommand} | ${rcloneCommand} 2>&1 >/dev/null) || {
303-
echo "[$(date)] ❌ Error: Upload to S3 failed" >> ${logPath};
304297
echo "Error: $UPLOAD_OUTPUT" >> ${logPath};
298+
${rcloneDeleteCommand} >/dev/null 2>&1 || true;
305299
exit 1;
306-
}
300+
};
307301
308-
echo "[$(date)] ✅ Upload to S3 completed successfully" >> ${logPath};
302+
echo "[$(date)] ✅ Backup uploaded to S3 successfully" >> ${logPath};
309303
echo "Backup done ✅" >> ${logPath};
310304
`;
311305
};

0 commit comments

Comments
 (0)