#!/bin/bash if [ "$(id -u)" != "0" ]; then echo -e "\033[31mThis script requires superuser rights\033[0m" exit 0 fi if [ -z "$1" ] || [ -z "$2" ]; then echo "Please provide both the POSTGRES_DB and POSTGRES_USER as arguments" exit 1 fi trap 'echo -e "\033[31mSomething went wrong\033[0m"; exit 1' EXIT set -e export DEBIAN_FRONTEND=noninteractive # configuration settings PGDB="$1" PGUSER="$2" CURRENT_MONTH=$(date +%Y-%m) CURRENT_DATE=$(date +%Y-%m-%d) #CURRENT_DATETIME=$(date +%d-%b-%Y_%H_%M_%Z) CURRENT_DATETIME=$(date +%d-%m-%Y_%H%M%S) BACKUPS_PATH=/backups/postgres DOCKER_SERVICE_NAME=postgres # backup PostgreSQL database BACKUP_FOLDER=$BACKUPS_PATH/$PGDB if [ ! -d "$BACKUP_FOLDER" ]; then mkdir -p "$BACKUP_FOLDER" fi echo 'Creating PostgreSQL backups...' cd "$BACKUP_FOLDER" if [ -f 'dump_'"$PGDB"'.sql' ]; then rm 'dump_'"$PGDB"'.sql' fi db_backup_filename=$PGDB'_'$CURRENT_DATETIME'.tar.gz' docker exec -t postgres pg_dump -U $PGUSER $PGDB > 'dump_'"$PGDB"'.sql' tar -cf - 'dump_'"$PGDB"'.sql' | gzip -9 > "$db_backup_filename" rm 'dump_'"$PGDB"'.sql' cd "$BACKUP_FOLDER" md5sum * > MD5SUMS trap - EXIT echo 'The backup of the '"$PGDB"' database was completed successfully'