1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
#!/bin/sh
#include <genscript.warning>
set -e
#
# Script postgresql-dump
#
# This script is used to dump and destroy a database when PostgreSQL is updated
# to an incompatible database format.
syntax () {
help
exit 1
}
help () {
echo Usage:
echo "`basename $0` [options] -t target"
echo "`basename $0` -h"
echo " where target is a file or tape device."
echo " -b blksz Use dd with a blocksize of <blksz> to write the dump"
echo " -c list the ASCII dump to screen for the user to check"
echo " -d destroy the old database framework"
echo " -f fix certain errors in older dumped data"
echo " -F force a reload even if versions are up-to-date"
echo " -h print this help message"
echo " -i create a new database framework with initdb"
echo " -l load the ASCII dump into the new database"
echo " -p dir preserve the old database framework under directory <dir>"
echo " -t target create the ASCII dump on file or device <target>"
echo " -u dump only the database structure; use pg_upgrade to fill this"
echo " from the existing database"
echo " -v verbose output"
echo " -X do not dump the old database; cancels -c"
echo " -x do not dump the old database; forces -c"
}
add_option () {
if [ -n "$OPTIONS" ]
then
OPTIONS=$OPTIONS\ $1
else
OPTIONS=$1
fi
shift
while [ -n "$1" ]
do
OPTIONS=$OPTIONS\ $1
shift
done
}
checkpath () {
dp=`dirname $1`
dir=`(cd $dp; pwd)` # cope with $PGDATA/../ and so on
while [ -n "`echo $dir | awk -F/ '{print $2}'`" ]
do
if [ $dir = $PGDATA ]
then
echo $dp/$1 is in or under PGDATA \($PGDATA\). >&2
echo It will be destroyed when PGDATA is cleared. Specify a different path.>&2
exit 11
fi
dir=`dirname $dir`
done
}
SHELL=/bin/sh
current=%PG_VERSION% # Current PostgreSQL database version
check=
destroy=
create=
load=
sed_command=cat
preserve=
upgrade=
verbose=
exclude_dump=
no_check=
force_reload=
blksz=
dir=`pwd`
while [ -n "`echo $dir | awk -F/ '{print $2}'`" ]
do
if [ $dir = $PGDATA ]
then
echo The current directory is in or under PGDATA \($PGDATA\). >&2
echo Please move to a different directory.>&2
exit 11
fi
dir=`dirname $dir`
done
# Read command line
while [ $OPTIND -le $# ]
do
getopts b:cdFfilp:t:uvXx arg
case "$arg"
in
b)
blksz=$OPTARG;;
c)
check=TRUE;;
d)
destroy=TRUE;;
F)
force_reload=TRUE;;
f)
sed_command="sed -e '/^CREATE RULE /s/current\./old./g' -e \"/create database /s/with encoding=''/with encoding='SQL_ASCII'/\"" ;;
h)
help
exit 0;;
i)
create=TRUE;;
l)
load=TRUE;;
p)
preserve=$OPTARG;;
t)
target=$OPTARG;;
u)
upgrade=upgrade;;
v)
verbose=TRUE;;
X)
no_check=TRUE
exclude_dump=TRUE;;
x)
exclude_dump=TRUE
check=TRUE;;
*)
syntax;;
esac
done
if [ -z "${exclude_dump}" ]
then
check=TRUE
fi
if [ -n "${no_check}" ]
then
check=
fi
if [ -z "${destroy}" ]
then
preserve=
fi
if [ -z "${target}" ]
then
echo No target file or device specified >&2
syntax
else
checkpath ${target}
fi
if [ -f /etc/postgresql/postmaster.init ]
then
. /etc/postgresql/postmaster.init
fi
PGDATA=${POSTGRES_DATA:=/var/lib/postgres/data}
PGLIB=/usr/lib/postgresql
PATH=/usr/lib/postgresql/bin:/bin:/usr/bin
ENCODINGFILE=/usr/lib/postgresql/dumpall/default_encoding
export PGLIB PGDATA LANG PATH
# Since this script has to dump the entire database, it must be run by the
# PostgreSQL superuser
if [ `id | cut -d\( -f2 | cut -d\) -f1` != postgres ]
then
echo $0: this script must be run by the user postgres. >&2
exit 2
fi
# Make sure either the ${PGDATA} directory or the dump target exists
dumped=
if [ ! -d ${PGDATA:=/var/lib/postgres/data}/base ]
then
if [ ! -f ${target} ]
then
echo The PostgreSQL data directory, ${PGDATA}, and the dump file are missing. >&2
echo >&2
echo Please check the settings in /etc/postgresql/postmaster.init >&2
exit 3
else
dumped=TRUE
destroy=""
preserve=""
fi
fi
if [ -n "${preserve}" ]
then
checkpath ${preserve}
fi
if [ -n "$blksz" ]
then
writeout="dd of=$target bs=$blksiz"
readin="dd if=$target bs=$blksiz"
header="$readin count=1 | head -1"
else
writeout="cat >$target"
readin="cat $target"
header="head -1 $target"
fi
load_only=
if [ -z "${dumped}" ]
then
# Find out what the current database format version is
installed=`cat ${PGDATA}/PG_VERSION`
must_dump=
if [ -z "${installed}" ]
then
echo "There is no existing installation of Postgresql" >&2
if [ -d ${PGDATA}/template1 ]
then
echo However, there is a template1 directory in ${PGDATA}, which >&2
echo is unexpected. Please analyse and repair the directory by >&2
echo hand. This script is unable to handle it in its present >&2
echo state. >&2
echo >&2
exit 4
fi
else
# check the current and installed version numbers
majorc=`echo ${current} | cut -d. -f1`
minorc=`echo ${current} | cut -d. -f2`
majori=`echo ${installed} | cut -d. -f1`
minori=`echo ${installed} | cut -d. -f2`
ccomp=`expr 100 + $majorc``expr 100 + $minorc`
icomp=`expr 100 + $majori``expr 100 + $minori`
if [ ${ccomp} -eq ${icomp} -a -z "${force_reload}" ]
then
echo The PostgreSQL database format has not changed\; there is >&2
echo no need to do a dump and restore with this script. >&2
load_only=TRUE
else
must_dump=TRUE
if [ ${ccomp} -ge 106105 -a ${icomp} -le 106104 -a -n "$upgrade" ]
then
echo "It is not possible to use the -u option to upgrade from 6.4 or lower"
echo "to 6.5 or higher, because the internal structure of tables changed"
echo "at 6.5."
exit 1
fi
if [ $icomp -le 106104 ]
then
dump_options=-z
fi
if [ -n "$upgrade" ]
then
dump_options=$dump_options' -s'
fi
fi
fi
if [ -n "${must_dump}" -a -z "${exclude_dump}" ]
then
# Look for existing postgresql binaries, for the previous release
export BINDIR=/usr/lib/postgresql/dumpall/${installed}
if [ ! -d ${BINDIR} ]
then
echo There are no binaries for dumping database format ${installed} >&2
exit 5
fi
if [ -n "${verbose}" ]
then
echo "Stopping and restarting the postmaster" >&2
fi
set +e
pm_pid=`ps axc | grep -s ' postmaster *$' | awk '{print $1}'`
set -e
if [ -n "$pm_pid" ]
then
kill $pm_pid
fi
OPTIONS=
export LANG PGDATESTYLE
# Check for old-style postmaster.init
if [ "${PGDATESTYLE}" = American -o "${PGDATESTYLE}" = European ]
then
if [ "${PGDATESTYLE}" = American ]
then
PGDATESTYLE=US
else
PGDATESTYLE=EURO
fi
elif [ -z "${PGDATESTYLE}" ]
then
PGDATESTYLE=EURO
fi
POSTMASTER=${BINDIR}/postmaster
POSTGRES=${BINDIR}/postgres
PORT="-p 5431" # change the port to stop normal users connecting
${POSTMASTER} -S -D ${PGDATA} ${AUTH} ${PORT} ${OPTIONS}
sleep 4
set +e
new_pm_pid=`ps axc | grep -s ' postmaster *$' | awk '{print $1}'`
set -e
if [ A${new_pm_pid} = A ]
then
echo "Failed to start the postmaster" 2>&1
exit 7
fi
if [ A${new_pm_pid} = A${pm_pid} ]
then
echo "Failed to stop the running postmaster" 2>&1
exit 6
fi
if [ -n "${verbose}" ]
then
if [ -n "$upgrade" ]
then
echo "Dumping the database structure to ${target}" >&2
else
echo "Dumping the database to ${target}" >&2
fi
fi
(
echo "-- postgresql-dump ${upgrade} on `date` from version ${installed}"
/usr/lib/postgresql/dumpall/pg_dumpall $dump_options
echo "-- postgresql-dump ${upgrade} completed on `date`"
) | eval $writeout
if [ -n "${verbose}" ]
then
echo "Finding the default encoding" >&2
fi
if ${BINDIR}/psql -q $PORT -d template1 -c "select * from pg_database where datname = 'template1'" | head -1 | grep -q encoding
then
/usr/lib/postgresql/bin/pg_encoding `${BINDIR}/psql $PORT -q -t -d template1 -c "select encoding from pg_database where datname = 'template1'"` > $ENCODINGFILE
else
echo SQL_ASCII > $ENCODINGFILE
fi
if [ -n "${verbose}" ]
then
echo "Killing the postmaster" >&2
fi
kill $new_pm_pid
fi
fi
if [ -f "$target" ]
then
dumptype=`eval $header | cut -f3 -d\ `
if [ "$dumptype" = upgrade ]
then
unset destroy # We must not destroy the database, because the data
# is not in the dump
else
dumptype=full
fi
fi
if [ -n "${check}" ]
then
if echo ${target} | grep -q '^/dev/'
then
mt -f ${target} rewind
else
if [ ! -f ${target} ]
then
echo ASCII dump file ${target} does not exist.
exit 13
fi
fi
(echo This is the ASCII output of the dump for you to check:
echo
eval ${readin} | eval ${sed_command}) | ${PAGER:-more}
if [ -n "${destroy}" ]
then
echo -n On the basis of this dump, is it OK to delete the old database? [y/n]\
read x
case $x
in
y|Y|Yes|yes|YES)
echo Destroying old database...
;;
*)
exit 11;;
esac
else
echo -n Is this dump suitable for loading into the new database? [y/n]\
read x
case $x
in
y|Y|Yes|yes|YES)
;;
*)
exit 11;;
esac
fi
fi
if echo ${target} | grep -q '^/dev/'
then
mt -f ${target} rewind
fi
if [ -n "${destroy}" -o -n "$upgrade" ]
then
if [ -n "${preserve}" ]
then
# Copy the database tree to ${preserve} -- just in case...
if [ -n "${verbose}" ]
then
echo "Copying ${PGDATA} to ${preserve}" >&2
fi
if [ ! -d ${preserve} ]
then
mkdir ${preserve} || exit 8
fi
# Use cp rather than mv in case we are being asked
# to move between filesystems.
cp -a $PGDATA/* ${preserve} ||
{ echo Could not copy ${PGDATA}/\* to ${preserve} >&2; exit 9; }
fi
if [ -n "${verbose}" -a -n "${destroy}" ]
then
echo "Deleting ${PGDATA}" >&2
fi
if [ -n "$upgrade" ]
then
old_database=${PGDATA}.old
i=0
while [ -d $old_database ]
do
i=`expr $i + 1`
old_database=${PGDATA}.old.$i
done
if [ -n "${verbose}" ]
then
echo "Moving ${PGDATA} to $old_database" >&2
fi
mv ${PGDATA} $old_database
mkdir $PGDATA
chmod 700 $PGDATA
else
rm -rf ${PGDATA}/* ||
{ echo Could not delete ${PGDATA}/\* >&2
exit 10; }
fi
fi
set +e
unset POSTGRES
unset POSTMASTER
unset PGLIB
unset PGDATA
unset PORT
unset BINDIR
. /etc/postgresql/postgresql.env
set -e
if [ -n "${create}" ]
then
if [ -n "${verbose}" ]
then
echo "Creating the new database structure" >&2
fi
failed=
ENCODING=`cat $ENCODINGFILE 2>/dev/null`
if [ -n "$ENCODING" ]
then
INITOPTS=" --pgencoding=$ENCODING"
fi
USER=postgres initdb $INITOPTS || failed=TRUE
if [ -n "$failed" ]
then
echo initdb failed
exit 2
fi
fi
if [ -n "${load}" ]
then
if [ -n "${verbose}" ]
then
echo "Checking that the postmaster is running" >&2
fi
set +e
x=`ps axc | grep -s ' postmaster *$'`
set -e
if [ -z "$x" ]
then
# start up the postmaster
. /etc/postgresql/postmaster.init
PGDATA=${POSTGRES_DATA:=/var/lib/postgres/data}
PGLIB=/usr/lib/postgresql
export PGLIB PGDATA PGPORT LANG
POSTMASTER=${PGLIB}/bin/postmaster
POSTGRES=${PGLIB}/bin/postgres
OPTIONS=
if [ -n "${verbose}" ]
then
echo "Starting the postmaster" >&2
fi
# Shared-memory buffers
if [ ! -z "${PGBUFFERS}" ]
then
BUFFERS="-B ${PGBUFFERS}"
fi
# Debugging level
if [ ! -z "${PGDEBUG}" ]
then
DEBUGLEVEL="-d ${PGDEBUG}"
fi
# TCP port
PORT="-p ${PGPORT:=5432}"
# Backend options
# Query echoing
if [ "${PGECHO}" = yes ]
then
add_option -E
fi
# Timing stats
if [ "${PGSTATS}" = yes ]
then
add_option -s
fi
# Fsync()
if [ "${PGFSYNC}" = no ]
then
add_option -F
fi
# Sort memory
if [ ! -z "${PGSORTMEM}" ]
then
add_option -S ${PGSORTMEM}
fi
# American or European date format
if [ "${PGDATESTYLE}" != American ]
then
add_option -e
fi
# Make sure that we don't get started if there is no options file.
# We certainly don't want to get started if the executable is missing.
if [ ! -x ${POSTMASTER} ]
then
echo No postmaster executable for postgresql
exit 3
fi
LOGFILE=${POSTGRES_LOG:-/var/log/postgres.log}
# Ready to go: stand clear...
echo Starting PostgreSQL postmaster
echo ${POSTMASTER} -i -b ${POSTGRES} ${BUFFERS} \
-D ${PGDATA} ${DEBUGLEVEL} ${PORT} ${OPTIONS:+-o \'${OPTIONS}\'} \>$LOGFILE 2\>\&1 \&
nohup ${POSTMASTER} -i -b ${POSTGRES} ${BUFFERS} \
-D ${PGDATA} ${DEBUGLEVEL} ${PORT} ${OPTIONS:+-o "${OPTIONS}"} >$LOGFILE 2>&1 &
fi
if [ -n "${verbose}" ]
then
if [ -n "$upgrade" ]
then
echo "Loading the database structure from ${target}" >&2
else
echo "Loading the database from ${target}" >&2
fi
fi
# Give the postmaster a chance to start up
sleep 5
echo Reloading databases...
if [ -n "$upgrade" ]
then
cd ${PGDATA}/..
echo Current directory now `pwd`
rm -f ${target}.fixed
eval $readin | eval ${sed_command} > ${target}.fixed
echo Running pg_upgrade
pg_upgrade -f $target.fixed `basename $old_database`
rm ${target}.fixed
else
eval $readin | eval ${sed_command} |
psql -e template1 &&
echo Reload succeeded ||
echo Reload failed
fi
fi
exit 0
|