backup.cronjob.yaml 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. apiVersion: batch/v1
  2. kind: CronJob
  3. metadata:
  4. name: backup
  5. spec:
  6. schedule: "30 3 * * 1,3,5"
  7. jobTemplate:
  8. spec:
  9. template:
  10. spec:
  11. initContainers:
  12. - name: prepare-wrapper
  13. image: busybox
  14. command: ["/bin/sh", "-c"]
  15. args:
  16. - |
  17. cat > /shared/wrapper.sh << 'EOF'
  18. #!/bin/sh
  19. # Execute the original entrypoint with all arguments
  20. /usr/bin/restic "$@"
  21. # Capture the exit code
  22. echo $? > /shared/exit-code
  23. EOF
  24. chmod +x /shared/wrapper.sh
  25. volumeMounts:
  26. - name: shared-data
  27. mountPath: /shared
  28. containers:
  29. - name: restic
  30. image: restic/restic:0.17.2
  31. command: ["/shared/wrapper.sh"]
  32. args: ["backup", "--repo", "/data/repo", "--insecure-no-password", "/data/glusterfs"]
  33. imagePullPolicy: IfNotPresent
  34. volumeMounts:
  35. - name: restic-repo-vol
  36. mountPath: /data/repo
  37. - name: backup-data-vol
  38. mountPath: /data/glusterfs
  39. - name: shared-data
  40. mountPath: /shared
  41. - name: notify
  42. image: curlimages/curl
  43. command: ["/bin/sh"]
  44. args:
  45. - -c
  46. - |
  47. while [ ! -f /shared/exit-code ]; do sleep 1; done
  48. AUTH_HEADER="Authorization: Bearer tk_up3glyzhhojl1w5lt32jq5vqjzdgb"
  49. URL="http://ntfy/backup"
  50. MESSAGE="Local restic backup"
  51. if [ "$(cat /shared/exit-code)" -eq 0 ]; then
  52. STATUS="was successful"
  53. else
  54. STATUS="FAILED"
  55. fi
  56. curl -X POST -H "$AUTH_HEADER" -d "${MESSAGE} ${STATUS}." "$URL"
  57. volumeMounts:
  58. - name: shared-data
  59. mountPath: /shared
  60. volumes:
  61. - name: restic-repo-vol
  62. hostPath:
  63. path: /data/backup
  64. type: Directory
  65. - name: backup-data-vol
  66. hostPath:
  67. path: /mnt
  68. type: Directory
  69. - name: shared-data
  70. emptyDir: {}
  71. restartPolicy: Never
  72. nodeSelector:
  73. kubernetes.io/hostname: raspberrypi4