-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate-oracle-databases
More file actions
executable file
·179 lines (152 loc) · 3.84 KB
/
create-oracle-databases
File metadata and controls
executable file
·179 lines (152 loc) · 3.84 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/bash -eu
# create-oracle-databases: create Oracle databases
# Copyright (C) 2020 "Michael G. Morey" <mgmorey@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
abort() {
printf "$@" >&2
exit 1
}
assert() {
"$@" || abort '%s: Assertion failed: %s\n' "$0" "$*"
}
check_dirs() {
for dir in ${ORACLE_BASE-} ${ORACLE_HOME-}; do
if [ ! -d $dir ]; then
abort '%s: %s: No such directory\n' "$0" "$dir"
fi
done
}
cat_init() {
assert [ $# -eq 2 ]
assert [ -n "$1" ]
assert [ -n "$2" ]
cat <<EOF
DB_NAME=$1
DB_CREATE_FILE_DEST=$2/dbs
EOF
}
cat_sql_create() {
assert [ $# -eq 2 ]
assert [ -n "$1" ]
assert [ -n "$2" ]
cat <<EOF
CONNECT / AS SYSDBA;
CREATE SPFILE FROM PFILE;
STARTUP NOMOUNT;
CREATE DATABASE $1
USER SYS IDENTIFIED BY "$2"
USER SYSTEM IDENTIFIED BY "$2";
EXIT;
EOF
}
cat_sql_sysdba() {
cat <<EOF
CONNECT / AS SYSDBA;
@?/rdbms/admin/catalog.sql
@?/rdbms/admin/catproc.sql
@?/rdbms/admin/utlrp.sql
EXIT;
EOF
}
cat_sql_system() {
cat <<EOF
CONNECT /;
@?/sqlplus/admin/pupbld.sql
EXIT;
EOF
}
cat_tns() {
assert [ $# -eq 1 ]
assert [ -n "$1" ]
cat <<EOF
$1=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = $1)
)
)
EOF
}
create_database() {
assert [ $# -eq 1 ]
assert [ -n "$1" ]
export ORACLE_SID=$(get_system_id "$1")
db_name=$(get_db_name "$ORACLE_SID")
initfile=$(get_initfile "$ORACLE_SID" "$ORACLE_HOME")
logfile=$(get_logfile "$db_name" "$HOME")
tnsfile=$(get_tnsfile "${TNS_ADMIN-$ORACLE_HOME/network/admin}")
if [ -f "$initfile" ]; then
print_warn "Parameter file $initfile already exists"
return 0
fi
print_info "Creating database $db_name SID $ORACLE_SID"
cat_init $db_name $ORACLE_HOME >$initfile
cat_sql_create $db_name "$ORACLE_PWD" | sqlplus /nolog | \
tee $logfile | filter_sqlplus
if grep -q "^Database created." $logfile; then
print_info "Database $db_name created"
print_info "Building data dictionary views"
cat_sql_sysdba | sqlplus /nolog >>$logfile
cat_sql_system | sqlplus /nolog >>$logfile
print_info "Registering SID in $(basename $tnsfile)"
cat_tns $ORACLE_SID >>"$tnsfile"
print_info "Compressing $(basename $logfile)"
gzip -9f $logfile || true
status=0
else
print_error "Database $db_name creation failed"
print_error "Check $logfile for errors"
status=1
fi
return $status
}
create_databases() {
: ${ORACLE_BASE?}
: ${ORACLE_HOME?}
: ${ORACLE_PWD?}
unset TWO_TASK
check_dirs
for database; do
create_database "$database"
done
}
get_initfile() {
assert [ $# -eq 2 ]
assert [ -n "$1" ]
assert [ -n "$2" ]
printf '%s/dbs/init%s.ora\n' "$2" "$1"
}
get_logfile() {
assert [ $# -eq 2 ]
assert [ -n "$1" ]
assert [ -n "$2" ]
printf '%s/create-%s.log\n' "$2" "$1"
}
get_tnsfile() {
assert [ $# -eq 1 ]
assert [ -n "$1" ]
printf '%s/tnsnames.ora\n' "$1"
}
parse_arguments() {
databases=
if [ $# -gt 0 ]; then
databases="$@"
fi
}
script_basename=$(basename "$0")
script_dirname=$(realpath "$(dirname "$0")")
. "$script_dirname/oracle-config.sh"
. "$script_dirname/oracle-library.sh"
parse_arguments "$@"
create_databases ${databases:-$DATABASES}