Add postgres backup

This commit is contained in:
Bart Geesink 2023-09-17 11:10:08 +02:00
parent eed4145553
commit fa5e2869ec
3 changed files with 99 additions and 0 deletions

41
postgres_backup/job.yml Normal file
View File

@ -0,0 +1,41 @@
apiVersion: batch/v1
kind: Job
metadata:
name: mysql-backup
spec:
template:
spec:
containers:
- name: mysql-backup
image: mariadb:latest
command: [ "/bin/sh" ]
args: [ "-c", "sleep 3600" ]
env:
- name: MYSQL_HOST
value: galera-mariadb-galera
- name: MYSQL_USER
value: backupuser
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: backupscrets
key: backupsecret
volumeMounts:
- name: backup-volume
mountPath: /backup
subPath: mysql
- name: backup-script
mountPath: /backup-script.sh
subPath: backup-script.sh
readOnly: true
restartPolicy: OnFailure
volumes:
- name: backup-volume
nfs:
server: 192.168.86.86
path: /volume1/backupk8s
- name: backup-script
configMap:
name: backup-script
backoffLimit: 1

View File

@ -0,0 +1,13 @@
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-backup-mysql
spec:
capacity:
storage: 1Mi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.86.86
path: "/volume1/backupk8s/mysql/"
storageClassName: nfs

45
postgres_backup/script.sh Normal file
View File

@ -0,0 +1,45 @@
#!/bin/bash
# Set default values
PG_USER=${PG_USER:-postgres}
PG_PASSWORD=${PG_PASSWORD:-postgres}
PG_HOST=${PG_HOST:-localhost}
PG_PORT=${PG_PORT:-5432}
# Set backup directory
BACKUP_DIR="/data/backups/postgres"
# Generate backup timestamp with format "YYYY-MM-DD_HH-MM-SS"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
# Set backup file name with timestamp
BACKUP_FILE="${BACKUP_DIR}/pg_dump_all_${TIMESTAMP}.sql.gz"
# Dump all databases to backup file
pg_dumpall --clean --if-exists --dbname=postgres --username="${PG_USER}" --host="${PG_HOST}" --port="${PG_PORT}" | gzip >"${BACKUP_FILE}"
# Set retention periods
WEEKLY_THRESHOLD=$(date --date="-1 week" +"%s")
MONTHLY_THRESHOLD=$(date --date="-1 month" +"%s")
YEARLY_THRESHOLD=$(date --date="-1 year" +"%s")
# Rename backups that are old enough to weekly, monthly, or yearly
find $BACKUP_DIR -maxdepth 1 -type f -name "*.gz" -not -name "*weekly*" \
-not -name "*monthly*" -not -name "*yearly*" \
-mtime +7 -mtime -28 -exec mv -f {} ${BACKUP_DIR}/pg_dump_all_weekly.sql.gz \; # move daily backups older than 7 days to weekly backups
find $BACKUP_DIR -maxdepth 1 -type f -name "*.gz" -not -name "*monthly*" \
-not -name "*yearly*" -mtime +28 -mtime -365 \
-exec mv -f {} ${BACKUP_DIR}/pg_dump_all_monthly.sql.gz \; # move weekly backups older than 28 days to monthly backups
find $BACKUP_DIR -maxdepth 1 -type f -name "*.gz" -not -name "*yearly*" \
-mtime +365 -exec mv -f {} ${BACKUP_DIR}/pg_dump_all_yearly.sql.gz \; # move monthly backups older than 365 days to yearly backups
# Clean up backups older than retention periods
find $BACKUP_DIR -maxdepth 1 -type f -name "*.gz" \
-mtime +7 -mtime -28 -not -name "*weekly*" -not -name "*monthly*" -exec rm -f {} + # remove daily backups older than 7 days but exclude those that are weekly or monthly backups
find $BACKUP_DIR -maxdepth 1 -type f -name "*.gz" \
-mtime +28 -mtime -365 -not -name "*monthly*" -not -name "*yearly*" -exec rm -f {} + # remove weekly backups older than 28 days but exclude those that are monthly or yearly backups
find $BACKUP_DIR -maxdepth 1 -type f -name "*.gz" \
-mtime +365 -not -name "*yearly*" -exec rm -f {} + # remove monthly backups older than 365 days but exclude those that are yearly backups
# Print confirmation message
echo "Done. Backup file: ${BACKUP_FILE}"