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
This commit is contained in:
sebseb7
2025-10-02 15:13:56 +02:00
parent 307a373667
commit 27e1354129

View File

@@ -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
`;