From 41afbc0fa178d317370370387845f3fc9a75ef5d Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 5 Oct 2024 19:07:16 +1000 Subject: [PATCH 1/3] add crates pg backup cron --- crates/crates.cron.yaml | 81 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 crates/crates.cron.yaml diff --git a/crates/crates.cron.yaml b/crates/crates.cron.yaml new file mode 100755 index 0000000..6c6bcd8 --- /dev/null +++ b/crates/crates.cron.yaml @@ -0,0 +1,81 @@ +--- + +apiVersion: v1 +kind: PersistentVolume +metadata: + name: crates-postgres-backup-pv +spec: + storageClassName: "" + capacity: + storage: 1Gi + accessModes: + - ReadWriteOnce + persistentVolumeReclaimPolicy: Retain + claimRef: + namespace: default + name: crates-postgres-backup-pvc + hostPath: + # Into homedir so we can scp it ez + path: "/home/ntr/archives/crates-postgres" + type: DirectoryOrCreate + +--- + +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: crates-postgres-backup-pvc +spec: + volumeName: crates-postgres-backup-pv + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + +--- + +apiVersion: batch/v1 +kind: CronJob +metadata: + name: crates-db-backup +spec: + schedule: "20 4 * * *" + jobTemplate: + spec: + template: + metadata: + labels: + app: crates-postgres-backup + spec: + restartPolicy: Never + containers: + - name: crates-postgres-backup + image: postgres:16 + command: + - "/bin/sh" + - "-c" + - + | + TZ="Australia/Melbourne" + TIMESTAMP=$(date '+%Y-%m-%d-%H-%M-%S') + DEST=/mnt/dest/crates-$TIMESTAMP-sql.gz + + echo "Backing up to $DEST" + pg_dump --dbname=$POSTGRES_URL | gzip > $DEST + echo "Backup Complete" + env: + - name: POSTGRES_URL + valueFrom: + secretKeyRef: + name: crates + key: POSTGRES_URL + volumeMounts: + - name: crates-postgres-backup-storage + mountPath: /mnt/dest + volumes: + - name: crates-postgres-backup-storage + persistentVolumeClaim: + claimName: crates-postgres-backup-pvc + +--- From c77fe2ebb50ae5c6c9719dc87f4ff90eb3a64182 Mon Sep 17 00:00:00 2001 From: ntr Date: Sat, 5 Oct 2024 19:09:22 +1000 Subject: [PATCH 2/3] perms --- crates/crates.cron.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 crates/crates.cron.yaml diff --git a/crates/crates.cron.yaml b/crates/crates.cron.yaml old mode 100755 new mode 100644 From 6056f3277f8a7ae6af9a90ac0437fbbd1e89b44b Mon Sep 17 00:00:00 2001 From: Nathan Rashleigh Date: Sat, 5 Oct 2024 23:07:18 +1000 Subject: [PATCH 3/3] update cron --- crates/crates.cron.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/crates.cron.yaml b/crates/crates.cron.yaml index 6c6bcd8..e0d5604 100644 --- a/crates/crates.cron.yaml +++ b/crates/crates.cron.yaml @@ -57,13 +57,20 @@ spec: - "-c" - | + cd /mnt/dest/ TZ="Australia/Melbourne" TIMESTAMP=$(date '+%Y-%m-%d-%H-%M-%S') - DEST=/mnt/dest/crates-$TIMESTAMP-sql.gz + DEST=crates-$TIMESTAMP.sql.gz echo "Backing up to $DEST" pg_dump --dbname=$POSTGRES_URL | gzip > $DEST echo "Backup Complete" + + echo "Removing old copies..." + find . -type f -name "*.sql.gz" -mtime 7 | xargs rm -v + + echo "Setting latest link..." + ln -nfs $DEST latest.sql.gz env: - name: POSTGRES_URL valueFrom: