Pages

Tuesday, December 31, 2013

Skip DML Replication of a Table to Logical Standby site and Re-Instantiate it

ENVIRONMENT


DATABASE - ORACLE 11gR2 11.2.0.3.0


PRIMARY DATABASE - PRODDB

STANDBY DATABASE - LOGSTDB (LOGICAL STANDBY)

SCHEMA NAME - TEST


NOTE: Already two tables are created in the TEST schema namely T1 and T2.


On Primary Database

  • Connect to the schema and check the table rows.

PRODDB>conn test/test@PRODDB  
Connected.

PRODDB>select * from tab;

TNAME  TABTYPE CLUSTERID
------------- -------      -------------
T1  TABLE
T2  TABLE


PRODDB>select * from t1; 

NO
----------
1
2
3

PRODDB>select * from t2;   

NO
----------
1
2
3
4
4
5
6

7 rows selected.

On Logical Standby Database

  • Stop the Logical Standby apply.
alter database stop logical standby apply;


LOGSTDB-LOGICAL>alter database stop logical standby apply;

Database altered.

  • Execute the below statement to skip a table replication.,

exec dbms_logstdby.skip('DML','SCHEMA_NAME','TABLE_NAME',null);


LOGSTDB-LOGICAL>exec dbms_logstdby.skip('DML','TEST','T1',null);

PL/SQL procedure successfully completed.


  • Start the Logical Standby apply.

alter database start logical standby apply immediate;


LOGSTDB-LOGICAL>alter database start logical standby apply immediate;

Database altered.

On Primary Database

  • Insert rows in to the tables T1 and T2.

PRODDB>insert into t1 values(4);

1 row created.

PRODDB>commit;

Commit complete.

PRODDB>select * from t1; 

NO
----------
 1
 2
 3
 4


PRODDB>insert into t2 values(7);

1 row created.

PRODDB>commit;

Commit complete.

PRODDB>select * from t2;

NO
----------
1
2
3
4
4
5
6
7

8 rows selected.


    On Logical Standby Database


    • Now Check whether the inserted rows in to tables T1 and T2 are replicated to the Logical Standby Database.


    LOGSTDB-LOGICAL>select * from test.t1;

    NO
    ----------
    1
    2
    3


    LOGSTDB-LOGICAL>select * from test.t2;

    NO
    ----------
    1
    2
    3
    4
    6
    7
    4
    5

    8 rows selected.


    NOTE: 
    • From the above output we can clearly see that the inserted rows are not replicated to the Logical Standby Database. Replication to table T1 is skipped.
    • Rows in the table T2 is replicated since it is not skipped


    RE-INSTANTIATE THE SKIPPED TABLES

    On Logical Standby Database

    • Stop SQL apply process.

    LOGSTDB-LOGICAL>alter database stop logical standby apply;    

    Database altered.

    • Drop and recreate the table test.t1.

    LOGSTDB-LOGICAL>drop table test.t1;

    Table dropped.

    LOGSTDB-LOGICAL>create table test.t1 (no number(4));

    Table created.

    • On the logical standby database execute the following the query.

    LOGSTDB-LOGICAL>SELECT APPLIED_SCN, LATEST_SCN, MINING_SCN, RESTART_SCN FROM V$LOGSTDBY_PROGRESS;

    APPLIED_SCN    LATEST_SCN       MINING_SCN    RESTART_SCN
    ----------------  ----------------    ---------------   ----------------
    2087190          2090492                             2087191

    • Take a backup of the table test.T1 from the production database.

     host expdp test/test@PRODDB dumpfile=test.dmp directory=data_pump_dir tables=test.T1 flashback_scn=2087191


    PRODDB>host expdp test/test@PRODDB dumpfile=test.dmp directory=data_pump_dir tables=test.T1 flashback_scn=2087191

    Export: Release 11.2.0.3.0 - Production on Wed Feb 22 20:15:01 2012

    Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    Starting "TEST"."SYS_EXPORT_TABLE_01":  test/********@PRODDB dumpfile=test.dmp directory=data_pump_dir tables=test.T1 flashback_scn=2087191 
    Estimate in progress using BLOCKS method...
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    Total estimation using BLOCKS method: 64 KB
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    . . exported "TEST"."T1"                                 5.078 KB       4 rows
    Master table "TEST"."SYS_EXPORT_TABLE_01" successfully loaded/unloaded
    ******************************************************************************
    Dump file set for TEST.SYS_EXPORT_TABLE_01 is:
      /cbsprddbs/oracle/admin/PRODDB/dpdump/test.dmp
    Job "TEST"."SYS_EXPORT_TABLE_01" successfully completed at 20:15:53


    Note: Flashback scn will be restart_scn which you get from v$logstdby_progress.


    • Then import the datapump data to the Logical.

    host impdp dumpfile=test.dmp directory=data_pump_dir table_exists_action=append

    LOGSTDB-LOGICAL>host impdp dumpfile=test.dmp directory=data_pump_dir table_exists_action=append

    Import: Release 11.2.0.3.0 - Production on Wed Feb 22 20:18:44 2012

    Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

    Username: sys@LOGSTDB as sysdba
    Password: 

    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    Master table "SYS"."SYS_IMPORT_FULL_01" successfully loaded/unloaded
    Starting "SYS"."SYS_IMPORT_FULL_01":  sys/********@LOGSTDB AS SYSDBA dumpfile=test.dmp directory=data_pump_dir table_exists_action=append 
    Processing object type TABLE_EXPORT/TABLE/TABLE
    Table "TEST"."T1" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
    Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
    . . imported "TEST"."T1"                                 5.078 KB       4 rows
    Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
    Job "SYS"."SYS_IMPORT_FULL_01" successfully completed at 20:19:20

    • Then unskip the skip rule for the table.

    EXECUTE DBMS_LOGSTDBY.UNSKIP('DML','SCHEMA_NAME','TABLE_NAME');


    LOGSTDB-LOGICAL>EXECUTE DBMS_LOGSTDBY.UNSKIP('DML','TEST','T1');

    PL/SQL procedure successfully completed.

    • Then start sql apply.

    ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATE;


    LOGSTDB-LOGICAL>ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATE;

    Database altered.

    Note: If required analyze table test.T1 compute statistics or use dbms_stats to gather statistics.



    REPLICATION TESTING AFTER RE-INSTANTIATION


    On Primary Database


    PRODDB>insert into test.t1 values(5);

    1 row created.

    PRODDB>commit;

    Commit complete.

    PRODDB>select * from test.t1;

    NO
    ----------
    1
    2
    3
    4
    5

    On Logical Standby Database


    LOGSTDB-LOGICAL>select * from test.t1;

    NO
    ----------
    1
    2
    3
    4
    5





    Effect of Creating Tablespace / Datafile on Primary when Logical Standby in Place (ORA-01119)


    Effect of Creating Tablespace / Datafile on Primary when Logical Standby in Place (ORA-01119)


    SYMPTOMS


    Dataguard environment with Logical standby. 

    When we add a new datafile on primary and both primary and standby are in different file name directory, SQL APPLY from production database to Logical Standby database will fail with ORA-01119.

    CAUSE


    1. If databse is OMF on both primary and standby then no issues in creating datafile at logical standby side.


    2. If no OMF then Apply terminate with (ORA-1119) below errors, Because logcal standby will not honour the db_file_name_convert. 



    SOLUTION

    • Use OMF as a proactive measure.

    • If not run skip handler.


    WORKOUT

    On Production


    • create a tablespace as below,


    PRODDB>create tablespace tbstest2 datafile '/oradata1/PRODDB/tbstestt201.dbf' size 1m autoextend on next 1m maxsize unlimited;

    Tablespace created.

    PRODDB>select name from v$tablespace where name ='TBSTEST2';

    NAME
    ------------------------------

    TBSTEST2

    On Logical Standby database

    • Check whether the tablespace has replicated from primary site to logical standby site.


    LOGSTDB-LOGICAL>select name from v$database where name='TBSTEST2';


    no rows selected

    • From the above result we can understand that, the tablespace created in primary site did not replicate to logical standby. The following steps has to be followed to replicate the tablespace to logical standby site.

    • Stop the logical standby apply process.


    LOGSTDB-LOGICAL>alter database stop logical standby apply;

    Database altered.


    • Execute the below procedure.


    LOGSTDB-LOGICAL>CREATE OR REPLACE PROCEDURE SYS.HANDLE_TBS_DDL (
    OLD_STMT IN VARCHAR2,
    STMT_TYP IN VARCHAR2,
    SCHEMA IN VARCHAR2,
    NAME IN VARCHAR2,
    XIDUSN IN NUMBER,
    XIDSLT IN NUMBER,
    XIDSQN IN NUMBER,
    ACTION OUT NUMBER,
    NEW_STMT OUT VARCHAR2
    ) AS
    BEGIN
    NEW_STMT := REPLACE(OLD_STMT,'/oradata1/PRODDB','/oradata2/LOGSTDB');
    ACTION := DBMS_LOGSTDBY.SKIP_ACTION_REPLACE;
    EXCEPTION
    WHEN OTHERS THEN
    ACTION := DBMS_LOGSTDBY.SKIP_ACTION_ERROR;
    NEW_STMT := NULL;
    END HANDLE_TBS_DDL;
    /  2    3    4    5    6    7    8    9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39  

    Procedure created.

    • After creating the procedure, Execute the below dbms statement.

    LOGSTDB-LOGICAL>EXECUTE DBMS_LOGSTDBY.SKIP (stmt => 'TABLESPACE',proc_name => 'sys.handle_tbs_ddl');

    PL/SQL procedure successfully completed.
    • Start the logical standby apply process.


    LOGSTDB-LOGICAL>ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATE;

    Database altered.
    • Check whether the tablespace is replicated in logical standby site.

    LOGSTDB-LOGICAL>select name from v$tablespace where name='TBSTEST2';

    NAME
    ------------------------------
    TBSTEST2




    PREVENT USERS (SCHEMAS) FROM GETTING DROPPED



    The below Trigger is used to prevent users and schemas from Getting Dropped Mistakenly,

    Create OR Replace Trigger TrgDropUserRestrict
    Before Drop On Database
    Declare
    Begin
         If Ora_Dict_Obj_Name In ('SH','OUTLN','SCOTT','user1')    Then
                         Raise_Application_Error(-20001,'Cannot Drop User
                        '||ora_dict_obj_name||' Contact Your Database Administrator For Dropping This User !'); 
         End If;
    End;

    /


    1. create a new user and grant privileges to the new user.


    ORADB>create user user1 identified by laser default tablespace users;


    ORADB>grant connect,resource,debug connect session,debug any procedure to user1;


    Grant succeeded.


    ORADB>select username from all_users where username = 'USER1';

    USERNAME
    ------------------------------
    USER1


    2. Run the procedure to prevent the user to be dropped.


    ORADB>Create OR Replace Trigger TrgDropUserRestrict
    Before Drop On Database
    Declare
    Begin
         If Ora_Dict_Obj_Name In ('SH','OUTLN','SCOTT','USER1')    Then
                         Raise_Application_Error(-20001,'Cannot Drop User
                        '||ora_dict_obj_name||' Contact Your Database Administrator For Dropping This User !'); 
         End If;
    End;
    /  2    3    4    5    6    7    8    9   10  

    Trigger created.

    ORADB>


    Note: You can specify N number of users in the Ora_Dict_Obj_Name which should not get dropped.


    3. Try to drop the user and check if the user is dropped.

    ORADB>drop user user1 cascade;
    drop user user1 cascade
    *
    ERROR at line 1:
    ORA-00604: error occurred at recursive SQL level 1
    ORA-20001: Cannot Drop User
    user1 Contact Your Database Administrator For Dropping This User !
    ORA-06512: at line 4

    ORADB>drop user scott cascade;
    drop user scott cascade
    *
    ERROR at line 1:
    ORA-00604: error occurred at recursive SQL level 1
    ORA-20001: Cannot Drop User
    SCOTT Contact Your Database Administrator For Dropping This User !
    ORA-06512: at line 4

    Switchover and Switchback with Logical Standby

    SWITCHOVER IN LOGICAL STANDBY


    DATABASE - Oracle 11gR2 11.2.0.3.0

    Operating System - Oracle Enterprise Linux

    Primary Database  - PRODDB

    Logical Standby Database - LOGSTDB



    On Primary Database:

    • Check the switchover status and role of the  Primary Database.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    TO STANDBY


    SQL> select name,db_unique_name,database_role,open_mode from v$database;

    NAME  DB_UNIQUE_NAME DATABASE_ROLE   OPEN_MODE
    --------- --------------------- -------------------   --------------------
    proddb  proddb  PRIMARY            READ WRITE


    • Issue the below command to prepare the Primary Database to switchover to logical standby.

    alter database prepare to switchover to logical standby;


    SQL> alter database prepare to switchover to logical standby;

    Database altered.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    --------------------------------
    PREPARING SWITCHOVER


    Here the switchover status is PREPARING SWITCHOVER. Change the switchover status of Logical standby database and Wait for some time so that the status of primary database will change as TO LOGICAL STANDBY.


    On Logical Standby Database

    • Check the switchover status and database role of the database.


    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    NOT ALLOWED

    SQL> select name,db_unique_name,database_role,open_mode from v$database;

    NAME     DB_UNIQUE_NAME    DATABASE_ROLE     OPEN_MODE
    --------- -------------------    --------------------   ---------------- 
    logstdb   logstdb         LOGICAL STANDBY   READ WRITE



    • Issue the command to prepare the Logical standby database to switchover to Primary Database.


    alter database prepare to switchover to logical standby;


    SQL> alter database prepare to switchover to primary;

    Database altered.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    TO PRIMARY

    On Primary Database

    • Now the check the switchover status of primary database. The status will be changed as TO LOGICAL STANDBY.


    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    TO LOGICAL STANDBY


    • Now switchover primary database to Logical Standby using the  below command.

    alter database commit to switchover to logical standby;


    SQL> alter database commit to switchover to logical standby;

    Database altered.


    • Now check the database role of the primary database. It will be changed to Logical Standby.


    SQL> select name,db_unique_name,database_role,open_mode from v$database;

    NAME     DB_UNIQUE_NAME    DATABASE_ROLE     OPEN_MODE
    --------- -------------------    --------------------   ---------------- 
    proddb   proddb         LOGICAL STANDBY   READ WRITE

    SQL>


    On Logical Standby Database

    • Now check the switchover status of Logical Standby Database.


    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    -----------------------------
    TO PRIMARY


    • Switchover the  Logical standby database to Primary using the below command.

    alter database commit to switchover to primary;


    SQL> alter database commit to switchover to primary;

    Database altered.


    • Check the database role of the Logical Standby database. It will be changed as Primary.


    SQL> select name,db_unique_name,database_role,open_mode from v$database;

    NAME     DB_UNIQUE_NAME    DATABASE_ROLE     OPEN_MODE
    --------- -------------------    --------------------   ---------------- 
    logstdb   logstdb         PRIMARY                READ WRITE



    On Primary Database

    • Start the Logical Standby apply process.

    alter database start logical standby apply immediate;


    SQL> alter database start logical standby apply immediate;

    Database altered.


    • Check the Replication between the New Primary and Switched Logical Standby Database.


    On Logical Standby Database (Current Primary Database)

    • Insert a row in a table and check whether it has been replicated to Primary Database(Currently Logical Standby).


    SQL> select * from scott.t1;

    NO
    ----------
    2
    1
    3

    SQL> insert  into scott.t1 values(4);

    1 row created.

    SQL> commit;

    Commit complete.

    SQL> select * from scott.t1;

    NO
    ----------
    2
    1
    3
    4

    SQL> 


    On Primary Database (Current Logical Standby Database)

    • Check the replication to Primary Database(Current Logical Standby).

    SQL> select * from scott.t1;

    NO
    ----------
    2
    1
    3


    SQL> select * from scott.t1;

    NO
    ----------
    2
    1
    3
    4

    *****************************************
    SAME HAS TO BE FOLLOWED FOR SWITCHBACK
    *****************************************

    SWITCHBACK TO OLD ROLE


    On Logical Standby Database(Current Primary Database)


    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    -----------------------------
    TO STANDBY


    • Prepare the Database for switchover.

    alter database prepare to switchover to logical standby;


    SQL> alter database prepare to switchover to logical standby;

    Database altered.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    --------------------------------
    PREPARING SWITCHOVER


    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    TO LOGICAL STANDBY


    • Now Switchover the role of Current primary database to logical standby.

    alter database commit to switchover to logical standby;

    SQL> alter database commit to switchover to logical standby;

    Database altered.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    NOT ALLOWED

    SQL> select open_mode,database_role,name from v$database;

    OPEN_MODE DATABASE_ROLE       NAME
    ---------------     ---------------------   --------
    READ WRITE LOGICAL STANDBY    logstdb


    • Start the Logical Standby apply process.

    SQL> alter database start logical standby apply immediate;

    Database altered.

    SQL> archive log list
    Database log mode       Archive Mode
    Automatic archival       Enabled
    Archive destination       /oradata3/logstdb/archives
    Oldest online log sequence     17
    Next log sequence to archive   19
    Current log sequence       19

    SQL> select * from scott.t1;

    NO
    ----------
    2
    1
    3
    4


    On primary Database (Current Logical Standby Database)

    • Prepare the Database for switchover.

    alter database prepare to switchover to primary;


    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    NOT ALLOWED

    SQL> archive log list
    Database log mode       Archive Mode
    Automatic archival       Enabled
    Archive destination       /oradata2/proddb/archives
    Oldest online log sequence     33
    Next log sequence to archive   35
    Current log sequence       35

    SQL> alter database prepare to switchover to primary;

    Database altered.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    PREPARING SWITCHOVER

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    ----------------------------
    TO PRIMARY

    • Now Switchover the role of Logical Standby database to Primary Database.

    alter database commit to switchover to primary;


    SQL> alter database commit to switchover to primary;

    Database altered.

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    -----------------------------
    LOG SWITCH GAP


    • Check the role and switchover status of the Primary database. 

    select switchover_status from v$database;


    SQL> select open_mode,database_role,name from v$database;

    OPEN_MODE     DATABASE_ROLE  NAME
    ---------------   ------------------  --------
    READ WRITE    PRIMARY       proddb

    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    -----------------------------
    LOG SWITCH GAP

    SQL> alter system switch logfile;

    System altered.

    SQL> archive log list
    Database log mode       Archive Mode
    Automatic archival       Enabled
    Archive destination       /oradata2/proddb/archives
    Oldest online log sequence     40
    Next log sequence to archive   42
    Current log sequence       42
     
    SQL> select switchover_status from v$database;

    SWITCHOVER_STATUS
    -----------------------------
    TO STANDBY


    Note: logfile is switched to change the switchover status of the Primary Database.


    SQL> select * from scott.t1;

    NO
    ----------
    4
    3
    1
    2

    SQL> insert into scott.t1 values(5);

    1 row created.

    SQL> commit;

    Commit complete.

    SQL> select * from scott.t1;

    NO
    ----------
    4
    3
    1
    2
    5


    On Logical Standby Database


    SQL> select * from scott.t1;

    NO
    ----------
     2
     5
     1
     3
     4


    FULL DATABASE RESTORE AND RECOVER USING RMAN


    SCENARIO


    Recovering the Database when database is fully crashed and no files are available using RMAN Backup files


    1. Created a database called 'TESTDB' using DBCA.

    2. Converted the database from NoArchivelog to Archivelog mode.

    3. Configured RMAN.

    4. Taken Incremental level 0 backup using RMAN (Database plus Archivelog).

    5. Shutdown the Database.

    6. Deleted the controlfiles, spfile, pfile, redolog files and datafiles.

    7. Deleted the sid also.


    8. Recovered the full database using RMAN backup.

    ENVIRONMENT

    DATABASE - ORACLE 11.2.0.3.0 64Bit

    OS - Windows 2008 R2 64Bit

    Database Name - testdb


    *********************************************************************************

    STEPS TO FOLLOW


    1. Create New SID for TESTDB.


    C:\>oradim -new -sid testdb -syspwd oracle -startmode auto
    Instance created.


    2. start the instance using a dummy pfile. The pfile may contain only the db_name and db_unique_name                  parameters.


    C:\>sqlplus /nolog

    SQL*Plus: Release 11.2.0.3.0 Production on Mon Jun 4 21:09:12 2012

    Copyright (c) 1982, 2011, Oracle.  All rights reserved.

    SQL> conn sys/oracle@testdb as sysdba
    Connected to an idle instance.

    SQL> startup nomount pfile='f:\inittestdb.ora';
    ORACLE instance started.

    Total System Global Area 1068937216 bytes
    Fixed Size                  2262048 bytes
    Variable Size             616565728 bytes
    Database Buffers          444596224 bytes
    Redo Buffers                5513216 bytes
    SQL>




    3. Connect to RMAN.

    C:\>rman

    Recovery Manager: Release 11.2.0.3.0 - Production on Mon Jun 4 21:14:41 2012


    Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

    4. Restore the SPFILE from autobackup.

    RMAN> set dbid=2568236785

    executing command: SET DBID

    RMAN> connect target sys/oracle@testdb

    connected to target database:  (not mounted)

    RMAN> SET CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO 'f:\testdb\controlfile_bkup\CTL_%F';

    executing command: SET CONTROLFILE AUTOBACKUP FORMAT

    Restoration of SPFILE from 

    RMAN> restore spfile from autobackup;

    Starting restore at 04-JUN-12
    using channel ORA_DISK_1

    recovery area destination: d:\oracle\fast_recovery_area
    database name (or database unique name) used for search: TESTDB
    channel ORA_DISK_1: no AUTOBACKUPS found in the recovery area
    channel ORA_DISK_1: looking for AUTOBACKUP on day: 20120604
    channel ORA_DISK_1: AUTOBACKUP found: f:\testdb\controlfile_bkup\CTL_c-2568236785-20120604-00
    channel ORA_DISK_1: restoring spfile from AUTOBACKUP f:\testdb\controlfile_bkup\CTL_c-2568236785-20120604-00
    channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete
    Finished restore at 04-JUN-12


    RMAN>


    5. Restore the CONTROLFILE from autobackup.


    RMAN> set dbid=2568236785

    executing command: SET DBID

    RMAN> SET CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO 'f:\testdb\controlfile_bkup\CTL_%F';

    executing command: SET CONTROLFILE AUTOBACKUP FORMAT

    RMAN> RESTORE CONTROLFILE FROM AUTOBACKUP;

    Starting restore at 04-JUN-12
    using channel ORA_DISK_1

    recovery area destination: d:\oracle\fast_recovery_area
    database name (or database unique name) used for search: TESTDB
    channel ORA_DISK_1: no AUTOBACKUPS found in the recovery area
    channel ORA_DISK_1: looking for AUTOBACKUP on day: 20120604
    channel ORA_DISK_1: AUTOBACKUP found: f:\testdb\controlfile_bkup\CTL_c-2568236785-20120604-00
    channel ORA_DISK_1: restoring control file from AUTOBACKUP f:\testdb\controlfile_bkup\CTL_c-2568236785-20120604-00
    channel ORA_DISK_1: control file restore from AUTOBACKUP complete
    output file name=F:\TESTDB\CONTROL01.CTL
    output file name=D:\ORACLE\FAST_RECOVERY_AREA\TESTDB\CONTROL02.CTL
    Finished restore at 04-JUN-12


    RMAN>


    6. Mount the Database TESTDB.

    RMAN> alter database mount;

    database mounted
    released channel: ORA_DISK_1


    7. Restore the Database.

    RMAN> restore database;

    Starting restore at 04-JUN-12
    Starting implicit crosscheck backup at 04-JUN-12
    allocated channel: ORA_DISK_1
    channel ORA_DISK_1: SID=10 device type=DISK
    allocated channel: ORA_DISK_2
    channel ORA_DISK_2: SID=11 device type=DISK
    Crosschecked 6 objects
    Finished implicit crosscheck backup at 04-JUN-12

    Starting implicit crosscheck copy at 04-JUN-12
    using channel ORA_DISK_1
    using channel ORA_DISK_2
    Finished implicit crosscheck copy at 04-JUN-12

    searching for all files in the recovery area
    cataloging files...
    no files cataloged

    using channel ORA_DISK_1
    using channel ORA_DISK_2

    channel ORA_DISK_1: starting datafile backup set restore
    channel ORA_DISK_1: specifying datafile(s) to restore from backup set
    channel ORA_DISK_1: restoring datafile 00002 to F:\TESTDB\SYSAUX01.DBF
    channel ORA_DISK_1: restoring datafile 00003 to F:\TESTDB\UNDOTBS01.DBF
    channel ORA_DISK_1: restoring datafile 00005 to F:\TESTDB\TBSRMAN01.DBF
    channel ORA_DISK_1: reading from backup piece F:\TESTDB\DATAFILE_BKUP\DBF_0INCNGIR_1_1
    channel ORA_DISK_2: starting datafile backup set restore
    channel ORA_DISK_2: specifying datafile(s) to restore from backup set
    channel ORA_DISK_2: restoring datafile 00001 to F:\TESTDB\SYSTEM01.DBF
    channel ORA_DISK_2: restoring datafile 00004 to F:\TESTDB\USERS01.DBF
    channel ORA_DISK_2: reading from backup piece F:\TESTDB\DATAFILE_BKUP\DBF_0HNCNGIR_1_1
    channel ORA_DISK_1: piece handle=F:\TESTDB\DATAFILE_BKUP\DBF_0INCNGIR_1_1 tag=TAG20120604T205137
    channel ORA_DISK_1: restored backup piece 1
    channel ORA_DISK_1: restore complete, elapsed time: 00:00:56
    channel ORA_DISK_2: piece handle=F:\TESTDB\DATAFILE_BKUP\DBF_0HNCNGIR_1_1 tag=TAG20120604T205137
    channel ORA_DISK_2: restored backup piece 1
    channel ORA_DISK_2: restore complete, elapsed time: 00:01:36
    Finished restore at 04-JUN-12


    RMAN>


    8. After restoring checked the status of the DATABASE.


    role from v$database;

    NAME      DB_UNIQUE_NAME                 OPEN_MODE            DATABASE_ROLE
    --------- ------------------------------ -------------------- ----------------
    TESTDB    testdb                         MOUNTED              PRIMARY

    SQL> archive log list;
    Database log mode              Archive Mode
    Automatic archival             Enabled
    Archive destination            f:\testdb\archives
    Oldest online log sequence     10
    Next log sequence to archive   12
    Current log sequence           12

    SQL>


    9.  Recover the DATABASE.

    RMAN> recover database;

    Starting recover at 04-JUN-12
    using channel ORA_DISK_1
    using channel ORA_DISK_2

    starting media recovery

    archived log for thread 1 with sequence 11 is already on disk as file F:\TESTDB\ARCHIVES\ARC0000000011_0784908660.0001
    archived log file name=F:\TESTDB\ARCHIVES\ARC0000000011_0784908660.0001 thread=1 sequence=11
    unable to find archived log
    archived log thread=1 sequence=12
    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure of recover command at 06/04/2012 22:15:20
    RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 12 and starting SCN of 1076477


    Note: From the above Error, use the Last SCN specified "1076477"

    RMAN> recover database until SCN 1076477;

    Starting recover at 04-JUN-12
    using channel ORA_DISK_1
    using channel ORA_DISK_2

    starting media recovery
    media recovery complete, elapsed time: 00:00:01

    Finished recover at 04-JUN-12

    RMAN>

    RMAN> alter database open;

    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure of alter db command at 06/04/2012 22:15:48
    ORA-01589: must use RESETLOGS or NORESETLOGS option for database open


    Note: Open the Database with RESETLOGS option.

    RMAN> alter database open resetlogs;

    database opened


    RMAN>


    10. After Recovering the DATABASE check the status.


    SQL> select name,db_unique_name,open_mode,database_role from v$database;

    NAME      DB_UNIQUE_NAME                 OPEN_MODE            DATABASE_ROLE
    --------- ------------------------------ -------------------- ----------------
    TESTDB    testdb                         READ WRITE           PRIMARY

    SQL> archive log list;
    Database log mode              Archive Mode
    Automatic archival             Enabled
    Archive destination            f:\testdb\archives
    Oldest online log sequence     1
    Next log sequence to archive   1
    Current log sequence           1
    SQL>

    SQL> desc scott.t1
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     EMPNO                                              NUMBER(4)
     ENAME                                              VARCHAR2(10)
     JOB                                                VARCHAR2(9)
     MGR                                                NUMBER(4)
     HIREDATE                                           DATE
     SAL                                                NUMBER(7,2)
     COMM                                               NUMBER(7,2)
     DEPTNO                                             NUMBER(2)

    SQL>


    NOTE:

    Before Deleting the DATABASE i had created a table T1 using scott schema.


    From the above status we can clearly see that the table T1 exists in scott schema.



    Monday, December 30, 2013

    Flashback the Database Using SCN




    Bringing Back The Database to Previous State after Importing a schema Using Flashback Technology         


    Step 1 : Firstly we should enable flashback on the database.


                          shutdown immediate;

         startup mount;        

         alter database flashback on;  

         alter database open;      



    Step 2: To check whether the flashback in enabled or not issue the below query.,

                         select flashback_on from v$database;


    SQL> select flashback_on from v$database;

    FLASHBACK_ON
    ------------------
    YES


    Step 3: create the schema to which you have to import the data (dump).


                           create user test identified by test default tablespace users;       

                           grant connect,resource,debug connect session,debug any procedure to test;     


    SQL> create user test identified by laser default tablespace users;

    User created.

    SQL> grant connect,resource to test;

    Grant succeeded.



    Step 4: Make sure that there is no data in the newly created schema "test".

         connect test/test@db3

         select * from tab;         


    The result will be "no rows selected"


    SQL> conn test/laser@db3

    Connected.

    SQL> select * from tab;

    no rows selected




    Step 5: We need to take a note of the current SCN (System Change Number) of the database. To know the 
    current SCN issue the below query.,


         select current_scn from v$database;     



    SQL> select current_scn from v$database;

    CURRENT_SCN
    -----------
           1039995



    Step 6: Import the data to the newly created schema 'test'


    SQL> host imp file='d:\db3\exp_scott.dmp' commit=y ignore=y fromuser=scott touser=test

    Import: Release 11.2.0.3.0 - Production on Mon Mar 4 12:46:15 2013

    Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

    Username: sys/oracle@db3 as sysdba

    Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options

    Export file created by EXPORT:V11.02.00 via direct path

    Warning: the objects were exported by SCOTT, not by you

    import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
    . importing SCOTT's objects into TEST
    . . importing table                        "BONUS"          0 rows imported
    . . importing table                         "DEPT"          4 rows imported
    . . importing table                          "EMP"         14 rows imported
    . . importing table                     "SALGRADE"          5 rows imported
    About to enable constraints...
    Import terminated successfully without warnings.


    Step 7: Check the tables of the new schema 'test'.


         connect test/laser@db3     

         select * from tab;        


    SQL> conn test/laser@db3
    Connected.
    SQL> select * from tab;

    TNAME                          TABTYPE  CLUSTERID
    ------------------------------ ------- ----------
    BONUS                          TABLE
    DEPT                           TABLE
    EMP                            TABLE
    SALGRADE                       TABLE




    Step 8: If you need to bring back the database to the previous stage (the stage before import) issue the following command.,

    NOte: You had taken the current scn of the database before importing the data.


         conn sys/password@db3 as sysdba

         Shutdown immediate;

         startup mount;

         flashback database to scn scn_no;

         alter database open resetlogs;



    SQL> conn sys/oracle@db3 as sysdba
    Connected.



    SQL> shut immediate
    Database closed.
    Database dismounted.
    ORACLE instance shut down.

    SQL> startup mount
    ORACLE instance started.

    Total System Global Area 1703624704 bytes
    Fixed Size                  2255864 bytes
    Variable Size             989856776 bytes
    Database Buffers          704643072 bytes
    Redo Buffers                6868992 bytes
    Database mounted.


    SQL> flashback database to scn 1039995;

    Flashback complete.


    SQL> alter database open resetlogs;

    Database altered.



    Step 9: Check the archive log list of the database. It will start from the sequence 1.


    archive log list;



    SQL> archive log list
    Database log mode              Archive Mode
    Automatic archival             Enabled
    Archive destination            D:\db3\arch
    Oldest online log sequence     1
    Next log sequence to archive   1
    Current log sequence           1


    Step 10: connect as schema 'test' and check whether there is any tables/data in it.,


        conn test/test@db3       

        select * from tab;                       



    SQL> conn test/laser@db3

    Connected.


    SQL> select * from tab;

    no rows selected