docker-postgres/backup-database.sh
RouxAntoine b0d1616f8a
All checks were successful
Main / build (push) Successful in 8m48s
feature: purge backup older than 14 days
2023-12-03 15:08:02 +01:00

27 lines
936 B
Bash
Executable File

#!/bin/bash
set -e
dt=$(date '+%d/%m/%Y %H:%M:%S');
fileDt=$(date '+%d_%m_%Y_%H_%M_%S');
backUpFileName="postgres-backup-$fileDt.gz"
backUpFilePath="$BACKUP_DESTINATION_FOLDER/$backUpFileName"
echo "Following file are older than 14 day, it will be deleted (dry-run $PURGE_OLD)"
find /var/lib/backup/ -type f -mtime +14 -name '*.gz' -execdir ls -- '{}' \;
if [ $PURGE_OLD == 'true' ]; then
find /var/lib/backup/ -type f -mtime +14 -name '*.gz' -execdir rm -- '{}' \;
fi
echo "$dt - Starting DB backup into file: $backUpFilePath";
echo "$dt - Running: pg_dumpall -h $BACKUP_SOURCE_DB_HOST_NAME -U $POSTGRES_USER -c | gzip > $backUpFilePath"
pg_dumpall --if-exists -h $BACKUP_SOURCE_DB_HOST_NAME -U $POSTGRES_USER -c | gzip > $backUpFilePath
if [ $? -ne 0 ]; then
rm $backUpFilePath
echo "Unable to execute a BackUp. Please check DB connection settings"
exit 1
fi
echo "$dt - DB backup completed into file: $backUpFilePath";