apiVersion: batch/v1
kind: CronJob
metadata:
  annotations:
    a8r.io/repository: ssh://git@git.tr1ceracop.de:222/gitea_admin/k8s-and-chill.git
  name: forgejo-git-backup
  namespace: forgejo
spec:
  concurrencyPolicy: Forbid
  failedJobsHistoryLimit: 3
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - command:
                - sh
                - -c
                - |
                  set -e
                  apk add --no-cache rclone > /dev/null 2>&1

                  mkdir -p /tmp/rclone
                  cat > /tmp/rclone/rclone.conf <<CONF
                  [s3]
                  type = s3
                  provider = Other
                  access_key_id = ${ACCESS_KEY_ID}
                  secret_access_key = ${SECRET_ACCESS_KEY}
                  endpoint = https://fsn1.your-objectstorage.com
                  acl = private
                  CONF

                  echo "Syncing git repositories to S3..."
                  rclone sync /data/git/ s3:k8s-and-chill-backups/forgejo/git/ \
                    --config /tmp/rclone/rclone.conf \
                    --transfers 4 \
                    -v

                  echo "Syncing gitea data (avatars, attachments, keys)..."
                  rclone sync /data/gitea/ s3:k8s-and-chill-backups/forgejo/gitea/ \
                    --config /tmp/rclone/rclone.conf \
                    --exclude 'conf/**' \
                    --exclude 'queues/**' \
                    --transfers 4 \
                    -v

                  rm -rf /tmp/rclone
                  echo "Backup complete."
              env:
                - name: ACCESS_KEY_ID
                  valueFrom:
                    secretKeyRef:
                      key: ACCESS_KEY_ID
                      name: forgejo-backup-s3
                - name: SECRET_ACCESS_KEY
                  valueFrom:
                    secretKeyRef:
                      key: SECRET_ACCESS_KEY
                      name: forgejo-backup-s3
              image: alpine:3.20
              name: backup
              volumeMounts:
                - mountPath: /data
                  name: data
                  readOnly: true
          nodeSelector:
            kubernetes.io/hostname: ubuntu-4gb-nbg1-3
          restartPolicy: OnFailure
          serviceAccountName: forgejo-git-backup
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: forgejo-git-storage
      ttlSecondsAfterFinished: 86400
  schedule: 0 3 * * *
  successfulJobsHistoryLimit: 3
