1+ #! /usr/bin/env bash
2+ set -Eeo pipefail
3+ shopt -s extglob
4+
5+ CURRENT_PGVERSION=" "
6+ EXPECTED_PGVERSION=" $PG_MAJOR "
7+ if [[ -f " /var/lib/postgresql/data/PG_VERSION" ]]; then
8+ CURRENT_PGVERSION=" $( cat /var/lib/postgresql/data/PG_VERSION) "
9+ elif [[ -f " /var/lib/postgresql/data/PG_MIGRATING_FROM_VERSION" ]]; then
10+ echo " Previous migration failed"
11+ CURRENT_PGVERSION=" $( cat /var/lib/postgresql/data/PG_MIGRATING_FROM_VERSION) "
12+ fi
13+
14+ if [[ " $CURRENT_PGVERSION " != " $EXPECTED_PGVERSION " ]] && \
15+ [[ " $CURRENT_PGVERSION " != " " ]]; then
16+
17+ if ! [ -f " /usr/lib/postgresql/$CURRENT_PGVERSION /bin/pg_upgrade" ]; then
18+ echo " Trying to install Postgres $CURRENT_PGVERSION migration tools"
19+ sed -i " s/$/ $CURRENT_PGVERSION /" /etc/apt/sources.list.d/pgdg.list
20+ if ! apt-get update; then
21+ echo " apt-get update failed. Are you using raspberry pi 4? If yes, please follow https://blog.samcater.com/fix-workaround-rpi4-docker-libseccomp2-docker-20/"
22+ exit 1
23+ fi
24+ if ! apt-get install -y --no-install-recommends \
25+ postgresql-$CURRENT_PGVERSION \
26+ postgresql-contrib-$CURRENT_PGVERSION ; then
27+ # On arm32, postgres doesn't ship those packages, so we download
28+ # the binaries from an archive we built from the postgres 9.6.20 image's binaries
29+ FALLBACK=" https://aois.blob.core.windows.net/public/$CURRENT_PGVERSION -$( uname -m) .tar.gz"
30+ FALLBACK_SHARE=" https://aois.blob.core.windows.net/public/share-$CURRENT_PGVERSION -$( uname -m) .tar.gz"
31+ echo " Failure to install postgresql-$CURRENT_PGVERSION and postgresql-contrib-$CURRENT_PGVERSION trying fallback $FALLBACK "
32+ apt-get install -y wget
33+ pushd . > /dev/null
34+ cd /usr/lib/postgresql
35+ wget $FALLBACK
36+ tar -xvf * .tar.gz
37+ rm -f * .tar.gz
38+ cd /usr/share/postgresql
39+ wget $FALLBACK_SHARE
40+ tar -xvf * .tar.gz
41+ rm -f * .tar.gz
42+ popd > /dev/null
43+ echo " Successfully installed PG utilities via the fallback"
44+ fi
45+ else
46+ echo " Migration binaries already present on the image"
47+ fi
48+
49+ export PGBINOLD=" /usr/lib/postgresql/$CURRENT_PGVERSION /bin"
50+ export PGDATABASE=" /var/lib/postgresql/data"
51+ export PGDATAOLD=" /var/lib/postgresql/data/$CURRENT_PGVERSION "
52+ export PGDATANEW=" /var/lib/postgresql/data/$EXPECTED_PGVERSION "
53+
54+ if ! [ -f " $PGDATABASE /PG_MIGRATING_FROM_VERSION" ]; then
55+ mkdir -p " $PGDATANEW " " $PGDATAOLD "
56+ find " $PGDATABASE " -maxdepth 1 -mindepth 1 \
57+ -not -wholename " $PGDATAOLD " \
58+ -not -wholename " $PGDATANEW " \
59+ -exec mv {} " $PGDATAOLD /" \;
60+ cp " $PGDATAOLD /PG_VERSION" " $PGDATABASE /PG_MIGRATING_FROM_VERSION"
61+ else
62+ echo " Previous migration failed, trying one more time..."
63+ fi
64+
65+ chmod 700 " $PGDATAOLD " " $PGDATANEW "
66+ chown postgres .
67+ chown -R postgres " $PGDATAOLD " " $PGDATANEW " " $PGDATABASE "
68+
69+ [[ " $POSTGRES_USER " ]] && export PGUSER=" $POSTGRES_USER "
70+ [[ " $POSTGRES_PASSWORD " ]] && export PGPASSWORD=" $POSTGRES_PASSWORD "
71+ if [ ! -s " $PGDATANEW /PG_VERSION" ]; then
72+ if [[ " $PGUSER " ]]; then
73+ PGDATA=" $PGDATANEW " eval " gosu postgres initdb -U$PGUSER $POSTGRES_INITDB_ARGS "
74+ else
75+ PGDATA=" $PGDATANEW " eval " gosu postgres initdb $POSTGRES_INITDB_ARGS "
76+ fi
77+ fi
78+
79+ if ! gosu postgres pg_upgrade; then
80+ echo " Failed to upgrade the server, showing pg_upgrade_server.log"
81+ cat pg_upgrade_server.log
82+ cp * .log " $PGDATABASE /"
83+ if [ -f " $PGDATAOLD /PG_VERSION" ] && [ -f " $PGDATANEW /PG_VERSION" ]; then
84+ echo " Cleaning up the new cluster"
85+ rm -r $PGDATANEW /*
86+ fi
87+
88+ # If the db hasn't started cleanly, this may solve it.
89+ echo " Attempt to start/stop the old cluster if it hasn't exit cleanly"
90+ gosu postgres $PGBINOLD /pg_ctl start -w -D $PGDATAOLD
91+ gosu postgres $PGBINOLD /pg_ctl stop -w -D $PGDATAOLD
92+
93+ exit 1
94+ fi
95+
96+ rm " $PGDATABASE /PG_MIGRATING_FROM_VERSION"
97+ rm $PGDATANEW /* .conf
98+ mv $PGDATANEW /* " $PGDATABASE "
99+ mv $PGDATAOLD /* .conf " $PGDATABASE "
100+ rm -r " $PGDATANEW "
101+ ./delete_old_cluster.sh
102+ rm ./analyze_new_cluster.sh
103+ unset PGUSER
104+ unset PGPASSWORD
105+ unset PGDATABASE
106+ fi
107+
108+ if [ -f " docker-entrypoint.sh" ]; then
109+ exec ./docker-entrypoint.sh " $@ "
110+ else
111+ exec docker-entrypoint.sh " $@ "
112+ fi
0 commit comments