apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: bookstack-backup
  namespace: default
spec:
  schedule: "0 * * * *"
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      parallelism: 1
      template:
        spec:
          restartPolicy: OnFailure
          volumes:
            - name: backup
              nfs:
                server: ${SECRET_BACKUP_SERVER}
                path: /backup/data/bookstack

          securityContext:
            fsGroup: 1000
            runAsGroup: 1000
            runAsUser: 1000

          containers:
            - name: mariadb
              # same image as the mariadb sidecar
              image: ghcr.io/linuxserver/mariadb:10.5.13
              imagePullPolicy: IfNotPresent
              command: ["/bin/sh"]
              args:
                - "-c"
                - "mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASS --quote-names --databases $DB_DATABASE >/backup/$DB_DATABASE.sql"
              env:
                - name: DB_DATABASE
                  value: bookstack
                - name: DB_HOST
                  value: bookstack-mariadb.default
                - name: DB_USER
                  value: bookstack
                - name: DB_PASS
                  valueFrom:
                    secretKeyRef:
                      name: bookstack
                      key: mariadb_password
                - name: TZ
                  value: America/Toronto
              volumeMounts:
                - name: backup
                  mountPath: /backup
              resources:
                requests:
                  memory: "128Mi"
                  cpu: "1"
                limits:
                  memory: "512Mi"
                  cpu: "1"
