From 0089acd8e1c1f06a79b4bf5f7e27f42278e6263c Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Fri, 22 Oct 2021 14:09:18 +0900 Subject: [PATCH] Cleaning up new cluster if migration fail --- Postgres/13.4/migrate-docker-entrypoint.sh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Postgres/13.4/migrate-docker-entrypoint.sh b/Postgres/13.4/migrate-docker-entrypoint.sh index 5a2dbf7..3cb484c 100755 --- a/Postgres/13.4/migrate-docker-entrypoint.sh +++ b/Postgres/13.4/migrate-docker-entrypoint.sh @@ -57,15 +57,15 @@ if [[ "$CURRENT_PGVERSION" != "$EXPECTED_PGVERSION" ]] && \ -not -wholename "$PGDATAOLD" \ -not -wholename "$PGDATANEW" \ -exec mv {} "$PGDATAOLD/" \; - - chmod 700 "$PGDATAOLD" "$PGDATANEW" - chown postgres . - chown -R postgres "$PGDATAOLD" "$PGDATANEW" "$PGDATABASE" cp "$PGDATAOLD/PG_VERSION" "$PGDATABASE/PG_MIGRATING_FROM_VERSION" else echo "Previous migration failed, trying one more time..." fi + chmod 700 "$PGDATAOLD" "$PGDATANEW" + chown postgres . + chown -R postgres "$PGDATAOLD" "$PGDATANEW" "$PGDATABASE" + [[ "$POSTGRES_USER" ]] && export PGUSER="$POSTGRES_USER" [[ "$POSTGRES_PASSWORD" ]] && export PGPASSWORD="$POSTGRES_PASSWORD" if [ ! -s "$PGDATANEW/PG_VERSION" ]; then @@ -77,8 +77,19 @@ if [[ "$CURRENT_PGVERSION" != "$EXPECTED_PGVERSION" ]] && \ fi if ! gosu postgres pg_upgrade; then - echo "Failed to upgrade the server" + echo "Failed to upgrade the server, showing pg_upgrade_server.log" cat pg_upgrade_server.log + cp *.log "$PGDATABASE/" + if [ -f "$PGDATAOLD/PG_VERSION" ] && [ -f "$PGDATANEW/PG_VERSION" ]; then + echo "Cleaning up the new cluster" + rm -r $PGDATANEW/* + fi + + # If the db hasn't started cleanly, this may solve it. + echo "Attempt to start/stop the old cluster if it hasn't exit cleanly" + gosu postgres $PGBINOLD/pg_ctl start -w -D $PGDATAOLD + gosu postgres $PGBINOLD/pg_ctl stop -w -D $PGDATAOLD + exit 1 fi