From da349a7b4f3265d9077c56a032300dec037108c3 Mon Sep 17 00:00:00 2001 From: Haneef <298208+haneef95@users.noreply.github.com> Date: Wed, 17 Nov 2021 23:50:02 +0000 Subject: [PATCH] Allow users to specify BACKUP_DB_FILENAME for MSSQL DB backups Rename variables to match the rest. Also: Allow users to specify both $BACKUP_DB_FILENAME and $BACKUP_DB_FILENAME_SUFFIX backup-db.sh minor syntax fix --- util/MsSql/backup-db.sh | 7 ++++--- util/MsSql/backup-db.sql | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/util/MsSql/backup-db.sh b/util/MsSql/backup-db.sh index 210fa51ac..b0dbcec64 100644 --- a/util/MsSql/backup-db.sh +++ b/util/MsSql/backup-db.sh @@ -13,18 +13,19 @@ do # Backup timestamp export now=$(date +%Y%m%d_%H%M%S) - export BACKUP_FILENAME="${now}${BACKUP_FILENAME_SUFFIX}" + BACKUP_DB_FILENAME=${BACKUP_DB_FILENAME:-"${now}"} + export BACKUP_DB_FILENAME="${BACKUP_DB_FILENAME}${BACKUP_DB_FILENAME_SUFFIX}" # Do a new backup /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P ${SA_PASSWORD} -i /backup-db.sql # Delete backup files older than 30 days - grep -B1 "BACKUP DATABASE successfully" /var/opt/mssql/log/errorlog | grep -q _${BACKUP_FILENAME}.BAK && + grep -B1 "BACKUP DATABASE successfully" /var/opt/mssql/log/errorlog | grep -q _${BACKUP_DB_FILENAME}.BAK && find $BACKUP_DB_DIR -mindepth 1 -type f -name '*.BAK' -mtime +32 -delete # Common variables used in the next two if blocks. DATABASE=${DATABASE:-'vault'} - BACKUP_DB_LATEST_FILENAME="${DATABASE}_FULL_${BACKUP_FILENAME}.BAK" + BACKUP_DB_LATEST_FILENAME="${DATABASE}_FULL_${BACKUP_DB_FILENAME}.BAK" # Make a copy (overwrite) with a consistent filename. # Helps when taking snapshots with deduplication algorithms e.g. restic diff --git a/util/MsSql/backup-db.sql b/util/MsSql/backup-db.sql index 37bd03662..72969e7be 100644 --- a/util/MsSql/backup-db.sql +++ b/util/MsSql/backup-db.sql @@ -7,10 +7,10 @@ DECLARE @DatabaseNameSafe varchar(100) SET @DatabaseNameSafe = 'vault' DECLARE @BackupFile varchar(100) -SET @BackupFile = '$(BACKUP_DB_DIR)' + @DatabaseNameSafe + '_FULL_$(BACKUP_FILENAME).BAK' +SET @BackupFile = '$(BACKUP_DB_DIR)' + @DatabaseNameSafe + '_FULL_$(BACKUP_DB_FILENAME).BAK' DECLARE @BackupName varchar(100) -SET @BackupName = @DatabaseName + ' full backup for $(BACKUP_FILENAME)' +SET @BackupName = @DatabaseName + ' full backup for $(BACKUP_DB_FILENAME)' DECLARE @BackupCommand NVARCHAR(1000) SET @BackupCommand = 'BACKUP DATABASE [' + @DatabaseName + '] TO DISK = ''' + @BackupFile + ''' WITH INIT, NAME= ''' + @BackupName + ''', NOSKIP, NOFORMAT'