postgresql_backup. Allow multiple servers.
This commit is contained in:
parent
a2b01d0685
commit
aed0c982a8
|
@ -8,32 +8,60 @@ else
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Set file extension based on DUMP_FORMAT
|
# Ensure BACKUP_PATH is set
|
||||||
EXTENSION="dump" # Default extension for custom format
|
if [ -z "$BACKUP_PATH" ]; then
|
||||||
if [[ "$DUMP_FORMAT" == "sql" ]]; then
|
echo "BACKUP_PATH not set in .env file!"
|
||||||
EXTENSION="sql.gz"
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Create backup directory if it doesn't exist
|
# Extract unique database prefixes (DB1, DB2, ...)
|
||||||
mkdir -p "$BACKUP_PATH"
|
DATABASE_PREFIXES=$(env | grep -o 'DB[0-9]*_' | sort -u)
|
||||||
|
|
||||||
# Get a list of all databases, excluding template databases
|
# Loop through each database configuration prefix
|
||||||
DATABASES=$(psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -t -c "SELECT datname FROM pg_database WHERE datistemplate = false;")
|
for PREFIX in $DATABASE_PREFIXES; do
|
||||||
|
HOST_VAR="${PREFIX}HOST"
|
||||||
|
PORT_VAR="${PREFIX}PORT"
|
||||||
|
USER_VAR="${PREFIX}USER"
|
||||||
|
FORMAT_VAR="${PREFIX}DUMP_FORMAT"
|
||||||
|
|
||||||
# Loop over each database and create a backup
|
HOST=${!HOST_VAR}
|
||||||
for DB in $DATABASES; do
|
PORT=${!PORT_VAR}
|
||||||
BACKUP_FILE="$BACKUP_PATH/$DB.$EXTENSION"
|
USER=${!USER_VAR}
|
||||||
|
DUMP_FORMAT=${!FORMAT_VAR:-"custom"} # Default to "custom" if not set
|
||||||
|
|
||||||
if [[ "$DUMP_FORMAT" == "custom" ]]; then
|
if [ -z "$HOST" ] || [ -z "$PORT" ] || [ -z "$USER" ]; then
|
||||||
# Create a compressed custom-format backup
|
echo "Incomplete configuration for $PREFIX. Skipping..."
|
||||||
pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc "$DB" -f "$BACKUP_FILE"
|
continue
|
||||||
elif [[ "$DUMP_FORMAT" == "sql" ]]; then
|
|
||||||
# Create a plain-text SQL backup and compress it with gzip
|
|
||||||
pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB" | gzip > "$BACKUP_FILE"
|
|
||||||
else
|
|
||||||
echo "Invalid DUMP_FORMAT specified: $DUMP_FORMAT"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Backup for database '$DB' created at '$BACKUP_FILE'"
|
# Create a directory for each server using host and port
|
||||||
|
SERVER_DIR="$BACKUP_PATH/${HOST}_${PORT}"
|
||||||
|
mkdir -p "$SERVER_DIR"
|
||||||
|
|
||||||
|
# Get a list of all databases for this server, excluding template databases
|
||||||
|
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
|
||||||
|
# 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"
|
||||||
|
|
||||||
|
# Perform the backup based on the specified format
|
||||||
|
if [[ "$DUMP_FORMAT" == "custom" ]]; then
|
||||||
|
pg_dump -h "$HOST" -p "$PORT" -U "$USER" -Fc "$DB_NAME" -f "$BACKUP_FILE"
|
||||||
|
elif [[ "$DUMP_FORMAT" == "sql" ]]; then
|
||||||
|
pg_dump -h "$HOST" -p "$PORT" -U "$USER" "$DB_NAME" | gzip > "$BACKUP_FILE"
|
||||||
|
else
|
||||||
|
echo "Invalid DUMP_FORMAT for $PREFIX: $DUMP_FORMAT. Skipping..."
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Backup for database '$DB_NAME' created at '$BACKUP_FILE'"
|
||||||
|
done
|
||||||
done
|
done
|
Loading…
Reference in a new issue