docker-postgres/backup-database.sh

28 lines
988 B
Bash
Raw Normal View History

2023-05-11 09:39:09 +00:00
#!/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"
if $PURGE_OLD; then DRY_RUN=false; else DRY_RUN=true; fi
echo "Following file are older than 14 day, it will be deleted (dry-run $DRY_RUN)"
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
2023-05-11 09:39:09 +00:00
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
2023-05-11 09:39:09 +00:00
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";