-- ==================================================================
--	00. Configure a variavel $PATH com o caminho do OPatch:
-- ================================================================== 
[oracle@host ~]$ vi .bash_profile
...
export PATH=$PATH:$ORACLE_HOME/OPatch
...
[oracle@host ~]$ . .bash_profile
[oracle@host ~]$ which opatch
/u01/app/oracle/product/19.0.0/dbhome_1/OPatch/opatch

-- ================================================================== 
--	01. Verificar a versão do Opatch:
-- ================================================================== 
opatch version
	OPatch Version: 12.2.0.1.XX
	OPatch succeeded.

opatch lspatches
	29585399;OCW RELEASE UPDATE 19.3.0.0.0 (29585399)
	29517242;Database Release Update : 19.3.0.0.190416 (29517242)

opatch lsinventory
	Oracle Interim Patch Installer version 12.2.0.1.XX
	Copyright (c) 2023, Oracle Corporation.  All rights reserved.
	Oracle Home       : /u01/app/oracle/product/19.3.0/dbhome_1
	Central Inventory : /u01/app/oraInventory
	   from           : /u01/app/oracle/product/19.3.0/dbhome_1/oraInst.loc
	OPatch version    : 12.2.0.1.XX
	OUI version       : 12.2.0.7.0

-- ================================================================== 
--	02. DOWNLOAD OPatch and CRITICAL PATCH UPDATES
-- ================================================================== 
OPatch Tool:  https://updates.oracle.com/download/6880880.html

Critical Patch Updates:  https://www.oracle.com/security-alerts/

-- ================================================================== 
--	03. RENAME folder OPatch to OPatch_OLD
-- ================================================================== 
mv -v /u01/app/oracle/product/19.0.0/dbhome_1/OPatch /u01/app/oracle/product/19.0.0/dbhome_1/OPatch_old

-- ================================================================== 
--	04. DESCOMPACTAR OPatch
-- ================================================================== 
unzip -q p6880880_190000_Linux-x86-64.zip -d /u01/app/oracle/product/19.3.0/dbhome_1 

-- ================================================================== 
--	05. Verify the OPatch has been upgraded
-- ================================================================== 
/u01/app/oracle/product/19.3.0/dbhome_1/OPatch/opatch version

	OPatch Version: 12.2.0.1.XX
	OPatch succeeded.

-- ================================================================== 
--	06. Encerre o banco de dados
-- ================================================================== 
	sqlplus /nolog
	CONNECT / AS SYSDBA
	SHUTDOWN IMMEDIATE;
	QUIT
-- Comando Alternativo ------
	dbshut
	
-- ==================================================================
--	07. Verfificando o serviço
-- ==================================================================
ps -ef | grep smon | grep -v grep
echo $?
##	1 --> Onde 1 significa que a instância não existe.	##

-- ==================================================================
--	08. Backup Oracle Home (Opcional )
-- ==================================================================
export ORACLE_HOME=/u01/app/oracle/product/19.3.0/dbhome_1
cd /u01/app/oracle/product/19.3.0
	
## Comando para compactar:	##
tar -zcvf /home/oracle/software/DB_HOME_`date +%Y%m%d`.tar.gz dbhome_1 > /home/oracle/software/DB_HOME_`date +%Y%m%d`_tar_backup.log

## Em outra ABA:	##
tail -f /home/oracle/software/DB_HOME_20230215_tar_backup.log

-- ==================================================================
--	09. Verifique a integridade do arquivo:
-- ==================================================================
unzip -t p36522340_190000_Linux-x86-64.zip

	Archive:  p36522340_190000_Linux-x86-64.zip
	testing: 36522340/                OK
	...
	testing: PatchSearch.xml          OK
	No errors detected in compressed data of p99999999_190000_Linux-x86-64.zip.

-- ==================================================================
--	10. Descompactar o patch e Acessar o diretório:
-- ==================================================================
unzip -q p99999999_190000_Linux-x86-64.zip
cd 99999999

-- ==================================================================
--	08:Verificando os pre requisitos:
-- ==================================================================
opatch prereq CheckConflictAgainstOHWithDetail -ph ./
-- Comando Alternativo ------
$ORACLE_HOME/OPatch/opatch prereq CheckConflictAgainstOHWithDetail -ph ./

opatch prereq CheckConflictAgainstOHWithDetail -ph ./
	Oracle Interim Patch Installer version 12.2.0.1.XX
	Copyright (c) 2023, Oracle Corporation.  All rights reserved.
	PREREQ session
	Oracle Home       : /u01/app/oracle/product/19.3.0/dbhome_1
	Central Inventory : /u01/app/oraInventory
	   from           : /u01/app/oracle/product/19.3.0/dbhome_1/oraInst.loc
	OPatch version    : 12.2.0.1.XX
	OUI version       : 12.2.0.7.0
	Log file location : /u01/app/oracle/product/19.3.0/dbhome_1/cfgtoollogs/opatch/opatch2023-02-15_14-20-35PM_1.log
	Invoking prereq "checkconflictagainstohwithdetail"
	Prereq "checkConflictAgainstOHWithDetail" passed.
	OPatch succeeded.

-- ==================================================================
--	09:Aplicando o patch:
-- ==================================================================
[oracle@host 99999999]$ opatch apply
	Oracle Interim Patch Installer version 12.2.0.1.XX
	Copyright (c) 2023, Oracle Corporation.  All rights reserved.
	Oracle Home       : /u01/app/oracle/product/19.3.0/dbhome_1
	Central Inventory : /u01/app/oraInventory
	   from           : /u01/app/oracle/product/19.3.0/dbhome_1/oraInst.loc
	OPatch version    : 12.2.0.1.XX
	OUI version       : 12.2.0.7.0
	Log file location : /u01/app/oracle/product/19.3.0/dbhome_1/cfgtoollogs/opatch/opatch-xxxx_1.log
	Verifying environment and performing prerequisite checks...
	Do you want to proceed? [Y]
	Is the local system ready for patching? [Y]

## Comando Alternativo -- FORMA SILENCIOSA	##
$ORACLE_HOME/OPatch/opatch apply -silent   

-- ==================================================================
--	10. Verificando o novo patch e inventório:
-- ==================================================================
opatch lspatches
opatch lsinventory

-- ==================================================================
--	11. Inicie o Banco de dados
-- ==================================================================
sqlplus /nolog
CONNECT / AS SYSDBA
STARTUP QUIET
ALTER PLUGGABLE DATABASE ALL OPEN;
QUIT

##  Comando Alternativo 	## 
dbstart $ORACLE_HOME

### Em Data Guard esse procedimento deverá ser feito apenas no Primary  ###

-- ==================================================================
--	12. Ele informará se o seu ambiente de banco de dados está pronto para correção de SQL ou não… .
-- ==================================================================
$ORACLE_HOME/OPatch/datapatch -sanity_checks
	SQL Patching sanity checks version 19.24.0.0.0  
	Copyright (c) 2021, 2024, Oracle.  All rights reserved.
	Checks completed. Printing report:
	...
	Check: Locale - OK
	SQL Patching sanity checks completed.

-- ==================================================================
--	13. Ele carrega dados SQL modificados.
-- ==================================================================
$ORACLE_HOME/OPatch/datapatch -verbose
	Copyright (c) 2012, 2024, Oracle.  All rights reserved.
	...
	SQL Patching tool complete 

-- ==================================================================
--	14. Verificar o nível do patch SQL consultando
-- ==================================================================
sqlplus / as sysdba
SELECT DESCRIPTION FROM DBA_REGISTRY_SQLPATCH ORDER BY ACTION_TIME DESC;

-- ==================================================================
--	15. Recompilar Objetos Inválidos
-- ==================================================================
@?/rdbms/admin/utlrp.sql

-- ==================================================================
--	16. Verifique os patches do SQLPLUS conectando-se ao SYSDBA
-- ==================================================================
SELECT * FROM SYS.REGISTRY$HISTORY;
SELECT DBMS_QOPATCH.GET_OPATCH_LIST FROM DUAL;

##-- SYS.REGISTRY$HISTORY--	##
COLUMN action_time FORMAT A20
COLUMN action FORMAT A20
COLUMN version FORMAT A10
COLUMN comments FORMAT A30
COLUMN bundle_series FORMAT A10
SELECT 
	TO_CHAR(ACTION_TIME, 'DD-MON-YYYY HH24:MI:SS') AS ACTION_TIME,
	ACTION,	VERSION, 		ID, 	COMMENTS,	 	BUNDLE_SERIES
FROM   SYS.REGISTRY$HISTORY
ORDER BY ACTION_TIME;

##--DBA_REGISTRY_SQLPATCH--	##
COL ACTION_TIME FOR A28
COL ACTION FOR A8
COL TARGET_VERSION FOR A8
COL COMMENTS FOR A30
COL STATUS FOR A10
SET LINE 999 PAGES 999
SELECT 
	PATCH_ID, TARGET_VERSION, STATUS, ACTION, ACTION_TIME 
FROM DBA_REGISTRY_SQLPATCH 
ORDER BY ACTION_TIME;

-- ==================================================================
-- 	17. Verifique se o patch foi aplicado ou não com o pacote DBMS
-- ==================================================================
##--Put patch number which you want to check --	##
SELECT
	XMLTRANSFORM(DBMS_QOPATCH.IS_PATCH_INSTALLED ('XXXX'),
	DBMS_QOPATCH.GET_OPATCH_XSLT) "PATCH INSTALLED?" 
FROM DUAL;

##-- VERIFIQUE TODOS OS PATCHES APLICADOS DO DBMS	--##
SET SERVEROUT ON;
EXEC DBMS_QOPATCH.GET_SQLPATCH_STATUS;

-- ==================================================================
-- LISTA DE COMANDOS RÁPIDOS 
-- ==================================================================
opatch version
opatch lspatches
opatch lsinventory
$ORACLE_HOME/OPatch/opatch prereq CheckConflictAgainstOHWithDetail -ph ./
$ORACLE_HOME/OPatch/opatch apply -silent
sqlplus /nolog
   CONNECT / AS SYSDBA
   STARTUP QUIET
   ALTER PLUGGABLE DATABASE ALL OPEN;
   QUIT
$ORACLE_HOME/OPatch/datapatch -sanity_checks
$ORACLE_HOME/OPatch/datapatch -verbose

hostgator