apiVersion: batch/v1
kind: CronJob
metadata:
  name: rsync-k8s-to-nas-backup
spec:
  # Run daily at 8:15 AM
  schedule: "18 8 * * *"
  timeZone: "Australia/Melbourne"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: rsync-client
            # A minimal image with rsync installed
            image: eeacms/rsync:2.2
            command: ["rsync"]
            args:
              - "-avh"
              - "--delete"
              - "/source-data/"
              - "rsync://junv@192.168.1.4/backups/192.168.1.2"
            env:
              # This injects the password from the secret into the environment
              # The rsync client automatically uses this variable.
              - name: RSYNC_PASSWORD
                valueFrom:
                  secretKeyRef:
                    name: nas-rsync-secret
                    key: password
            volumeMounts:
              # Mount the host path into the container
              - name: k8s-data-volume
                mountPath: /source-data
                readOnly: true # Good practice if you're only reading from the source
          volumes:
            # This volume gives the pod access to the node's filesystem
            - name: k8s-data-volume
              hostPath:
                # Path on the Kubernetes node where your data resides
                path: /mnt/k8s/
                type: Directory
          # Ensure the pod doesn't restart if the rsync command completes successfully
          restartPolicy: OnFailure
