pg_dumpall 1.6 KB
Newer Older
B
Bruce Momjian 已提交
1 2 3 4
#!/bin/sh
#
# pg_dumpall [pg_dump parameters]
# dumps all databases to standard output
B
Bruce Momjian 已提交
5
# It also dumps the pg_shadow table
B
Bruce Momjian 已提交
6
#
7
# to adapt to System V vs. BSD 'echo'
8
#set -x
9 10 11 12 13 14
if echo '\\' | grep '\\\\' >/dev/null 2>&1
then	
	BS='\'			# BSD
else
	BS='\\'			# System V
fi
B
Bruce Momjian 已提交
15 16 17 18
#
# Dump everyone but the postgres user
# initdb creates him
#
19 20
# get the postgres user id
#
B
Bruce Momjian 已提交
21 22 23 24 25
POSTGRES_SUPER_USER_ID="`echo \" \
			select datdba \
			from pg_database \
			where datname = 'template1'; \" | \
			psql -A -q -t template1`"
26 27 28 29
echo "${BS}connect template1"
#
# delete all users in case they run this twice
#
30 31 32
# we don't use POSTGRES_SUPER_USER_ID because the postgres super user id
# could be different on the two installations
#
B
Bruce Momjian 已提交
33
echo "select datdba into table tmp_pg_shadow \
34
      from pg_database where datname = 'template1';"
B
Bruce Momjian 已提交
35 36
echo "delete from pg_shadow where usesysid <> tmp_pg_shadow.datdba;"
echo "drop table tmp_pg_shadow;"
37 38 39
#
# load all the non-postgres users
#
B
Bruce Momjian 已提交
40
echo "copy pg_shadow from stdin;"
B
Bruce Momjian 已提交
41
psql -q template1 <<END
B
Bruce Momjian 已提交
42 43 44
select pg_shadow.* 
into table tmp_pg_shadow
from pg_shadow
B
Bruce Momjian 已提交
45
where usesysid <> $POSTGRES_SUPER_USER_ID;
B
Bruce Momjian 已提交
46 47
copy tmp_pg_shadow to stdout;
drop table tmp_pg_shadow;
B
Bruce Momjian 已提交
48
END
49
echo "${BS}."
50
psql -l -A -q -t| tr '|' ' ' | grep -v '^template1 ' | \
51
while read DATABASE DBUSERID DATAPATH
52
do
B
Bruce Momjian 已提交
53 54
	POSTGRES_USER="`echo \" \
		select usename \
B
Bruce Momjian 已提交
55
		from pg_shadow \
56
		where usesysid = $DBUSERID; \" | \
B
Bruce Momjian 已提交
57
		psql -A -q -t template1`"
58 59 60
	echo "${BS}connect template1 $POSTGRES_USER"
	echo "create database $DATABASE;"
	echo "${BS}connect $DATABASE $POSTGRES_USER"
M
 
Marc G. Fournier 已提交
61
	pg_dump ${1+"$@"} $DATABASE
62 63 64 65
	if [ "$?" -ne 0 ]
	then	echo "pg_dump failed on $DATABASE, exiting" 1>&2
		exit 1
	fi
66
done