Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB Backups #1322

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions chart/templates/db-backup.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
10 changes: 10 additions & 0 deletions chart/templates/mongo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
Expand Down
8 changes: 8 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * *"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting choice of time but why not :)

#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)
Expand Down