File: pg_dumpall

package info (click to toggle)
postgresql 7.2.1-2woody8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 42,424 kB
  • ctags: 30,027
  • sloc: ansic: 290,568; java: 18,529; sh: 12,197; sql: 11,401; yacc: 11,189; tcl: 8,063; perl: 4,067; makefile: 3,332; xml: 2,874; lex: 2,799; python: 1,237; cpp: 845; pascal: 81; asm: 70; awk: 20; sed: 8
file content (99 lines) | stat: -rw-r--r-- 2,545 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
#!/bin/bash
#
# pg_dumpall [pg_dump parameters]
# dumps all databases to standard output
# It also dumps the pg_shadow table
#
# to adapt to System V vs. BSD 'echo'
if echo '\\' | grep '\\\\' >/dev/null 2>&1
then	
	BS='\'			# BSD
else
	BS='\\'			# System V
fi

export PGPORT=5431    # use a different port to stop other users connecting

# Find out which shared library we need to use
if [ -r ${BINDIR}/libpq.so.1 ]
then
	sfx=1
else
	sfx=2
fi
SHLIB=${BINDIR}/libpq.so.${sfx}

# Dump everyone but the postgres user
# initdb creates him
#
# get the postgres user id
#
POSTGRES_SUPER_USER_ID="`echo \" \
			select datdba \
			from pg_database \
			where datname = 'template1'; \" | \
			LD_PRELOAD=${SHLIB} ${BINDIR}/psql -A -q -t template1`"
echo "${BS}connect template1"

# delete all users in case they run this twice
#
# we don't use POSTGRES_SUPER_USER_ID because the postgres super user id
# could be different on the two installations
#
echo "select datdba into table tmp_pg_shadow \
      from pg_database where datname = 'template1';"
echo "delete from pg_shadow where usesysid <> tmp_pg_shadow.datdba;"
echo "drop table tmp_pg_shadow;"

# load all the non-postgres users
#
echo "copy pg_shadow from stdin;"
set -e
if [ -f ${PGDATA}/pg_shadow ]
then
	SHADOW=pg_shadow
	LD_PRELOAD=${SHLIB} ${BINDIR}/psql -q template1 <<END
	select pg_shadow.* 
	into table tmp_pg_shadow
	from pg_shadow
	where usesysid <> $POSTGRES_SUPER_USER_ID;
	copy tmp_pg_shadow to stdout;
	drop table tmp_pg_shadow;
END
else
	SHADOW=pg_user
	LD_PRELOAD=${SHLIB} ${BINDIR}/psql -q template1 <<END
	select pg_user.*, ''::text as passwd, 'Sat 31 Jan 06:00:00 2037 GMT'::abstime as valuntil 
	into table tmp_pg_shadow
	from pg_user
	where usesysid <> $POSTGRES_SUPER_USER_ID;
	copy tmp_pg_shadow to stdout;
	drop table tmp_pg_shadow;
END
fi

echo "${BS}."
LD_PRELOAD=${SHLIB} ${BINDIR}/psql -l -A -q -t| tr '|' ' ' | grep -v '^template1 ' | \
while read DATABASE DBUSERID ENCODING DATAPATH
do
	POSTGRES_USER="`echo \" \
		select usename \
		from ${SHADOW} \
		where usesysid = $DBUSERID; \" | \
		LD_PRELOAD=${SHLIB} ${BINDIR}/psql -A -q -t template1`"
	echo "${BS}connect template1 $POSTGRES_USER"

	if createdb --help|grep encoding >/dev/null
	then
		echo "create database $DATABASE with encoding='`pg_encoding $ENCODING`';"
	else
		echo "create database $DATABASE;"
	fi

	echo "${BS}connect $DATABASE $POSTGRES_USER"
	LD_PRELOAD=${SHLIB} ${BINDIR}/pg_dump ${1+"$@"} $DATABASE
	if [ "$?" -ne 0 ]
	then	echo "pg_dump failed on $DATABASE, exiting" 1>&2
		exit 1
	fi
done