Skip to content

Commit 88e3ed4

Browse files
authored
Merge pull request #490 from Lex-2008/ENT-4242-postgres11-migration-improvements
ENT-4242-postgres11-migration-improvements PR
2 parents 7e4c20e + 25851ad commit 88e3ed4

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

packaging/common/cfengine-hub/postinstall.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ generate_new_postgres_conf() {
376376
}
377377

378378
#POSTGRES RELATED
379-
BACKUP_DIR=$PREFIX/backup-before-postgres10-migration
379+
BACKUP_DIR=$PREFIX/backup-before-postgres11-migration
380380
if [ ! -d $PREFIX/state/pg/data ]; then
381381
mkdir -p $PREFIX/state/pg/data
382382
chown -R cfpostgres $PREFIX/state/pg

packaging/common/cfengine-hub/preinstall.sh

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,56 @@ if is_upgrade; then
1515
fi
1616
fi
1717

18-
BACKUP_DIR=$PREFIX/backup-before-postgres10-migration
18+
BACKUP_DIR=$PREFIX/backup-before-postgres11-migration
1919

20-
# If upgrading from a version below 3.12 that has PostgreSQL, and the data dir exists.
21-
if is_upgrade && egrep '^3\.([6-9]|1[01])\.' "$PREFIX/UPGRADED_FROM.txt" >/dev/null && [ -d "$PREFIX/state/pg/data" ]; then
22-
if [ -d "$BACKUP_DIR" ]; then
20+
# If upgrading from a version below 3.13 that has PostgreSQL, and the data dir exists.
21+
if is_upgrade && egrep '^3\.([6-9]|1[012])\.' "$PREFIX/UPGRADED_FROM.txt" >/dev/null && [ -d "$PREFIX/state/pg/data" ]; then
22+
if [ -d "$BACKUP_DIR/data" ]; then
2323
cf_console echo "Old backup in $BACKUP_DIR already exists. Please remove before attempting upgrade."
2424
exit 1
2525
fi
26+
mkdir -p "$BACKUP_DIR"
27+
# Try to check if free space on $BACKUP_DIR drive is not less than $PREFIX/state/pg/data contains
28+
if command -v df >/dev/null && command -v du >/dev/null && command -v awk >/dev/null; then
29+
# We have enough commands to test it.
30+
# Explanation of arguments:
31+
# `df`
32+
# `-P` - use POSIX output format (free space in 4th column),
33+
# `-BM` - print output in Megabytes
34+
# `awk`
35+
# `FNR==2` - take record (line) number two (first line in `df` output is table header)
36+
# `gsub(...)` - remove non-numbers from 4th column
37+
# `print $4` - well, print 4th column
38+
# `du`
39+
# `-s` - print only summary - i.e. only one line with total size of all
40+
# files in direcrory passed as argument - unlike in normal case when it
41+
# prints disk usage by each nested directory, recursively
42+
# `-BM` - print output in Megabytes
43+
#
44+
# Example of `df -PBM .` output on my machine:
45+
# ```
46+
# Filesystem 1048576-blocks Used Available Capacity Mounted on
47+
# /dev/sda1 246599M 210974M 24564M 90% /
48+
# ```
49+
# and awk would extract "24564" number from it
50+
# Example of `du -sBM .` output on my machine:
51+
# ```
52+
# 172831M .
53+
# ```
54+
# and awk would extract "172831" number from it
55+
#
56+
megabytes_free="$(df -PBM $BACKUP_DIR | awk 'FNR==2{gsub(/[^0-9]/,"",$4);print $4}')"
57+
megabytes_need="$(du -sBM $PREFIX/state/pg/data | awk '{gsub(/[^0-9]/,"",$1);print $1}')"
58+
if [ "$megabytes_free" -le "$megabytes_need" ]; then
59+
cf_console echo "Not enough disk space to create DB backup:"
60+
cf_console echo "${megabytes_free}M available in $BACKUP_DIR"
61+
cf_console echo "${megabytes_need}M used by $PREFIX/state/pg/data"
62+
cf_console echo "Please free up some disk space before upgrading or disable upgrade by removing/renaming $PREFIX/state/pg/data prior to upgrade."
63+
exit 1
64+
fi
65+
fi
2666
cf_console echo "Attempting to migrate Mission Portal database. This can break stuff."
67+
cf_console echo "Copy will be created in $BACKUP_DIR dir."
2768
cf_console echo "It can be disabled by shutting down CFEngine and removing/renaming $PREFIX/state/pg/data prior to upgrade."
2869
cf_console echo "Press Ctrl-C in the next 15 seconds if you want to cancel..."
2970
sleep 15
@@ -89,7 +130,28 @@ if is_upgrade && egrep '^3\.([6-9]|1[01])\.' "$PREFIX/UPGRADED_FROM.txt" >/dev/n
89130
# Now that PostgreSQL is shut down, move the old data out of the way.
90131
mkdir -p "$BACKUP_DIR/lib"
91132
mkdir -p "$BACKUP_DIR/share"
92-
mv "$PREFIX/state/pg/data" "$BACKUP_DIR"
133+
134+
# instead of `mv`, do `cp && rm` - in case `cp` operation fails, we won't
135+
# have "some files here, some files there" situation
136+
# First, try copying files creating hardlinks
137+
if cp -al "$PREFIX/state/pg/data" "$BACKUP_DIR"; then
138+
# Copy succeeded - so we can delete old dir.
139+
rm -rf "$PREFIX/state/pg/data"
140+
else
141+
# Copy creating hardlinks failed, so remove partially-copied data and try simple copying
142+
rm -rf "$BACKUP_DIR/data"
143+
if cp -a "$PREFIX/state/pg/data" "$BACKUP_DIR"; then
144+
# Copy succeeded - so we can delete old dir.
145+
rm -rf "$PREFIX/state/pg/data"
146+
else
147+
# Copy failed, so remove partially-copied data and abort
148+
rm -rf "$BACKUP_DIR/data"
149+
cf_console echo "Backup creation failed"
150+
cf_console echo "Please fix it before upgrading or disable upgrade by removing/renaming $PREFIX/state/pg/data prior to upgrade."
151+
exit 1
152+
fi
153+
fi
154+
93155
if ! diff "$BACKUP_DIR/data/postgresql.conf" "$PREFIX/share/postgresql/postgresql.conf.cfengine" > /dev/null; then
94156
# diff exits with 0 if the files are the same
95157
# the postgresql.conf file was modified, we should try to use it after migration

0 commit comments

Comments
 (0)