SHELL SCRIPT TO DROP DATABASE IN ORACLE

Shell script to drop database

#!/bin/bash -x

trap 'rm -rf /home/oracle/dbstatus' EXIT

#Set the environment variables
_SET_ENV_1()
{

export ORACLE_SID=testdb
export ORACLE_HOME=/u01/app/oracle/product/12.1.0/dbhome_1
export PATH=$PATH:$ORACLE_HOME/bin

}

#Drop the database
_DROP_DB_2()
{

select X in "Yes" "No"
   do
     case $X in
     Yes)
          dbstatus=/home/oracle/dbstatus
          echo -ne "Dropping $ORACLE_SID..."
          exit|sqlplus -S "/ as sysdba" << EOF > $dbstatus
          set feedback off
          set heading off
          select status from v\$instance;
EOF
x=$(cat $dbstatus|grep -v '^$'|head -1|awk '{print $1}')

   case $x in
      "STARTED")
      exit|sqlplus -S "/ as sysdba" << EOF
      alter database mount;
      alter database open;
      shu immediate;
      startup mount exclusive restrict;
      drop database;
EOF
break
   ;;
      "MOUNTED")
      exit|sqlplus -S "/ as sysdba" << EOF
      alter database open;
      shu immediate;
      startup mount exclusive restrict;
      drop database;
EOF
break
   ;;
      "OPEN"|"ORA-01081")
      exit|sqlplus -S "/ as sysdba" << EOF
      shu immediate;
      startup mount exclusive restrict;
      drop database;
EOF
break
   ;;
      *)
      exit|sqlplus -S "/ as sysdba" << EOF
      startup mount exclusive restrict;
      drop database;
EOF
break
   ;;
   esac
   ;;
   No)
   echo "Exiting script"
   exit 1
   break
   ;;
      esac
   done
}

_INVOKE_DBCA_3()
{
dbca -silent -deleteDatabase -sourceDB $ORACLE_SID -sysDBAPassword password -sysDBAUserName sys
}

_REM_ORATAB_ENTRY_4()
{
cat /etc/oratab > /tmp/oratab_`date +%F`.bak
sed '/'"$ORACLE_SID"'/d' /etc/oratab
}


_SET_ENV_1
_DROP_DB_2
_INVOKE_DBCA_3
_REM_ORATAB_ENTRY_4

Leave a Reply