Database Startup Script-Oracle

vi db_start.sh

  1 #!/bin/bash
  2 ##################################
  3 ##Database startup script
  4 ##################################
  5 #
  6 #
  7 ##################################
  8 
  9 ##########################
 10 ## Script Usage check
 11 ##########################
 12 
 13 Script_Usage()
 14 {
 15 if [[ $# -ne 1 ]]; then
 16   echo "Usage: sh db_start.sh db_name"
 17   exit ;
 18 fi
 19 }
 20 
 21 ###############################
 22 ## Define globals
 23 ##############################
 24 
 25 Define_Globals()
 26 {
 27 export db_name=$1
 28 export log_dir=/u01/logs
 29 export LOG_FILE=$log_dir/db_start.log
 30 export script_dir=/u01/scripts/database
 31 export script_name=$0
 32 
 33 export UPPER_DB_NAME=`echo "${db_name}" | tr '[a-z]' '[A-Z]'`
 34 }
 35 
 36 ##########################################
 37 ## Logging into logfile
 38 ##########################################
 39 
 40 logit()
 41 {  
 42    if [ ! -d "$log_dir" ]; then
 43      if [ ! -f "$LOG_FILE" ]; then
 44        echo "Log directory $log_dir or Log file $LOG_FILE does not exist. Please create and re-execute."
 45        exit ;
 46      fi
 47    fi
 48    echo "[${USER}][`date`] - ${*}" >> ${LOG_FILE}
 49 }
 50 
 51 ############################################
 52 ## Check ORATAB file exists or not
 53 ############################################
 54 
 55 Check_oratab()
 56 {
 57 logit "Checking existence of ORATAB"
 58 export ORATAB=/etc/oratab
 59 if [ ! $ORATAB ] ; then
 60   echo "$ORATAB not found"
 61   logit "$ORATAB file not found on the server"
 62   exit ;
 63 else 
 64 echo "$ORATAB file found on the server, Continuing to next step...."
 65 logit "$ORATAB file found on the server, Continuing to next step...."
 66 fi
 67 }
 68 
 69 #############################################
 70 ## Check if the database exists in the ORATAB
 71 #############################################
 72 
 73 Check_database_oratab()
 74 {
 75 logit "checking existence of $db_name in ORATAB"
 76 export ORATAB=/etc/oratab
 77 export DB=`grep "^${db_name}:" /etc/oratab|cut -d: -f1 -s`
 78 export UPPER_DB=`echo "${DB}" | tr '[a-z]' '[A-Z]'`
 79 if [[ "$UPPER_DB_NAME" == "$UPPER_DB" ]] ; then
 80      echo "$DB database found in ORATAB, continuing to next step...."
 81      logit "$DB database found in ORATAB, continuing to next step...."
 82      export ORACLE_SID=$DB
 83      export ORACLE_HOME=`grep "^${DB}:" /etc/oratab|cut -d: -f2 -s`
 84      export PATH=$ORACLE_HOME/bin:$PATH
 85 else
 86 echo "$DB database not found in ORATAB"
 87 logit "$DB database not found in ORATAB"
 88 exit;
 89 fi
 90 }
 91 
 92 ##########################################
 93 ##Check if the database is already running
 94 ##########################################
 95 
 96 Check_database_Running()
 97 {
 98 logit "Check if the $db_name databse is already running"
 99 pslist="`ps -ef | grep pmon`"
100 for i in $DB ; do
101 echo  "$pslist" | grep  "ora_pmon_$i"  > /dev/null 2>&1
102 if (( $? )) ; then
103         echo "Oracle Instance: $i is Down, continuing to next step...."
104         logit "Oracle Instance: $i is Down, continuing to next step...."
105 else
106         echo "Oracle Instance: $i is already  UP and running, terminating the script execution"
107         logit "Oracle Instance: $i is already  UP and running terminating the script execution"
108         exit ;
109 fi
110 done
111 }
112 
113 ########################################
114 ## Starting Database
115 ########################################
116 
117 DB_Start()
118 {
119 logit "starting Database"
120 sqlplus '/as sysdba'<<ENDSQL >> ${LOG_FILE}
121 startup
122 show parameter db_name
123 select open_mode from v\$database ;
124 ENDSQL
125 logit "Database started"
126 logit "end of script: $script_name $db_name"
127 }
128 
129 ############
130 ## Main
131 ############
132 Script_Usage $*
133 Define_Globals $*
134 logit
135 Check_oratab
136 Check_database_oratab
137 Check_database_Running
138 DB_Start



Execution:
[oracle@localhost database]$ pwd
/u01/scripts/database

[oracle@localhost database]$ ./db_start.sh test

Log File:

view /u01/logs/db_start.log

[oracle][Sun May 17 21:33:26 EDT 2020] - 
[oracle][Sun May 17 21:33:26 EDT 2020] - Checking existence of ORATAB
[oracle][Sun May 17 21:33:26 EDT 2020] - /etc/oratab file found on the server, Continuing to next step....
[oracle][Sun May 17 21:33:26 EDT 2020] - checking existence of test in ORATAB
[oracle][Sun May 17 21:33:26 EDT 2020] - test database found in ORATAB, continuing to next step....
[oracle][Sun May 17 21:33:26 EDT 2020] - Check if the test databse is already running
[oracle][Sun May 17 21:33:26 EDT 2020] - Oracle Instance: test is Down, continuing to next step....

[oracle][Sun May 17 21:33:26 EDT 2020] - starting Database


SQL*Plus: Release 12.1.0.2.0 Production on Sun May 17 21:33:26 2020

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> ORACLE instance started.


Total System Global Area  289406976 bytes
Fixed Size                  2923536 bytes
Variable Size             230687728 bytes
Database Buffers           50331648 bytes
Redo Buffers                5464064 bytes
Database mounted.
Database opened.

SQL>
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_name                              string      test

SQL>
OPEN_MODE
--------------------
READ WRITE

SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

[oracle][Sun May 17 21:33:57 EDT 2020] - Database started
[oracle][Sun May 17 21:33:57 EDT 2020] - end of script: ./db_start.sh test
                                                                                                                                                                                                    

Comments

Popular posts from this blog

Oracle Database Client(12.1.0.2.0) Installation for Microsoft Windows(x64)

configuring goldengate

sample extract,pump and replicate(goldengate)