Skip to content

Instantly share code, notes, and snippets.

@jamesaspence
Last active October 31, 2023 21:44
Show Gist options
  • Save jamesaspence/ecaa7249de250c4e13083cb1359bc37f to your computer and use it in GitHub Desktop.
Save jamesaspence/ecaa7249de250c4e13083cb1359bc37f to your computer and use it in GitHub Desktop.
Upgrades our local postgres 10 -> 14. Provides a rollback script that will allow rolling back.
#!/usr/bin/env bash
set -euxo pipefail
echo -e "Let's get a backup created for your data, just in case!\n"
echo -e "Shutting down the app...\n"
# First, shut down the app if it's running.
docker compose -f docker-compose.yml -f docker-optional-app.yaml down
echo -e "Creating a backup of the docker volume.\n"
# First, set up a backup of your data (just in case!)
docker volume create sora_dbdata_10
echo -e "Copying data over to the backup.\n"
# Copy data over from sora_dbdata to sora_dbdata_10
docker run --rm \
-v sora_dbdata:/from \
-v sora_dbdata_10:/to \
alpine ash -c "cd /from; cp -av . /to;"
echo -e "\n===================================================================\n"
echo "Backup is ready. Now you can run the upgrade script."
#!/usr/bin/env bash
set -euxo pipefail
echo "Uh oh! Something went wrong with the upgrade? No worries, let's get you rolled back."
# First, shut down the app.
echo "First let's shut down the app if it's running."
docker compose -f docker-compose.yml -f docker-optional-app.yaml down
echo "Clearing out the sora_dbdata volume..."
# Then, we need to clear out our sora_dbdata.
docker run --rm \
-v sora_dbdata:/to \
alpine ash -c "rm -r /to/*;"
echo "Copying the backup into the original volume..."
# Copy the backup back into the original volume
docker run --rm \
-v sora_dbdata_10:/from \
-v sora_dbdata:/to \
alpine ash -c "cd /from; cp -av . /to;"
echo -e "\n===================================================================\n"
echo "All done! Your data should be restored and you should be able to restart."
echo "The backup was left JUST IN CASE. If you'd like to clean it up, simply run:"
echo " docker volume rm sora_dbdata_10"
#!/usr/bin/env bash
set -euxo pipefail
echo -e "Welcome! Let's get you migrated from Postgres 10 -> 14.\n"
echo -e "Shutting down the app...\n"
# First, shut down the app if it's running.
docker compose -f docker-compose.yml -f docker-optional-app.yaml down
echo -e "Clearing out sora_dbdata and prepping it for upgrade.\n"
# Clear out sora_dbdata entirely in preparation for the migration
docker run --rm \
-v sora_dbdata:/to \
alpine ash -c "rm -r /to/*;"
echo -e "Upgrading the data...\n"
# Use pg_upgrade docker image to upgrade our data in the backup to sora_dbdata
docker run --rm \
--platform=linux/amd64 \
-v sora_dbdata_10:/var/lib/postgresql/10/data \
-v sora_dbdata:/var/lib/postgresql/14/data \
tianon/postgres-upgrade:10-to-14
# Then we copy a line into our local pg_hba.conf to allow local connections via md5
# (I'm not sure why this isn't configured by default)
echo -e "Adding connectivity config to pg_hba.conf...\n"
docker run --rm \
-v sora_dbdata:/data \
alpine ash -c "
cat >> /data/pg_hba.conf<< EOF
host all all all md5
EOF
"
echo -e "\n===================================================================\n"
echo "All done! You should be set. You can run the app now. Give it a go:"
echo " docker compose -f docker-compose.yml -f docker-optional-app.yaml up"
echo "If everything works as expected, you can delete the backup via:"
echo " docker volume rm sora_dbdata_10"
# Then, restart the app, and you should be good!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment