apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: backup-namespaces
  namespace: cluster-operations
spec:
  successfulJobsHistoryLimit: 3
  failedJobsHistoryLimit: 2
  concurrencyPolicy: "Forbid"
  schedule: "55 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          nodeSelector:
            node-role.kubernetes.io/infra: ""
          containers:
          - name: backup-namespaces
            image: registry.redhat.io/openshift4/ose-cli
            env:
              - name: BACKUP_CLUSTERRESOURCES
                valueFrom:
                  configMapKeyRef:
                    name: backup-config 
                    key: backup-clusterresources 
              - name: NS_SELECTOR
                valueFrom:
                  configMapKeyRef:
                    name: backup-config 
                    key: backup-namespace-selector 
            envFrom:
              - configMapRef:
                  name: backup-config 
            command:
            - /bin/bash
            - -c
            - |
              #/bin/bash
              # workaround: ose-cli image sets $HOME to "/" which is not writable and prevents 'oc' to create $HOME/.kube directory
              # hence we set to writable '/tmp' directory
              export HOME=/tmp
              #
              # backup timestamp
              TIMESTAMP=$(date +%F--%H-%M-%S)
              #
              # process config
              selector=""
              [ $NS_SELECTOR ] && selector="-l $NS_SELECTOR"
              echo "INFO: starting backup at " $TIMESTAMP
              echo "INFO: backup cluster resources: " $BACKUP_CLUSTERRESOURCES
              echo "INFO: list of namespaces for backup (selector: " $$NS_SELECTOR "): $(oc get namespaces $selector -o jsonpath='{range .items[*]}{.metadata.name}{", "}')"
              echo "INFO:"
              echo
              if [ $BACKUP_CLUSTERRESOURCES == "true" ]
              then
                 #
                 # Backup cluster resource
                 #
                 echo "Backing up cluster resources"
                 oc get --ignore-not-found $( oc api-resources --verbs=list --namespaced=false -o name | xargs | sed 's/\ /,/g') -o yaml 2>/backup/"$TIMESTAMP"_backup_clusterresources.log | gzip -9 -c >/backup/"$TIMESTAMP"_backup_clusterresources.yaml.gz
              fi
              #
              # Backup individual namespace resources
              #
              echo "Backing up namespaces ..."
              for ns in $(oc get namespaces $selector -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}')
              do
                echo
                echo "Backing up namespace $ns at $TIMESTAMP";
                oc get -n $ns --ignore-not-found  $( oc api-resources -n $ns --verbs=list --namespaced -o name | xargs | sed 's/\ /,/g') -o yaml 2>/backup/"$TIMESTAMP"_backup_"$ns".log | gzip -9 -c >/backup/"$TIMESTAMP"_backup_"$ns".yaml.gz
              done
            volumeMounts:
              - name: backup-claim
                mountPath: /backup
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure
          serviceAccountName: cron-namespace-backup-sa
          imagePullSecrets:
          - name: xyz-user-pull-secret
          volumes:
            - name: backup-claim
              persistentVolumeClaim:
                claimName: backup-claim
