-
Notifications
You must be signed in to change notification settings - Fork 7
Add script that automatically updates postgres databases to a later version #635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/usr/bin/env sh | ||
|
|
||
| # This script updates all postgres databases that are used by components in this repository. | ||
| # This includes magpie and all WPS birds that use the postgres component. | ||
| # This does not include test component like optional-components/generic_bird and will not update | ||
| # custom components (ones not from this repository). | ||
| # | ||
| # It will update postgres databases to the version specified by the POSTGRES_VERSION_UPDATE | ||
| # environment variable. | ||
| # All of the old database files will be copied to a temporary directory in case you want to inspect | ||
| # them or revert this operation later on. To specify which directory to write these backups to | ||
| # set the BIRDHOUSE_BACKUP_DATA_DIR variable (default: ${TMPDIR:-/tmp}/birdhouse-postgres-migrate-backup/) | ||
| # Note that backups in the form of database dumps will also be written to the named volume or directory | ||
| # specified by the BIRDHOUSE_BACKUP_VOLUME variable. | ||
| # | ||
| # For example, to update the current postgres databases to version 18.1 and write backups to /tmp/test/ | ||
| # | ||
| # $ POSTGRES_VERSION_UPDATE=18.1 BIRDHOUSE_BACKUP_DATA_DIR=/tmp/test/ ./update-postgresh.sh | ||
| # | ||
| # This script will not update other postgres databases that may be present in this deployment but it can | ||
| # be used as a starting point to figure out how to update other databases. For example, it may be possible | ||
| # to update the postgres database used by generic_bird with (untested, please proceed with caution): | ||
| # | ||
| # $ birdhouse backup create --no-restic -a generic_bird | ||
| # $ birdhouse compose down | ||
| # $ ${THIS_DIR}/backup-datavolume.sh birdhouse_postgres_generic_bird "${BIRDHOUSE_BACKUP_DATA_DIR}" | ||
| # $ docker volume rm birdhouse_postgres_generic_bird | ||
| # $ GENERIC_BIRD_POSTGRES_IMAGE=postgres:18.1 birdhouse compose up -d | ||
| # $ birdhouse backup restore --no-restic -a generic_bird | ||
| # | ||
|
|
||
| THIS_FILE="$(readlink -f "$0" || realpath "$0")" | ||
| THIS_DIR="$(dirname "${THIS_FILE}")" | ||
| BIRDHOUSE_EXE="${THIS_DIR}/../../bin/birdhouse" | ||
|
|
||
| # load logging functions | ||
| eval $(${BIRDHOUSE_EXE} configs --print-log-command) | ||
|
|
||
| : ${POSTGRES_VERSION_UPDATE:?"$(log ERROR "POSTGRES_VERSION_UPDATE must be set")"} | ||
mishaschwartz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| BIRDHOUSE_BACKUP_DATA_DIR="${BIRDHOUSE_BACKUP_DATA_DIR:-"${TMPDIR:-/tmp}"/birdhouse-postgres-migrate-backup/}" | ||
| POSTGRES_COMPONENTS="-a magpie -a $(birdhouse -q configs -c 'echo $POSTGRES_DATABASES_TO_CREATE' | sed 's/ / -a /g')" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I want to hack this so it also handle |
||
|
|
||
| log INFO "Migrating postgres databases to version ${POSTGRES_VERSION_UPDATE}" | ||
| log INFO "Postgres databases for the following components will be updated: $(echo "${POSTGRES_COMPONENTS}" | sed 's/[[:space:]]*-a[[:space:]]*/ /g')" | ||
| log INFO "Current databases files will be backed up to the directory: ${BIRDHOUSE_BACKUP_DATA_DIR}" | ||
|
|
||
| log INFO "Starting the birdhouse stack in order to backup existing databases." | ||
| ${BIRDHOUSE_EXE} compose up -d | ||
|
|
||
| log INFO "Backing up postgres databases from the following components: $(echo "${POSTGRES_COMPONENTS}" | sed 's/[[:space:]]*-a[[:space:]]*/ /g')" | ||
| ${BIRDHOUSE_EXE} backup create --no-restic ${POSTGRES_COMPONENTS} | ||
mishaschwartz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| log INFO "Stopping the birdhouse stack in order to update postgres version" | ||
| ${BIRDHOUSE_EXE} compose down | ||
|
|
||
| log INFO "Backing up all postgres data to ${BIRDHOUSE_BACKUP_DATA_DIR}." | ||
| eval "$(${BIRDHOUSE_EXE} -q configs -c 'echo "MAGPIE_PERSIST_DIR=$MAGPIE_PERSIST_DIR; POSTGRES_DATA_DIR=$POSTGRES_DATA_DIR"')" | ||
|
|
||
| mkdir -p ${BIRDHOUSE_BACKUP_DATA_DIR} | ||
| mv "${MAGPIE_PERSIST_DIR}" "${BIRDHOUSE_BACKUP_DATA_DIR}" | ||
| mv "${POSTGRES_DATA_DIR}" "${BIRDHOUSE_BACKUP_DATA_DIR}" | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah crap, |
||
| MAGPIE_POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE} POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE} ${BIRDHOUSE_EXE} compose up -d | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Humm will need |
||
|
|
||
| ${BIRDHOUSE_EXE} backup restore --no-restic ${POSTGRES_COMPONENTS} | ||
|
|
||
| log WARN "Migration is now complete. Please ensure that the data has been upgraded properly. | ||
| If you are satisfied that the databases have been updated properly please add the following to your local environment file: | ||
|
|
||
| export MAGPIE_POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE} | ||
| export POSTGRES_VERSION=${POSTGRES_VERSION_UPDATE} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not put these
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is part of a log message |
||
|
|
||
| If you are not satified that the databases have been updated properly and you wish to revert these changes, you can do so by running: | ||
|
|
||
| ${BIRDHOUSE_EXE} compose down | ||
| rm -r "${MAGPIE_PERSIST_DIR}" | ||
| rm -r "${POSTGRES_DATA_DIR}" | ||
| mv "${BIRDHOUSE_BACKUP_DATA_DIR}/$(basename "${MAGPIE_PERSIST_DIR}")" "${MAGPIE_PERSIST_DIR}" | ||
| mv "${BIRDHOUSE_BACKUP_DATA_DIR}/$(basename "${POSTGRES_DATA_DIR}")" "${POSTGRES_DATA_DIR}" | ||
| ${BIRDHOUSE_EXE} compose up -d | ||
| " | ||
Uh oh!
There was an error while loading. Please reload this page.