# apiVersion: batch/v1
# kind: CronJob
# metadata:
#   name: etcd-snapshot
#   namespace: default
# spec:
#   schedule: "0 8 * * 6" # 8:00 AM every Saturday
#   jobTemplate:
#     spec:
#       template:
#         spec:
#           containers:
#             - name: backup
#               # Same image as in /etc/kubernetes/manifests/etcd.yaml
#               image: k8s.gcr.io/etcd:3.5.1-0
#               env:
#                 - name: ETCDCTL_API
#                   value: "3"
#               command: ["/bin/sh"]
#               args: ["-c", "etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/etcd/ca.pem --cert=/etc/etcd/etcd.pem --key=/etc/etcd/etcd-key.pem snapshot save /backup/etcd/snapshot-$(printf \"%(%Y-%m-%d-%H:%M:%S_%Z)T\n\").db"]
#               volumeMounts:
#                 - mountPath: /etc/etcd
#                   name: etcd-certs
#                   readOnly: true
#                 - mountPath: /backup/etcd
#                   subPath: etcd
#                   name: backup
#           hostNetwork: true
#           restartPolicy: Never
#           nodeSelector:
#             etcd: "true"
#           # affinity:
#           #   nodeAffinity:
#           #     requiredDuringSchedulingIgnoredDuringExecution:
#           #       nodeSelectorTerms:
#           #         - matchExpressions:
#           #           - key: node-role.kubernetes.io/master
#           #             operator: Exists
#           tolerations:
#             - effect: NoSchedule
#               operator: Exists
#           volumes:
#             - name: etcd-certs
#               hostPath:
#                 path: /etc/etcd
#                 type: Directory
#             - name: backup
#               persistentVolumeClaim:
#                 claimName: etcd-backups-nfs-pvc
---

apiVersion: batch/v1
kind: CronJob
metadata:
  name: pg-bakcup
  namespace: db
spec:
  schedule: "48 5 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: backup
              # Same image as in /etc/kubernetes/manifests/etcd.yaml
              image: mirror.gcr.io/postgres:14
              env:
                - name: DB_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: postgresql-credentials
                      key: password
                - name: DB_USER
                  value: postgres
                - name: DB_HOST
                  value: 192.168.1.2
              command: ["/bin/sh"]
              args: ["-c", "PGPASSWORD=$DB_PASSWORD pg_dumpall -c -h $DB_HOST -p 5432 -U $DB_USER| gzip > /backups/postgres/dump-$(date +%Y-%m-%d_%H-%M-%S_%Z).tar.gz"]
              volumeMounts:
                - mountPath: /backups/postgres
                  subPath: postgres
                  name: backup
          restartPolicy: OnFailure
          # tolerations:
          #   - effect: NoSchedule
          #     operator: Exists
          volumes:
            - name: backup
              persistentVolumeClaim:
                claimName: postgres-nfs-backup-pvc

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: backups-nfs
spec:
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 192.168.1.5
    path: "/fs/1000/nfs/backups"


---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: "db"
  name: "postgres-nfs-backup-pvc"
spec:
  storageClassName: ""
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Gi
