Guest User

pg-92-96-upgrade-centos7.sh

a guest
Jul 14th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.95 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -e
  4.  
  5. PG96RepoRpm='https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm'
  6.  
  7. error() {
  8.     local -i rc=$1
  9.     shift
  10.     echo "ERROR($rc): $@" >&2
  11.     exit $rc
  12. }
  13.  
  14. find_bin_dir() {
  15.     local p=$(type -P psql) || return 1
  16.     while test -L "$p"; do
  17.         p=$(readlink -e "$p")
  18.     done
  19.     echo $(dirname "$p")
  20. }
  21.  
  22. BaseRepo=/etc/yum.repos.d/CentOS-Base.repo
  23. OsRelease=/etc/os-release
  24. PgDataOld= # have to be sure later on
  25. PgDataDir=/var/lib/pgsql/9.6/data
  26. PgBinDir=/usr/pgsql-9.6/bin
  27. PgSetup="$PgBinDir/postgresql96-setup"
  28. PgUpgrade="$PgBinDir/pg_upgrade"
  29. PgBinOld=$(find_bin_dir) || error 1 "Cannot detect old bin dir."
  30. service=/usr/lib/systemd/system/postgresql-9.6.service
  31. include=/lib/systemd/system/postgresql-9.6.service
  32.  
  33. declare -a RpmsToRemove=(`rpm -qa | grep ^postgresql-`)
  34.  
  35. exe0="$0"
  36. [ -f "$exe0" ] || exe0=$(type -P "$exe0")
  37.  
  38. # 7601 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
  39. last_word=''
  40. while read word; do
  41.     if [ "$last_word" = '-D' ]; then
  42.         PgDataOld="$word"
  43.         break
  44.     fi
  45.     last_word="$word"
  46. done < <(pgrep -a postgres | grep /usr/bin/postgres | tr -s '[ \t]' '\n')
  47.  
  48. [ -z "$PgDataOld" ] && error 1 "Cannot detect old data location."
  49. [ -d "$PgDataOld/." ] || error 1 "Wrong old data '$PgDataOld' detected."
  50. echo "* Old data dir: '$PgDataOld'"
  51. echo "* Old bin dir : '$PgBinOld'"
  52. echo "* Old RPMs    : ${RpmsToRemove[@]}"
  53.  
  54. setup_pg_data_dir() {
  55.     local pgdata="$1"
  56.     test -d "$pgdata/data/." || error $? "No valid PG data dir in '$pgdata/data'."
  57.     chown -R postgres.postgres "$pgdata" || error $? "Cannot chown '$pgdata'."
  58.     chmod -R 700 "$pgdata" || error $? "Cannot chmod '$pgdata'."
  59.     chcon -t postgresql_db_t "$pgdata" || error $? "Cannot chcon '$pgdata'."
  60.     chcon -t postgresql_db_t "$pgdata/data" || error $? "Cannot chcon '$pgdata/data'."
  61.     su -c "/usr/pgsql-9.6/bin/initdb -D '$pgdata/data'" postgres
  62. }
  63.  
  64. test -f "$OsRelease" || error $? "No '$OsRelease' file."
  65. source "$OsRelease"
  66.  
  67. { echo "$NAME" | grep -iwq centos; } || error 1 "It's not a CentOS ($NAME)."
  68. [ "$VERSION_ID" != 7 ] && error 1 "I don't know how to upgrade PgSQL on CentOS '$VERSION_ID'."
  69.  
  70. echo "* Ok, the system pretends to be '$PRETTY_NAME'..."
  71.  
  72. list_options() {
  73.     grep -o '^[ \t]\+--skip-[^)]\+' < "$exe0"
  74. }
  75.  
  76. while [ -n "$1" ]; do
  77.     case "$1" in
  78.     --skip-base-repo-fix)   skip_base_repo_fix=yes; shift;;
  79.     --skip-new-repo-pull)   skip_new_repo_pull=yes; shift;;
  80.     --skip-installation)    skip_installation=yes; shift;;
  81.     --skip-upgrade)         skip_upgrade=yes; shift;;
  82.     --skip-systemd-config)  skip_systemd_config=yes; shift;;
  83.     --skip-start-new)       skip_start_new=yes; shift;;
  84.     --skip-remove-old)      skip_remove_old=yes; shift;;
  85.     run|do)                 run_it=yes; shift;;
  86.     *) list_options; error 1 "What did you mean for '$1'?";;
  87.     esac
  88. done
  89.  
  90. [ -z "$run_it" ] && error 0 "Run was NOT requested."
  91.  
  92. if [ "$skip_base_repo_fix" != yes ]; then
  93.     test -f "$BaseRepo" || error $? "No repo '$BaseRepo'."
  94.     sed -e 's/\[base\]/&\nexclude=postgresql*/' \
  95.         -e 's/\[updates\]/&\nexclude=postgresql*/' \
  96.         -i "$BaseRepo" || error $? "Cannot edit '$BaseRepo'."
  97. else
  98.     echo "* Ok, I suppose, you've edited the repos ('exclude=postgresql*' in [base] and [updates])..."
  99. fi
  100.  
  101. if yum repolist 2>/dev/null | grep -E '^(epel|pgdg96)/'; then
  102.     echo "* Ok, I see you've installed EPEL and PGDB96..."
  103. elif [ "$skip_new_repo_pull" != yes ]; then
  104.     yum -y install epel-release  || error $? "Cannot install EPEL."
  105.     yum -y install "$PG96RepoRpm" || error $? "Cannot install '$PG96RepoRpm'."
  106. else
  107.     error 1 "No PGDG96 and/or EPEL repos installed."
  108. fi
  109.  
  110. if rpm -qa | grep ^postgresql96-; then
  111.     echo "* Ok, it looks like you've installed the PG96 here..."
  112. elif [ "$skip_installation" != yes ]; then
  113.     yum list postgresql* || error $? "I see no PG packages available."
  114.     yum -y install postgresql96 postgresql96-server || error $? "Cannot install GP96."
  115.     test -d "$PgDataDir" || error $? "PG96 still not installed (data)."
  116.     test -d "$PgBinDir" || error $? "PG96 still not installed (bin)."
  117.     test -x "$PgSetup" || error $? "No setup utility '$PgSetup'."
  118.     "$PgSetup" initdb || error $? "Cannot initialize DB."
  119. else
  120.     error 1 "You have no PG96 packages installed."
  121. fi
  122.  
  123. hack_pg_ctl() {
  124.     mv /usr/bin/pg_ctl{,-orig}
  125.     echo '#!/bin/bash' > /usr/bin/pg_ctl
  126.     echo 'exec "$0"-orig "${@/unix_socket_directory/unix_socket_directories}"' >> /usr/bin/pg_ctl
  127.     chmod +x /usr/bin/pg_ctl
  128. }
  129.  
  130. unhack_pg_ctl() {
  131.     [ -f /usr/bin/pg_ctl-orig ] && mv -f /usr/bin/pg_ctl{-orig,}
  132. }
  133. trap unhack_pg_ctl EXIT
  134.  
  135. if [ "$skip_upgrade" != yes ]; then
  136.     test -x "$PgUpgrade" || error $? "No upgade tool '$PgUpgrade'."
  137.     echo "*:*:*:postgres:pg" > ~/.pgpass
  138.     echo "* NOTE: user=postgres password=pg !"
  139.     cmd="$PgUpgrade"
  140.     cmd="$cmd --old-datadir \"$PgDataOld\" --new-datadir \"$PgDataDir\""
  141.     cmd="$cmd --old-bindir \"$PgBinOld\" --new-bindir \"$PgBinDir\""
  142.  
  143.     hack_pg_ctl
  144.  
  145.     su -c "cd /tmp && $cmd --check" postgres || error $? "Cannot validate '$PgUpgrade'."
  146.     echo "* Ok, looks like we can upgrade..."
  147.     systemctl stop postgresql.service || error $? "Cannot stop 'postgresql.service'."
  148.     echo "* Ok, migrating..."
  149.     echo "* NOTE: user=postgres password=pg !"
  150.     su -c "cd /tmp && $cmd" postgres || error $? "Cannot run upgrade '$PgUpgrade'."
  151.     echo "* Wow! It looks like the data has been migrated!"
  152.     rm ~/.pgpass
  153. else
  154.     echo "* Ok, skipping the data upgrade..."
  155. fi
  156.  
  157. if [ "$skip_systemd_config" != yes ]; then
  158.     systemctl enable postgresql-9.6.service || error $? "Cannot enable postgresql service."
  159.     test -f "$service" || error $? "No systemd service '$service'."
  160.     test -f "$include" || error $? "No systemd service template '$include'."
  161.     fn=$(basename "$service")
  162.     cp -v "$service" /etc/systemd/system/ \
  163.         || error $? "Cannot cp '$service' into /etc/systemd/system/."
  164.     echo "include $include" > "/etc/systemd/system/$fn" || error $? "Cannot edit (1) '$fn'."
  165.     cat "$service" >> "/etc/systemd/system/$fn" || error $? "Cannot edit (2) '$fn'."
  166.     systemctl daemon-reload || error $? "Cannot reload daemons."
  167.     systemctl start postgresql-9.6.service || error $? "Cannot try postgresql service."
  168.     systemctl stop postgresql-9.6.service || error $? "Cannot stop postgresql service."
  169. else
  170.     echo "* Ok, I wont configure systemd for you..."
  171. fi
  172.  
  173. if [ "$skip_start_new" != yes ]; then
  174.     systemctl start postgresql-9.6.service || error $? "Cannot start postgresql service."
  175.     systemctl status postgresql-9.6.service
  176.     echo "* Ok, it looks like you've started the PG96 here..."
  177. fi
  178.  
  179. if [ "$skip_remove_old" != yes ]; then
  180.     yum -y remove "${RpmsToRemove[@]}" || error $? "Cannot remove '${RpmsToRemove[@]}'."
  181. else
  182.     echo "* Ok, I wont remove your old stuff..."
  183. fi
  184.  
  185. echo "* Done (you may copy your configs and 'rm -rf $PgDataOld' now)."
  186. # EOF #
Advertisement
Add Comment
Please, Sign In to add comment