File: postgresql-dump.in

package info (click to toggle)
postgresql 7.4.7-6sarge6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,168 kB
  • ctags: 27
  • sloc: sh: 1,903; makefile: 337; ansic: 204; perl: 69; sed: 6; tcl: 1
file content (674 lines) | stat: -rw-r--r-- 16,193 bytes parent folder | download
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/bin/bash
#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 "      -e encoding  Force the new framework to use this encoding as default"
	echo "      -f        fix the change of 'current' to 'old' in rules"
	echo "      -n        fix the change of DEFAULT 'now' to DEFAULT now() in rules"
	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 "      -v        verbose output"
	echo "      -X        do not dump the old database; cancels -c"
	echo "      -x        do not dump the old database; forces -c"
}

restore_pg_options() {
	rm -f $PGDATA/pg_options
	if [ -f $PGDATA/pg_options.pgdbak ]
	then
		mv $PGDATA/pg_options.pgdbak $PGDATA/pg_options
	fi
	if [ -d "$preserve" ]
	then
		there=`pwd`
		cd $preserve
		if [ -f pg_options.pgdbak ]
		then
		     mv pg_options.pgdbak pg_options
		fi
		cd $there
	fi
}

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 $1 is or is under PGDATA. It will be destroyed when PGDATA is cleared. >&2
			echo Specify a different path.>&2
			exit 11
		fi
		dir=`dirname $dir`
	done
}

fixup_libpath () {
	if [ -z "$LD_LIBRARY_PATH" ];
	then
		LD_LIBRARY_PATH="/var/lib/postgres/dumpall/${installed}"
	else
		LD_LIBRARY_PATH="/var/lib/postgres/dumpall/${installed}:$LD_LIBRARY_PATH"
	fi
	export LD_LIBRARY_PATH
}

recoverconfig () {
# put the config files back where they were
    rm -f $PGDATA/pg_hba.conf
    ln -s /etc/postgresql/pg_hba.conf $PGDATA/pg_hba.conf
    rm -f $PGDATA/postgresql.conf
    ln -s /etc/postgresql/postgresql.conf $PGDATA/postgresql.conf
    if [ -d "$preserve" ]
    then
	there=`pwd`
        cd $preserve
	if [ -f postgresql.conf.pgdbak ]
	then
	    rm -f postgresql.conf
	    mv postgresql.conf.pgdbak postgresql.conf
	fi
	if [ -f pg_hba.conf.pgdbak ]
	then
	    rm -f pg_hba.conf
	    mv pg_hba.conf.pgdbak pg_hba.conf
	fi
	cd $there
    fi
    restore_pg_options
}

## Start of execution ##

SHELL=/bin/sh
trap recoverconfig 0

if [ -r /etc/postgresql/postmaster.conf ]
then
	. /etc/postgresql/postmaster.conf
fi

PGDATA=${POSTGRES_DATA:=/var/lib/postgres/data}
PGLIB=/usr/lib/postgresql
PATH=/usr/lib/postgresql/bin:/bin:/usr/bin
ENCODINGFILE=/var/lib/postgres/dumpall/default_encoding
export PGLIB PGDATA LANG PATH
current=%PG_VERSION%          # Current PostgreSQL database version

if [ -f ${PGDATA}/PG_VERSION ]; then
    installed=`cat ${PGDATA}/PG_VERSION`
fi

# 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

check=
destroy=
create=
load=
sed_command=cat
preserve=
verbose=
exclude_dump=
no_check=
force_reload=
blksz=
set +e
unset PGHOST
set -e

# Read command line
while [ $OPTIND -le $# ]
do
	getopts b:cde:Ffnilp:t:vXx arg
	case "$arg"
		in
		b)
		   blksz=$OPTARG;;
		c)
			check=TRUE;;
		d)
			destroy=TRUE;;
		e)
			encoding=$OPTARG;;
		F)
			force_reload=TRUE;;
		f)
			sed_command="sed -e '/^CREATE RULE /s/current\./old./g'";;
		n)
			dump_options="-N";;
		h)
			help
			exit 0;;
		i)
			create=TRUE;;
		l)
			load=TRUE;;
		p)
			preserve=$OPTARG;;
		t)
			target=$OPTARG;;
		v)
			verbose=TRUE;;
		X)
			no_check=TRUE
			exclude_dump=TRUE;;
		x)
			exclude_dump=TRUE
			check=TRUE;;
		*)
			syntax;;
	esac
done

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

# Make sure either the ${PGDATA} directory or the dump target exists
dumped=
if [ ! -d $PGDATA/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.conf >&2
		exit 3
	else
		dumped=TRUE
		destroy=""
		preserve=""
	fi
else
	# source the LC_* settings from the old database
	export $(/usr/lib/postgresql/bin/pg_controldata $PGDATA |
		grep '^LC_' |
		sed -e 's/:[ 	]*/=/')
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
	must_dump=

	if [ -z "${installed}" ]
	then
		echo "There is no existing installation of Postgresql" >&2
		if [ -d ${PGDATA}/base ]
		then
			echo However, there is a base 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 [ $icomp -le 106104 ]
			then
				dump_options="$dump_options -z"
			fi
		        # make sure the old database has its own copy of
		        # pg_hba.conf and postgresql.conf
		        if [ -L $PGDATA/pg_hba.conf ]
		        then
			    rm $PGDATA/pg_hba.conf
			    cp /etc/postgresql/pg_hba.conf $PGDATA/pg_hba.conf
		        fi
		        if [ -L $PGDATA/postgresql.conf ]
		        then
			    rm $PGDATA/postgresql.conf
			    cp /etc/postgresql/postgresql.conf  $PGDATA/postgresql.conf
		        fi
			cat $PGDATA/pg_hba.conf >$PGDATA/pg_hba.conf.pgdbak
			cat $PGDATA/postgresql.conf >$PGDATA/postgresql.conf.pgdbak
			# now make sure we have full access
			if [ $icomp -le 107102 ]
			then
			    echo "local all trust" >$PGDATA/pg_hba.conf
			else
			    echo "local all all trust" >$PGDATA/pg_hba.conf
			fi
		fi
	fi

	if [ -n "${must_dump}" -a -z "${exclude_dump}" ]
	then
		# Look for existing postgresql binaries, for the previous release
		export BINDIR=/var/lib/postgres/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
		pm_pid=`pidof /usr/lib/postgresql/bin/postmaster` && kill $pm_pid || true

		OPTIONS='-o -d0'
		
		export LANG PGDATESTYLE

		# Check for old-style postmaster.conf
		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

		if [ -f $PGDATA/pg_options -o -L $PGDATA/pg_options ]
		then
			mv $PGDATA/pg_options $PGDATA/pg_options.pgdbak
		fi
		rm -f $PGDATA/pg_options
		echo 'all = -1' > $PGDATA/pg_options

		# There are new postgresql.conf options at 7.3 that 7.2 and
		# earlier cannot cope with
		if [ -L $PGDATA/postgresql.conf -a -f /var/lib/postgres/dumpall/$installed/postgresql.conf ]
		then
			rm $PGDATA/postgresql.conf
			ln -s /var/lib/postgres/dumpall/$installed/postgresql.conf $PGDATA/postgresql.conf
		fi
		# Just to be sure, we will replace it with an empty one
		mv $PGDATA/postgresql.conf $PGDATA/postgresql.conf.pgdbak
		touch $PGDATA/postgresql.conf
		# ... but we may need the locale data
		grep -i '^ *(LC_CTYPE|LC_COLLATE)' $PGDATA/postgresql.conf.pgdbak |
			sed -e 's/lc_ctype/LC_CTYPE/' -e 's/lc_collate/LC_COLLATE/' -e 's/ *= */=/' -e "s/'//g" >$PGDATA/postgresql.conf || true
                . $PGDATA/postgresql.conf
		export LC_COLLATE LC_CTYPE

		echo "${POSTMASTER} -D ${PGDATA} ${AUTH} ${PORT} ${OPTIONS}"
		${POSTMASTER} -D ${PGDATA} ${AUTH} ${PORT} ${OPTIONS} &
		sleep 5

		# some older versions of postmaster put the socket in /tmp
		# rather than /var/run/postgresql
		if [ -S /tmp/.s.PGSQL.5431 ]
		then
			# make a symlink to cope, and arrange to destroy it
			# when this script exits
			ln -sf /tmp/.s.PGSQL.5431 /var/run/postgresql/.s.PGSQL.5431
		fi

		new_pm_pid=`pidof /usr/lib/postgresql/bin/postmaster` || {
			echo "Failed to start the postmaster" 2>&1
			exit 7
                }
		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
			echo "Dumping the database to ${target}" >&2
		fi

		echo pg_dumpall "$dump_options"
		for PG_DUMPALL in /usr/lib/postgresql/bin/pg_dumpall /var/lib/postgres/dumpall/${installed}/pg_dumpall
		do
			if [ -n "$RETRYING" ]
			then
				echo Retrying with $PG_DUMPALL
				export PATH=/var/lib/postgres/dumpall/${installed}:$PATH
				fixup_libpath
			else
				echo Dumping with new pg_dumpall >&2
				export LD_LIBRARY_PATH=/usr/lib/postgresql/lib
			fi
			export PGPORT=5431
			(
				echo "-- postgresql-dump on `date` from version ${installed}"
				if $PG_DUMPALL $dump_options 
				then
					echo "-- postgresql-dump completed on `date`"
				else
					echo "-- dump failed"
					echo "Dump failed" >&2
					RETRYING=true
				fi
			) | eval $writeout
			[ -z "$RETRYING" ] && break || true
		done

		if [ -n "${verbose}" ]
		then
			echo "Finding the default encoding" >&2
		fi
		(
			fixup_libpath
			default_encoding=`${BINDIR}/psql $PORT -q -t -d template1 -c "select pg_encoding_to_char(encoding) from pg_database where datname = 'template1'"`
			echo $default_encoding >$ENCODINGFILE
		)
		if [ -n "${verbose}" ]
		then
			echo "Killing the postmaster" >&2
		fi
		kill $new_pm_pid
		while kill -0 $new_pm_pid
		do
			sleep 1
		done
	fi
fi

if [ -f "$target" ]
then
	dumptype=`eval $header | cut -f3 -d\ `
	if [ "$dumptype" = upgrade ]
	then
# this section was for use only with pg_upgrade
		#unset destroy	# We must not destroy the database, because the data
				## is not in the dump
		echo "You are attempting to upgrade with an 'upgrade only' dumpfile.
This is no longer supported, because pg_upgrade has been dropped."
		exit 14
	#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:-/usr/bin/pager}
	
	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}" ]
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
			echo Directory ${preserve} already exists || exit 8
		fi

		# Use cp rather than mv in case we are being asked
		# to move between filesystems.
		if ! cp -a $PGDATA ${preserve}
		then
			echo Could not copy ${PGDATA}/\* to ${preserve} >&2
			exit 9
		fi
		# ensure the saved directory is not readable except to its
		# owner - who will be postgres or root
		chmod 700 ${preserve}
	fi
	if [ -n "${verbose}" -a -n "${destroy}" ]
	then
		echo "Deleting ${PGDATA}" >&2
	fi

	rm -rf ${PGDATA}/*
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

	PGDATA=${POSTGRES_DATA:=/var/lib/postgres/data}
	failed=
	ENCODING=`cat $ENCODINGFILE 2>/dev/null`
	# override with the command line option, if given
	if [ -n "$encoding" ]
	then
	   ENCODING=$encoding
	fi
	if [ -n "$ENCODING" ]
	then
		INITOPTS=" --encoding $ENCODING"
	fi
	USER=postgres initdb $INITOPTS || failed=TRUE
	if [ -n "$failed" ]
	then
		echo initdb failed
		exit 2
	fi
	# copy any authentication files from old PGDATA to new one; this covers
	# the lists of users and databases introduced at release 7.3
	for f in $(grep '^[[space]]*[^#].*[[space]]*@' $preserve/*)
	do
		for word in $f
		do
			if [ `echo $word | cut -c1` = @ ]
			then
				file=`echo $word | cut -c2-9999`
				[ -r "$file" ] && cp $preserve/$file $PGDATA || true
			fi
		done
	done
fi

if [ -n "${load}" ]
then
	if [ -n "${verbose}" ]
	then
		echo "Checking that the postmaster is running" >&2
	fi
        if ! pidof /usr/lib/postgresql/bin/postmaster > /dev/null
	then
		. /etc/postgresql/postmaster.conf
		PGDATA=${POSTGRES_DATA:=/var/lib/postgres/data}
		# make sure we have full access
		rm $PGDATA/pg_hba.conf
		echo "local all all trust" > $PGDATA/pg_hba.conf
		# start up the postmaster
		PGLIB=/usr/lib/postgresql
		PGPORT=5431
		PORT="-p $PGPORT"
		export PGLIB PGDATA PGPORT LANG
		POSTMASTER=${PGLIB}/bin/postmaster
		POSTGRES=${PGLIB}/bin/postgres
		OPTIONS=

		if [ -n "${verbose}" ]
		then
			echo "Preparing to start the postmaster" >&2
		fi

		# 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/postgresql/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
		echo "Loading the database from ${target}" >&2
	fi

	# Give the postmaster a chance to start up
	sleep 5
	echo Reloading databases...
	report_error=true
	DUMP_LOC=`dirname $target`
	eval $readin | eval ${sed_command} |
		psql -p $PGPORT -e template1 > $DUMP_LOC/reload.trace 2>&1 &&
		report_error= ||
		echo Reload failed >&2
	if grep -q '^ERROR' $DUMP_LOC/reload.trace
	then
		echo There were errors on reloading the data.  Review the trace in >&2
		echo $DUMP_LOC/reload.trace to see what happened and >&2
		echo what action is needed to correct the situation. >&2
		echo >&2
		echo The reload command was: >&2
		echo $readin \| ${sed_command} \| psql -p $PGPORT -e template1 >&2
		report_error=true
	fi

	# stop the postmaster
	[ -r /var/run/postgresql/.s.PGSQL.$PGPORT.lock ] &&
	    kill -INT `head -1 /var/run/postgresql/.s.PGSQL.$PGPORT.lock` || true
fi

if [ -n "$report_error" ]
then
	exit 1
fi

exit 0