Showing posts with label Backup and Restore. Show all posts
Showing posts with label Backup and Restore. Show all posts

Thursday, November 27, 2025

Stop Guessing: The "One-Shot" Script to Check Oracle Data Guard Health (Lags, Gaps & MRP)

Oracle Data Guard Health Check Script Dashboard Primary Standby



If you are a DBA, you know the panic of a "Quiet Standby." The alerts are silent. The phone isn't ringing. But deep down, you wonder: Is my Disaster Recovery (DR) site actually in sync, or has it been stuck on Sequence #10452 since last Tuesday?

Too many monitoring tools (like OEM or Zabbix) only trigger an alert when the lag hits a threshold (e.g., "Lag > 30 Mins"). By then, it’s often too late. You don't just want to know if there is a lag; you need to know where the lag is.

Is it the Network (Transport Lag)? Or is it the Disk/CPU (Apply Lag)?

Below is the exact script I use in my daily health checks. It consolidates 4 different dynamic performance views (v$dataguard_statsv$managed_standbyv$archive_gapv$database) into one single "Truth" report.

The Script (dg_health_check.sql)

Save this as dg_health_check.sql and run it on your Standby Database.

SQL
SET LINESIZE 200 PAGESIZE 1000 CHECK OFF FEEDBACK OFF ECHO OFF VERIFY OFF
COL name FORMAT a30
COL value FORMAT a20
COL unit FORMAT a30
COL time_computed FORMAT a25
COL process FORMAT a10
COL status FORMAT a15
COL sequence# FORMAT 99999999
COL block# FORMAT 999999
COL error_message FORMAT a50

PROMPT ========================================================
PROMPT  ORACLE DATA GUARD HEALTH CHECK (Run on Standby)
PROMPT ========================================================

PROMPT
PROMPT 1. DATABASE ROLE & PROTECTION MODE
PROMPT ----------------------------------------
SELECT name, db_unique_name, database_role, open_mode, protection_mode 
FROM v$database;

PROMPT
PROMPT 2. REAL-TIME LAG STATISTICS (The Source of Truth)
PROMPT ----------------------------------------
-- Transport Lag = Delay in receiving data (Network Issue)
-- Apply Lag     = Delay in writing data (IO/CPU Issue)
SELECT name, value, unit, time_computed 
FROM v$dataguard_stats 
WHERE name IN ('transport lag', 'apply lag', 'estimated startup time');

PROMPT
PROMPT 3. MRP (MANAGED RECOVERY PROCESS) STATUS
PROMPT ----------------------------------------
-- IF NO ROWS SELECTED: Your recovery is STOPPED.
-- Look for 'APPLYING_LOG' or 'WAIT_FOR_LOG'
SELECT process, status, thread#, sequence#, block# 
FROM v$managed_standby 
WHERE process LIKE 'MRP%';

PROMPT
PROMPT 4. GAP DETECTION
PROMPT ----------------------------------------
-- If rows appear here, you have a missing archive log that FAL_SERVER could not fetch.
SELECT * FROM v$archive_gap;

PROMPT
PROMPT 5. RECENT ERRORS (Last 10 Events)
PROMPT ----------------------------------------
SELECT TO_CHAR(timestamp, 'DD-MON-RR HH24:MI:SS') as err_time, message 
FROM v$dataguard_status 
WHERE severity IN ('Error','Fatal') 
AND timestamp > sysdate-1
ORDER BY timestamp DESC
FETCH FIRST 10 ROWS ONLY;

PROMPT ========================================================
PROMPT  END OF REPORT
PROMPT ========================================================

Oracle Data Guard Transport Lag vs Apply Lag Architecture Diagram


How to Analyze the Output (Like a Senior DBA)

Scenario A: High Transport Lag

  • What you see: Transport Lag is high (e.g., +00 01:20:00), but Apply Lag is low.

  • What it means: Your Primary database is generating Redo faster than your network can ship it.

  • The Fix: Check your network bandwidth. If you are using Oracle 19c or 23ai, consider enabling Redo Compression in your Data Guard broker configuration (EditDatabase Set Property RedoCompression='ENABLE').

Scenario B: High Apply Lag

  • What you see: Transport Lag is near 0, but Apply Lag is climbing (e.g., +00 00:45:00).

  • What it means: The data is there (on the standby server), but the database can't write it to disk fast enough. This often happens during batch loads or index rebuilds on the Primary.

  • The Fix: Check I/O stats on the Standby. Ensure you are using Real-Time Apply so the MRP (Managed Recovery Process) reads directly from Standby Redo Logs (SRLs) rather than waiting for archive logs to be finalized.

Scenario C: MRP Status is "WAIT_FOR_GAP"

  • What you see: In Section 3, the status is WAIT_FOR_GAP.

  • What it means: A severe gap has occurred. The Standby is missing a specific sequence number and cannot proceed until you manually register that file.

  • The Fix: Run the query in Section 4 (v$archive_gap) to identify the missing sequence, restore it from backup, and register it.

Why this works in 2026

Old school scripts relied on v$archived_log, which only tells you history. In modern Oracle Cloud (OCI) and Hybrid environments, v$dataguard_stats is the only view that accurately calculates the time difference between the Primary commit and the Standby visibility.

Chetan Yadav is a Senior Oracle, PostgreSQL, MySQL & Cloud DBA with 14+ years of experience supporting high-traffic production environments across AWS, Azure, and on-premise systems. His core expertise includes Oracle RAC, ASM, Data Guard, performance tuning, HA/DR design, monitoring frameworks, and real-world troubleshooting.

He also trains DBAs globally through deep-dive technical content, hands-on sessions, and automation workflows using n8n, AI tools, and modern monitoring stacks. His mission is to help DBAs solve real production problems and grow into high-paying remote roles worldwide.

Chetan regularly publishes expert content across Oracle, PostgreSQL, MySQL, and Cloud DBA technologies—including performance tuning guides, DR architectures, monitoring tools, scripts, and real incident-based case studies.


Explore More Technical Work

LinkedIn (Professional Profile & Articles)
https://www.linkedin.com/in/chetanyadavvds/

YouTube – Oracle Foundations Playlist
https://www.youtube.com/playlist?list=PL5TN6ECUWGROHQGXep_5hff-2ageWTp4b

Telegram – LevelUp_Careers DBA Tips
https://t.me/LevelUp_Careers

Instagram – Oracle/Cloud Learning Reels
https://www.instagram.com/levelup_careers/

Facebook Page – OracleDBAInfo
https://www.facebook.com/OracleDBAInfo

These platforms feature guides, scripts, diagrams, troubleshooting workflows, and real-world DBA case studies designed for database professionals worldwide.

Monday, August 30, 2010

EXPORT AND IMPORT USING DATAPUMP




C:\Documents and Settings\Chetan>sqlplus

SQL*Plus: Release 10.2.0.3.0 - Production on Fri Jul 30 11:55:29 2010

Copyright (c) 1982, 2006, Oracle. All Rights Reserved.

Enter user-name: sys as sysdba
Enter password:

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select name from v$database;

NAME
---------
ORA9


SQL> create directory data_p_test as 'c;\data_p_test';

Directory created.


Create manually a directory in the same location with same name ..

SQL> grant read ,write on directory data_p_test to scott;

Grant succeeded.

SQL> select * from dba_directories;
to check you directory is created or not.........................

SQL> grant exp_full_database to scott;

Grant succeeded.



SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Production
With the Partitioning, OLAP and Data Mining options.

C:\Documents and Settings\Chetan>expdp scott/tiger@ora9 full=y directory=data_p_test dumpfile=full.dmp logfile=full.log

Now connect with your new database where u wanna import .

my database name is "newtest"


SQL> create directory data_p_test as 'c:\data_p_test';

Directory created.



SQL> grant read ,write on directory data_p_test to scott;

Grant succeeded.

SQL> grant imp_full_database to scott;
Grant succeeded.


SQL> alter tablespace temp add tempfile 'tempfilelocation' size 200M autoextend on next 30M;

Come on your command prompt

C:\Documents and Settings\Chetan>impdp scott/tiger@newtest  full=y directory=data_p_test dumpfile=full.dmp logfile=full2.log


for export and import table  use all above command and just use the blow command for imp and exp

 export
SQL>expdp scott/tiger@new test tables=CHETAN directory=table_test dumpfile=table.dmp logfile=tableimp.log;

import
SQL>impdp scott/tiger@ora9 tables=CHETAN directory=table_test dumpfile=table.dmp logfile=tableimp2.log;



any mistake is there or u wanna give any suggestion so please write to me ............

Thursday, August 26, 2010

RENAME DATABASE AND INSTACE




RENAME DATABASE AND INSTACE



Let suppose  your db name is TEST and wanna change your db to NEWTEST

C:\> set ORACLE_SID=TEST

C:\> sqlplus  sys/sys as sysdba

SQL>select name from v$database;

Name
----------
TEST

SQL>alter database backup controlfile to trace as 'c:\c.txt' reuse;

SQL> shutdown immediate;

copy and past your old controlfile to another loction and edit your INITTEST.ora in this change your  "db_name=NEWTEST"

SQL>startup nomount pfile='D:\oracle\product\10.2.0\db_1\database\inittest.ora'

(the path where you save your pfile )

now create the new control file ..........

SQL>alter database open resetlogs;

SQL>select name from v$database;

Name
----------
NEWTEST


****this will change only your db name not instance name 







---------------------------------------------------------------------------------------------------------------


TO CHANGE  INSTANCE  INSTANCE NAME 


SQL> show parameter db_name;

NAME                                 TYPE        VALUE
------------------------ ----------- ---------------------------
db_name                              string           newtest


SQL>show parameter instance_name;


NAME                                 TYPE        VALUE
------------------------------------ ----------- ---------------
instance_name                        string      test 

if we are using spfile  we need to do ........

SQL> alter system reset instance_name scope= spfile sid ='*";

SQL> startup force ; 

BUT If we are using PFILE then....

In this case:
 remove instance_name parameter from PFILE

 shutdown/restart instance 

c:\> set ORACLE_SID=NEWTEST

if instance is  running in LINUX then every thing is done .
But in windows we need to crate through ORADIM

for ORADIM

create new instance -

c:\>E:\oracle\product\10.2.0\db_1\bin\oradim -new -sid NEWTEST -SRVC OracleServiceNEWTEST 
    -STARTMODE auto -SRVCSTART system -PFILE
                 e:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\INITTEST.ORA
         

 "    e:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\INITTEST.ORA"  this is the path where your pfile is saved.

    This will create a new instance .

Now delete your old instance 
c:\>E:\oracle\product\10.2.0\db_1\bin\oradim -DELETE -SID TEST

Until you did not delete your old instance  you always got error so first delete your old instance and then check your db name and instance name ...




any mistake is there or u wanna give any suggestion so please write to me ............



Monday, August 23, 2010

HOT BACKUP





set your archive log on

SQL> shutdown abort ;

SQL> startup mount;

SQL>alter database archivelog;

you got an  error cose  we use shutdown abort so never use shutdown abort if u wanna set archive log
SQL>shutdown immediate ;

SQL>startup mount ;

SQL >alter database archivelog;

SQL>alter database open;

SQL>alter system switch logfile;

SQL>exit

SQL>rman target/

RMAN> backup database plus archivelog;


done

-------------------------------------------------------------------------------



Restore database 



SQL >  SHUTDOWN

SQL > EXIT

C:/>RMAN TARGET /

RMAN>   ALTER DATABASE  NOMOUNT ;

RMAN>  RESTORE CONTROLFILE FROM  'D:\BACKUP\C-33354'

** 'D:\BACKUP\C-33354' this is the path where u store ur backup .and C-33354 this the controll file use ur contoll file name .

controll file name always start with 'c-'

RMAN>ALTER DATABASE MOUNT ;

RMAN>RESTORE DATABASE;

RMAN> RECOVER DATABASE;

RMAN>ALTER DATABASE OPEN RESETLOGS;




any mistake is there or u wanna give any suggestion so please write to me ............

Monday, August 16, 2010

cold backup database

Cold Backup using RMAN


Database should be in archive mode
check the database is up or not

c:\> RMAN TARGET /

RMAN:\> SHOW ALL ;

to check configuration of RMAN  . Configure three things in this configuration

RMAN:\>  CONFIGURE CONTROLFILE AUTOBACKUP ON;

RMAN:\>  CONFIGURE DEVICE TYPE DISK FORMAT   'D:\BACKUP\%F';

** this is the path where u wanna take backup

RMAN:\>  CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT 'D:\BACKUP\%U';

RMAN:\>  SHUTDOWN IMMEDIATE ;

RMAN:\>  STARTUP MOUNT;

RMAN:\>  BACKUP DATABASE;

RMAN:\>  ALTER DATABASE OPEN;

--------------------------------------------------------------------------------------------------------
Restore database 



SQL >  SHUTDOWN

SQL > EXIT

C:/>RMAN TARGET /

RMAN>   ALTER DATABASE  NOMOUNT ;

RMAN>  RESTORE CONTROLFILE FROM  'D:\BACKUP\C-33354'

** 'D:\BACKUP\C-33354' this is the path where u store ur backup .and C-33354 this the controll file use ur contoll file name .

controll file name always start with 'c-'

RMAN>ALTER DATABASE MOUNT ;

RMAN>RESTORE DATABASE;

RMAN> RECOVER DATABASE;

RMAN>ALTER DATABASE OPEN RESETLOGS;





any mistake is there or u wanna give any suggestion so please write to me ............

Monday, June 14, 2010

CLONING ORACLE DATA BASE

STEPS FOR CLONING 
THROUGH COLD BACKUP

 


My old database name is "HRDEMO91"
My clone database name is "FSCLON"
  While using this steps fellow your database name instead or mine
1. Get the file path information using below query
                 select name from v$datafile;
                 select member from v$logfile;
                select name from v$controlfile;
2. Parameter file backup
               If HRDEMO91 database running on spfile then create pfile
                "Create pfile=E:\oracle\product\10.2.0\db_1\database\SPFILEHRDEMO91.ora  from spfile;"
              Now copy this pfile in another location .
3. Taken the control file backup
               "Alter database backup controlfile to trace ;"
4. Shutdown immediate
5. Create a folder with new db name (eg. db name is 'fsdemo')path to create the folder is
              "E:\oracle\product\10.2.0\oradata\"
                1} copy all tablespace and redo logs in this folder (except control file )
6. Now create folder with db name in another location .
                1} The location is "E:\oracle\product\10.2.0\admin\"
                2} In folder fsdemo create adump, bdump,cdump,udump,dpdump ,pfile .
           So you got this folder in "E:\oracle\product\10.2.0\admin\fsdemo\"
7. Now edit that pfile which we copy in another location edit db name file and save it with
           new name like "initfsdemo.ora"
                    1} copy this file and past it in "E:\oracle\product\10.2.0\db_1\database\"
                       2}past another copy in "E:\oracle\product\10.2.0\admin\fsdemo\pfile\"
8. Run this command from your c:\ prompt
             " E:\oracle\product\10.2.0\db_1\bin\oradim -new -sid FSDEMO -SRVC OracleServiceFSDEMO
              -STARTMODE auto -SRVCSTART system -PFILE
                 e:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\INITFSDEMO.ORA"
             This will create a new instance .

9. Now from "c:\ set oracle_sid=fsdemo"
10. Startup the Database in NOMOUNT stage .
11. Create the control file for cloning database .
             In udump of your old db i.e.HEREMO91 u got acurrent trace control file open it delete
             everything Excep control file ,logfile & datafile.
             Make sure you use SET instead REUSE and use your cloned database
             name on place of old DB and use "RESETLOGS" instead of "NORESETLOGS". Change all path of your dbf and log files according to your cloned
             DB and save it in sql file like c.sql and run it from sql promt
             "SQL> @E:\clone\c.sql"
     User this script for creating control file.
12. Now open database with
            "alter database open resetlogs;"
13.Create a TNS name i.e. Listner for the cloned database name
open     "E:\oracle\product\10.2.0\db_1\network\ADMIN\tnsname.ora"
FSDEMO =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.129.3)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = FSDEMO)
)
)
Copy this script in in your tnsname.ora and save it .

congratulation  your cloning is done 


hoping this information will help you . if any query then please reply me .