Add postgres backup scripts and cronjobs
This commit is contained in:
parent
d02f655c05
commit
a71bb956d6
3
postgres_backup/README.md
Normal file
3
postgres_backup/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
kubectl create cm postgres-backup-script --from-file backup-script.sh \
|
||||||
|
--dry-run=client -o yaml | kubectl apply -f -
|
||||||
|
|
40
postgres_backup/backup-script.sh
Normal file
40
postgres_backup/backup-script.sh
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#!/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="/backup/"
|
||||||
|
|
||||||
|
# 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 --username="${PG_USER}" --host="${PG_HOST}" --port="${PG_PORT}" | gzip >"${BACKUP_FILE}"
|
||||||
|
|
||||||
|
# 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}"
|
40
postgres_backup/cronjob.yml
Normal file
40
postgres_backup/cronjob.yml
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
apiVersion: batch/v1
|
||||||
|
kind: CronJob
|
||||||
|
metadata:
|
||||||
|
name: postgres-backup
|
||||||
|
spec:
|
||||||
|
schedule: "0 0 * * *"
|
||||||
|
jobTemplate:
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: postgres-backup
|
||||||
|
image: postgres:15-alpine
|
||||||
|
command: ["sh", "/backup-script.sh"]
|
||||||
|
env:
|
||||||
|
- name: PG_HOST
|
||||||
|
value: postgres-postgresql
|
||||||
|
- name: PG_USER
|
||||||
|
value: backup
|
||||||
|
- name: PGPASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: backupscrets
|
||||||
|
key: pgbackupsecret
|
||||||
|
volumeMounts:
|
||||||
|
- name: backup-volume
|
||||||
|
mountPath: /backup
|
||||||
|
subPath: postgres
|
||||||
|
- name: backup-script
|
||||||
|
mountPath: /backup-script.sh
|
||||||
|
subPath: backup-script.sh
|
||||||
|
readOnly: true
|
||||||
|
restartPolicy: OnFailure
|
||||||
|
volumes:
|
||||||
|
- name: backup-volume
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: nfs-postgresbackup-claim
|
||||||
|
- name: backup-script
|
||||||
|
configMap:
|
||||||
|
name: postgres-backup-script
|
@ -1,29 +1,29 @@
|
|||||||
apiVersion: batch/v1
|
apiVersion: batch/v1
|
||||||
kind: Job
|
kind: Job
|
||||||
metadata:
|
metadata:
|
||||||
name: mysql-backup
|
name: postgres-backup
|
||||||
spec:
|
spec:
|
||||||
template:
|
template:
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: mysql-backup
|
- name: postgres-backup
|
||||||
image: mariadb:latest
|
image: postgres:15-alpine
|
||||||
command: [ "/bin/sh" ]
|
command: [ "/bin/sh" ]
|
||||||
args: [ "-c", "sleep 3600" ]
|
args: [ "-c", "sleep 3600" ]
|
||||||
env:
|
env:
|
||||||
- name: MYSQL_HOST
|
- name: PG_HOST
|
||||||
value: galera-mariadb-galera
|
value: postgres-postgresql
|
||||||
- name: MYSQL_USER
|
- name: PG_USER
|
||||||
value: backupuser
|
value: backup
|
||||||
- name: MYSQL_PASSWORD
|
- name: PGPASSWORD
|
||||||
valueFrom:
|
valueFrom:
|
||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
name: backupscrets
|
name: backupscrets
|
||||||
key: backupsecret
|
key: pgbackupsecret
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- name: backup-volume
|
- name: backup-volume
|
||||||
mountPath: /backup
|
mountPath: /backup
|
||||||
subPath: mysql
|
subPath: postgres
|
||||||
- name: backup-script
|
- name: backup-script
|
||||||
mountPath: /backup-script.sh
|
mountPath: /backup-script.sh
|
||||||
subPath: backup-script.sh
|
subPath: backup-script.sh
|
||||||
@ -36,6 +36,5 @@ spec:
|
|||||||
path: /volume1/backupk8s
|
path: /volume1/backupk8s
|
||||||
- name: backup-script
|
- name: backup-script
|
||||||
configMap:
|
configMap:
|
||||||
name: backup-script
|
name: postgres-backup-script
|
||||||
backoffLimit: 1
|
backoffLimit: 1
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: PersistentVolume
|
kind: PersistentVolume
|
||||||
metadata:
|
metadata:
|
||||||
name: nfs-backup-mysql
|
name: nfs-backup-postgres
|
||||||
spec:
|
spec:
|
||||||
capacity:
|
capacity:
|
||||||
storage: 1Mi
|
storage: 1Mi
|
||||||
@ -9,5 +9,5 @@ spec:
|
|||||||
- ReadWriteMany
|
- ReadWriteMany
|
||||||
nfs:
|
nfs:
|
||||||
server: 192.168.86.86
|
server: 192.168.86.86
|
||||||
path: "/volume1/backupk8s/mysql/"
|
path: "/volume1/backupk8s/postgres/"
|
||||||
storageClassName: nfs
|
storageClassName: nfs
|
||||||
|
12
postgres_backup/pvc_nas.yml
Normal file
12
postgres_backup/pvc_nas.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolumeClaim
|
||||||
|
metadata:
|
||||||
|
name: nfs-postgresbackup-claim
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteMany
|
||||||
|
storageClassName: "nfs"
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 1Mi
|
||||||
|
|
@ -1,45 +0,0 @@
|
|||||||
#!/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}"
|
|
Loading…
Reference in New Issue
Block a user