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'