|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +# postgresql-upgrade - one-hit shell command running initdb && pg_upgrade with |
| 4 | +# preconfigured options to simplify the upgrade process. This script has |
| 5 | +# nothing to do with systemd services, for upgrading systemd postgresql services |
| 6 | +# please use @bindir@/postgresql-setup. |
| 7 | + |
| 8 | +set -e |
| 9 | + |
| 10 | +builddir_source () |
| 11 | +{ |
| 12 | + # To simplify manpage generator. Build-time-only. |
| 13 | + file=$(echo "$1" | sed -e "s|@rawpkgdatadir@|share/postgresql-setup|") |
| 14 | + . "@abs_top_builddir@/$file" |
| 15 | +} |
| 16 | + |
| 17 | +builddir_source "@rawpkgdatadir@/library.sh" |
| 18 | + |
| 19 | +run_cmd () |
| 20 | +{ |
| 21 | + echo >&2 |
| 22 | + info "cmd: $(eval echo "$1")" |
| 23 | + echo >&2 |
| 24 | + eval "$1" |
| 25 | +} |
| 26 | + |
| 27 | +run_cmd_args () |
| 28 | +{ |
| 29 | + local space='' cmd='' |
| 30 | + for arg; do |
| 31 | + cmd+="$space$(printf %q "$arg")" |
| 32 | + space=' ' |
| 33 | + done |
| 34 | + run_cmd "$cmd" |
| 35 | +} |
| 36 | + |
| 37 | +# We upgrade by default from system's default PostgreSQL installation |
| 38 | +option_upgradefrom="@NAME_DEFAULT_PREV_SERVICE@" |
| 39 | + |
| 40 | +# PostgreSQL data directory after upgrade. |
| 41 | +option_datadir= |
| 42 | + |
| 43 | +# For non-inplace upgrades. |
| 44 | +option_datadir_old= |
| 45 | + |
| 46 | +# use pg_upgrade --link? |
| 47 | +option_hardlink=false |
| 48 | + |
| 49 | +# ensure privacy |
| 50 | +umask 0077 |
| 51 | + |
| 52 | +: "${RESTORECON=/sbin/restorecon}" |
| 53 | +test -x "$RESTORECON" || RESTORECON=: |
| 54 | + |
| 55 | +@SCL_SOURCE@ |
| 56 | + |
| 57 | +PGENGINE=@bindir@ |
| 58 | + |
| 59 | +long_opts="\ |
| 60 | +upgrade-ids,\ |
| 61 | +upgrade-from:,\ |
| 62 | +version,usage,help" |
| 63 | + |
| 64 | +USAGE_STRING="Usage: $0 [--upgrade-from=ID] DATADIR |
| 65 | +
|
| 66 | +Wrapper script for pg_upgrade. It has pre-configured pg_upgrade options and |
| 67 | +environment variables. |
| 68 | +
|
| 69 | +Options: |
| 70 | + --upgrade-ids Print list of available IDs of upgrade scenarios to |
| 71 | + standard output. |
| 72 | + --upgrade-from=ID Specify id \"old\" postgresql stack to upgrade |
| 73 | + from. List of available IDs can be listed by |
| 74 | + --upgrade-ids. Default is '$option_upgradefrom'. |
| 75 | +
|
| 76 | +Other options: |
| 77 | + --help show this help |
| 78 | + --version show version of this package |
| 79 | +
|
| 80 | +Environment: |
| 81 | + PGSETUP_INITDB_OPTIONS Options carried by this variable are passed to |
| 82 | + subsequent call of \`initdb\` binary (see man |
| 83 | + initdb(1)). This variable is used also during |
| 84 | + 'upgrade' mode because the new cluster is actually |
| 85 | + re-initialized from the old one. |
| 86 | + PGSETUP_PGUPGRADE_OPTIONS Options in this variable are passed next to the |
| 87 | + subsequent call of \`pg_upgrade\`. For more info |
| 88 | + about possible options please look at man |
| 89 | + pg_upgrade(1)." |
| 90 | + |
| 91 | +print_version() |
| 92 | +{ |
| 93 | + echo "@NAME_BINARYBASE@-upgrade @VERSION@" |
| 94 | + echo "Built against PostgreSQL version @PGVERSION@." |
| 95 | +} |
| 96 | + |
| 97 | +args=$(getopt -o "" -l "$long_opts" -n "@NAME_BINARYBASE@-setup" -- "$@") |
| 98 | +eval set -- "$args" |
| 99 | + |
| 100 | +while true; do |
| 101 | + case "$1" in |
| 102 | + --help|--usage) |
| 103 | + echo "$USAGE_STRING" |
| 104 | + exit 0 |
| 105 | + ;; |
| 106 | + |
| 107 | + --upgrade-from) |
| 108 | + option_upgradefrom="$2" |
| 109 | + shift 2 |
| 110 | + ;; |
| 111 | + |
| 112 | + --upgrade-ids) |
| 113 | + parse_upgrade_setup help |
| 114 | + exit 0 |
| 115 | + ;; |
| 116 | + |
| 117 | + --version) |
| 118 | + print_version |
| 119 | + exit 0 |
| 120 | + ;; |
| 121 | + |
| 122 | + --) |
| 123 | + shift |
| 124 | + break |
| 125 | + ;; |
| 126 | + |
| 127 | + *) |
| 128 | + die "author's fault: option $1 not handled" |
| 129 | + break |
| 130 | + ;; |
| 131 | + esac |
| 132 | +done |
| 133 | + |
| 134 | +test $# -eq 1 || die "exactly one DATADIR option required" |
| 135 | +option_datadir=$(readlink -f "$1") || die "wrong datadir $1" |
| 136 | + |
| 137 | +inplace=false |
| 138 | +test -n "$option_datadir_old" || { |
| 139 | + option_datadir_old=${option_datadir}_old |
| 140 | + option_hardlink=: |
| 141 | + inplace=: |
| 142 | +} |
| 143 | + |
| 144 | +if ! parse_upgrade_setup config "$option_upgradefrom"; then |
| 145 | + die "bad --upgrade-from parameter '$option_upgradefrom'," \ |
| 146 | + "try --upgrade-ids" |
| 147 | +fi |
| 148 | + |
| 149 | +version_file=$option_datadir/PG_VERSION |
| 150 | + |
| 151 | +test -f "$version_file" || die "can't read '$version_file'" |
| 152 | + |
| 153 | +old_data_version="$(cat "$version_file")" |
| 154 | + |
| 155 | +if test "$old_data_version" != "$upgradefrom_major"; then |
| 156 | + error "Cannot upgrade because the database in $option_datadir of" |
| 157 | + error_q "version $old_data_version but it should be $upgradefrom_major" |
| 158 | + exit 1 |
| 159 | +fi |
| 160 | + |
| 161 | +if [ ! -x "$upgradefrom_engine/postgres" ]; then |
| 162 | + error "Please install the $upgradefrom_package package." |
| 163 | + exit 1 |
| 164 | +fi |
| 165 | + |
| 166 | +exit_handler_revert_datadir=false |
| 167 | + |
| 168 | +exit_handler () |
| 169 | +{ |
| 170 | + exit_status=$? |
| 171 | + |
| 172 | + ! $exit_handler_revert_datadir || { |
| 173 | + info "restoring previous datadir" |
| 174 | + rm -rf "$option_datadir" |
| 175 | + mv "$option_datadir_old" "$option_datadir" |
| 176 | + } |
| 177 | + |
| 178 | + exit $exit_status |
| 179 | +} |
| 180 | + |
| 181 | +trap exit_handler EXIT |
| 182 | + |
| 183 | +if $inplace; then |
| 184 | + ! test -e "$option_datadir_old" || die "$option_datadir_old already exists" |
| 185 | + mv "$option_datadir" "$option_datadir_old" |
| 186 | + exit_handler_revert_datadir=: |
| 187 | +fi |
| 188 | + |
| 189 | +test -e "$option_datadir" || mkdir "$option_datadir" |
| 190 | +$RESTORECON "$option_datadir" |
| 191 | + |
| 192 | +initdbcmd="\"\$PGENGINE\"/initdb --pgdata=\"\$option_datadir\" --auth=ident $PGSETUP_INITDB_OPTIONS" |
| 193 | +run_cmd "$initdbcmd" |
| 194 | + |
| 195 | +test -n "$option_workdir" || { |
| 196 | + option_workdir=$(mktemp -d "/tmp/postgresql_upgrade_XXXXXX") |
| 197 | + info "logs are stored in $option_workdir" |
| 198 | +} |
| 199 | +cd "$option_workdir" |
| 200 | + |
| 201 | +set -- "$PGENGINE"/pg_upgrade \ |
| 202 | + --old-bindir="$upgradefrom_engine" \ |
| 203 | + --new-bindir="$PGENGINE" \ |
| 204 | + --old-datadir="$option_datadir_old" \ |
| 205 | + --new-datadir="$option_datadir" |
| 206 | + |
| 207 | +$option_hardlink && set -- "$@" --link |
| 208 | + |
| 209 | +test -n "$PGSETUP_PGUPGRADE_OPTIONS" && eval 'set -- "$@" '"$PGSETUP_PGUPGRADE_OPTIONS" |
| 210 | + |
| 211 | +run_cmd_args "$@" |
| 212 | +exit_handler_revert_datadir=false |
| 213 | +info "old data directory and configuration is in $option_datadir_old" |
0 commit comments