From 8471f558e34e934df3e036b91765168a4f2d2ef5 Mon Sep 17 00:00:00 2001 From: Mart124 <37041094+Mart124@users.noreply.github.com> Date: Tue, 29 May 2018 14:17:43 +0200 Subject: [PATCH] Improve mssql backups (#298) * Improve mssql backups * Launch DB backup at 23:59 --- util/MsSql/backup-db.sh | 8 ++++++-- util/MsSql/backup-db.sql | 7 ++----- util/MsSql/crontab | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/util/MsSql/backup-db.sh b/util/MsSql/backup-db.sh index 1d6566411..885b5c07f 100644 --- a/util/MsSql/backup-db.sh +++ b/util/MsSql/backup-db.sh @@ -1,7 +1,11 @@ #!/bin/sh -# Delete backup files older than 30 days -find /etc/bitwarden/mssql/backups/ -type f -name '*.BAK' -mindepth 1 -mtime +30 -delete +# Backup timestamp +export now=${1:-$(date +%Y%m%d_%H%M%S)} # 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 _$now.BAK && +find /etc/bitwarden/mssql/backups/ -mindepth 1 -type f -name '*.BAK' -mtime +32 -delete diff --git a/util/MsSql/backup-db.sql b/util/MsSql/backup-db.sql index 8da3041ca..d838700ec 100644 --- a/util/MsSql/backup-db.sql +++ b/util/MsSql/backup-db.sql @@ -1,11 +1,8 @@ -DECLARE @TheDate NVARCHAR(20) -SET @TheDate = REPLACE(CONVERT(VARCHAR, GETDATE(),101),'/','') + '_' + REPLACE(CONVERT(VARCHAR, GETDATE(),108),':','') - DECLARE @BackupFile varchar(100) -SET @BackupFile = '/etc/bitwarden/mssql/backups/vault_FULL_'+ @TheDate + '.BAK' +SET @BackupFile = '/etc/bitwarden/mssql/backups/vault_FULL_$(now).BAK' DECLARE @BackupName varchar(100) -SET @BackupName = 'vault full backup for '+ @TheDate +SET @BackupName = 'vault full backup for $(now)' DECLARE @BackupCommand NVARCHAR(1000) SET @BackupCommand = 'BACKUP DATABASE [vault] TO DISK = ''' + @BackupFile + ''' WITH INIT, NAME= ''' + @BackupName + ''', NOSKIP, NOFORMAT' diff --git a/util/MsSql/crontab b/util/MsSql/crontab index 451e14d6b..1c53d00e0 100644 --- a/util/MsSql/crontab +++ b/util/MsSql/crontab @@ -1,3 +1,3 @@ -0 0 * * * bitwarden /backup-db.sh >> /var/log/cron.log 2>&1 +59 23 * * * bitwarden /backup-db.sh >> /var/log/cron.log 2>&1 -# An empty line is required at the end of this file for a valid cron file. \ No newline at end of file +# An empty line is required at the end of this file for a valid cron file.