postgresql_backup. Progress output and smart directory creation.

This commit is contained in:
Alexey Skobkin 2024-11-06 19:20:13 +00:00
parent 6498a3ab1d
commit e5731a3529

View file

@ -34,15 +34,23 @@ for PREFIX in $DATABASE_PREFIXES; do
continue continue
fi fi
# Create a directory for each server using host and port # Create a directory for each server using host and port, only if it doesn't exist
SERVER_DIR="$BACKUP_PATH/${HOST}_${PORT}" SERVER_DIR="$BACKUP_PATH/${HOST}_${PORT}"
if [ ! -d "$SERVER_DIR" ]; then
echo "Creating backup directory: $SERVER_DIR"
mkdir -p "$SERVER_DIR" mkdir -p "$SERVER_DIR"
else
echo "Backup directory $SERVER_DIR already exists, skipping creation."
fi
# Get a list of all databases for this server, excluding template databases # Get a list of all databases for this server, excluding template databases
echo "Fetching databases for $HOST:$PORT ..."
DATABASES=$(psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;") DATABASES=$(psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
# Loop over each database and create a backup # Loop over each database and create a backup
for DB_NAME in $DATABASES; do for DB_NAME in $DATABASES; do
echo "Starting backup for database '$DB_NAME' on $HOST:$PORT..."
# Set file extension based on DUMP_FORMAT # Set file extension based on DUMP_FORMAT
EXTENSION="dump" EXTENSION="dump"
if [[ "$DUMP_FORMAT" == "sql" ]]; then if [[ "$DUMP_FORMAT" == "sql" ]]; then
@ -51,19 +59,22 @@ for PREFIX in $DATABASE_PREFIXES; do
# Set backup file path within the server-specific directory # Set backup file path within the server-specific directory
BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION" BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION"
echo "Backup file: $BACKUP_FILE"
# Perform the backup based on the specified format # Perform the backup based on the specified format
if [[ "$DUMP_FORMAT" == "custom" ]]; then if [[ "$DUMP_FORMAT" == "custom" ]]; then
# Custom format and highest ZSTD compression level echo "Performing custom format backup with zstd compression level 21..."
pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE" pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE"
elif [[ "$DUMP_FORMAT" == "sql" ]]; then elif [[ "$DUMP_FORMAT" == "sql" ]]; then
# Plain-text SQL backup compressed with gzip at high compression level echo "Performing SQL format backup with gzip compression..."
pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE" pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE"
else else
echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..." echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..."
continue continue
fi fi
echo "Backup for database '$DB_NAME' created at '$BACKUP_FILE'" echo "Backup for database '$DB_NAME' on $HOST:$PORT completed successfully."
done done
done done
echo "Backup process completed."