2

I wrote a CronJob that periodically takes volume snapshot of my PVC. Below is the CronJob file:

kind: CronJob
metadata:
  name: mycronjob
spec:
  schedule: "*/2 * * * *"   # Run the job every 2 minutes
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: webserver-container
            image: kubectl:latest
            command:
            - /bin/sh
            - -c
            - /app/script.sh
            volumeMounts:
            - name: script-volume
              mountPath: /app
          restartPolicy: OnFailure
          volumes:
          - name: script-volume
            configMap:
              name: script-configmap

The Volume Snapshot file looks like this:

kind: VolumeSnapshot
metadata:
  name: snap-shot-test
spec:
  volumeSnapshotClassName: csi-snapclass
  source:
    persistentVolumeClaimName: my-test-apps

And here is the script file:

#!/bin/bash
kubectl apply -f volumesnapshot.yml

First time the cronjob is successfully executed, but after that it says volumesnapshot.snapshot.storage.k8s.io/snap-shot-test unchanged

How can I periodically take volumesnapshot of a PVC with having latest 2 copies of the snapshot?

Varshney P.
  • 208
  • 1
  • 12

1 Answers1

1

It seems to me that what you describe mirrors the YAML files you shared; there is nothing that changes the name of the backup and therefore it will never create a new one.

If you want to make a backup of one PVC to another in a "raw" way with scripts, maybe think of a job that mounts 2 Volumes (source and destination) and executes a simple cp -Rp /volume-source/* /volume-destination/

Otherwise, if you want to get the job done right, consider using a tool like Velero.

https://velero.io/

glv
  • 994
  • 1
  • 1
  • 15
  • obviously to make the copy dynamic, you will have to put some intelligence into the script to generate a new folder and copy the contents of the PVC into it. – glv Mar 27 '23 at 07:46
  • Yeah, those are the yaml files.. I do not want a raw backup, rather I want to create K8 snapshots dynamically without using any third-party tool.. I think I need a script to change the name of snapshot everytime it executes – Varshney P. Mar 27 '23 at 08:23
  • 1
    ok, with all these clarifications I would really tell you to customize the VolumeSnapshot; I saw that you already run the command inside a SH script, so it should be simple... I would generate a hash, associate it with a variable and with a SED I would change the name of the snapshot. – glv Mar 27 '23 at 10:06
  • thanks. thats what we did and it worked. We appended current date to the volumesnapshot name and for optimization we deleted previous date sanpshots – Varshney P. Mar 27 '23 at 10:32