11#! /usr/bin/env bash
22
33# THIS FILE IS FOR MIGRATION OF EXISTING DB TO TIMESCALEDB IMAGE AS TIMESCALE INIT SCRIPTS AREN'T RUN WHEN DB
4- # ALREADY EXISTS; IT ALSO DOES AN AUTOMATIC REINDEX OF THE DB WHEN OR_REINDEX_COUNTER changes TO SIMPLIFY MIGRATIONS
4+ # ALREADY EXISTS; IT ALSO DOES AN AUTOMATIC REINDEX OF THE DB WHEN OR_REINDEX_COUNTER CHANGES TO SIMPLIFY MIGRATIONS
5+ # IT ALSO AUTOMATICALLY HANDLES UPGRADING OF DATABASE AND DURING MAJOR VERSION CHANGES
56
67source /docker-entrypoint.sh
78docker_setup_env
@@ -20,6 +21,168 @@ if [ -n "$DATABASE_ALREADY_EXISTS" ]; then
2021 echo " timescaledb.telemetry_level=off" >> " $PGDATA /postgresql.conf"
2122 fi
2223
24+ # #######################################################################################
25+ # Do upgrade checks - Adapted from https://github.com/pgautoupgrade/docker-pgautoupgrade
26+ # #######################################################################################
27+
28+ # Get the version of the PostgreSQL data files
29+ DB_VERSION=$PG_MAJOR
30+ if [ -s " ${PGDATA} /PG_VERSION" ]; then
31+ DB_VERSION=$( cat " ${PGDATA} /PG_VERSION" )
32+ fi
33+
34+ # Try and upgrade if needed
35+ if [ " $DB_VERSION " != " $PG_MAJOR " ] && [ " $OR_DISABLE_AUTO_UPGRADE " != " true" ]; then
36+
37+ echo " ----------------------------------------------------------------------"
38+ echo " Postgres major version is newer than the existing DB, performing auto upgrade..."
39+ echo " ----------------------------------------------------------------------"
40+
41+ if [ ! -d " /usr/lib/postgresql/${DB_VERSION} " ]; then
42+ echo " Postgres executable version '$DB_VERSION ' is not included in this image so cannot auto upgrade"
43+ exit 1
44+ fi
45+
46+ # Check for presence of old/new directories, indicating a failed previous autoupgrade
47+ echo " ----------------------------------------------------------------------"
48+ echo " Checking for left over artifacts from a failed previous autoupgrade..."
49+ echo " ----------------------------------------------------------------------"
50+ OLD=" ${PGDATA} /old"
51+ NEW=" ${PGDATA} /new"
52+ if [ -d " ${OLD} " ]; then
53+ echo " *****************************************"
54+ echo " Left over OLD directory found. Aborting."
55+ echo " *****************************************"
56+ exit 10
57+ fi
58+ if [ -d " ${NEW} " ]; then
59+ echo " *****************************************"
60+ echo " Left over NEW directory found. Aborting."
61+ echo " *****************************************"
62+ exit 11
63+ fi
64+ echo " -------------------------------------------------------------------------------"
65+ echo " No artifacts found from a failed previous autoupgrade. Continuing the process."
66+ echo " -------------------------------------------------------------------------------"
67+
68+ # Don't automatically abort on non-0 exit status, as that messes with these upcoming mv commands
69+ set +e
70+
71+ # Move the PostgreSQL data files into a subdirectory of the mount point
72+ echo " ---------------------------------------"
73+ echo " Creating OLD temporary directory ${OLD} "
74+ echo " ---------------------------------------"
75+ mkdir " ${OLD} "
76+ if [ ! -d " ${OLD} " ]; then
77+ echo " *********************************************************************"
78+ echo " Creation of temporary directory '${OLD} ' failed. Aborting completely"
79+ echo " *********************************************************************"
80+ exit 7
81+ fi
82+ echo " --------------------------------------------"
83+ echo " Creating OLD temporary directory is complete"
84+ echo " --------------------------------------------"
85+
86+ echo " -------------------------------------------------------"
87+ echo " Moving existing data files into OLD temporary directory"
88+ echo " -------------------------------------------------------"
89+ mv -v " ${PGDATA} " /* " ${OLD} "
90+ echo " -------------------------------------------------------------------"
91+ echo " Moving existing data files into OLD temporary directory is complete"
92+ echo " -------------------------------------------------------------------"
93+
94+ echo " ---------------------------------------"
95+ echo " Creating NEW temporary directory ${NEW} "
96+ echo " ---------------------------------------"
97+ mkdir " ${NEW} "
98+ if [ ! -d " ${NEW} " ]; then
99+ echo " ********************************************************************"
100+ echo " Creation of temporary directory '${NEW} ' failed. Aborting completely"
101+ echo " ********************************************************************"
102+ # With a failure at this point we should be able to move the old data back
103+ # to its original location
104+ mv -v " ${OLD} " /* " ${PGDATA} "
105+ exit 8
106+ fi
107+ echo " --------------------------------------------"
108+ echo " Creating NEW temporary directory is complete"
109+ echo " --------------------------------------------"
110+
111+ echo " -----------------------------------------------------"
112+ echo " Changing permissions of temporary directories to 0700"
113+ echo " -----------------------------------------------------"
114+ chmod 0700 " ${OLD} " " ${NEW} "
115+ echo " ---------------------------------------------------------"
116+ echo " Changing permissions of temporary directories is complete"
117+ echo " ---------------------------------------------------------"
118+
119+ # Return the error handling back to automatically aborting on non-0 exit status
120+ set -e
121+
122+ # Initialise the new PostgreSQL database directory
123+ echo " --------------------------------------------------------------------------------------------------------------------"
124+ echo " Initialising new database directory"
125+ echo " --------------------------------------------------------------------------------------------------------------------"
126+ initdb -D $PGDATA /new
127+ echo " ------------------------------------"
128+ echo " New database directory initialisation complete"
129+ echo " ------------------------------------"
130+
131+ # Change into the PostgreSQL database directory, to avoid a pg_upgrade error about write permissions
132+ cd " ${PGDATA} "
133+
134+ # Run the pg_upgrade command itself
135+ echo " ---------------------------------------"
136+ echo " Running pg_upgrade command, from $( pwd) "
137+ echo " ---------------------------------------"
138+ pg_upgrade --link -b /usr/lib/postgresql/${DB_VERSION} /bin -B /usr/lib/postgresql/${PG_MAJOR} /bin -d $OLD -D $NEW
139+ echo " --------------------------------------"
140+ echo " Running pg_upgrade command is complete"
141+ echo " --------------------------------------"
142+
143+ # Move the new database files into place
144+ echo " -----------------------------------------------------"
145+ echo " Moving the upgraded database files to the active directory"
146+ echo " -----------------------------------------------------"
147+ mv -v " ${NEW} " /* " ${PGDATA} "
148+ echo " -----------------------------------------"
149+ echo " Moving the upgraded database files is complete"
150+ echo " -----------------------------------------"
151+
152+ # Re-use the pg_hba.conf and pg_ident.conf from the old data directory
153+ echo " --------------------------------------------------------------"
154+ echo " Copying the old pg_hba and pg_ident configuration files across"
155+ echo " --------------------------------------------------------------"
156+ cp -f " ${OLD} /pg_hba.conf" " ${OLD} /pg_ident.conf" " ${PGDATA} "
157+ echo " -------------------------------------------------------------------"
158+ echo " Copying the old pg_hba and pg_ident configuration files is complete"
159+ echo " -------------------------------------------------------------------"
160+
161+ # Copy any reindex counter files
162+ echo " --------------------------------------------------------------"
163+ echo " Copying reindex files across"
164+ echo " --------------------------------------------------------------"
165+ cp -f ${OLD} /OR_REINDEX_* ${PGDATA}
166+ echo " -------------------------------------------------------------------"
167+ echo " Copying reindex files is complete"
168+ echo " -------------------------------------------------------------------"
169+
170+ # Remove the left over database files
171+ echo " ---------------------------------"
172+ echo " Removing left over database files"
173+ echo " ---------------------------------"
174+ set +e
175+ rm -rf " ${OLD} " " ${NEW} " delete_old_cluster.sh
176+ set -e
177+ echo " ---------------------------------------------"
178+ echo " Removing left over database files is complete"
179+ echo " ---------------------------------------------"
180+
181+ echo " **********************************************************"
182+ echo " Automatic upgrade process finished with no errors reported"
183+ echo " **********************************************************"
184+ fi
185+
23186 # Do re-indexing check
24187 if [ " $OR_DISABLE_REINDEX " == ' true' ] || [ -z " $OR_REINDEX_COUNTER " ]; then
25188 echo " REINDEX check is disabled"
0 commit comments