2024-05-28 22:19:46 -06:00
|
|
|
#!/bin/bash
|
|
|
|
|
2024-05-28 22:30:54 -06:00
|
|
|
set -euo pipefail
|
|
|
|
|
2024-05-28 22:19:46 -06:00
|
|
|
# USAGE:
|
|
|
|
# migrate-postgresql-version.sh <data_directory> <old_version> <new_version>
|
|
|
|
|
|
|
|
if [ "$#" -ne 3 ]; then
|
|
|
|
echo "USAGE: migrate-postgresql-version.sh <data_directory> <old_version> <new_version>"
|
|
|
|
echo "Example: migrate-postgresql-version.sh pgdata 14 16"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
data_directory=$1
|
|
|
|
old_version=$2
|
|
|
|
new_version=$3
|
2024-05-28 23:02:20 -06:00
|
|
|
new_data_directory=$data_directory.new
|
|
|
|
|
|
|
|
# we'll need the credentials to create the new container
|
|
|
|
echo "Please provide the credentials for your database"
|
|
|
|
echo "If you set a different password for the old container, you'll need to provide it here! Check your config file if you're not sure"
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
echo "Database user (default 'akkoma'):"
|
|
|
|
read DB_USER
|
|
|
|
echo "Database password (default: 'akkoma'):"
|
|
|
|
read DB_PASS
|
|
|
|
echo "Database name (default: 'akkoma'):"
|
|
|
|
read DB_NAME
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
echo "Ok! Using user:$DB_USER to migrate db:$DB_NAME from version $old_version to $new_version"
|
|
|
|
|
|
|
|
trap "docker stop pg$old_version pg$new_version" INT TERM
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Start a PostgreSQL 14 container
|
2024-05-28 23:02:20 -06:00
|
|
|
docker run --rm -d --name pg$old_version \
|
|
|
|
-v $(pwd)/$data_directory:/var/lib/postgresql/data \
|
|
|
|
-e "POSTGRES_PASSWORD=$DB_PASS" \
|
|
|
|
-e "POSTGRES_USER=$DB_USER" \
|
|
|
|
-e "POSTGRES_DB=$DB_NAME" \
|
|
|
|
postgres:$old_version-alpine
|
2024-05-28 22:30:54 -06:00
|
|
|
|
|
|
|
# wait a bit for the container to start
|
|
|
|
sleep 10
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Dump the db from the old container
|
2024-05-28 23:02:20 -06:00
|
|
|
echo "Dumping your old database..."
|
|
|
|
|
|
|
|
docker exec pg$old_version pg_dumpall -U $DB_USER > dump.sql
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Stop the old container
|
2024-05-28 23:02:20 -06:00
|
|
|
echo "Stopping the old database..."
|
|
|
|
docker stop pg$old_version
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Start a PostgreSQL 16 container
|
2024-05-28 23:02:20 -06:00
|
|
|
echo "Creating a new database with version $new_version..."
|
|
|
|
docker run --rm -d --name pg$new_version \
|
|
|
|
-v $(pwd)/$new_data_directory:/var/lib/postgresql/data \
|
|
|
|
-e "POSTGRES_PASSWORD=password" \
|
|
|
|
-e "POSTGRES_USER=$DB_USER" \
|
|
|
|
-e "POSTGRES_DB=$DB_NAME" \
|
|
|
|
postgres:$new_version-alpine
|
|
|
|
|
|
|
|
# wait for it
|
|
|
|
sleep 10
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Load the db into the new container
|
2024-05-28 23:02:20 -06:00
|
|
|
docker exec -i pg$new_version psql -U $DB_USER < dump.sql
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Stop the new container
|
2024-05-28 23:02:20 -06:00
|
|
|
docker stop pg$new_version
|
2024-05-28 22:19:46 -06:00
|
|
|
|
|
|
|
# Remove the dump file
|
2024-05-28 23:02:20 -06:00
|
|
|
# rm dump.sql
|
2024-05-28 22:19:46 -06:00
|
|
|
|
2024-05-28 22:30:54 -06:00
|
|
|
echo "Migration complete! Your new database folder is $data_directory.new - you can now move your old data and replace it"
|
|
|
|
|
|
|
|
echo "mv $data_directory $data_directory.old"
|
2024-05-28 23:02:20 -06:00
|
|
|
echo "mv $new_data_directory $data_directory"
|