1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-30 18:17:36 +01:00
bitwarden-server/util/MsSql/backup-db.sh
David Lundgren d63eb376c4
Allow for slight customization of the mssql db backup interval (#1008)
* Allow for slight customization of the mssql db backup interval

* Honor env TZ if set and clean up -u in sleep calculation
2020-12-15 11:08:30 -05:00

26 lines
821 B
Bash

#!/bin/sh
BACKUP_INTERVAL=${BACKUP_INTERVAL:-next day}
BACKUP_INTERVAL_FORMAT=${BACKUP_INTERVAL_FORMAT:-%Y-%m-%d 00:00:00}
while true
do
# Sleep until next day
if [ "$1" = "loop" ]; then
interval_start=`date "+${BACKUP_INTERVAL_FORMAT} %z" -d "${BACKUP_INTERVAL}"`
sleep $((`date +%_s -d "${interval_start}"` - `date +%_s`))
fi
# Backup timestamp
export now=$(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
# Break if called manually (without loop option)
[ "$1" != "loop" ] && break
done