-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgodb
executable file
·297 lines (266 loc) · 9.96 KB
/
godb
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
#!/bin/bash
### This script sets the oracle environment variables, based upon the configuration in the Grid Infrastructure.
### It can be used on every (linux) setup where the GI is installed (rac, rac one node, oracle restart).
### To use it you source the script and pass it the db unique name of the database for which you want the env variables to be set (. godb <db_unique_name>)
### The script expects the oracle local registry to be /etc/oracle/olr.loc. If not change the _OLRLOC variable in the beginning of the script
###
### It should not be used to setup the env variables for the GI infrastructure itself.
### Use the gogrid script for this instead
###
###
### This script replaces script from Oracle by allowing you to pass the db unique name, while it also sets the ORACLE_SID to the actual instance name (instead of the db unique name)
### Making it especially usefull for Oracle RAC and Oracle RAC One Node, but it also can be used in Oracle Restart environments or for single instance databases on RAC environments
###
###
### The environment variable set are:
### - ORA_DBNAME: Oracle database name
### - ORACLE_UNQNAME: The oracle db unique name
### - ORACLE_SID: The oracle instance name on this node
### - ORACLE_BASE: The oracle db base directory for the given db
### - ORACLE_HOME: The oracle home directory for the given db
### - PATH: Removes the existing $ORACLE_HOME/bin from the PATH and adds the new $ORACLE_HOME/bin to it
### - LD_LIBRARY_PATH: Shared library path
###
###
### Exit codes:
### 0: OK
### 1: Failure
### could be that the local registry does not exist, something went wrong with the srvctl command, the passed unique name does not exist
### or the database has no instance configured for the current node
### 2: Warning
### The instance is not running on this node or in case of RAC One Node is running on the other node (or not running on any node)
###
###
### location of the local registry
_OLRLOC=/etc/oracle/olr.loc
rval=0
### abuse a while loop to avoid a complicated if then construct when testing for errors, as exit can not be used in a sourced script (would kill the current shell)
while true
do
### Use the db unique name given as argument
if [ $# -ne 1 ]
then
echo "usage: godb <db_unique_name>"
rval=1
break
fi
### set _DB_UNIQUE to the passed argument
_DB_UNIQUE=$1
### store the ORACLE_HOME that is currently set in the environment, we will need to remove it from the PATH variable later on
OLDHOME=${ORACLE_HOME}
### clean up existing env
unset ORA_DBNAME
unset ORACLE_UNQNAME
unset ORACLE_SID
### test if the local registry exists and is readable
if [[ ! -r "${_OLRLOC}" ]]
then
echo "local registry file: ${_ORALOC} does not exists or is not readable by you"
rval=1
break
fi
### set the CRS_HOME variable
CRS_HOME=`awk -F'=' '/^crs_home=/ {print $2}' ${_OLRLOC}`
### test if grid srvctl exists and is executable
if [[ ! -x "${CRS_HOME}/bin/srvctl" ]]
then
echo "grid srvctl could not be found or is not executable by you"
rval=1
break
fi
### test if the passed db unique name is known in the cluster registry and get the DB Home
ORACLE_HOME=$(${CRS_HOME}/bin/srvctl config database -verbose | grep ${_DB_UNIQUE} | cut -f 2)
if [ -z ${ORACLE_HOME} ]
then
echo "Passed DB Unique Name is not known in the cluster registry"
rval=1
break
fi
### test if db srvctl exists and is executable
if [[ ! -x "${ORACLE_HOME}/bin/srvctl" ]]
then
echo "db srvctl could not be found or is not executable by you"
rval=1
break
fi
### run srvctl and parse the output for the Home, the unique name, the db name, the instance name and the type
### instance name will only be filled in when this is an single instance on RAC or Oracle Restart
### db name can be null if the db name is not different from the db unique name
eval `ORACLE_HOME=${ORACLE_HOME} ${ORACLE_HOME}/bin/srvctl config database -d ${_DB_UNIQUE} |
awk -F':' '
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
/Database unique name:/ {
$2=trim($2);
print "ORACLE_UNQNAME="$2" ;"
}
/Database name:/ {
$2=trim($2);
print "ORA_DBNAME="$2" ;"
}
/Database instance:/ {
$2=trim($2);
print "ORA_INSTANCE="$2" ;"
}
/Type:/ {
$2=trim($2);
print "ORA_TYPE="$2" ;"
}
/Instance name prefix:/ {
$2=trim($2);
print "ORA_INSTANCE="$2" ;"
}
/Candidate servers:/ {
$2=trim($2);
print "ORA_SERVER_LIST="$2" ;"
}
'
`
### the ORACLE_UNQNAME should always be filled in, if not, something went wrong in the srvctl command or in the parsing
if [ "${ORACLE_UNQNAME}" == "" ]
then
echo "Something went wrong in executing srvctl, or the output was not parsed correctly"
rval=1
break
fi
### set the ORACLE_HOME, LD_LIBRARY_PATH and PATH variables
### don't use the oraenv script as the oratab file is no longer maintained by the ora_agent in 12.2
export ORACLE_HOME
###Reset LD_LIBRARY_PATH
case ${LD_LIBRARY_PATH:-""} in
*$OLDHOME/lib*) LD_LIBRARY_PATH=`echo $LD_LIBRARY_PATH | \
sed "s;$OLDHOME/lib;$ORACLE_HOME/lib;g"` ;;
*$ORACLE_HOME/lib*) ;;
"") LD_LIBRARY_PATH=$ORACLE_HOME/lib ;;
*) LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH ;;
esac
export LD_LIBRARY_PATH
case "$OLDHOME" in
"") OLDHOME=$PATH ;; #This makes it so that null OLDHOME can't match
esac #anything in next case statement
case "$PATH" in
*$OLDHOME/bin*) PATH=`echo $PATH | \
sed "s;$OLDHOME/bin;$ORACLE_HOME/bin;g"` ;;
*$ORACLE_HOME/bin*) ;;
*:) PATH=${PATH}$ORACLE_HOME/bin: ;;
"") PATH=$ORACLE_HOME/bin ;;
*) PATH=$PATH:$ORACLE_HOME/bin ;;
esac
export PATH
### set the ORACLE_BASE (unset it first otherwise the orabase utility will just return the current value)
unset ORACLE_BASE
ORACLE_BASE=`$ORACLE_HOME/bin/orabase`
export ORACLE_BASE
### The db name is not mandatory in the GI config if it does not differ from the unique name
### So, when it is empty, set it to the ORACLE_UNQNAME
if [ "${ORA_DBNAME}" == "" ]
then
ORA_DBNAME=${ORACLE_UNQNAME}
fi
### Test for the type of db (rac, rac one or single instance)
if [ "${ORA_TYPE}" == "" ]
then
### Oracle Restart installation
CRS_RESULT=$(${ORACLE_HOME}/bin/srvctl status database -d ${_DB_UNIQUE})
if [ "${CRS_RESULT}" == "Database is running." ]
then
ORACLE_SID=${ORA_INSTANCE}
elif [ "${CRS_RESULT}" == "Database is not running." ]
then
ORACLE_SID=${ORA_INSTANCE}
### set a non zero rval, but continue
rval=2
else
### should never get here, because of earlier check on oracle_unqname result
echo "DB ${_DB_UNIQUE} is not defined for this node"
rval=1
break
fi
elif [ "${ORA_TYPE}" == "SINGLE" ]
then
CRS_RESULT=$(${ORACLE_HOME}/bin/srvctl status database -d ${_DB_UNIQUE})
### single instance on RAC
CRS_RESULT=$(echo ${CRS_RESULT} | grep $(hostname -s))
ORACLE_SID=`echo ${CRS_RESULT} | cut -d' ' -f2`
### check if this node is defined as target for this database
if [ "${ORACLE_SID}" = "" ]
then
echo "No instance for DB ${_DB_UNIQUE} is defined for this node"
rval=1
break
fi
### check if the instance is actually running
if [[ "${CRS_RESULT}" =~ "is not running" ]]
then
### set a non zero rval, but continue
rval=2
fi
elif [ "${ORA_TYPE}" == "RAC" ]
then
### Oracle rac or rac OneNode
### get the db instance status for the current node
CRS_RESULT=`${ORACLE_HOME}/bin/srvctl status database -d ${_DB_UNIQUE} | grep \`hostname -s\``
ORACLE_SID=`echo ${CRS_RESULT} | cut -d' ' -f2`
### check if this node is defined as target for this database
if [ "${ORACLE_SID}" = "" ]
then
echo "No instance for DB ${_DB_UNIQUE} is defined for this node"
rval=1
break
fi
### check if the instance is actually running
if [[ "${CRS_RESULT}" =~ "is not running" ]]
then
### set a non zero rval, but continue
rval=2
fi
elif [ "${ORA_TYPE}" == "RACOneNode" ]
then
CRS_RESULT=`${ORACLE_HOME}/bin/srvctl status database -d ${_DB_UNIQUE} | head -1`
if [ "${CRS_RESULT}" = "Database is not running." ]
then
### database is no running on any node, set the SID to the default <instance>_1
ORACLE_SID=${ORA_INSTANCE}_1
rval=2
else
ORACLE_SID=`echo ${CRS_RESULT} |grep \`hostname -s\` | cut -d' ' -f2`
if [ "${ORACLE_SID}" == "" ]
then
if [[ ${ORA_SERVER_LIST} =~ $(hostname -s) ]]
then
echo "${_DB_UNIQUE} is running on a different node"
rval=2
break
else
echo "${_DB_UNIQUE} is not configured for this node"
rval=1
break
fi
fi
fi
else
### unknown GI type, don't know what to do
echo "Type: ${ORA_TYPE} is unknown to this script and it does not know what to do now"
rval=1
break
fi
### check if ORACLE_SID has been filled in
if [ "${ORACLE_SID}" == "" ] && [ "${ORA_TYPE}" != "RACOneNode" ]
then
echo "ORACLE_SID has no value, something went wrong during parsing"
rval=1
break
fi
### export the set oracle variables
export ORACLE_UNQNAME ORA_DBNAME ORACLE_SID
break
done
### unset some temporary variables
unset ORA_INSTANCE
unset ORA_TYPE
unset _ORAENV
unset DB_CNT
unset _DB_UNIQUE
### return rval
return ${rval}