Last active
February 13, 2026 16:11
-
-
Save aaannz/266c2bd175be27d4881161b1de768d3f to your computer and use it in GitHub Desktop.
Uyuni postgresql18 migration script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| DB_UPGRADE_SCRIPT=db-upgrade-script.sh | |
| cat > $DB_UPGRADE_SCRIPT <<EOF | |
| #!/bin/bash | |
| set -e | |
| echo "PostgreSQL version upgrade" | |
| OLD_VERSION=16 | |
| NEW_VERSION=18 | |
| echo "Testing presence of postgresql\$NEW_VERSION..." | |
| test -d /usr/lib/postgresql\$NEW_VERSION/bin | |
| echo "Testing presence of postgresql\$OLD_VERSION..." | |
| test -d /usr/lib/postgresql\$OLD_VERSION/bin | |
| BACKUP_DIR=/var/lib/pgsql/data-backup/backup | |
| restore_database() { | |
| echo "Migration failed. Restoring original database..." | |
| if [ -d "\$BACKUP_DIR" ] && [ "\$(ls -A "\$BACKUP_DIR")" ]; then | |
| if [ -f "\$BACKUP_DIR/postgresql.conf.bak" ]; then | |
| echo "Restoring postgresql.conf..." | |
| mv "\$BACKUP_DIR/postgresql.conf.bak" "\$BACKUP_DIR/postgresql.conf" | |
| fi | |
| if [ -f "\$BACKUP_DIR/pg_hba.conf.bak" ]; then | |
| echo "Restoring pg_hba.conf..." | |
| mv "\$BACKUP_DIR/pg_hba.conf.bak" "\$BACKUP_DIR/pg_hba.conf" | |
| fi | |
| echo "Cleaning up /var/lib/pgsql/data..." | |
| rm -rf /var/lib/pgsql/data/* | |
| echo "Restoring from \$BACKUP_DIR..." | |
| shopt -s dotglob | |
| mv "\$BACKUP_DIR"/* /var/lib/pgsql/data/ | |
| shopt -u dotglob | |
| echo "Database restored." | |
| fi | |
| } | |
| echo "Create a database backup at \$BACKUP_DIR" | |
| test -d "\$BACKUP_DIR" && mv "\$BACKUP_DIR" "\${BACKUP_DIR}\$(date '+%Y%m%d_%H%M%S')" | |
| mkdir -p "\$BACKUP_DIR" | |
| chown postgres:postgres "\$BACKUP_DIR" | |
| chmod 700 "\$BACKUP_DIR" | |
| trap restore_database ERR | |
| shopt -s dotglob | |
| mv /var/lib/pgsql/data/* "\$BACKUP_DIR" | |
| shopt -u dotglob | |
| echo "Create new database directory..." | |
| chown -R postgres:postgres /var/lib/pgsql | |
| if [ -e /etc/pki/tls/private/pg-spacewalk.key ]; then | |
| echo "Enforce key permission" | |
| chown postgres:postgres /etc/pki/tls/private/pg-spacewalk.key | |
| chown postgres:postgres /etc/pki/tls/certs/spacewalk.crt | |
| fi | |
| echo "Initialize new postgresql \$NEW_VERSION database..." | |
| . /etc/sysconfig/postgresql 2>/dev/null # Load locale for SUSE | |
| if [ -z "\$POSTGRES_LANG" ]; then | |
| POSTGRES_LANG="en_US.UTF-8" | |
| [ -n "\$LC_CTYPE" ] && POSTGRES_LANG="\$LC_CTYPE" | |
| fi | |
| 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" | |
| su -s /bin/bash - postgres -c "pg_checksums --disable --pgdata /var/lib/pgsql/data" | |
| echo "Successfully initialized new postgresql \$NEW_VERSION database." | |
| echo "Temporarily disable SSL in the old posgresql configuration" | |
| cp "\${BACKUP_DIR}/postgresql.conf" "\${BACKUP_DIR}/postgresql.conf.bak" | |
| sed 's/^ssl/#ssl/' -i "\${BACKUP_DIR}/postgresql.conf" | |
| echo "Temporarily allow local postgres user connection " | |
| cp "\${BACKUP_DIR}/pg_hba.conf" "\${BACKUP_DIR}/pg_hba.conf.bak" | |
| echo "local all postgres trust" >> "\${BACKUP_DIR}/pg_hba.conf" | |
| cp "/var/lib/pgsql/data/pg_hba.conf" "/var/lib/pgsql/data/pg_hba.conf.bak" | |
| echo "local all postgres trust" >> "/var/lib/pgsql/data/pg_hba.conf" | |
| 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=\"\$BACKUP_DIR\" --new-datadir=/var/lib/pgsql/data" | |
| echo "Enable SSL again" | |
| mv "\${BACKUP_DIR}/postgresql.conf.bak" "\${BACKUP_DIR}/postgresql.conf" | |
| echo "Restore pg_hba.conf and postgresql.conf" | |
| mv "\${BACKUP_DIR}/pg_hba.conf.bak" "\${BACKUP_DIR}/pg_hba.conf" | |
| cp "\${BACKUP_DIR}/pg_hba.conf" "/var/lib/pgsql/data/pg_hba.conf" | |
| cp "\${BACKUP_DIR}/postgresql.conf" "/var/lib/pgsql/data/postgresql.conf" | |
| echo "Reenabling checksums" | |
| su -s /bin/bash - postgres -c "pg_checksums --enable --pgdata /var/lib/pgsql/data" | |
| echo "DONE" | |
| EOF | |
| mgradm stop | |
| podman run --rm -v var-pgsql-backup:/var/lib/pgsql/data-backup -v var-pgsql:/var/lib/pgsql/data -v ./$DB_UPGRADE_SCRIPT:/uyuni-db-migrate.sh:z registry.opensuse.org/systemsmanagement/uyuni/snapshots/2026.01/containerfile/uyuni/server-database-migration:latest bash /uyuni-db-migrate.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment