From bfeb91ebf256393710638a2b0f8c7cc6cc013976 Mon Sep 17 00:00:00 2001 From: Michael Williams Date: Thu, 29 Feb 2024 10:16:10 -0600 Subject: [PATCH] Update backup job to match old self-host backups. --- examples/database-backup/backup-cronjob.yaml | 55 ++++++++++++++++++++ examples/database-backup/backup-job.yaml | 10 +++- 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 examples/database-backup/backup-cronjob.yaml diff --git a/examples/database-backup/backup-cronjob.yaml b/examples/database-backup/backup-cronjob.yaml new file mode 100644 index 00000000..00b6b56a --- /dev/null +++ b/examples/database-backup/backup-cronjob.yaml @@ -0,0 +1,55 @@ +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: "bitwarden-backup" + labels: + app.kubernetes.io/component: bitwarden-backup + app: bitwarden-backup +spec: + schedule: "0 0 * * *" + jobTemplate: + spec: + ttlSecondsAfterFinished: 10 + backoffLimit: 1 + template: + metadata: + name: "bitwarden-backup" + labels: + app.kubernetes.io/component: bitwarden-backup + app: bitwarden-backup + spec: + containers: + - name: backup-db + env: + - name: MSSQL_CONN_STRING + valueFrom: + secretKeyRef: + name: bitwarden-sql-connection-string + key: globalSettings__sqlServer__connectionString + image: "mcr.microsoft.com/mssql-tools" + volumeMounts: + - name: mssql-backups + mountPath: /backups + command: + - "/bin/bash" + - "-c" + args: [ + " + svr=\"$(echo \"${MSSQL_CONN_STRING}\" | grep -Po \"Data Source=tcp:\\K[^,]*(?=.*)\")\"; + pass=\"$(echo \"${MSSQL_CONN_STRING}\" | grep -Po \".*Password=\\K[^;]*(?=.*)\")\"; + now=$(date +%Y%m%d_%H%M%S); + echo \"$pass\" | + /opt/mssql-tools/bin/sqlcmd -S $svr -U SA -q + \"BACKUP DATABASE [vault] TO DISK = '/var/opt/mssql/backups/vault_FULL_${now}.BAK' WITH NOFORMAT, INIT, NAME = 'vault full backup for ${now}', NOSKIP\"; + if [ $? -eq 0 ]; then + ln -sf vault_FULL_${now}.BAK /backups/vault.BAK; + find /backups/ -mindepth 1 -maxdepth 1 -type f -name '*.BAK' -mtime +32 -delete; + fi + " + ] + restartPolicy: Never + volumes: + - name: mssql-backups + persistentVolumeClaim: + claimName: bitwarden-self-host-mssqlbackups diff --git a/examples/database-backup/backup-job.yaml b/examples/database-backup/backup-job.yaml index 1410daba..48085638 100644 --- a/examples/database-backup/backup-job.yaml +++ b/examples/database-backup/backup-job.yaml @@ -33,10 +33,16 @@ spec: - "-c" args: [ " - [ ! -f /backups/vault.bak ] || mv /backups/vault.bak \"/backups/vault.bak.$(date -r /backups/vault.bak -u +'%Y-%m-%dT%H:%M:%SZ')\"; svr=\"$(echo \"${MSSQL_CONN_STRING}\" | grep -Po \"Data Source=tcp:\\K[^,]*(?=.*)\")\"; pass=\"$(echo \"${MSSQL_CONN_STRING}\" | grep -Po \".*Password=\\K[^;]*(?=.*)\")\"; - echo \"$pass\" | /opt/mssql-tools/bin/sqlcmd -S $svr -U SA -q \"BACKUP DATABASE [vault] TO DISK = '/var/opt/mssql/backups/vault.bak' WITH FORMAT, INIT\" + now=$(date +%Y%m%d_%H%M%S); + echo \"$pass\" | + /opt/mssql-tools/bin/sqlcmd -S $svr -U SA -q + \"BACKUP DATABASE [vault] TO DISK = '/var/opt/mssql/backups/vault_FULL_${now}.BAK' WITH NOFORMAT, INIT, NAME = 'vault full backup for ${now}', NOSKIP\"; + if [ $? -eq 0 ]; then + ln -sf vault_FULL_${now}.BAK /backups/vault.BAK; + find /backups/ -mindepth 1 -maxdepth 1 -type f -name '*.BAK' -mtime +32 -delete; + fi " ] restartPolicy: Never