Namespace-aware databases backups #5

Merged
saretter merged 1 commits from feature/namespace-aware-backup-restore into main 1 year ago
  1. 4
      Chart.yaml
  2. 14
      README.md
  3. 72
      postgresql-restore.yaml
  4. 11
      scripts/backup.sh
  5. 13
      scripts/restore.sh
  6. 4
      templates/cronjob.yaml
  7. 2
      values.yaml

@ -1,7 +1,7 @@
apiVersion: v2
name: postgresql
version: 1.1.0
appVersion: 11.5.0
version: 1.2.0
appVersion: 12
description: This is a fork of https://github.com/cetic/helm-postgresql. PostgreSQL is an open-source object-relational database management system (ORDBMS) emphasizing extensibility and technical standards compliance.
keywords:
- postgresql

@ -61,13 +61,13 @@ In order to restore a backup you can use the `restore.sh` provided in configmap
apiVersion: batch/v1
kind: Job
metadata:
name: restore
name: postgresql-restore
spec:
template:
spec:
containers:
- name: postgresql-restore
image: postgres:11.5
image: postgres:12
command:
- /bin/sh
- -c
@ -84,7 +84,7 @@ spec:
name: postgresql
key: username
- name: PGHOST
value: "postgre-postgresql" # Make sure this matches the hostname of you target PostgreSQL instance
value: "postgresql" # Make sure this matches the hostname of you target PostgreSQL instance
- name: PGDATABASE
valueFrom:
secretKeyRef:
@ -110,8 +110,12 @@ spec:
secretKeyRef:
name: restic
key: s3-key-secret
- name: TIMESTAMP # Timestamp of backup according to below pattern
value: "07-27-2021-16-54"
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: RESTORE_ID # ID of backup or latest
value: "latest"
volumeMounts:
- name: restore-backup-script
mountPath: "/restore.sh"

@ -0,0 +1,72 @@
apiVersion: batch/v1
kind: Job
metadata:
name: postgresql-restore
spec:
template:
spec:
containers:
- name: postgresql-restore
image: postgres:12
command:
- /bin/sh
- -c
- /restore.sh
env:
- name: PGPASS
valueFrom:
secretKeyRef:
name: postgresql
key: password
- name: PGUSER
valueFrom:
secretKeyRef:
name: postgresql
key: username
- name: PGHOST
value: "postgresql" # Make sure this matches the hostname of you target PostgreSQL instance
- name: PGDATABASE
valueFrom:
secretKeyRef:
name: postgresql
key: database
- name: RESTIC_REPOSITORY
valueFrom:
secretKeyRef:
name: restic
key: repository-location
- name: RESTIC_PASSWORD
valueFrom:
secretKeyRef:
name: restic
key: repository-password
- name: AWS_ACCESS_KEY_ID # Make sure to adapt that to your specific setup
valueFrom:
secretKeyRef:
name: restic
key: s3-key-id
- name: AWS_SECRET_ACCESS_KEY # Make sure to adapt that to your specific setup
valueFrom:
secretKeyRef:
name: restic
key: s3-key-secret
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: RESTORE_ID # ID of backup or latest
value: "34067f09"
volumeMounts:
- name: restore-backup-script
mountPath: "/restore.sh"
subPath: "restore.sh"
readOnly: false
restartPolicy: Never
volumes:
- name: restore-backup-script
configMap:
name: postgresql-backup
items:
- key: restore.sh
path: restore.sh
defaultMode: 0777

@ -10,21 +10,22 @@ apt-get install ca-certificates -y
# Prepare for pg_dump
echo "$PGHOST:5432:$PGDATABASE:$PGUSER:$PGPASS" > /root/.pgpass
chmod 600 /root/.pgpass
mkdir /tmp/backup
mkdir -p /tmp/backup/$NAMESPACE
# Execute pg_dump
pg_dump --clean > /tmp/backup/$PGHOST-$PGDATABASE.sql
pg_dump --clean > /tmp/backup/$NAMESPACE/$PGHOST-$PGDATABASE.sql
# Clean old dumps
find /tmp/backup/ \( -name '*' \) -mtime $BACKUP_RETENTION_DAYS -exec rm -rf {} \;
# Check if restic repository is initialized and initialize if not
if ! restic snapshots; then
if ! restic --no-cache snapshots; then
echo "Repo doesn't exist. Creating it ..."
restic --no-cache init
fi
# Backup to restic-repository
restic backup /tmp/backup
restic --no-cache backup /tmp/backup/$NAMESPACE/
# Prune old backups
restic forget --keep-last $BACKUP_RETENTION_DAYS
restic --no-cache forget --keep-last $BACKUP_RETENTION_DAYS

@ -10,20 +10,15 @@ apt install ca-certificates -y
# Prepare for pg_dump
echo "$PGHOST:5432:$PGDATABASE:$PGUSER:$PGPASS" > /root/.pgpass
chmod 600 /root/.pgpass
mkdir /tmp/backup
mkdir -p /tmp/backup/$NAMESPACE
# Check if restic repository is initialized and initialize if not
if ! restic snapshots; then
if ! restic --no-cache snapshots; then
exit -1
fi
# Restore backup to /tmp/backup
restic --no-cache restore latest --target /
restic --no-cache restore $RESTORE_ID --target /
# Import pg_dump
ls -all -h /tmp/backup
echo $PGHOST
echo $PGDATABASE
echo "Restoring $PGHOST-$PGDATABASE.sql"
echo "pg_restore --clean --create -F c -f /tmp/backup/$PGHOST-$PGDATABASE.sql"
psql < "/tmp/backup/$PGHOST-$PGDATABASE.sql"
psql < "/tmp/backup/$NAMESPACE/$PGHOST-$PGDATABASE.sql"

@ -104,6 +104,10 @@ spec:
name: {{ .Values.backup.restic.existingSecret }}
{{- end }}
key: repository-password
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
{{- if .Values.backup.extraEnv }}
{{ tpl (toYaml .Values.backup.extraEnv) $ | indent 12 }}
{{- end }}

@ -4,7 +4,7 @@
##
image:
repository: postgres
tag: "11.5"
tag: "12"
pullPolicy: IfNotPresent
## Optionally specify an imagePullSecret.

Loading…
Cancel
Save