-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMpostgres
More file actions
157 lines (136 loc) · 3.15 KB
/
Mpostgres
File metadata and controls
157 lines (136 loc) · 3.15 KB
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
#!/bin/sh
#
#description:
#Mpostgres This is the script to exec sql
#
#### BEGIN INFO
# Provides: maycap
# Required-Start: null
# Should-Start: null
# version:1.0
# date:2015-7-10
### END INFO
PGLIST=/home/postgres/bin/pg.list
PSQL=/web/pgsql/bin/psql
dbtag=0
function check_path()
{
if [ -f "$1" ];then
return 0
else
return 1
fi
}
function get_info(){
type=$1
case $type in
report|oms|std|share_center|default)
for tmpinfo in `grep ^$type $PGLIST`
do
hostinfo=${tmpinfo#*:}
host=`echo $hostinfo | awk -F - '{print $1}'`
port=`echo $hostinfo | awk -F - '{print $2}'`
dbname=`echo $hostinfo | awk -F - '{print $3}'`
$PSQL -U postgres -h $host -p $port -d $dbname -c "" 2>/dev/null 1>/dev/null
RC=$?
if [ $RC -eq 0 ];then
dblist[$dbtag]=${host}-${port}-${dbname}
dbtag=$((dbtag+1))
fi
done
;;
*)
for tmpinfo in `grep ^'zhuku' $PGLIST`
do
hostinfo=${tmpinfo#*:}
host=`echo $hostinfo | awk -F - '{print $1}'`
port=`echo $hostinfo | awk -F - '{print $2}'`
dbname=$type
$PSQL -U postgres -h $host -p $port -d $dbname -c "" 2>/dev/null 1>/dev/null
RC=$?
if [ $RC -eq 0 ];then
dblist[$dbtag]=${host}-${port}-${dbname}
dbtag=$((dbtag+1))
fi
done
;;
esac
}
if [ "$#" -eq 0 ];then
echo -e "\e[1;31mUsage:`basename $0` NOT Parameters!"
echo -e "\te.g. `basename $0` -p [uc] -f uc.sql\e[0m"
exit 1
else
if [ "${1##-}" = "$1" ];then
echo -e "\e[1;31mParameters ERROR!"
echo -e "\te.g. `basename $0` -p [uc] -f uc.sql\e[0m"
exit 1
fi
if [ "$1" = "-h" ] ;then
echo -e "\e[1;32mUsage:`basename $0` -p [uc] -f [uc.sql]"
echo -e "\t-p select dbname ,only and must!"
echo -e "\t-h request for help."
echo -e "\t-f postgresql file."
echo -e "\t-v look version.\e[0m"
exit 0
fi
if [ "$1" = "-v" ];then
echo -e "\e[1;32m \tversion 1.0"
echo -e "\tCreate by Maycap."
echo -e "\tThanks for used.\e[0m"
exit 0
fi
while [ "$1" != "${1##-}" ];do
case $1 in
-p)
get_info $2
if [ $dbtag -eq 0 ];then
echo -e "\e[1;32mDB $2 NOT FOUND!\e[0m"
exit 2
fi
dbname=$2
shift 2
;;
-f)
check_path $2
if [ $? -ne 0 ];then
echo -e "\e[1;32mFile $2 NOT FOUND!\e[0m"
exit 2
fi
sql=$2
shift 2
;;
*)
echo -e "\e[1;31mParameters ERROR!"
echo -e "\te.g. `basename $0` -p [uc] -f uc.sql\e[0m"
exit 3
;;
esac
done
fi
if [ -z $sql ];then
echo -e "\e[1;32mJust check $dbname list,not exec!\e[0m"
for((i=0;i<$dbtag;i++))
do
host=`echo ${dblist[$i]} | awk -F - '{print $1}'`
port=`echo ${dblist[$i]} | awk -F - '{print $2}'`
dbname=`echo ${dblist[$i]} | awk -F - '{print $3}'`
echo -e "\e[1;32m\tThere is ${dblist[$i]}.\e[0m"
done
exit 0
fi
for((i=0;i<$dbtag;i++))
do
host=`echo ${dblist[$i]} | awk -F - '{print $1}'`
port=`echo ${dblist[$i]} | awk -F - '{print $2}'`
dbname=`echo ${dblist[$i]} | awk -F - '{print $3}'`
echo -e "\e[1;32mNow exec sql on ${dblist[$i]}...\e[0m"
$PSQL -U postgres -h $host -p $port -d $dbname -f $sql
done
#cat pg.list
#report:cloud2-5433-report
#report:cloud2-5433-report_std1
#share_center:cloud2-5433-share_center
#std:cloud2-5433-std1
#oms:cloud2-5433-oms
#default:cloud2-5433-default