diff --git a/chart/templates/db-backup.yaml b/chart/templates/db-backup.yaml new file mode 100644 index 0000000000..b614d3f286 --- /dev/null +++ b/chart/templates/db-backup.yaml @@ -0,0 +1,104 @@ +{{- if (.Values.db_backup).storage -}} + +{{ $backup := print "storage-" .Values.db_backup.storage }} + +# find matching storage in storages list +{{ $found := false }} +{{- range $storage := .Values.storages -}} + {{- if eq $storage.name $.Values.db_backup.storage -}} + {{- $found = true }} + {{- end }} +{{- end}} + +# error here if backup storage doesn't match one of the specified storages +{{- if not $found }} +{{ fail (print "Backup storage '" .Values.db_backup.storage "' not found, check the value of 'db_backups.storage' and 'storages' to ensure this is a valid storage name")}} +{{- end }} + +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: mongodb-backup + namespace: {{ $.Values.crawler_namespace }} +spec: + schedule: "{{ .Values.db_backup.schedule | default "26 0 * * *" }}" + failedJobsHistoryLimit: 2 + successfulJobsHistoryLimit: 1 + jobTemplate: + spec: + activeDeadlineSeconds: 600 + template: + spec: + restartPolicy: Never + initContainers: + - name: dump + image: {{ .Values.mongo_image }} + imagePullPolicy: {{ .Values.mongo_pull_policy }} + volumeMounts: + - name: backups + mountPath: /backups + + command: + - /bin/bash + - -c + - mongodump --uri=$MONGO_DB_URL --archive=/backups/backup.archive + + env: + - name: MONGO_DB_URL + valueFrom: + secretKeyRef: + name: mongo-auth + key: MONGO_DB_URL + + containers: + - name: upload + image: {{ .Values.minio_mc_image }} + imagePullPolicy: {{ .Values.minio_pull_policy }} + volumeMounts: + - name: backups + mountPath: /backups + + command: + - /bin/bash + - -c + - | + [[ "$ENDPOINT_URL" =~ (https?://[^/]+/)([^/]+)/(.*)$ ]]; + origin=${BASH_REMATCH[1]}; + bucket=${BASH_REMATCH[2]}; + path=${BASH_REMATCH[3]}; + + echo "endpoint: $ENDPOINT_URL" + echo "origin: $origin" + echo "bucket: $bucket" + echo "path: ${path}${DB_PATH}/" + + mc alias set BACKUP $origin $ACCESS_KEY $SECRET_KEY || exit 1; + mc cp /backups/backup.archive BACKUP/${bucket}/${path}${DB_PATH}/mongodb-$(date +%Y-%m-%dT%H-%M-%S).archive || exit 2; + env: + - name: ACCESS_KEY + valueFrom: + secretKeyRef: + name: {{ $backup }} + key: STORE_ACCESS_KEY + + - name: SECRET_KEY + valueFrom: + secretKeyRef: + name: {{ $backup }} + key: STORE_SECRET_KEY + + - name: ENDPOINT_URL + valueFrom: + secretKeyRef: + name: {{ $backup }} + key: STORE_ENDPOINT_URL + + - name: DB_PATH + value: {{ .Values.db_backup.path | default "db-backup" }} + + volumes: + - name: backups + emptyDir: {} + + {{- end }} diff --git a/chart/templates/mongo.yaml b/chart/templates/mongo.yaml index 0028211a9c..5aff4b9f39 100644 --- a/chart/templates/mongo.yaml +++ b/chart/templates/mongo.yaml @@ -13,7 +13,12 @@ stringData: MONGO_INITDB_ROOT_USERNAME: "{{ .Values.mongo_auth.username }}" MONGO_INITDB_ROOT_PASSWORD: "{{ .Values.mongo_auth.password }}" MONGO_HOST: "{{ .Values.mongo_host }}" + +{{- if .Values.mongo_auth.db_url }} MONGO_DB_URL: "{{ .Values.mongo_auth.db_url }}" +{{- else }} + MONGO_DB_URL: "mongodb://{{ urlquery .Values.mongo_auth.username }}:{{ urlquery .Values.mongo_auth.password }}@{{ .Values.mongo_host }}:27017/browsertrixcloud?tls=false&authSource=admin" +{{- end }} --- apiVersion: v1 @@ -27,7 +32,12 @@ stringData: MONGO_INITDB_ROOT_USERNAME: "{{ .Values.mongo_auth.username }}" MONGO_INITDB_ROOT_PASSWORD: "{{ .Values.mongo_auth.password }}" MONGO_HOST: "{{ .Values.mongo_host }}" +{{- if .Values.mongo_auth.db_url }} MONGO_DB_URL: "{{ .Values.mongo_auth.db_url }}" +{{- else }} + MONGO_DB_URL: "mongodb://{{ urlquery .Values.mongo_auth.username }}:{{ urlquery .Values.mongo_auth.password }}@{{ .Values.mongo_host }}:27017/browsertrixcloud?tls=false&authSource=admin" +{{- end }} + {{- if .Values.mongo_local }} diff --git a/chart/values.yaml b/chart/values.yaml index 3aa6f30f0c..50b126a3aa 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -273,6 +273,14 @@ storages: endpoint_url: "http://local-minio.default:9000/" +# if storage is set, mongodb backups will be created in this storage +# daily under /db-backups +db_backup: + #storage: "default" + #schedule: "26 0 * * *" + #path: "db-backup" + + # optional: duration in minutes for WACZ download links to be valid # used by webhooks and replay # max value = 10079 (one week minus one minute)