-
Notifications
You must be signed in to change notification settings - Fork 4
/
init_schema
385 lines (337 loc) · 8.74 KB
/
init_schema
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
#!/bin/sh
#
#description:
#init_schema this is slony copy schema and auto configure slony set shell script.
#
#### BEGIN INFO
# Provides: maycap
# Required-Start: slony cluster is running
# Should-Start: null
# version:1.0
# date:2014-11-25
### END INFO
#this is CONSTANT
CONFIG='./cluster.config'
PSQL='/usr/bin/psql'
SLONIK='/opt/pgsql/bin/slonik'
SYN=0
SQLTMP=/tmp/sql.$$
COMMENT_DATE="`date +"%Y/%m/%d/%T"`"
#check psql and slonik
if [[ -f $PSQL || -L $PSQL ]];then
:
else
PSQL=`which psql` 2>/dev/null 1>/dev/null
rc=$?
if [ $rc -ne 0 ];then
echo "Postgresql psql is not avaliable! Please check!"
exit $rc
else
:
fi
fi
if [[ -f $SLONIK || -L $SLONIK ]];then
:
else
SLONIK=`which slonik` 2>/dev/null 1>/dev/null
rc=$?
if [ $rc -ne 0 ];then
echo "Slony slonik is not avaliable! Please check!"
exit $rc
else
:
fi
fi
#Above is Functions
#check config exist and check config format
check_config(){
CLUSTERTMP=$1
CONFIGTMP=$2
if [ -s $CONFIG ];then
grep $CLUSTERTMP $CONFIGTMP 2>&1 >/dev/null
if [ $? -eq 0 ];then
return 0;
else
echo "No this cluster \"$CLUSTERTMP\" on config $CONFIGTMP!"
return 2;
fi
else
echo "Don't find config file!"
return 1;
fi
}
#check -c select configure file is exist
check_exist(){
CONFIGTMP="$1"
if [ -s "$CONFIGTMP" ];then
return 0
else
return 1
fi
}
#check -f select sql file is exist
check_sql(){
SQL=$1
if [ -f $SQL ];then
return 0;
else
echo "SQL file not exist!"
return 1;
fi
}
#use psql to excute sql
excute_sql(){
host=$1
port=$2
db=$3
sql=$4
schemaname=$5
#modify sql to excute sql on selected schema
cp $sql $SQLTMP
sed -i 1i"SET search_path = \"$schemaname\", pg_catalog;" $SQLTMP
sed -i 1a"create schema \"$schemaname\";" $SQLTMP
echo "Now create schema on $host $port $db..."
$PSQL -U postgres -h $host -p $port -d $db -f "$SQLTMP" 2>/dev/null 1>/dev/null
if [ $? -ne 0 ];then
echo "$host:$port:$db is bad! Please check database!"
exit 2
else
echo "$host:$port:$db is ok!"
fi
rm -fr $SQLTMP
}
#Get all tables name in selected schema on selected database. Use sql query!
Get_all_tables(){
host=$1
port=$2
dbname=$3
schema=$4
$PSQL -U $PGUSER -h $host -p $port -d $dbname -A -t -c "select tablename from pg_tables where schemaname = '$schema'"
}
#this is check parameters
if [ "$#" -eq 0 ];then
echo "Usage:`basename $0` -n [cluster] -f [sql] -S [schemaname] -m \"comment\" "
exit 1
else
if [ "${1##-}" = "$1" ];then
echo "Parameters ERROR!"
echo "e.g. `basename $0` -n [cluster] -f [sql] -S [schemaname] -m \"comment\" "
exit 1
fi
if [ "$1" = "-h" ] ;then
echo -e "`basename $0` is the config slony script ,copy schema and auto config.\n"
echo -e "Usage:"
echo -e "\t`basename $0` [OPTION]... "
echo -e "\tExample: -n [cluster] -f [sql] -S [schemaname] -m \"comment\" \n"
echo -e "General options:"
echo -e "\t-h request for help."
echo -e "\t-n select cluster name."
echo -e "\t-S new schema name."
echo -e "\t-f select sql."
echo -e "\t-t new setid."
echo -e "\t-T new table id."
echo -e "\t-m this is comment."
echo -e "\t-c select your configure file(default $CONFIG)."
echo -e "\t-v look version."
echo -e "\t-s syn (0|all,1|master,2|slave) DDL.\n"
echo -e "Report bugs to <gencat@21tb.com>."
exit 0
fi
if [ "$1" = "-v" ];then
echo "version 1.2"
echo "Create by Maycap."
echo "Thanks for used."
exit 0
fi
tag=`expr $# % 2`
if [ $tag -ne 0 ];then
echo "Parameters Error or lost! Please retry!"
exit 2
fi
while [ "$1" != "${1##-}" ];do
case $1 in
'')
echo "Parameters ERROR!"
echo e.g. `basename $0` -n [cluster] -f [sql]
exit 3;;
-n)
check_config $2 $CONFIG
rc=$?
if [ $rc -ne 0 ];then
exit $rc
fi
CLUSTER=$2
shift 2
;;
-f)
check_sql $2
rc=$?
if [ $rc -ne 0 ];then
exit $rc
fi
SQL=$2
shift 2
;;
-s)
SYN=$2
shift 2
;;
-t)
SETID=$2
shift 2
;;
-T)
TABLEID=$2
shift 2
;;
-m)
COMMENT="$2"
shift 2
;;
-c)
check_exist "$2"
rc=$?
if [ $rc -ne 0 ];then
echo "Your configure file not exist!Please check or use default!"
exit $rc
fi
CONFIG="$2"
shift 2
;;
-S)
SCHEMANAME=$2
shift 2
;;
*)
echo "Parameters ERROR!"
echo e.g. `basename $0` -n [cluster] -f [sql] -S [schemaname]
exit 3;;
esac
done
fi
#check clustername and sql and schemaname all not null.
if [[ -z $CLUSTER || -z $SQL || -z $SCHEMANAME ]];then
echo "Lost parameter!"
echo e.g. `basename $0` -n [cluster] -f [sql] -S [schemaname] -t [setid] -T [tableid]
exit 1
fi
#Parse the config file
masterhost=`grep $CLUSTER $CONFIG |awk -F':' '/masterhost/{print $2}'`
masterport=`grep $CLUSTER $CONFIG |awk -F':' '/masterport/{print $2}'`
masterdbname=`grep $CLUSTER $CONFIG |awk -F':' '/masterdbname/{print $2}'`
masternode=`grep $CLUSTER $CONFIG |awk -F':' '/masternode/{print $2}'`
slavehost=(`grep $CLUSTER $CONFIG |awk -F':' '/slavehost/{print $2}'|tr ',' ' '`)
slaveport=(`grep $CLUSTER $CONFIG |awk -F':' '/slaveport/{print $2}'|tr ',' ' '`)
slavedbname=(`grep $CLUSTER $CONFIG |awk -F':' '/slavedbname/{print $2}'|tr ',' ' '`)
slavenode=(`grep $CLUSTER $CONFIG |awk -F':' '/slavenode/{print $2}'|tr ',' ' '`)
#check schemaname is avaliable
schemaflag=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c " select * from pg_namespace where nspname = '$SCHEMANAME'"`
if [[ -z $schemaflag ]];then
:
else
echo "The schema name ($SCHEMANAME) is used!"
exit 1
fi
#check user specify setid and tableid is avaliable
if [ -z $SETID ];then
MAXSETID=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c "select max(set_id) from _$CLUSTER.sl_set"`
SETID=${SETID:-`expr $MAXSETID + 1`}
else
setflag=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c "select * from _$CLUSTER.sl_set where set_id = $SETID"`
if [[ -z $setflag ]];then
:
else
echo "The set id ($SETID) is used!"
exit 1
fi
fi
if [ -z $TABLEID ];then
MAXTABLEID=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c "select max(tab_id) from _$CLUSTER.sl_table"`
TABLEID=${TABLEID:-`expr $MAXTABLEID + 1`}
else
tabflag=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c "select * from _$CLUSTER.sl_table where tab_id = $TABLEID"`
if [[ -z $tabflag ]];then
:
else
echo "The table id ($TABLEID) is used!"
exit 1
fi
fi
#select -s to excute specified database
case $SYN in
0|all)
excute_sql $masterhost $masterport $masterdbname $SQL $SCHEMANAME
for i in `seq ${#slavehost[@]}`
do
pos=`expr $i - 1`
excute_sql ${slavehost[$pos]} ${slaveport[$pos]} ${slavedbname[$pos]} $SQL $SCHEMANAME
done
#exit 0
;;
1|master)
excute_sql $masterhost $masterport $masterdbname $SQL $SCHEMANAME
#exit 0
;;
2|slaver)
for i in `seq ${#slavehost[@]}`
do
pos=`expr $i - 1`
excute_sql ${slavehost[$pos]} ${slaveport[$pos]} ${slavedbname[$pos]} $SQL $SCHEMANAME
done
#exit 0
;;
*)
echo "Error parameter!"
exit 1
esac
#Create slony script shell
echo "Now config slony...."
#Get all tables name in selected schema on selected database. Use shell!
tableslist=(`awk '/CREATE TABLE/{print $3}' $SQL | awk -F '(' '{print $1}'`)
#echo some prompt
echo "Slony Cluster $CLUSTER setid will be $SETID."
echo "New add table will be $TABLEID."
#this is slony script shell
ADDSHELL=ADD_SET_$SCHEMANAME.sh
>$ADDSHELL
#Create ....
echo '#!/bin/sh' >>$ADDSHELL
echo "$SLONIK<<_EOF_" >>$ADDSHELL
echo "cluster name = $CLUSTER;" >>$ADDSHELL
#Query master conninfo and write script
master_conninfo=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c "select pa_conninfo from _$CLUSTER.sl_path where pa_server=$masternode"`
echo "node $masternode admin conninfo = '$master_conninfo';" >>$ADDSHELL
#Query slave conninfo and write script
Cnode=${#slavenode[@]}
for i in `seq $Cnode`
do
pos=`expr $i - 1`
slave_conninfo=`$PSQL -h $masterhost -p $masterport -d $masterdbname -A -t -c "select pa_conninfo from _$CLUSTER.sl_path where pa_server=${slavenode[$pos]}"`
echo "node $slavenode admin conninfo = '$slave_conninfo';" >>$ADDSHELL
done
#Create set id for new schema set
echo "create set ( id = $SETID,origin=$masternode,comment='$COMMENT_DATE $COMMENT');" >>$ADDSHELL
#Add table to slony script
count=${#tableslist[@]}
for i in `seq $count`
do
pos=`expr $i - 1`
ID=`expr $TABLEID + $i`
con="set add table ( set id = $SETID,origin = $masternode,id=$ID,fully qualified name = '$SCHEMANAME.${tableslist[$pos]}',comment='$COMMENT_DATE $COMMENT');"
echo "$con" >>$ADDSHELL
done
#subscibe set to start replication of Slony-I set
for i in `seq $Cnode`
do
pos=`expr $i - 1`
echo "subscribe set (id=$SETID,provider=$masternode,receiver=${slavenode[$pos]});" >>$ADDSHELL
done
echo '_EOF_' >>$ADDSHELL
#excute slony script
echo "Now init new set..."
/bin/sh $ADDSHELL
if [ $? -eq 0 ];then
echo "All Done!"
else
echo "Please call manager!"
fi