dbca.sh

#!/bin/sh
#
# dbca.sh
#
# NAME
#  dbca.sh - creates CDB database with no option in silent mode
#
# DESCRIPTION
#  creates CDB database without any database option using DBCA
#  generated scripts modified automatically
#
# OPTIONS
#  -dbname 
#  -cset 
#  -password 
#  -dgname 
#  -rgname 
#  -mempc 
#
# MODIFIED
#  Pierre Forstmann - 11-NOV-2015 - Creation
#
#
function usage ()
{
        echo "usage: dbca.sh"
        echo "-dbname "
        echo "-cset "
        echo "-password "
        echo "-dgname "
        echo "-rgname "
        echo "-mempc "
        exit 1
}
#
#
function init_args()
{
DBNAME=""
CSET=""
PASSWORD=""
DGNAME=""
RGNAME=""
MEMPC=""
}
#
#
function check_sid()
{
SID_LIST=$(cat /etc/oratab | grep -v '^#' | cut -f1 -d':')
#
FOUND=0
for S in $SID_LIST
do
        if [ $S = $DBNAME ]
        then
                FOUND=1
                break
        fi
done
if [ $FOUND = "1" ]
then
        echo "$DBNAME found in /etc/oratab."
        exit 1
fi
}
#
#
function check_args()
{
while [ $# -gt 0 ]
do
  case $1 in
  -dbname)
        if [ -z $2 ]
        then
                echo "missing database name value"
                usage
        fi
        DBNAME=$2
        shift
        check_sid
        ;;
  -cset)
        if [ -z $2 ]
        then
                echo "missing character set value"
                usage
        fi
        CSET=$2
        shift
        ;;
  -password)
        if [ -z $2 ]
        then
                echo "missing password value"
                usage
        fi
        PASSWORD=$2
        shift
        ;;
  -dgname)
        if [ -z $2 ]
        then
                echo "missing disk group name value"
                usage
        fi
        DGNAME=$2
        shift
        ;;
  -rgname)
        if [ -z $2 ]
        then
                echo "missing fast recovery area disk group name value"
                usage
        fi
        RGNAME=$2
        shift
        ;;
  -mempc)
        if [ -z $2 ]
        then
                echo "missing memory percentage value"
                usage
        fi
        MEMPC=$2
        shift
        ;;
  *) echo "unexpected argument: $1"
        usage;
        ;;
  esac
  shift
done
#
if [ -z $DBNAME ]
then
        echo "missing database name argument"
        usage
fi
if [ -z $CSET ]
then
        echo "missing character set argument"
        usage
fi
if [ -z $PASSWORD ]
then
        echo "missing password argument"
        usage
fi
if [ -z $DGNAME ]
then
        echo "missing datafile disk group name argument"
        usage
fi
if [ -z $RGNAME ]
then
        echo "missing fast recovery area disk group name argument"
        usage
fi
if [ -z $MEMPC ]
then
        echo "missing memory percentage argument"
        usage
fi
}
#
#
function check_env()
{
if [ -z $ORACLE_HOME ]
then
        echo "ORACLE_HOME must be set"
        exit 1
fi
if [ -z $ORACLE_BASE ]
then
        echo "ORACLE_BASE must be set"
        exit 1
fi
if [ -z $SQLPATH ]
then
        echo "SQLPATH must be set to use login.sql"
        exit 1
fi
if [ ! -w $SQLPATH/login.sql ]
then
        echo "$SQLPATH/login.sql must be writable"
        exit 1
fi
#
}
#
#
function run_dbca()
{
dbca -silent \
 -templateName New_Database.dbt \
 -generateScripts \
 -gdbName $DBNAME \
 -sid $DBNAME \
 -createAsContainerDatabase true \
 -SysPassword $PASSWORD \
 -SystemPassword $PASSWORD \
 -emConfiguration NONE \
 -storageType ASM \
 -diskGroupName $DGNAME \
 -characterSet $CSET \
 -memoryPercentage $MEMPC \
 -recoveryGroupName $RGNAME
}
#
#
function set_env()
{
export ORACLE_SID=$DBNAME
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/perl/bin:$PATH
DIR=$ORACLE_BASE/admin/${DBNAME}/scripts
#
rm -f ${DIR}/*.*
}
#
#
function modify_dbca_scripts()
{
# remove options in "DBNAME".sql
IDBSQL=${DIR}/${DBNAME}.sql
ODBSQL=${DIR}/${DBNAME}.sql.new
#
sed -e s,@${DIR}/JServer.sql,--, \
    -e s,@${DIR}/context.sql,--, \
    -e s,@${DIR}/ordinst.sql,--, \
    -e s,@${DIR}/interMedia.sql,--, \
    -e s,@${DIR}/cwmlite.sql,--, \
    -e s,@${DIR}/spatial.sql,--, \
    -e s,@${DIR}/labelSecurity.sql,--, \
    -e s,@${DIR}/apex.sql,--, \
    -e s,@${DIR}/datavault.sql,--,  $IDBSQL  > $ODBSQL
mv $IDBSQL ${IDBSQL}.old.1
mv $ODBSQL $IDBSQL
# remove Workspace Manager from CreateDBCatalog.sql
ICATSQL=$DIR/CreateDBCatalog.sql
OCATSQL=$DIR/CreateDBCatalog.sql.new
ADMIN=$ORACLE_HOME/rdbms/admin
CATCON=$ADMIN/catcon.pl
WM=owminst
PLB=$ADMIN/${WM}.plb
sed -e "s,host perl $CATCON -n 1 -l $DIR -b $WM $PLB;,," $ICATSQL > $OCATSQL
mv $ICATSQL ${ICATSQL}.old.1
mv $OCATSQL $ICATSQL
# remove password PROMPT in CreateDB.sql
sed "s,ACCEPT,--,g" $IDBSQL > $ODBSQL
mv $IDBSQL ${ODBSQL}.old.2
mv $ODBSQL $IDBSQL
# add SYS password to orapwd command in "DB_NAME".sql
sed -e "s,orapwd,orapwd password=$PASSWORD," $IDBSQL > $ODBSQL
mv $IDBSQL ${IDBSQL}.old.2
mv $ODBSQL $IDBSQL
# fix ASM SPFILE name in postDBCreation.sql
IPCSQL=$DIR/postDBCreation.sql
OPCSQL=$DIR/postDBCreation.sql.new
SPFILE=+${DGNAME}/${DBNAME}/spfile${DBNAME}.ora
sed -e "s,+${DGNAME},${SPFILE}," $IPCSQL > $OPCSQL
mv $IPCSQL ${IPCSQL}.old.1
mv $OPCSQL $IPCSQL
}
#
#
function modify_login_script()
{
SLOGSQL=${SQLPATH}/login.sql.$$
cp ${SQLPATH}/login.sql $SLOGSQL
# add SYS and SYSTEM passwords to $SQLPATH/login.sql
echo "define sysPassword=$PASSWORD" >> $SQLPATH/login.sql
echo "define systemPassword=$PASSWORD" >> $SQLPATH/login.sql
}
#
#
function end_ok()
{
echo ""
echo "$DBNAME successfully created.".
echo ""
}
#
# script main
#
if [ ! -z $TRACE_DBCA ]
then
        set -x
fi
# abort script if any command fails
set -e
#
check_env
init_args
check_args $@
set_env
run_dbca
modify_dbca_scripts
modify_login_script
# run the main DBCA shell script to create database
$DIR/${DBNAME}.sh
# restore login.sql
mv $SLOGSQL ${SQLPATH}/login.sql
if [ -w /etc/oratab ]
then
        echo "${DBNAME}:${ORACLE_HOME}:N # added by dbca.sh" >> /etc/oratab
else
        echo "add ${DBNAME}:${ORACLE_HOME}:N  to /etc/oratab"
fi
srvctl add database -db $DBNAME -oraclehome $ORACLE_HOME -spfile $SPFILE -diskgroup ${DGNAME},${RGNAME}
end_ok
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: