K8s CronJob to backup ArtifactoryDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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