Skip to content

Commit

Permalink
Backup the pgsql data after rsyncing them
Browse files Browse the repository at this point in the history
During a migration to kubernetes the server is deployed after the rsync
to prepare the SSL secrets and PVC. This has the nasty effect to
corrupt the synchronized data with a too recent catalog version ID.
This would let the DB migration to fail starting the old postgresql
server.

To workaround this, move the data to the the backup place after the
rsync instead of the begining of the db upgrade.
  • Loading branch information
cbosdo committed Aug 7, 2024
1 parent 916a88e commit a82eea8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 13 additions & 2 deletions mgradm/shared/templates/migrateScriptTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ echo "Extracting time zone..."
$SSH {{ .SourceFqdn }} timedatectl show -p Timezone >/var/lib/uyuni-tools/data
echo "Extracting postgresql versions..."
echo "image_pg_version=$(rpm -qa --qf '%{VERSION}\n' 'name=postgresql[0-8][0-9]-server' | cut -d. -f1 | sort -n | tail -1)" >> /var/lib/uyuni-tools/data
echo "current_pg_version=$(cat /var/lib/pgsql/data/PG_VERSION)" >> /var/lib/uyuni-tools/data
image_pg_version=$(rpm -qa --qf '%{VERSION}\n' 'name=postgresql[0-8][0-9]-server' | cut -d. -f1 | sort -n | tail -1)
current_pg_version=$(cat /var/lib/pgsql/data/PG_VERSION)
echo "image_pg_version=$image_pg_version" >> /var/lib/uyuni-tools/data
echo "current_pg_version=$current_pg_version" >> /var/lib/uyuni-tools/data
grep '^db_user' /etc/rhn/rhn.conf | sed 's/[ \t]//g' >>/var/lib/uyuni-tools/data
grep '^db_password' /etc/rhn/rhn.conf | sed 's/[ \t]//g' >>/var/lib/uyuni-tools/data
Expand All @@ -111,6 +113,15 @@ fi
sed 's/address=[^:]*:/address=*:/' -i /etc/tomcat/conf.d/remote_debug.conf
# Create a backup copy of the data to prepare DB upgrade.
# We need to upgrade the deployment before upgrading the database to get the SSL certificates ready.
# To avoid corrupting the database files, move them to where the upgrade script will expect them.
echo "Posgresql versions: image: $image_pg_version, current: $current_pg_version"
if test "$image_pg_version" != "$current_pg_version"; then
echo "Backing up the database files ..."
mv /var/lib/pgsql/data /var/lib/pgsql/data-pg$current_pg_version
fi
{{ if .Kubernetes }}
echo 'server.no_ssl = 1' >> /etc/rhn/rhn.conf;
echo "Extracting SSL certificate and authority"
Expand Down
7 changes: 5 additions & 2 deletions mgradm/shared/templates/pgsqlVersionUpgradeScriptTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ test -d /usr/lib/postgresql$NEW_VERSION/bin
echo "Testing presence of postgresql$OLD_VERSION..."
test -d /usr/lib/postgresql$OLD_VERSION/bin
echo "Create a backup at /var/lib/pgsql/data-pg$OLD_VERSION..."
mv /var/lib/pgsql/data /var/lib/pgsql/data-pg$OLD_VERSION
# Data have already been backed up at the end of the migration script
# Reset the potentially created new pgsql data
rm -rf /var/lib/pgsql/data
echo "Create new database directory..."
mkdir -p /var/lib/pgsql/data
chown -R postgres:postgres /var/lib/pgsql
Expand All @@ -44,6 +45,8 @@ echo "Running initdb using postgres user"
echo "Any suggested command from the console should be run using postgres user"
su -s /bin/bash - postgres -c "initdb -D /var/lib/pgsql/data --locale=$POSTGRES_LANG"
echo "Successfully initialized new postgresql $NEW_VERSION database."
su -s /bin/bash - postgres -c "pg_upgrade --old-bindir=/usr/lib/postgresql$OLD_VERSION/bin --new-bindir=/usr/lib/postgresql$NEW_VERSION/bin --old-datadir=/var/lib/pgsql/data-pg$OLD_VERSION --new-datadir=/var/lib/pgsql/data $FAST_UPGRADE"
cp /var/lib/pgsql/data-pg$OLD_VERSION/pg_hba.conf /var/lib/pgsql/data
Expand Down

0 comments on commit a82eea8

Please sign in to comment.