Skip to content

Commit 132cd45

Browse files
praiskuphhorak
authored andcommitted
Add postgresql-upgrade script
This should be a self-standing script which doesn't need a service file for successful data directory upgrade. It has similar functionality as 'postgresql-setup --upgrade' script -- but without the need to run it as root/postgres user, and without the need to have '*.service' file pre-configured. It's as easy as run 'postgresql-upgrdae <DATADIR>'. * bin/Makefile.inc: Administrivia. * bin/postgresql-upgrade.in: The new template for the new script. * doc/Makefile.inc: Automatically build the manual page for the new script.
1 parent b6c3f2d commit 132cd45

File tree

5 files changed

+238
-6
lines changed

5 files changed

+238
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ postgresql*-check-db-dir
2323
postgresql*-ctl
2424
postgresql*-new-systemd-unit
2525
postgresql*.service
26-
/postgresql*-setup
26+
postgresql*-setup
27+
postgresql*-upgrade
2728
postgresql*-setup.1
2829
README.rpm-dist*
2930
testsuite

NEWS

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
New in 8.5 version:
44

5-
*
5+
* Added 'postgresql-upgrade <DATADIR>' command. This is similar to
6+
'postgresql-setup --upgrade', but is useful to upgrade DATADIR for
7+
PostgreSQL servers which were run a non-standard way (under non-standard
8+
user, or without systemd service). Typical use-case can be to migrate
9+
database for KDE's Akonadi server; it is running it's own PostgreSQL server
10+
with datadir in user's ~/.local/ directory (under user's UID process).
611

712
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
813

bin/Makefile.inc

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
setup = %D%/$(NAME_BINARYBASE)-setup
22
setup_in = %D%/postgresql-setup.in
33

4-
bin_SCRIPTS = $(setup)
4+
postgresql_upgrade = %D%/$(NAME_BINARYBASE)-upgrade
5+
postgresql_upgrade_in = %D%/$(NAME_BINARYBASE)-upgrade.in
6+
7+
bin_SCRIPTS = $(setup) $(postgresql_upgrade)
58

69
$(setup): $(setup_in) $(text_tpl_deps)
710
$(text_tpl_gen_script)
811

9-
EXTRA_DIST += $(setup_in)
12+
$(postgresql_upgrade): $(postgresql_upgrade_in) $(text_tpl_deps)
13+
$(text_tpl_gen_script)
14+
15+
EXTRA_DIST += $(setup_in) $(postgresql_upgrade_in)

bin/postgresql-upgrade.in

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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"

doc/Makefile.inc

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ HELP2MAN_RUN = \
2323

2424
setup_man = %D%/$(NAME_BINARYBASE)-setup.1
2525

26+
upgrade_man = %D%/$(NAME_BINARYBASE)-upgrade.1
27+
2628
new_unit_man = %D%/$(NAME_BINARYBASE)-new-systemd-unit.1
2729

28-
dist_man_MANS = $(setup_man)
30+
dist_man_MANS = $(setup_man) $(upgrade_man)
2931

3032
if ! WANT_SYSVINIT
3133
dist_man_MANS += $(new_unit_man)
@@ -36,10 +38,15 @@ $(setup_man): $(builddir)/bin/$(NAME_BINARYBASE)-setup $(lib)
3638
export input=$(builddir)/bin/$(NAME_BINARYBASE)-setup ; \
3739
$(HELP2MAN_RUN)
3840

41+
$(upgrade_man): $(builddir)/bin/$(NAME_BINARYBASE)-upgrade $(lib)
42+
$(AM_V_GEN)odir=`dirname "$@"` ; mkdir -p "$$odir" ; \
43+
export input=$(builddir)/bin/$(NAME_BINARYBASE)-upgrade ; \
44+
$(HELP2MAN_RUN)
45+
3946
$(new_unit_man): sbin/$(NAME_BINARYBASE)-new-systemd-unit $(lib)
4047
$(AM_V_GEN)odir=`dirname "$@"`; mkdir -p "$$odir" ; \
4148
export PGSETUP_TEST=: ; \
4249
export input=$(builddir)/sbin/$(NAME_BINARYBASE)-new-systemd-unit ; \
4350
$(HELP2MAN_RUN)
4451

45-
DISTCLEANFILES += $(setup_man) $(new_unit_man)
52+
DISTCLEANFILES += $(dist_man_MANS) $(new_unit_man)

0 commit comments

Comments
 (0)