Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -e
- PG96RepoRpm='https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm'
- error() {
- local -i rc=$1
- shift
- echo "ERROR($rc): $@" >&2
- exit $rc
- }
- find_bin_dir() {
- local p=$(type -P psql) || return 1
- while test -L "$p"; do
- p=$(readlink -e "$p")
- done
- echo $(dirname "$p")
- }
- BaseRepo=/etc/yum.repos.d/CentOS-Base.repo
- OsRelease=/etc/os-release
- PgDataOld= # have to be sure later on
- PgDataDir=/var/lib/pgsql/9.6/data
- PgBinDir=/usr/pgsql-9.6/bin
- PgSetup="$PgBinDir/postgresql96-setup"
- PgUpgrade="$PgBinDir/pg_upgrade"
- PgBinOld=$(find_bin_dir) || error 1 "Cannot detect old bin dir."
- service=/usr/lib/systemd/system/postgresql-9.6.service
- include=/lib/systemd/system/postgresql-9.6.service
- declare -a RpmsToRemove=(`rpm -qa | grep ^postgresql-`)
- exe0="$0"
- [ -f "$exe0" ] || exe0=$(type -P "$exe0")
- # 7601 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
- last_word=''
- while read word; do
- if [ "$last_word" = '-D' ]; then
- PgDataOld="$word"
- break
- fi
- last_word="$word"
- done < <(pgrep -a postgres | grep /usr/bin/postgres | tr -s '[ \t]' '\n')
- [ -z "$PgDataOld" ] && error 1 "Cannot detect old data location."
- [ -d "$PgDataOld/." ] || error 1 "Wrong old data '$PgDataOld' detected."
- echo "* Old data dir: '$PgDataOld'"
- echo "* Old bin dir : '$PgBinOld'"
- echo "* Old RPMs : ${RpmsToRemove[@]}"
- setup_pg_data_dir() {
- local pgdata="$1"
- test -d "$pgdata/data/." || error $? "No valid PG data dir in '$pgdata/data'."
- chown -R postgres.postgres "$pgdata" || error $? "Cannot chown '$pgdata'."
- chmod -R 700 "$pgdata" || error $? "Cannot chmod '$pgdata'."
- chcon -t postgresql_db_t "$pgdata" || error $? "Cannot chcon '$pgdata'."
- chcon -t postgresql_db_t "$pgdata/data" || error $? "Cannot chcon '$pgdata/data'."
- su -c "/usr/pgsql-9.6/bin/initdb -D '$pgdata/data'" postgres
- }
- test -f "$OsRelease" || error $? "No '$OsRelease' file."
- source "$OsRelease"
- { echo "$NAME" | grep -iwq centos; } || error 1 "It's not a CentOS ($NAME)."
- [ "$VERSION_ID" != 7 ] && error 1 "I don't know how to upgrade PgSQL on CentOS '$VERSION_ID'."
- echo "* Ok, the system pretends to be '$PRETTY_NAME'..."
- list_options() {
- grep -o '^[ \t]\+--skip-[^)]\+' < "$exe0"
- }
- while [ -n "$1" ]; do
- case "$1" in
- --skip-base-repo-fix) skip_base_repo_fix=yes; shift;;
- --skip-new-repo-pull) skip_new_repo_pull=yes; shift;;
- --skip-installation) skip_installation=yes; shift;;
- --skip-upgrade) skip_upgrade=yes; shift;;
- --skip-systemd-config) skip_systemd_config=yes; shift;;
- --skip-start-new) skip_start_new=yes; shift;;
- --skip-remove-old) skip_remove_old=yes; shift;;
- run|do) run_it=yes; shift;;
- *) list_options; error 1 "What did you mean for '$1'?";;
- esac
- done
- [ -z "$run_it" ] && error 0 "Run was NOT requested."
- if [ "$skip_base_repo_fix" != yes ]; then
- test -f "$BaseRepo" || error $? "No repo '$BaseRepo'."
- sed -e 's/\[base\]/&\nexclude=postgresql*/' \
- -e 's/\[updates\]/&\nexclude=postgresql*/' \
- -i "$BaseRepo" || error $? "Cannot edit '$BaseRepo'."
- else
- echo "* Ok, I suppose, you've edited the repos ('exclude=postgresql*' in [base] and [updates])..."
- fi
- if yum repolist 2>/dev/null | grep -E '^(epel|pgdg96)/'; then
- echo "* Ok, I see you've installed EPEL and PGDB96..."
- elif [ "$skip_new_repo_pull" != yes ]; then
- yum -y install epel-release || error $? "Cannot install EPEL."
- yum -y install "$PG96RepoRpm" || error $? "Cannot install '$PG96RepoRpm'."
- else
- error 1 "No PGDG96 and/or EPEL repos installed."
- fi
- if rpm -qa | grep ^postgresql96-; then
- echo "* Ok, it looks like you've installed the PG96 here..."
- elif [ "$skip_installation" != yes ]; then
- yum list postgresql* || error $? "I see no PG packages available."
- yum -y install postgresql96 postgresql96-server || error $? "Cannot install GP96."
- test -d "$PgDataDir" || error $? "PG96 still not installed (data)."
- test -d "$PgBinDir" || error $? "PG96 still not installed (bin)."
- test -x "$PgSetup" || error $? "No setup utility '$PgSetup'."
- "$PgSetup" initdb || error $? "Cannot initialize DB."
- else
- error 1 "You have no PG96 packages installed."
- fi
- hack_pg_ctl() {
- mv /usr/bin/pg_ctl{,-orig}
- echo '#!/bin/bash' > /usr/bin/pg_ctl
- echo 'exec "$0"-orig "${@/unix_socket_directory/unix_socket_directories}"' >> /usr/bin/pg_ctl
- chmod +x /usr/bin/pg_ctl
- }
- unhack_pg_ctl() {
- [ -f /usr/bin/pg_ctl-orig ] && mv -f /usr/bin/pg_ctl{-orig,}
- }
- trap unhack_pg_ctl EXIT
- if [ "$skip_upgrade" != yes ]; then
- test -x "$PgUpgrade" || error $? "No upgade tool '$PgUpgrade'."
- echo "*:*:*:postgres:pg" > ~/.pgpass
- echo "* NOTE: user=postgres password=pg !"
- cmd="$PgUpgrade"
- cmd="$cmd --old-datadir \"$PgDataOld\" --new-datadir \"$PgDataDir\""
- cmd="$cmd --old-bindir \"$PgBinOld\" --new-bindir \"$PgBinDir\""
- hack_pg_ctl
- su -c "cd /tmp && $cmd --check" postgres || error $? "Cannot validate '$PgUpgrade'."
- echo "* Ok, looks like we can upgrade..."
- systemctl stop postgresql.service || error $? "Cannot stop 'postgresql.service'."
- echo "* Ok, migrating..."
- echo "* NOTE: user=postgres password=pg !"
- su -c "cd /tmp && $cmd" postgres || error $? "Cannot run upgrade '$PgUpgrade'."
- echo "* Wow! It looks like the data has been migrated!"
- rm ~/.pgpass
- else
- echo "* Ok, skipping the data upgrade..."
- fi
- if [ "$skip_systemd_config" != yes ]; then
- systemctl enable postgresql-9.6.service || error $? "Cannot enable postgresql service."
- test -f "$service" || error $? "No systemd service '$service'."
- test -f "$include" || error $? "No systemd service template '$include'."
- fn=$(basename "$service")
- cp -v "$service" /etc/systemd/system/ \
- || error $? "Cannot cp '$service' into /etc/systemd/system/."
- echo "include $include" > "/etc/systemd/system/$fn" || error $? "Cannot edit (1) '$fn'."
- cat "$service" >> "/etc/systemd/system/$fn" || error $? "Cannot edit (2) '$fn'."
- systemctl daemon-reload || error $? "Cannot reload daemons."
- systemctl start postgresql-9.6.service || error $? "Cannot try postgresql service."
- systemctl stop postgresql-9.6.service || error $? "Cannot stop postgresql service."
- else
- echo "* Ok, I wont configure systemd for you..."
- fi
- if [ "$skip_start_new" != yes ]; then
- systemctl start postgresql-9.6.service || error $? "Cannot start postgresql service."
- systemctl status postgresql-9.6.service
- echo "* Ok, it looks like you've started the PG96 here..."
- fi
- if [ "$skip_remove_old" != yes ]; then
- yum -y remove "${RpmsToRemove[@]}" || error $? "Cannot remove '${RpmsToRemove[@]}'."
- else
- echo "* Ok, I wont remove your old stuff..."
- fi
- echo "* Done (you may copy your configs and 'rm -rf $PgDataOld' now)."
- # EOF #
Advertisement
Add Comment
Please, Sign In to add comment