{{- if .Values.oss.enabled }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: upload-backup-to-oss
spec:
  schedule: "0 4 * * *"
  jobTemplate:
    spec:
      parallelism: 1
      completions: 1
      backoffLimit: 1
      template:
        spec:
          containers:
            - name: upload-backup-to-oss
              image: {{ if eq .Values.container_registry "Aliyun" }}registry.cn-beijing.aliyuncs.com/{{ end }}baijunyao/ossutil:1.7.0
              command: ["/bin/sh", "-c", "/usr/local/bin/ossutil cp -r /backup oss://{{ .Values.oss.bucket}} -u"]
              env:
                - name: TZ
                  value: {{ .Values.timezone }}
              volumeMounts:
                - mountPath: /backup
                  name: backup-pvc
                - name: config
                  mountPath: /root/.ossutilconfig
                  subPath: .ossutilconfig
          volumes:
            - name: backup-pvc
              persistentVolumeClaim:
                claimName: backup-pvc
            - name: config
              configMap:
                name: ossutil-config
                items:
                  - key: config
                    path: .ossutilconfig
          restartPolicy: Never

---
{{- end }}

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: generate-sitemap
spec:
  schedule: "0 3 * * *"
  jobTemplate:
    spec:
      parallelism: 1
      completions: 1
      backoffLimit: 1
      template:
        spec:
          containers:
            - name: php
              image: {{ if eq .Values.container_registry "Aliyun" }}registry.cn-beijing.aliyuncs.com/{{ end }}baijunyao/php:8.1-{{ .Values.env }}
              command: ["php", "artisan", "bjyblog:generate-sitemap"]
              volumeMounts:
                - mountPath: /app
                  name: code-pvc
          volumes:
            - name: code-pvc
              persistentVolumeClaim:
                claimName: code-pvc
          restartPolicy: Never

---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: database-backup
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      parallelism: 1
      completions: 1
      backoffLimit: 1
      template:
        spec:
          containers:
            - name: database-backup
              image: {{ if eq .Values.container_registry "Aliyun" }}registry.cn-beijing.aliyuncs.com/{{ end }}baijunyao/mysql-client:1.1
              command: ["/bin/sh", "-c", "/usr/bin/mysqldump -hmysql-service -uroot -p{{ .Values.mysql.password }} laravel_bjyblog | gzip > /backup/laravel_bjyblog_`date +%Y%m%d`.sql.gz"]
              env:
                - name: TZ
                  value: {{ .Values.timezone }}
              volumeMounts:
                - mountPath: /backup
                  name: backup-pvc
                  subPath: database
          volumes:
            - name: backup-pvc
              persistentVolumeClaim:
                claimName: backup-pvc
          restartPolicy: Never

---

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: site-backup
spec:
  schedule: "0 1 * * *"
  jobTemplate:
    spec:
      parallelism: 1
      completions: 1
      backoffLimit: 1
      template:
        spec:
          containers:
            - name: site-backup
              image: {{ if eq .Values.container_registry "Aliyun" }}registry.cn-beijing.aliyuncs.com/{{ end }}baijunyao/alpine:3.12
              command: ["/bin/sh", "-c", "/bin/tar -czvf /backup/laravel_bjyblog_`date +%Y%m%d`.tar.gz /app/.env /app/storage/app/public/uploads"]
              env:
                - name: TZ
                  value: {{ .Values.timezone }}
              volumeMounts:
                - mountPath: /backup
                  name: backup-pvc
                  subPath: site
                - mountPath: /app
                  name: code-pvc
          volumes:
            - name: backup-pvc
              persistentVolumeClaim:
                claimName: backup-pvc
            - name: code-pvc
              persistentVolumeClaim:
                claimName: code-pvc
          restartPolicy: Never
