Decommission v1
#!/bin/bash
# Check if ORACLE_SID is provided
if [ -z "$1" ]; then
echo "Error: ORACLE_SID not provided."
echo "Usage: $0 <ORACLE_SID>"
exit 1
fi
# Set environment variables using oraenv
export ORAENV_ASK=NO
export ORACLE_SID=$1
oraenv
LOG_FILE="decommission_$ORACLE_SID.log"
log() {
local message=$1
echo "$(date +'%Y-%m-%d %H:%M:%S') $message" | tee -a $LOG_FILE
}
log "Decommissioning process started for ORACLE_SID: $ORACLE_SID"
db_unique_name=$(sqlplus -s / as sysdba <<EOF
set heading off
set feedback off
set pagesize 0
select db_unique_name from v\$database;
exit;
EOF
)
db_unique_name=$(echo $db_unique_name | xargs) # trim spaces
log "Retrieved database unique name: $db_unique_name"
# Helper function to execute SQL commands
run_sql() {
sqlplus -s "/ as sysdba" <<EOF
$1
exit;
EOF
}
# Helper function to execute DGMGRL commands and capture output
run_dgmgrl() {
dgmgrl -silent <<EOF
connect /
$1
EOF
}
log "Capturing Data Guard configuration using DGMGRL"
run_dgmgrl "show configuration;" 2>&1 | tee -a $LOG_FILE
# Check if Data Guard is available
if grep -qE "ORA-16525|ORA-16596" $LOG_FILE; then
log "Data Guard is not available for ORACLE_SID: $ORACLE_SID"
exit 1
fi
log "Stopping the database: $db_unique_name"
# Stop the database
srvctl stop database -d $db_unique_name 2>&1 | tee -a $LOG_FILE
log "Creating PFILE from SPFILE"
# Create a PFILE from the SPFILE
run_sql "CREATE PFILE='/tmp/init${ORACLE_SID}.ora' FROM SPFILE;" 2>&1 | tee -a $LOG_FILE
log "Executing database drop sequence"
run_sql "
ALTER SYSTEM SET cluster_database=FALSE SCOPE=SPFILE sid='+';
STARTUP MOUNT;
ALTER SYSTEM SET use_large_pages=TRUE SCOPE=SPFILE sid='*';
SHUTDOWN IMMEDIATE;
STARTUP MOUNT EXCLUSIVE RESTRICT;
DROP DATABASE;
" 2>&1 | tee -a $LOG_FILE
log "Removing the database from SRVCTL configuration"
# Remove the database from SRVCTL
srvctl remove database -d $db_unique_name -f 2>&1 | tee -a $LOG_FILE
log "Cleaning up remaining files and directories"
rm -rf $ORACLE_HOME/dbs/init${ORACLE_SID}.ora 2>&1 | tee -a $LOG_FILE
rm -rf $ORACLE_BASE/diag/rdbms/$ORACLE_SID 2>&1 | tee -a $LOG_FILE
rm -rf /u01/app/oracle/admin/$ORACLE_SID/adump 2>&1 | tee -a $LOG_FILE
log "Updating listener.ora and oratab to remove database entries"
cp $ORACLE_HOME/network/admin/listener.ora $ORACLE_HOME/network/admin/listener.ora.bkp.$(date +%F) 2>&1 | tee -a $LOG_FILE
cp $ORACLE_HOME/network/admin/tnsnames.ora $ORACLE_HOME/network/admin/tnsnames.ora.bkp.$(date +%F) 2>&1 | tee -a $LOG_FILE
sed -i "/$ORACLE_SID/d" $ORACLE_HOME/network/admin/listener.ora 2>&1 | tee -a $LOG_FILE
cp /etc/oratab /tmp/oratab.bkp.$(date +%F) 2>&1 | tee -a $LOG_FILE
sed "/$ORACLE_SID/d" /tmp/oratab.bkp.$(date +%F) > /tmp/oratab.new 2>&1 | tee -a $LOG_FILE
mv /tmp/oratab.new /etc/oratab 2>&1 | tee -a $LOG_FILE
log "Removing ASM directories associated with the database"
asmcmd <<EOF 2>&1 | tee -a $LOG_FILE
cd +DATA
rm -rf $db_unique_name
cd +RECO
rm -rf $db_unique_name
EOF
log "Database $db_unique_name has been successfully decommissioned."
Comments
Post a Comment