backup.cronjob.yaml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. EXIT_CODE=$?
  23. echo $EXIT_CODE > /shared/exit-code
  24. echo "Exit code is $EXIT_CODE"
  25. exit $EXIT_CODE
  26. EOF
  27. chmod +x /shared/wrapper.sh
  28. volumeMounts:
  29. - name: shared-data
  30. mountPath: /shared
  31. containers:
  32. - name: restic
  33. image: restic/restic:0.18.1
  34. command: ["/shared/wrapper.sh"]
  35. args: ["backup", "--verbose", "--repo", "/data/repo", "--insecure-no-password", "/data/glusterfs"]
  36. imagePullPolicy: IfNotPresent
  37. volumeMounts:
  38. - name: restic-repo-vol
  39. mountPath: /data/repo
  40. - name: backup-data-vol
  41. mountPath: /data/glusterfs
  42. - name: shared-data
  43. mountPath: /shared
  44. - name: notify
  45. image: curlimages/curl
  46. command: ["/bin/sh"]
  47. args:
  48. - -c
  49. - |
  50. while [ ! -f /shared/exit-code ]; do sleep 1; done
  51. AUTH_HEADER="Authorization: Bearer tk_up3glyzhhojl1w5lt32jq5vqjzdgb"
  52. URL="http://ntfy/backup"
  53. MESSAGE="Local restic backup"
  54. EXIT_CODE=$(cat /shared/exit-code)
  55. echo "Captured exit code is $EXIT_CODE"
  56. if [ "$EXIT_CODE" -eq 0 ]; then
  57. STATUS="was successful"
  58. else
  59. STATUS="FAILED"
  60. fi
  61. curl -X POST -H "$AUTH_HEADER" -d "${MESSAGE} ${STATUS}." "$URL"
  62. exit $EXIT_CODE
  63. volumeMounts:
  64. - name: shared-data
  65. mountPath: /shared
  66. volumes:
  67. - name: restic-repo-vol
  68. hostPath:
  69. path: /data/backup
  70. type: Directory
  71. - name: backup-data-vol
  72. hostPath:
  73. path: /mnt
  74. type: Directory
  75. - name: shared-data
  76. emptyDir: {}
  77. restartPolicy: Never
  78. nodeSelector:
  79. kubernetes.io/hostname: raspberrypi4