Skip to content

Instantly share code, notes, and snippets.

@BorysVrublevskyi
Last active November 30, 2021 06:04
Show Gist options
  • Save BorysVrublevskyi/c61427a41ed0c796c52f05e85b556913 to your computer and use it in GitHub Desktop.
Save BorysVrublevskyi/c61427a41ed0c796c52f05e85b556913 to your computer and use it in GitHub Desktop.
Backup SonarQube Posgres DB to NFS share
apiVersion: batch/v1
# apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: sonar-postgres-backup
spec:
# schedule: "0 2 * * *"
schedule: "@daily"
jobTemplate:
spec:
template:
spec:
containers:
- name: sonar-postgres-backup
# image: bitnami/postgresql:latest # Getting error: /bin/sh: 1: cannot create /root/.pgpass: Permission denied
image: postgres:latest
# File .pgpass contents: hostname:port:database:username:password
# "-mtime n" - File's data was last modified n*24 hours ago.
command:
- /bin/sh
- -c
- |
echo "sonar-postgresql:5432:postgresDB:postgresUser:$POSTGRESPASS" > /root/.pgpass && \
chmod 600 /root/.pgpass && \
pg_dump -h sonar-postgresql -U postgresUser postgresDB | \
gzip > /mnt/backups/sonar-postgres-backup-$$(date "+%Y-%m-%d_%H-%M").gz && \
find /mnt/backups -type f -mtime +7 | xargs rm -f
env:
- name: POSTGRESPASS
valueFrom:
secretKeyRef:
name: sonar-postgresql
key: postgresql-password
volumeMounts:
- name: nfs-share
mountPath: /mnt/backups
restartPolicy: Never
volumes:
- name: nfs-share
nfs:
server: server.mydomain.loc
path: /opt/nfs/backups/k8s-SonarQube
# nodeSelector:
# kubernetes.io/os: linux
# tolerations:
# - key: "cattle.io/os"
# operator: "Exists"
# effect: "NoSchedule"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment