-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdblib.sh
571 lines (516 loc) · 11.8 KB
/
dblib.sh
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# vim: ts=4:sw=4
if [ x"$plelib_banner" == x ]
then
echo "inclure plelib avant dblib"
exit 1
fi
. ~/plescripts/global.cfg
typeset -r SQL_PROMPT="prompt SQL>"
#*> Si PLELIB_OUTPUT == FILE alors sqlplus log sa sortie dans $PLELIB_LOG_FILE
#*> Initialise la variable SPOOL
function sqlplus_init_spool
{
# La variable SPOOL permet de loger la sortie de sqplus.
if [ "$PLELIB_OUTPUT" == FILE ]
then
SPOOL="spool $PLELIB_LOG_FILE append\n"
else
SPOOL=""
fi
}
#*> Call oraenv with ORACLE_SID=$1
function load_oraenv_for
{
ORACLE_SID=$(to_upper $1)
info "Load oracle environment for $ORACLE_SID"
ORAENV_ASK=NO . oraenv -s
LN
}
#*> $1 database name
#*> exit 1 if $1 not exists.
function exit_if_database_not_exists
{
if command_exists crsctl
then
srvctl status database -db $1 >/dev/null 2>&1
typeset ret=$?
else
ps -ef|grep -q [p]mon_$(to_upper $1)
typeset ret=$?
fi
if [ $ret -ne 0 ]
then
error "Database $1 not exists."
LN
exit 1
fi
}
#*> if variable ORACLE_SID not defined : exit 1
function exit_if_ORACLE_SID_not_defined
{
if [[ x"$ORACLE_SID" == x || "$ORACLE_SID" == NOSID ]]
then
error "$(hostname -s) : ORACLE_SID not define."
LN
exit 1
fi
}
#*> print yes to stdout if dataguarg configutation available, else no.
function dataguard_config_available
{
dgmgrl -silent sys/$oracle_password 'show configuration' >/dev/null 2>&1
[ $? -eq 0 ] && echo "yes" || echo "no"
}
#*> $1 database name
#*> print to stdout database role : primary or physical
function read_database_role
{
typeset -r dbn=$(to_lower $1)
to_lower $(dgmgrl -silent sys/Oracle12 'show configuration' |\
grep $dbn | cut -d- -f2 | awk '{ print $1 }')
}
#*> arrays physical_list & stby_server_list must be declared
function load_stby_database
{
typeset name
while read name
do
physical_list+=( $name )
done<<<"$(dgmgrl sys/$oracle_password 'show configuration' |\
grep "Physical standby" | awk '{ print $1 }')"
typeset stby_name
for stby_name in ${physical_list[*]}
do
stby_server_list+=($(tnsping $stby_name | tail -2 | head -1 |\
sed "s/.*(\s\?HOST\s\?=\s\?\(.*\)\s\?)\s\?(\s\?PORT.*/\1/"))
done
}
#*> $1 standby name
#*> return 0 if stby is disabled, else return 1
function stby_is_disabled
{
dgmgrl sys/$oracle_password 'show configuration'|grep -i "$1" | grep -q "(disabled)"
}
#*> print to stdout primary database name
function read_primary_name
{
dgmgrl sys/$oracle_password 'show configuration' |\
grep "Primary database" | awk '{ print $1 }'
}
#*> Utilisé avec sqlplus_cmd permet d'afficher un prompt avec le message "$@"
#*> Voir set_sql_cmd pour un exemple.
function set_sql_prompt
{
cat<<-WT
prompt
prompt $@
WT
}
#*> $@ contient une commande à exécuter.
#*> La fonction n'exécute pas la commande elle :
#*> - affiche le prompt SQL> suivi de la commande.
#*> - affiche sur la seconde ligne la commande.
#*>
#*> Utilisé avec les fonctions sqlplus_cmd[_with]
#*>
#*> Ex 1 : sqlplus_cmd "$(set_sql_cmd "alter database open;")"
#*>
#*> Ex 2 :
#*> function open_pdb # $1 pdb name
#*> {
#*> set_sql_prompt "Open PDB $1"
#*> set_sql_cmd "alter session set container=$1;"
#*> set_sql_cmd "alter pluggable database open;"
#*> }
#*> sqlplus_cmd "$(open_pdb pdb01)"
function set_sql_cmd
{
cat<<WT
prompt
$SQL_PROMPT $@;
$@
WT
}
#*> $1 chaine de connection
#*> Exécute les commandes "$@" avec sqlplus
#*> Affichage correct sur la sortie std et la log.
#*> return :
#*> 1 if EXEC_CMD_ACTION = NOP
#*> 0 if EXEC_CMD_ACTION = EXEC
function sqlplus_cmd_with
{
typeset connect_string="$1"
shift
if [ "$1" == as ]
then
typeset -r connect_string="$connect_string as $2"
shift 2
fi
sqlplus_init_spool
typeset -r db_cmd="$*"
fake_exec_cmd sqlplus -s "$connect_string"
if [ $? -eq 0 ]
then
echo -e "${SPOOL}set timin on\n$db_cmd\n" | \
sqlplus -s $connect_string
return $?
else
echo -e "${SPOOL}set timin on\n$db_cmd\n"
return 1
fi
}
#*> Exécute les commandes "$@" avec sqlplus en sysdba
#*> Affichage correct sur la sortie std et la log.
#*> return :
#*> 1 if EXEC_CMD_ACTION = NOP
#*> 0 if EXEC_CMD_ACTION = EXEC
function sqlplus_cmd
{
sqlplus_cmd_with "sys/$oracle_password as sysdba" "$@"
}
#*> Exécute les commandes "$@" avec sqlplus en sysasm
#*> Affichage correct sur la sortie std et la log.
#*> return :
#*> 1 if EXEC_CMD_ACTION = NOP
#*> 0 if EXEC_CMD_ACTION = EXEC
function sqlplus_asm_cmd
{
sqlplus_init_spool
fake_exec_cmd sqlplus -s / as sysasm
if [ $? -eq 0 ]
then
printf "${SPOOL}set echo off\nset timin on\n$@\n" | \
sqlplus -s / as sysasm
return 0
else
printf "${SPOOL}set echo off\nset timin on\n$@\n"
return 1
fi
}
#*> $1 connect string
#*> $2 sql query
#*>
#*> Objectif de la fonction :
#*> Exécute une requête, seul son résultat est affiché, la sortie peut être 'parsée'
#*> Par exemple obtenir la liste de tous les PDBs d'un CDB.
#*>
#*> N'inscrit rien dans la log.
function sqlplus_exec_query_with
{
typeset -r string_connection="$1"
typeset -r seq_query="$(double_symbol_percent "$2")"
printf "whenever sqlerror exit 1\nset term off echo off feed off heading off\n$seq_query" | \
sqlplus -s "$string_connection"
}
#*> $1 sql query
#*>
#*> call sqlplus_exec_query_with "sys/$oracle_password as sysdba" "$1"
function sqlplus_exec_query
{
sqlplus_exec_query_with "sys/$oracle_password as sysdba" "$1"
}
#*> Affiche tous les PDB RW de l'instance $1
#*> Les bases en RO sont considérées comme des SEED.
#*> $1 instance name
function get_rw_pdbs
{
typeset -r l_sql_read_pdb_rw=\
"select
c.name
from
gv\$containers c
inner join gv\$instance i
on c.inst_id = i.inst_id
where
i.instance_name = '$(to_upper $1)'
and c.name not in ( 'PDB\$SEED', 'CDB\$ROOT' )
and c.open_mode = 'READ WRITE';
"
sqlplus_exec_query "$l_sql_read_pdb_rw"
}
#*> $1 database parameter
function orcl_parameter_value
{
typeset opv_query=\
" select
p.display_value
from
v\$parameter p
where
p.name = '$1'
;
"
sqlplus_exec_query "$opv_query" | xargs
}
#*> Objectif de la fonction :
#*> Exécute une requête dont le but n'est que l'affichage d'un résultat.
#*> Affiche la requête exécutée.
function sqlplus_print_query
{
typeset -r seq_query="$(double_symbol_percent "$1")"
sqlplus_init_spool
fake_exec_cmd "sqlplus -s sys/$oracle_password as sysdba"
printf "${SPOOL}whenever sqlerror exit 1\n$seq_query" | \
sqlplus -s sys/$oracle_password as sysdba
LN
}
#*> $1 db name
#*> $2 service name
#*>
#*> return 1 if db name or service name not exists, else return 0
#*>
#*> Si le crs n'est pas utilisé et que le pdb est fermé return 1
function service_exists
{
if command_exists crsctl
then
if grep -qE "^PRCR-1001"<<<"$(srvctl config service -db $1 -service $2)"
then
return 1
else
return 0
fi
else # ne test pas la base $1
# $1 service name
function sql
{
set_sql_cmd "set term off echo off feed off tim off heading off;"
set_sql_cmd "select count(*) from cdb_services where name = lower( '$1' );"
}
typeset -r ok="$(sqlplus_exec_query "$(sql $2)"|tail -1|tr -d [:space:])"
[ "$ok" == "1" ] && return 0 || return 1
fi
}
#*> $1 db name
#*> $2 service name
#*>
#*> exit 1 if service name not running else return 0
function exit_if_service_not_exists
{
typeset -r db_name_l="$1"
typeset -r service_name_l="$2"
info -n "Database $db_name_l, service $service_name_l exists "
if service_exists $db_name_l $service_name_l
then
info -f "$OK"
LN
else
info -f "$KO"
LN
exit 1
fi
}
#*> $1 db name
#*> $2 service name
#*>
#*> return 0 if service running else return 1
function service_running
{
if command_exists crsctl
then
typeset -r db_name_l=$1
typeset -r service_name_l=$(to_lower $2)
grep -iqE "Service $service_name_l is running.*"<<<"$(LANG=C srvctl status service -db $db_name_l -s $service_name_l)"
else
grep -qi "Service \"$2\" has .*"<<<"$(lsnrctl status)"
fi
}
#*> $1 db name
#*> $2 service name
#*>
#*> exit 1 if service name not running else return 0
function exit_if_service_not_running
{
typeset -r db_name_l="$1"
typeset -r service_name_l="$2"
info -n "Database $db_name_l, service $service_name_l running "
if service_running $db_name_l $service_name_l
then
info -f "$OK"
LN
else
info -f "$KO"
LN
exit 1
fi
}
#*> $1 pdb name
#*>
#*> return associate oci service name
function mk_oci_service
{
echo $(to_lower "$1")_oci
}
#*> $1 pdb name
#*>
#*> return associate oci service name for stby
function mk_oci_stby_service
{
echo $(to_lower "$1")_stby_oci
}
#*> $1 pdb name
#*>
#*> return associate java service name
function mk_java_service
{
echo $(to_lower "$1")_java
}
#*> $1 pdb name
#*>
#*> return associate java service name
function mk_java_stby_service
{
echo $(to_lower "$1")_stby_java
}
#*> print to stdout Oracle SW Version :
#*> 12.1.0.2, 12.2.0.1, 18.0.0.0
function read_orcl_version
{
$ORACLE_HOME/OPatch/opatch lsinventory |\
grep -E "Oracle Database [0-9][0-9]." |\
awk '{ print $4 }' |\
cut -d. -f1-4
}
#*> print to stdout Oracle SW Version :
#*> 12.1, 12.2, 18.0
function read_orcl_release
{
case "$(read_orcl_version)" in
12.1.*)
echo 12.1
;;
12.2.*)
echo 12.2
;;
18.0*)
echo 18.0
;;
*)
echo "Unknow release"
esac
}
#*> $1 db name
#*> return 0 if RAC One Node, else 1
function is_rac_one_node
{
srvctl status database -db $1|grep -q "Online relocation: INACTIVE"
}
#*> return 0 for Enterprise Edition
#*> return 1 for Standard Edition
function is_oracle_enterprise_edition
{
sqlplus -s sys/$oracle_password as sysdba \
<<<"select banner from v\$version where banner like '%Edition Release%';" \
|grep -q "Enterprise"
}
#*> $1 pdb name
#*> Print to stdout yes or no
function is_application_seed
{
typeset -r pdbseed_name=$(to_upper $1)
typeset -r query=\
"select
application_pdb
from
v\$containers
where
name='$pdbseed_name\$SEED'
;"
typeset val=$(sqlplus_exec_query "$query")
[ x"$val" == x ] && echo no || echo yes
}
#*> return 0 if PDB $1 is refreshable, else return 1
function refreshable_pdb
{
typeset -r lpdb=$(to_upper $1)
typeset -r query=\
"select
refresh_mode
from
cdb_pdbs
where
pdb_name='$lpdb'
;"
[ "$(sqlplus_exec_query "$query"|tail -1)" == NONE ] && return 1 || return 0
}
#*> return 0 if PDB $1 exists, else return 1
function pdb_exists
{
typeset -r lpdb=$(to_upper $1)
typeset -r query="select name from v\$pdbs where name='$lpdb';"
[ "$(sqlplus_exec_query "$query"|tail -1)" == $lpdb ] && return 0 || return 1
}
#*> $1 dblink name
#*> return 0 if dblink $1 exists, else return 1.
function dblink_exists
{
typeset -r query="select count(*) from cdb_db_links where db_link = upper( '$1' );"
[ "$(sqlplus_exec_query "$query"|tail -1|tr -d [:space:])" == "1" ] && return 0 || return 1
}
#*> $1 dblink_name
#*> $2 user
#*> $3 password
#*> $4 tns alias
#*>
#*> Print to stdout ddl statement to create a db link.
function ddl_create_dblink
{
set_sql_cmd "create database link $1 connect to $2 identified by $3 using '$4';"
}
#*> $1 db link name
#*> exit 1 if db link test failed. Test : select 1 from dual@$1;
function exit_if_test_dblink_failed
{
info -n "Test database link $1 : select 1 from dual@$1; "
if [ "$(sqlplus_exec_query "select 1 from dual@$1;" | tail -1 | tr -d [:space:])" == 1 ]
then
info -f "[$OK]"
LN
else
info -f "[$KO]"
exit 1
fi
}
#*> $1 tns alias
#*> exit 1 if tnsping $1 failed.
function exit_if_tnsping_failed
{
info -n "tnsping $1 "
if ! tnsping $1 >/dev/null 2>&1
then
info -f "[$KO]"
LN
exit 1
else
info -f "[$OK]"
LN
fi
}
#*> $1 connect string
#*> $2 username
#*> return 0 if $1 exists, else return 1
function db_username_exists
{
typeset connect_string="$1"
shift
if [ "$1" == as ]
then
typeset -r connect_string="$connect_string as $2"
shift 2
fi
typeset -r username=$1
typeset -r query=\
"select
count(*)
from
dba_users
where
username=upper( '$username' )
;"
if [ "$(sqlplus_exec_query_with "$connect_string" "$query" | tail -1 | tr -d [:space:])" == 1 ]
then
return 0
else
return 1
fi
}