-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiupgrade.ksh
258 lines (217 loc) · 5.54 KB
/
iupgrade.ksh
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
#!/bin/ksh
#
# Mason Hua 2015/09/16 V0.1
#
# Usage: $0 -i inst1,inst2,...
# -b <the code path you use to upgrade the instances>
# -l <license file with full path>
#
# Must run it as root
#
export OS=`uname -s | tr [a-z] [A-Z]`
if [ `echo "$0" | grep -c '^\./'` -eq 1 ]; then
# use it by this way: ./istop.ksh
WPATH=`pwd`
PROGM=${0#./}
else
# use it with full path
WPATH=${0%/*}
PROGM=${0##*/}
fi
# Functions
Usage ( )
{
echo " "
echo "Run it as root "
echo "Usage: $0 -i <instances, use comma(,) to separate each instance>
-b <the code path you use to upgrade the instances>
-l <license file with full path>
Mandatory parameters:
-i, -b
Note: Make sure every instance have access to the work directory
"
echo " "
exit 1
}
# stop_instance
stop_instance ( ) {
if [ "$INST" == "" ]; then
INST=$USER
fi
echo "Going to stop instance $INST ..."
if [ `ps -ef | grep db2sys | grep -i -w $INST | grep -cv grep` -ge 1 ]; then
echo "stopping Instances $INST"
if [[ $OS == "AIX" ]]; then
db2 force application all
db2 force application all
for db in `db2 list db directory | grep -p 'Indirect' | grep 'Database alias' | awk -F'=' '{print $2}'`
do
db2 deactivate db $db
done
db2stop force && ipclean
else
db2 force application all
db2 force application all
for db in `db2 list db directory | grep -B 5 'Indirect' | grep 'Database alias' | awk -F'=' '{print $2}'`
do
db2 deactivate db $db
done
db2stop force && ipclean
fi
else
echo "No db2 instance running in current id: $INST"
fi
echo " "
echo "Check if db2sys process exists ..."
ps -ef | grep db2sys | grep $USER | grep -v grep
exit 0
}
# end of stop instance
# start instance
start_instance() {
if [ "$INST" == "" ]; then
INST=$USER
fi
echo "Going to start instance $INST ..."
if [ `ps -ef | grep db2sys | grep -i -w $INST | grep -cv grep` -lt 1 ]; then
db2start
else
echo "Instance $INST is already running, no need to start it ..."
fi
echo " "
echo "db2sys processes ..."
ps -ef | grep db2sys | grep $USER | grep -v grep
exit 0
}
# end of start instance
# upgrade instance
upgrade_instance () {
if [ "$INST" == "" ]; then
echo "-i <instances> is mandatory when calling upgrade instance"
Usage
fi
if [ "$CPATH" == "" ]; then
echo "-b <code path> is mandatory when calling upgrade instance"
Usage
fi
echo "upgrading instance: $INST"
if [[ -f $CPATH/instance/db2iupdt ]]; then
echo "$CPATH/instance/db2iupgrade -k $INST"
$CPATH/instance/db2iupgrade -k $INST
else
echo "db2iupgrade is not exist on $CPATH/instance..."
exit 1
fi
echo "end of upgrade instance: $INST"
}
# end of upgrade_instance
# upgrade database
upgrade_database () {
echo "Upgrading databases in instance $USER"
if [[ $OS == "AIX" ]]; then
if [ `db2 list db directory | grep -p 'Indirect' | grep -c 'Database name'` -eq 0 ]; then
echo "No database in this instance $USER"
echo "No upgrade database is needed"
exit 0
fi
else
if [ `db2 list db directory | grep -B 5 'Indirect' | grep -c 'Database name'` -eq 0 ]; then
echo "No database in this instance $USER"
echo "No upgrade database is needed"
exit 0
fi
fi
if [[ $OS == "AIX" ]]; then
for db in `db2 list db directory | grep -p 'Indirect' | grep 'Database name' | sort -u | awk -F'=' '{print $2}'`
do
echo "db2 upgrade db $db"
db2 upgrade db $db
done
else
for db in `db2 list db directory | grep -B 5 'Indirect' | grep 'Database name' | sort -u | awk -F'=' '{print $2}'`
do
echo "db2 upgrade db $db"
db2 upgrade db $db
done
fi
exit 0
}
# end of upgrade database
# apply license
apply_license () {
if [ -f "$LFILE" ]; then
echo "apply license"
echo "$CPATH/adm/db2licm -a $LFILE"
$CPATH/adm/db2licm -a $LFILE
fi
}
# end of apply_license
# End of functions
# Main function
OPTIND=1
while getopts ":t:b:i:l:" opt
do
case ${opt} in
t ) OPER=${OPTARG} ;;
b ) CPATH=${OPTARG} ;;
i ) INSTS=${OPTARG} ;;
l ) LFILE=${OPTARG} ;;
esac
done
case $OPER in
stop )
stop_instance ;;
start )
start_instance ;;
upgrade_instance )
upgrade_instance ;;
upgrade_database )
upgrade_database ;;
apply_license )
apply_license ;;
esac
# if run it with root, make all instances have access to this script
if [ `id -u` -eq 0 ]; then
chmod 755 $WPATH/$PROGM >/dev/null 2>&1
else
Usage
exit 1
fi
# check parameters
if [ "$CPATH" == "" ]; then
echo "-b is mandatory for updating instance"
Usage
fi
if [ "$LFILE" == "" ]; then
echo "No license file provided, will not apply license"
echo "Pls apply it manually if needed"
fi
if [ "$INSTS" == "" ]; then
echo "-i is mandatory for updating instance"
Usage
fi
# End of check parameters
# main function
INSTS=${INSTS},
count=2
INST=`echo "$INSTS" | cut -d, -f 1`
while [ "$INST" != "" ]
do
INST=`echo $INST | tr [A-Z] [a-z]`
id $INST
if [ $? -ne 0 ]; then
echo "Instance $INST is not exists, please check it!!!"
else
# stop instances before update instance
su - $INST -c "$WPATH/$PROGM -t stop -i $INST"
upgrade_instance
su - $INST -c "$WPATH/$PROGM -t start -i $INST"
su - $INST -c "$WPATH/$PROGM -t upgrade_database -i $INST"
fi
INST=`echo "$INSTS" | cut -d, -f $count`
(( count=$count + 1 ))
done
apply_license
exit 0
# End of main function
# End of istart