upgrade-postgres.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. OLD_VERSION="9.6"
  3. NEW_VERSION="14"
  4. PG_IMAGE="ghcr.io/getsentry/image-mirror-library-postgres:${NEW_VERSION}-alpine"
  5. PROJECT=${PROJECT:-sentry}
  6. VOLUME_NAME="${PROJECT}_postgres"
  7. TMP_VOLUME_NAME="${VOLUME_NAME}_${NEW_VERSION}"
  8. TMP_CONTAINER="${PROJECT}_pg_migration"
  9. echo "Stop the container"
  10. docker stop sentry_postgres
  11. echo "Check existence of a volume"
  12. if [[ -z "$(docker volume ls -q --filter name="^${VOLUME_NAME}$")" ]]
  13. then
  14. echo "PostgreSQL volume with name ${VOLUME_NAME} does not exist. Nothing to upgrade."
  15. exit 0
  16. fi
  17. echo "Get the current PostgreSQL version"
  18. CURRENT_VERSION=$(docker run --rm -v ${VOLUME_NAME}:/db busybox cat /db/PG_VERSION 2>/dev/null)
  19. echo "Current PostgreSQL version is ${CURRENT_VERSION}"
  20. if [[ "${CURRENT_VERSION}" != "${OLD_VERSION}" ]]
  21. then
  22. echo "Expected current PostgreSQL version is ${OLD_VERSION}."
  23. exit 1
  24. fi
  25. docker volume rm "${TMP_VOLUME_NAME}" || true
  26. docker run --rm \
  27. -v ${VOLUME_NAME}:/var/lib/postgresql/${OLD_VERSION}/data \
  28. -v ${TMP_VOLUME_NAME}:/var/lib/postgresql/${NEW_VERSION}/data \
  29. tianon/postgres-upgrade:${OLD_VERSION}-to-${NEW_VERSION}
  30. # Get rid of the old volume as we'll rename the new one to that
  31. docker volume rm ${VOLUME_NAME}
  32. docker volume create --name ${VOLUME_NAME}
  33. # There's no rename volume in Docker so copy the contents from old to new name
  34. # Also append the `host all all all trust`
  35. docker run --rm -v ${TMP_VOLUME_NAME}:/from -v ${VOLUME_NAME}:/to alpine ash -c \
  36. "cd /from ; cp -av . /to ; echo 'host all all all trust' >> /to/pg_hba.conf"
  37. # Finally, remove the new old volume as we are all in sentry-postgres now
  38. docker volume rm ${TMP_VOLUME_NAME}
  39. echo "Due to glibc change re-indexing"
  40. echo "Starting up new PostgreSQL version"
  41. PG_VERSION=${NEW_VERSION} ${PROJECT} devservices up postgres
  42. # Wait for postgres
  43. RETRIES=5
  44. until docker exec ${VOLUME_NAME} psql -U postgres -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do
  45. echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..."
  46. sleep 1
  47. done
  48. # VOLUME_NAME is the same as container name
  49. # Reindex all databases and their system catalogs which are not templates
  50. DBS=$(docker exec ${VOLUME_NAME} psql -qAt -U postgres -c "select datname from pg_database where datistemplate = false;")
  51. for db in ${DBS}
  52. do
  53. echo "Re-indexing database: ${db}"
  54. docker exec ${VOLUME_NAME} psql -qAt -U postgres -d ${db} -c "reindex system ${db}"
  55. docker exec ${VOLUME_NAME} psql -qAt -U postgres -d ${db} -c "reindex database ${db};"
  56. done
  57. _PROFILE_LINE="export PG_VERSION=${NEW_VERSION}"
  58. echo
  59. echo "To configure your environment to use PostgreSQL with ${PROJECT}, PG_VERSION variable must be set."
  60. echo "Save the following to your shell rc file:"
  61. echo
  62. echo "${_PROFILE_LINE}"
  63. echo