2024-11-06 17:51:27 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-11-06 17:55:49 +00:00
|
|
|
# Load environment variables from .env file
|
|
|
|
if [ -f .env ]; then
|
|
|
|
export $(grep -v '^#' .env | xargs)
|
|
|
|
else
|
|
|
|
echo ".env file not found! Please create it with the necessary configuration."
|
|
|
|
exit 1
|
|
|
|
fi
|
2024-11-06 17:51:27 +00:00
|
|
|
|
2024-11-06 18:31:04 +00:00
|
|
|
# Ensure BACKUP_PATH is set
|
|
|
|
if [ -z "$BACKUP_PATH" ]; then
|
|
|
|
echo "BACKUP_PATH not set in .env file!"
|
|
|
|
exit 1
|
2024-11-06 17:51:27 +00:00
|
|
|
fi
|
|
|
|
|
2024-11-06 18:31:04 +00:00
|
|
|
# Extract unique database prefixes (DB1, DB2, ...)
|
|
|
|
DATABASE_PREFIXES=$(env | grep -o 'DB[0-9]*_' | sort -u)
|
|
|
|
|
|
|
|
# Loop through each database configuration prefix
|
|
|
|
for PREFIX in $DATABASE_PREFIXES; do
|
|
|
|
HOST_VAR="${PREFIX}HOST"
|
|
|
|
PORT_VAR="${PREFIX}PORT"
|
|
|
|
USER_VAR="${PREFIX}USER"
|
|
|
|
FORMAT_VAR="${PREFIX}DUMP_FORMAT"
|
|
|
|
|
|
|
|
HOST=${!HOST_VAR}
|
|
|
|
PORT=${!PORT_VAR}
|
|
|
|
USER=${!USER_VAR}
|
|
|
|
DUMP_FORMAT=${!FORMAT_VAR:-"custom"} # Default to "custom" if not set
|
|
|
|
|
|
|
|
if [ -z "$HOST" ] || [ -z "$PORT" ] || [ -z "$USER" ]; then
|
|
|
|
echo "Incomplete configuration for $PREFIX. Skipping..."
|
|
|
|
continue
|
2024-11-06 17:51:27 +00:00
|
|
|
fi
|
|
|
|
|
2024-11-06 19:20:13 +00:00
|
|
|
# Create a directory for each server using host and port, only if it doesn't exist
|
2024-11-06 18:31:04 +00:00
|
|
|
SERVER_DIR="$BACKUP_PATH/${HOST}_${PORT}"
|
2024-11-06 19:20:13 +00:00
|
|
|
if [ ! -d "$SERVER_DIR" ]; then
|
|
|
|
echo "Creating backup directory: $SERVER_DIR"
|
|
|
|
mkdir -p "$SERVER_DIR"
|
|
|
|
else
|
|
|
|
echo "Backup directory $SERVER_DIR already exists, skipping creation."
|
|
|
|
fi
|
2024-11-06 18:31:04 +00:00
|
|
|
|
|
|
|
# Get a list of all databases for this server, excluding template databases
|
2024-11-06 19:20:13 +00:00
|
|
|
echo "Fetching databases for $HOST:$PORT ..."
|
2024-11-06 18:31:04 +00:00
|
|
|
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
|
|
|
|
for DB_NAME in $DATABASES; do
|
2024-11-06 19:20:13 +00:00
|
|
|
echo "Starting backup for database '$DB_NAME' on $HOST:$PORT..."
|
|
|
|
|
2024-11-06 18:31:04 +00:00
|
|
|
# Set file extension based on DUMP_FORMAT
|
|
|
|
EXTENSION="dump"
|
|
|
|
if [[ "$DUMP_FORMAT" == "sql" ]]; then
|
|
|
|
EXTENSION="sql.gz"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set backup file path within the server-specific directory
|
|
|
|
BACKUP_FILE="$SERVER_DIR/$DB_NAME.$EXTENSION"
|
2024-11-06 19:20:13 +00:00
|
|
|
echo "Backup file: $BACKUP_FILE"
|
2024-11-06 18:31:04 +00:00
|
|
|
|
|
|
|
# Perform the backup based on the specified format
|
|
|
|
if [[ "$DUMP_FORMAT" == "custom" ]]; then
|
2024-11-06 18:40:00 +00:00
|
|
|
pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc --compress="zstd:21" "$DB_NAME" -f "$BACKUP_FILE"
|
2024-11-06 18:31:04 +00:00
|
|
|
elif [[ "$DUMP_FORMAT" == "sql" ]]; then
|
2024-11-06 18:40:00 +00:00
|
|
|
pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip -9 > "$BACKUP_FILE"
|
2024-11-06 18:31:04 +00:00
|
|
|
else
|
|
|
|
echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..."
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2024-11-06 19:20:13 +00:00
|
|
|
echo "Backup for database '$DB_NAME' on $HOST:$PORT completed successfully."
|
2024-11-06 18:31:04 +00:00
|
|
|
done
|
|
|
|
done
|
2024-11-06 19:20:13 +00:00
|
|
|
|
|
|
|
echo "Backup process completed."
|