Decommission

 #!/bin/bash


# Set environment variables using oraenv

export ORAENV_ASK=NO

export ORACLE_SID=$1

. oraenv


# Helper function to execute SQL commands

run_sql() {

  sqlplus -s "/ as sysdba" <<EOF

  $1

EOF

}


# Helper function to execute DGMGRL commands and capture output

run_dgmgrl() {

  dgmgrl -silent <<EOF

  connect /

  $1

EOF

}


# Run DGMGRL show configuration and capture output

run_dgmgrl "show configuration;" > dr_info.txt


# Check if Data Guard is available

if grep -qE "ORA-16525|ORA-16596" dr_info.txt; then

  echo "Data Guard not available for $ORACLE_SID" >> dr_info.txt

  exit 1

fi


# Get the primary and standby database names

PRIMARY_DB=$(grep -E "Primary database" dr_info.txt | awk '{print $3}')

STANDBY_DB=$(grep -E "Standby database" dr_info.txt | awk '{print $3}')


# Ensure that we found the primary and standby databases

if [ -z "$PRIMARY_DB" ] || [ -z "$STANDBY_DB" ]; then

  echo "Failed to determine primary or standby database names." >> dr_info.txt

  exit 1

fi


# Set the standby database state to APPLY-OFF

run_dgmgrl "edit database '$STANDBY_DB' set state='APPLY-OFF';"

run_dgmgrl "remove configuration;"


# Stop the database

srvctl stop database -d $ORACLE_SID


# Create a PFILE from the SPFILE

run_sql "CREATE PFILE='/tmp/init${ORACLE_SID}.ora' FROM SPFILE;"


# Update parameters in PFILE

sed -i "s/cluster_database=true/cluster_database=false/; s/use_large_pages='ONLY'/use_large_pages=true/" /tmp/init${ORACLE_SID}.ora


# Apply updated PFILE and drop the database

run_sql "

  STARTUP MOUNT PFILE='/tmp/init${ORACLE_SID}.ora';

  ALTER SYSTEM SET cluster_database=FALSE SCOPE=SPFILE;

  ALTER SYSTEM SET use_large_pages=TRUE SCOPE=SPFILE;

  SHUTDOWN IMMEDIATE;

  STARTUP MOUNT RESTRICT;

  DROP DATABASE;

"


# Remove the database from SRVCTL

srvctl remove database -d $ORACLE_SID


# Clean up remaining files and directories

echo "Cleaning up remaining files..."

rm -rf $ORACLE_HOME/dbs/init${ORACLE_SID}.ora

rm -rf $ORACLE_BASE/diag/rdbms/$ORACLE_SID

rm -rf /u01/app/oracle/admin/$ORACLE_SID/adump


# Remove entries from listener.ora and oratab

cp $ORACLE_HOME/network/admin/listener.ora $ORACLE_HOME/network/admin/listener.ora.bkp.$(date +%F)

sed -i "/$ORACLE_SID/d" $ORACLE_HOME/network/admin/listener.ora


cp /etc/oratab /tmp/oratab.bkp.$(date +%F)

sed -i "/$ORACLE_SID/d" /etc/oratab


# Remove ASM directories

asmcmd <<EOF

cd +DATA

rm -rf $ORACLE_SID

cd +RECO

rm -rf $ORACLE_SID

EOF


echo "Database $ORACLE_SID has been decommissioned successfully."

Comments

Popular posts from this blog

SQL Loader V3

Db Unix

Standby redo log add