From 634d4dd8215a34fe3d386627cd2c9baeaf7c12dc Mon Sep 17 00:00:00 2001 From: Zack Pollard Date: Thu, 7 Nov 2024 13:37:20 +0000 Subject: [PATCH] fix: attempt to delete failed backup files immediately after failure --- server/src/services/backup.service.spec.ts | 8 ++++++++ server/src/services/backup.service.ts | 3 +++ 2 files changed, 11 insertions(+) diff --git a/server/src/services/backup.service.spec.ts b/server/src/services/backup.service.spec.ts index df2f80b37c99f..13a09c8b9b97d 100644 --- a/server/src/services/backup.service.spec.ts +++ b/server/src/services/backup.service.spec.ts @@ -165,6 +165,7 @@ describe(BackupService.name, () => { storageMock.readdir.mockResolvedValue([]); processMock.spawn.mockReturnValue(mockSpawn(0, 'data', '')); storageMock.rename.mockResolvedValue(); + storageMock.unlink.mockResolvedValue(); systemMock.get.mockResolvedValue(systemConfigStub.backupEnabled); storageMock.createWriteStream.mockReturnValue(new PassThrough()); }); @@ -207,5 +208,12 @@ describe(BackupService.name, () => { const result = await sut.handleBackupDatabase(); expect(result).toBe(JobStatus.FAILED); }); + it('should ignore unlink failing and still return failed job status', async () => { + processMock.spawn.mockReturnValueOnce(mockSpawn(1, '', 'error')); + storageMock.unlink.mockRejectedValue(new Error('error')); + const result = await sut.handleBackupDatabase(); + expect(storageMock.unlink).toHaveBeenCalled(); + expect(result).toBe(JobStatus.FAILED); + }); }); }); diff --git a/server/src/services/backup.service.ts b/server/src/services/backup.service.ts index 1febe791305b4..a1b39fc3eaed3 100644 --- a/server/src/services/backup.service.ts +++ b/server/src/services/backup.service.ts @@ -150,6 +150,9 @@ export class BackupService extends BaseService { await this.storageRepository.rename(backupFilePath, backupFilePath.replace('.tmp', '')); } catch (error) { this.logger.error('Database Backup Failure', error); + await this.storageRepository + .unlink(backupFilePath) + .catch((error) => this.logger.error('Failed to delete failed backup file', error)); return JobStatus.FAILED; }