67 lines
2.2 KiB
Bash
67 lines
2.2 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
DATA_DIR="/var/lib/postgresql/data"
|
|
OLD_VERSION_FILE="$DATA_DIR/PG_VERSION"
|
|
NEW_VERSION="16"
|
|
OLD_BIN="/usr/lib/postgresql/15/bin"
|
|
NEW_BIN="/usr/lib/postgresql/16/bin"
|
|
|
|
if [ -f "$OLD_VERSION_FILE" ]; then
|
|
OLD_VERSION=$(cat "$OLD_VERSION_FILE")
|
|
|
|
if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then
|
|
echo "Data version ($OLD_VERSION) differs from engine ($NEW_VERSION). Starting upgrade..."
|
|
|
|
# Move old data to a temporary folder
|
|
mkdir -p $DATA_DIR/old_data
|
|
mv $DATA_DIR/* $DATA_DIR/old_data/ || true
|
|
chmod 700 $DATA_DIR/old_data
|
|
|
|
# Initialize new data directory
|
|
mkdir $DATA_DIR/new_data
|
|
initdb -D $DATA_DIR/new_data
|
|
|
|
# Prevent error "You must have read and write access in the current directory."
|
|
cd $DATA_DIR
|
|
|
|
# Run pg_upgrade
|
|
# --link is used to avoid copying files (fast, but requires a backup!)
|
|
if ! pg_upgrade \
|
|
-d $DATA_DIR/old_data \
|
|
-D $DATA_DIR/new_data \
|
|
-b "$OLD_BIN" \
|
|
-B "$NEW_BIN" \
|
|
-s /tmp \
|
|
--link; then
|
|
|
|
echo "========================================"
|
|
echo "pg_upgrade FAILED! Here is the actual reason why the old server crashed:"
|
|
echo "========================================"
|
|
|
|
# Print the contents of the server log
|
|
cat "$DATA_DIR"/new_data/pg_upgrade_output.d/*/log/pg_upgrade_server.log
|
|
|
|
# Exit to prevent the script from continuing
|
|
exit 1
|
|
fi
|
|
|
|
# Migrate configuration files
|
|
echo "Upgrade complete. Cleaning up..."
|
|
cp "$DATA_DIR/old_data/pg_hba.conf" "$DATA_DIR/new_data/pg_hba.conf"
|
|
cp "$DATA_DIR/old_data/postgresql.conf" "$DATA_DIR/new_data/postgresql.conf"
|
|
if [ -f "$DATA_DIR/old_data/pg_ident.conf" ]; then
|
|
cp "$DATA_DIR/old_data/pg_ident.conf" "$DATA_DIR/new_data/pg_ident.conf"
|
|
fi
|
|
|
|
# Ensure the postgres user owns the restored files
|
|
chown postgres:postgres "$DATA_DIR"/new_data/*.conf
|
|
|
|
# Clean-up
|
|
rm -rf $DATA_DIR/old_data
|
|
mv $DATA_DIR/new_data/* $DATA_DIR/
|
|
rmdir $DATA_DIR/new_data
|
|
fi
|
|
fi
|
|
|
|
exec $@ |