Skip to content

Instantly share code, notes, and snippets.

@TJM
Created September 25, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TJM/b969b6af82c6625e5bc53e597283f010 to your computer and use it in GitHub Desktop.
Save TJM/b969b6af82c6625e5bc53e597283f010 to your computer and use it in GitHub Desktop.
K8s CronJob to backup ArtifactoryDB
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: artifactory-pg-backup
namespace: artifactory
spec:
schedule: "30 */4 * * *"
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 3
startingDeadlineSeconds: 100
jobTemplate:
spec:
activeDeadlineSeconds: 3600
template:
spec:
containers:
- name: artifactory-pg-backup
image: bitnami/postgresql:12
command: ["/bin/bash", "-c"]
args:
- |
#!/bin/bash
# Borrowed from: https://github.com/Annixa/docker-pg_dump
set -e
PREFIX=${PREFIX:-dump}
PGUSER=${PGUSER:-postgres}
PGDB=${PGDB:-postgres}
PGHOST=${PGHOST:-db}
PGPORT=${PGPORT:-5432}
DATE=$(date +%Y%m%d_%H%M%S)
FILE="/dump/$PREFIX-$DATE.sql"
echo "Job started: $(date). Dumping to ${FILE}"
pg_dump -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -f "$FILE" -d "$PGDB"
gzip "$FILE"
ls -l "${FILE}.gz"
if [[ ! -z "${DELETE_OLDER_THAN}" ]]; then
echo "Deleting backups older than ${DELETE_OLDER_THAN} minutes..."
find /dump/* -mmin "+${DELETE_OLDER_THAN}" -exec rm {} \;
fi
echo "Job finished: $(date)"
env:
- name: PGUSER
value: artifactory
- name: PGPASSWORD
valueFrom:
secretKeyRef:
name: artifactory-db-postgresql
key: postgresql-password
- name: PGDB
value: artifactory
- name: PGHOST
value: artifactory-db-postgresql
- name: DELETE_OLDER_THAN
value: "10080"
- name: PREFIX
value: artifactory
volumeMounts:
- mountPath: /dump
name: database-backups
volumes:
- name: database-backups
persistentVolumeClaim:
claimName: database-backups
restartPolicy: OnFailure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment