From 27e1354129402cf8f6897f0d561cb90b5e0c91eb Mon Sep 17 00:00:00 2001 From: sebseb7 Date: Thu, 2 Oct 2025 15:13:56 +0200 Subject: [PATCH] Fix: Close existing SQL connections before backup to prevent abnormal termination - Added explicit connection cleanup in createDatabaseBackup() - Resolves 'BACKUP DATABASE is terminating abnormally' error in runBackupProcess() - Prevents connection pool conflicts when running scheduled backups --- index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6edac6a..ba0fd38 100644 --- a/index.js +++ b/index.js @@ -144,13 +144,21 @@ async function getDbSizeBytes() { async function createDatabaseBackup() { try { console.log('Connecting to database...'); + + // Close any existing connection to avoid conflicts + try { + await sql.close(); + } catch (closeErr) { + // Ignore error if no connection exists + } + await sql.connect(config); console.log('Creating database backup...'); const backupQuery = ` BACKUP DATABASE [${process.env.MSSQL_DATABASE}] TO DISK = N'${backupFilePath}' - WITH FORMAT, INIT, NAME = N'${process.env.MSSQL_DATABASE}-Vollständig Datenbank Sichern', + WITH FORMAT, INIT, COMPRESSION, NAME = N'${process.env.MSSQL_DATABASE}-Vollständig Datenbank Sichern', SKIP, NOREWIND, NOUNLOAD, STATS = 10 `;