File: gfdump.postgresql.in

package info (click to toggle)
gfarm 2.3.2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,972 kB
  • ctags: 10,894
  • sloc: ansic: 84,510; sh: 13,707; java: 6,866; makefile: 2,286; python: 771; perl: 325; sql: 130; xml: 50; asm: 37; csh: 2
file content (218 lines) | stat: -rw-r--r-- 4,648 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
#
# $Id$

${DEBUG:="false"} && set -xu
PROGNAME=`basename $0`

# Gfarm installation directory
prefix="@prefix@"
sysconfdir="@sysconfdir@"
config_dir="@datadir@/gfarm/config"

PATH="${prefix}/bin:${prefix}/sbin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:/usr/pkg/bin:/usr/pkg/sbin:/usr/local/bin:/usr/local/sbin:$PATH"
export PATH
search_path="$PATH"

awk=awk
if [ -f /usr/bin/nawk ]; then awk=/usr/bin/nawk; fi

status=1
admin_password=/tmp/ap$$
password=/tmp/up$$
rm -f $password $admin_password
trap 'rm -f $password $admin_password; stty echo 2>/dev/null; exit $status' 0 1 2 15

getvalue()
{
	$awk '$1 == "'"$1"'" { print $2 }' $GFMD_CONF
}

ABORT()
{
	[ $# -gt 0 ] && echo >&2 "${PROGNAME}: $@"
	echo >&2 "$PROGNAME failure"
	exit 1
}

usage() {
	echo >&2 "usage: $PROGNAME -d [options]"
	echo >&2 "       $PROGNAME -r [options]"
	echo >&2 " options:"
	echo >&2 "	[-t]			- display configuration, do not execute anything"
	echo >&2 "	[--prefix prefix]	- directory specified in config-gfarm"
	echo >&2 "	[-f filename]		- file to dump or to restore"
	echo >&2 "	[-l metadata_directory]	- backend database data directory"
	exit 1
}

# most $BACKEND_TYPE calls the followings
# from display_backend_params_$BACKEND_TYPE, but some may not.
display_backend_data_dir_param()
{
    echo     "metadata     directory    [-l]: $BACKEND_DATA_DIR"
}

display_backend_log_dir_param()
{
    return 0
}

display_params()
{
    echo     "prefix [--prefix]: $CONFIG_PREFIX"
    echo     "metadata backend    [-b]: $BACKEND_TYPE"

    display_backend_params_$BACKEND_TYPE
    display_backend_port_params_$BACKEND_TYPE
    display_backend_optional_params_$BACKEND_TYPE
    exit 0
}

# default value
DUMPF=db.out
MODE=UNKNOWN

#
# parse arguments
#

while [ $# -gt 0 ]
do
	case $1 in

	# set parameters
	  --prefix) shift; [ $# -ge 1 ] || usage
		CONFIG_PREFIX=$1
		;;
	  -P) shift; [ $# -ge 1 ] || usage
		BACKEND_PREFIX=$1
		;;
	  -l) shift; [ $# -ge 1 ] || usage
		BACKEND_DATA_DIR=$1
		;;
	  -f) shift; [ $# -ge 1 ] || usage
		DUMPF=$1
		;;

	# control options
	  --help)
		usage
		;;
	  -t)
		DISPLAY_PARAMS=true
		;;
	  -d)
		MODE=DUMP
		;;
	  -r)
		MODE=RESTORE
		;;
	  -*) echo "unknown option: $1"
		usage ;;
	  *) usage ;;
	esac
	shift
done
[ X"$MODE" = XUNKNOWN ] && usage

: ${CONFIG_PREFIX:=}
if [ X"$CONFIG_PREFIX" != X ]; then
	: ${GFARM_CONF_DIR:="$CONFIG_PREFIX/etc"}
else
	: ${GFARM_CONF_DIR:="$sysconfdir"}
fi
: ${GFMD_CONF:="$GFARM_CONF_DIR/gfmd.conf"}

# sanity check
if [ ! -f ${GFMD_CONF} ]; then
	ABORT "${PROGNAME}: cannot find ${GFMD_CONF}, --prefix option required"
fi

# determine a backend database type
if grep '^[ 	]*postgresql_server_host[ 	]' ${GFMD_CONF} >/dev/null
then
	BACKEND_TYPE=postgresql
elif grep '^[ 	]*ldap_server_host[ 	]' ${GFMD_CONF} >/dev/null
then
	BACKEND_TYPE=ldap
else
	echo >&2 "${PROGNAME}: cannot determine the backend type"
	exit 1
fi

# load backend-dependent functions
. $config_dir/config-gfarm.$BACKEND_TYPE
. $config_dir/config-gfarm-update.$BACKEND_TYPE

#
# search default $BACKEND_PREFIX
#

set_default_backend_prefix_$BACKEND_TYPE
. $config_dir/config-gfarm.sysdep

#
# default values
#

set_first_defaults_$BACKEND_TYPE
sysdep_defaults

: ${DISPLAY_PARAMS:=false}

update_first_set_param_$BACKEND_TYPE
set_last_defaults_$BACKEND_TYPE
update_last_set_param_$BACKEND_TYPE

# -t option; display parameters
$DISPLAY_PARAMS && display_params

case ${BACKEND_TYPE} in

###
### postgresql
###

postgresql)
	BACKEND_HOST="`getvalue postgresql_server_host`"
	BACKEND_USER_PASSWD="`getvalue postgresql_password | tr -d '"'`"
	pg_dump=${PGSQL_BINDIR}/pg_dump
	dropdb=${PGSQL_BINDIR}/dropdb
	createdb=${PGSQL_BINDIR}/createdb

	case $MODE in
	    DUMP)
		update_pgpass_postgresql ${BACKEND_HOST} ${BACKEND_PORT} "${DB_NAME}" "${BACKEND_USER}" "${BACKEND_USER_PASSWD}" || ABORT "aborted"
		$pg_dump -h $BACKEND_HOST -p $BACKEND_PORT -U $BACKEND_USER -f $DUMPF $DB_NAME ||
			ABORT "pg_dump fails"
		;;
	    RESTORE)
		[ ! -f "$DUMPF" ] &&
			ABORT "$DUMPF: database dump file not exist"
		update_pgpass_postgresql localhost ${BACKEND_PORT} "*" "${BACKEND_ADMIN_USER}" "${BACKEND_ADMIN_PASSWD}" || ABORT "aborted"
		service_ctl gfmd stop
		$dropdb -p $BACKEND_PORT -U $BACKEND_ADMIN_USER $DB_NAME
		$createdb -p $BACKEND_PORT -U $BACKEND_ADMIN_USER $DB_NAME
		$psql -p $BACKEND_PORT -U $BACKEND_ADMIN_USER -d $DB_NAME -f $DUMPF > /dev/null ||
			ABORT "restore fails"
		service_ctl gfmd start
		;;
	    *)
		usage
		;;
	esac
	;;

###
### LDAP
###

ldap)
	echo >&2 "${PROGNAME}: ldap backend is not supported yet"
	;;

*)
	echo >&2 "${PROGNAME}: internal error - backend \"${BACKEND_TYPE}\""
	exit 1;;
esac