3
3
# # This script is run on the old (source) instance, mounting the data disk
4
4
# # of the newly launched instance, disabling extensions containing regtypes,
5
5
# # and running pg_upgrade.
6
- # # It reports the current status of the upgrade process to /tmp/pg-upgrade- status,
6
+ # # It reports the current status of the upgrade process to /root/pg_upgrade/ status,
7
7
# # which can then be subsequently checked through check.sh.
8
8
9
9
# Extensions to disable before running pg_upgrade.
@@ -37,7 +37,11 @@ PGVERSION=$1
37
37
MOUNT_POINT=" /data_migration"
38
38
LOG_FILE=" /var/log/pg-upgrade-initiate.log"
39
39
40
- POST_UPGRADE_EXTENSION_SCRIPT=" /tmp/pg_upgrade/pg_upgrade_extensions.sql"
40
+ create_pgupgrade_files_dir
41
+ PG_UPGRADE_DIR=" /root/pg_upgrade/$( date +%s) "
42
+
43
+ POST_UPGRADE_EXTENSION_SCRIPT=" ${PG_UPGRADE_DIR} /pg_upgrade_extensions.sql"
44
+
41
45
OLD_PGVERSION=$( run_sql -A -t -c " SHOW server_version;" )
42
46
43
47
SERVER_LC_COLLATE=$( run_sql -A -t -c " SHOW lc_collate;" )
@@ -48,7 +52,7 @@ POSTGRES_CONFIG_PATH="/etc/postgresql/postgresql.conf"
48
52
PGBINOLD=" /usr/lib/postgresql/bin"
49
53
PGLIBOLD=" /usr/lib/postgresql/lib"
50
54
51
- PG_UPGRADE_BIN_DIR=" /tmp/pg_upgrade_bin/ $ PGVERSION"
55
+ PG_UPGRADE_BIN_DIR=" ${PG_UPGRADE_DIR} / ${ PGVERSION} "
52
56
NIX_INSTALLER_PATH=" /tmp/persistent/nix-installer"
53
57
NIX_INSTALLER_PACKAGE_PATH=" $NIX_INSTALLER_PATH .tar.gz"
54
58
@@ -131,7 +135,7 @@ cleanup() {
131
135
echo " Unmounting data disk from ${MOUNT_POINT} "
132
136
umount $MOUNT_POINT
133
137
fi
134
- echo " $UPGRADE_STATUS " > /tmp/pg-upgrade-status
138
+ report_upgrade_status " $UPGRADE_STATUS "
135
139
136
140
if [ -z " $IS_CI " ]; then
137
141
exit " $EXIT_CODE "
@@ -227,6 +231,30 @@ function patch_wrappers {
227
231
fi
228
232
}
229
233
234
+ function mount_data_migration_disk {
235
+ if [ -z " $IS_CI " ] && [ -z " $IS_LOCAL_UPGRADE " ]; then
236
+ # awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
237
+ # excluding nvme0 since it is the root disk
238
+ echo " 5. Determining block device to mount"
239
+ BLOCK_DEVICE=$( lsblk -dprno name,size,mountpoint,type | grep " disk" | grep -v " nvme0" | awk ' NF==3 { print $1; }' )
240
+ echo " Block device found: $BLOCK_DEVICE "
241
+
242
+ mkdir -p " $MOUNT_POINT "
243
+ echo " 6. Mounting block device"
244
+
245
+ sleep 5
246
+ e2fsck -pf " $BLOCK_DEVICE "
247
+
248
+ sleep 1
249
+ mount " $BLOCK_DEVICE " " $MOUNT_POINT "
250
+
251
+ sleep 1
252
+ resize2fs " $BLOCK_DEVICE "
253
+ else
254
+ mkdir -p " $MOUNT_POINT "
255
+ fi
256
+ }
257
+
230
258
function initiate_upgrade {
231
259
mkdir -p " $MOUNT_POINT "
232
260
SHARED_PRELOAD_LIBRARIES=$( cat " $POSTGRES_CONFIG_PATH " | grep shared_preload_libraries | sed " s/shared_preload_libraries =\s\{0,1\}'\(.*\)'.*/\1/" )
@@ -266,13 +294,17 @@ function initiate_upgrade {
266
294
mkdir -p " $PG_UPGRADE_BIN_DIR "
267
295
mkdir -p /tmp/persistent/
268
296
echo " a7189a68ed4ea78c1e73991b5f271043636cf074" > " $PG_UPGRADE_BIN_DIR /nix_flake_version"
269
- tar -czf " /tmp/persistent/pg_upgrade_bin.tar.gz" -C " /tmp/pg_upgrade_bin " .
297
+ tar -czf " /tmp/persistent/pg_upgrade_bin.tar.gz" -C " $PG_UPGRADE_DIR " .
270
298
rm -rf /tmp/pg_upgrade_bin/
271
299
fi
272
300
273
301
echo " 1. Extracting pg_upgrade binaries"
274
- mkdir -p " /tmp/pg_upgrade_bin"
275
- tar zxf " /tmp/persistent/pg_upgrade_bin.tar.gz" -C " /tmp/pg_upgrade_bin"
302
+ mkdir -p " $PG_UPGRADE_DIR "
303
+
304
+ # upgrade job outputs a log in the cwd; needs write permissions
305
+ chown postgres:postgres " $PG_UPGRADE_DIR "
306
+
307
+ tar zxf " /tmp/persistent/pg_upgrade_bin.tar.gz" -C " $PG_UPGRADE_DIR "
276
308
277
309
PGSHARENEW=" $PG_UPGRADE_BIN_DIR /share"
278
310
@@ -289,7 +321,10 @@ function initiate_upgrade {
289
321
echo " 1.1.1. Installing Nix using the provided installer"
290
322
tar -xzf " $NIX_INSTALLER_PACKAGE_PATH " -C /tmp/persistent/
291
323
chmod +x " $NIX_INSTALLER_PATH "
292
- " $NIX_INSTALLER_PATH " install --no-confirm
324
+
325
+ " $NIX_INSTALLER_PATH " install --no-confirm \
326
+ --extra-conf " substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com" \
327
+ --extra-conf " trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
293
328
else
294
329
echo " 1.1.1. Installing Nix using the official installer"
295
330
@@ -323,7 +358,7 @@ function initiate_upgrade {
323
358
chown postgres:postgres " /var/lib/postgresql/extension/pgsodium_getkey"
324
359
fi
325
360
326
- chown -R postgres:postgres " /tmp/pg_upgrade_bin/ $PGVERSION "
361
+ chown -R postgres:postgres " $PG_UPGRADE_BIN_DIR "
327
362
328
363
# upgrade job outputs a log in the cwd; needs write permissions
329
364
mkdir -p /tmp/pg_upgrade/
@@ -350,27 +385,7 @@ function initiate_upgrade {
350
385
fi
351
386
locale-gen
352
387
353
- if [ -z " $IS_CI " ] && [ -z " $IS_LOCAL_UPGRADE " ]; then
354
- # awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
355
- # excluding nvme0 since it is the root disk
356
- echo " 5. Determining block device to mount"
357
- BLOCK_DEVICE=$( lsblk -dprno name,size,mountpoint,type | grep " disk" | grep -v " nvme0" | awk ' NF==3 { print $1; }' )
358
- echo " Block device found: $BLOCK_DEVICE "
359
-
360
- mkdir -p " $MOUNT_POINT "
361
- echo " 6. Mounting block device"
362
-
363
- sleep 5
364
- e2fsck -pf " $BLOCK_DEVICE "
365
-
366
- sleep 1
367
- mount " $BLOCK_DEVICE " " $MOUNT_POINT "
368
-
369
- sleep 1
370
- resize2fs " $BLOCK_DEVICE "
371
- else
372
- mkdir -p " $MOUNT_POINT "
373
- fi
388
+ retry 3 mount_data_migration_disk
374
389
375
390
if [ -f " $MOUNT_POINT /pgsodium_root.key" ]; then
376
391
cp " $MOUNT_POINT /pgsodium_root.key" /etc/postgresql-custom/pgsodium_root.key
@@ -481,11 +496,11 @@ EOF
481
496
482
497
trap cleanup ERR
483
498
484
- echo " running" > /tmp/pg-upgrade-status
499
+ report_upgrade_status " running"
485
500
if [ -z " $IS_CI " ] && [ -z " $IS_LOCAL_UPGRADE " ]; then
486
501
initiate_upgrade >> " $LOG_FILE " 2>&1 &
487
502
echo " Upgrade initiate job completed"
488
503
else
489
- rm -f /tmp/pg-upgrade-status
504
+ rm -f " $UPGRADE_STATUS_FILE "
490
505
initiate_upgrade
491
506
fi
0 commit comments