Partial Tablespace recovery using RMAN

 Prior to 12C: -

Scenario  : - We have to restore test database from PROD database, there was one schema in PROD database which holds users sensitive data, to secure that we have to take export backup of same schema from TEST and then drop the TEST database and restore PROD database as TEST and then drop sensitive data schema and import schema which we had taken from TEST database, but unfortunately that test dump got deleted from O/S after dropping the TEST database. :(

We have one day old RMAN level0 backup but still we cannot restore it because TEST database is of 6 TB, then we decided to do tablespace recovery. Below are the steps that we performed.

Note : Change file location and tablespace name as per your environment.

Restore the control file back that was taken after level0 backup.

Startup mount, and then get to know the max. sequence of archive file information from the v$log_history.
Select max(sequence#) from v$log_history;

Determine the list of tablespaces that needs to be restored. Look for sys objects in all tablespaces.

Build the set newname for datafiles for choosen tablespaces that needs to be restored.
select 'set newname for datafile '||d.file#||' to '||''''||'/RMAN_tmp/TST11G/oradata/'||substr(d.name,instr(d.name,'/',-1)+1)||''''||';'
from v$datafile d, v$tablespace t where t.ts#=d.ts# and t.name in ('USERS',’TST11G_D', ’TST11G_I','SYSTEM','UNDO_TS1','SYSAUX') order by 1;


Now, run the rman restore...
Export ORACLE_SID=TST11G
rman target = / <<! >TST11G_rman_restore.log
run {
     sql 'alter session set optimizer_mode=rule';
     allocate channel a1 type 'sbt_tape';
     allocate channel a2 type 'sbt_tape';
     allocate channel a3 type 'sbt_tape';

     allocate channel a4 type 'sbt_tape';
     allocate channel a5 type 'sbt_tape';
     allocate channel a6 type 'sbt_tape';
     allocate channel a7 type 'sbt_tape';
     allocate channel a8 type 'sbt_tape';
      send 'NB_ORA_SERV=denver, NB_ORA_CLIENT=TST11Gdb-g1';
      set until sequence 21496;

set newname for datafile 13 to '/u01/oradata/sysaux_2.dbf';
set newname for datafile 12 to '/u01/oradata/sysaux_02.dbf';
set newname for datafile 11 to '/u01/oradata/sysaux_01.dbf';
set newname for datafile 959 to '/u01/oradata/sysaux_03.dbf';
set newname for datafile 960 to '/u01/oradata/sysaux_05.dbf';
set newname for datafile 00919 to '/u01/oradata/undo_ts1_14.dbf';
set newname for datafile 00920 to
'/u01/oradata/undo_ts1_15.dbf';
set newname for datafile 00921 to '/u01/oradata/undo_ts1_16.dbf';

set newname for datafile 00475 to '/u01/oradata/undo_ts1_08.dbf';
set newname for datafile 00828  to '/u01/oradata/TST11G_lrg_i_15.dbf';
set newname for datafile 00888  to '/u01/oradata/TST11G_lrg_i_16.dbf';
     restore tablespace system;
     restore tablespace sysaux;
     restore tablespace undo_ts1;

     restore tablespace TST11G_MED_I;
     restore tablespace TST11G_LRG_I;
     restore tablespace TST11G_SML_D;
     restore tablespace TST11G_SML_I;
     restore tablespace TST11G_LRG_D;
     restore tablespace tools;
     restore tablespace audit_d;
     restore tablespace audit_i;
     restore tablespace users;
SWITCH DATAFILE ALL;
}
exit


Offline all other datafiles than restored tablespaces.
select 'alter database datafile '||''''||d.name||''''||' offline;'
from v$datafile d, v$tablespace t where t.ts#=d.ts# and t.name not in
('TOOLS','USERS','AUDIT_I','AUDIT_D','TST11G_MED_D',
'TST11G_MED_I','TST11G_LRG_I','TST11G_SML_D','TST11G_SML_I',
'TST11G_LRG_D','SYSTEM','UNDO_TS1','SYSAUX') order by 1;

Rename redologfile, before opening the database.
select 'alter database rename file '||''''||member||''''||' to '||''''||'/u02/oradata/RESTORE/REDO/'||substr(member,instr(member,'/',-1)+1)||''''||';' member from v$logfile;

Restore the all archive files.

We already have script to restore archive log, that we use to restore archives for goldengate.
run {
     sql 'alter session set optimizer_mode=rule';
     allocate channel a1 type 'sbt_tape';
     allocate channel a2 type 'sbt_tape';
     allocate channel a3 type 'sbt_tape';
     allocate channel a4 type 'sbt_tape';
     send 'NB_ORA_SERV=denver, NB_ORA_CLIENT=tbmktgdb-g1';
     restore archivelog from logseq 21496 until logseq 21508;
    }

 First logseq will be the sequence that needs to start recovery and the second logseq we can get from rman backup log or from v$log_history.

Start recovery.
Recover database using backup controlfile until cancel;
AUTO

Use resetlogs to open the database.
Alter database open resetlogs;
Select name, open_mode from v$database; 

Take the export backup of that schema, and import it newly refreshed database from PROD.




Row count of all the table under any schema

Some times we may get requirement to gather the row count of all the tables under any specific schema.

So there is a very easy way to get the same detail as below.


Query :

SELECT 

table_name, 

TO_NUMBER( 

EXTRACTVALUE( 

XMLTYPE( 

DBMS_XMLGEN.getxml('SELECT COUNT(*) c FROM '||table_name)) 

,'/ROWSET/ROW/C')) cnt 

FROM user_tables order by 

table_name; 



Demonstration :

SQL> show user

USER is "BRIJ"

SQL> set lines 300 pages 1000

SQL> col TABLE_NAME for a22

SQL> SELECT

table_name,

TO_NUMBER(

EXTRACTVALUE(

XMLTYPE(

DBMS_XMLGEN.getxml('SELECT COUNT(*) c FROM '||table_name))

,'/ROWSET/ROW/C')) cnt

FROM user_tables order by

table_name;

TABLE_NAME                    CNT

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

COUNTRY                         4

DPT                             8

EMP                            20

JOBS                           12

JOB_GRADE                       6

JOB_HIST                       10

LOC                             5

REGION                          4

 

8 rows selected.


Oracle Import (Impdp) slowness Issue.

 

Recently we faced an issue where 10GB dump was taking 10Hrs to complete in one of our oracle database 12.1.0.2 version.

In import we were using 2 parameter TABLE_EXIST_ACTION=TRUNCATE and CONTENT=DATA_ONLY.

We tried everything like importing without CONTENT=DATA_ONLY, applied one bug patch which was for Import slow during importing a partition table, but none has worked.

Later identified that there is a primary constraint on the partition table with ~500+ partitions.

Hence, I disabled the constraint and did the same import and this time import completed in 4 minute and enabling constraint took 3 minutes, so my total import too7 minutes which was earlier taking ~10hrs.


Fixing standby GAP issue with Primary using SCN backup

 Step 1:  The Current SCN of the Physical Standby Database.

Standby Database:


SQL> select name,open_mode from v$database;


NAME      OPEN_MODE

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

XYZDB     MOUNTED


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


NAME      OPEN_MODE  DATABASE_ROLE

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

XYZDB     MOUNTED    PHYSICAL STANDBY

 

SQL> select db_unique_name, to_char(current_scn, '999999999999999999') as Standby_Controlfile_SCN from v$database;

 

DB_UNIQUE_NAME                 STANDBY_CONTROLFILE

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

XYZDB                            10861224114929


Note down the CURRENT_SCN value of the standby database (10861224114929) to proceed further.

 

Last Sequence received:


SQL> select thread#, max(sequence#) "Last Standby Seq Received"

from v$archived_log val, v$database vdb

where val.resetlogs_change# = vdb.resetlogs_change#

group by thread# order by 1;

 

THREAD#    Last Standby Seq Received

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

1             266428


Last sequence applied :


SQL> select thread#, max(sequence#) "Last Standby Seq Applied"

from v$archived_log val, v$database vdb

where val.resetlogs_change# = vdb.resetlogs_change#

and applied='YES'

group by thread# order by 1; 

 

THREAD#    Last Standby Seq Applied

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

1             255271


Step 2:  Cancel the Managed Recovery Process on the Standby database.


Standby Database:


SQL> select PROCESS,STATUS,CLIENT_PROCESS from v$managed_standby;

PROCESS   STATUS       CLIENT_P

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

ARCH      CONNECTED    ARCH

ARCH      CONNECTED    ARCH

SQL> alter database recover managed standby database cancel;

alter database recover managed standby database cancel

*

ERROR at line 1:

ORA-16136: Managed Standby Recovery not active


Step 3:


Primary database  :


Taken  the incremental SCN backup from the SCN that is currently recorded on the standby database.


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


NAME      OPEN_MODE  DATABASE_ROLE

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

XYZDB     READ WRITE PRIMARY


SQL> select db_unique_name, to_char(current_scn, '999999999999999999') as Primary_Controlfile_SCN from v$database;

 

DB_UNIQUE_NAME                 PRIMARY_CONTROLFILE

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

XYZDB                            10878875895876


RMAN Backup script:


vi rman_standby_10NOV2012.sh


export ORACLE_SID=XYZDB

rman target sys/******* <<! >rman_incremental_10NOV2012.log

run

{

ALLOCATE CHANNEL C1 DEVICE TYPE DISK ;

ALLOCATE CHANNEL C2 DEVICE TYPE DISK ;

ALLOCATE CHANNEL C3 DEVICE TYPE DISK ;

backup incremental from scn 10861224114929 database format ‘/u01/apps/oracle/admin/XYZDB/bkup/RmanIncremental_%U_%d_%T’;

backup current controlfile for standby format '/u01/apps/oracle/admin/XYZDB/bkup/control_file_%T';

RELEASE CHANNEL C1;

RELEASE CHANNEL C2;

RELEASE CHANNEL C3;

}

exit

!


$ nohup sh rman_standby_10NOV2012.sh &


Step 4: Transfer the backups from the Primary  to the Standby .


Primary Database


scp RmanIncremental_06npu8a1_1_1_XYZDB_20121110 oracle@usa..com:/global/CEXOE/XYZDB/RMAN_CENXCT_BK

Password:xxxxx

scp RmanIncremental_05npu8a1_1_1_XYZDB_20121110 oracle@usa..com:/global/CEXOE/XYZDB/RMAN_CENXCT_BK

Password:xxxxx

scp RmanIncremental_04npu8a0_1_1_XYZDB_20121110 oracle@usa..com:/global/CEXOE/XYZDB/RMAN_CENXCT_BK

Password:xxxxx

scp control_file_20121110 oracle@usa..com:/global/CEXOE/XYZDB/RMAN_CENXCT_BK

Password:xxxxx

 

Step 5:


 Shutdown the physical standby database, start it in nomount stage and restore the standby control file and RMAN Incremental backup that we had taken from the primary database.


Standby Database:


$ rman target /

Recovery Manager: Release 10.2.0.4.0 - Production on Sat Nov 10 07:28:38 2012

 

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

connected to target database (not started)

RMAN> startup nomount

Oracle instance started

Total System Global Area    3154116608 bytes

Fixed Size                     2142240 bytes

Variable Size               1201623008 bytes

Database Buffers            1811939328 bytes

Redo Buffers                 138412032 bytes

 

RMAN> restore standby controlfile from '/global/CEXOE/XYZDB/RMAN_CENXCT_BK/control_file_20121110';

Starting restore at 10-NOV-12

using target database control file instead of recovery catalog

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=702 devtype=DISK

channel ORA_DISK_1: restoring control file

channel ORA_DISK_1: restore complete, elapsed time: 00:00:03

output filename=/global/CEXOE/XYZDB/oradata/group1/XYZDB_control01.ctl

output filename=/global/CEXOE/XYZDB/oradata/group2/XYZDB_control02.ctl

output filename=/global/CEXOE/XYZDB/oradata/group3/XYZDB_control03.ctl

Finished restore at 10-NOV-12


Backup restoration


RMAN> alter database mount;

database mounted

released channel: ORA_DISK_1

RMAN> catalog start with '/global/CEXOE/XYZDB/RMAN_CENXCT_BK';

searching for all files that match the pattern /global/CEXOE/XYZDB/RMAN_CENXCT_BK

List of Files Unknown to the Database

=====================================

File Name: /global/CEXOE/XYZDB/RMAN_CENXCT_BK/RmanIncremental_06npu8a1_1_1_XYZDB_20121110

File Name: /global/CEXOE/XYZDB/RMAN_CENXCT_BK/RmanIncremental_05npu8a1_1_1_XYZDB_20121110

File Name: /global/CEXOE/XYZDB/RMAN_CENXCT_BK/RmanIncremental_04npu8a0_1_1_XYZDB_20121110

File Name: /global/CEXOE/XYZDB/RMAN_CENXCT_BK/control_file_20121110

Do you really want to catalog the above files (enter YES or NO)? yes

cataloging files...

cataloging done

Step 6:

 Recover the standby database with the cataloged incremental backup pieces.

RMAN> recover database noredo;

Starting recover at 10-NOV-12

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=701 devtype=DISK

channel ORA_DISK_1: starting incremental datafile backupset restore

channel ORA_DISK_1: specifying datafile(s) to restore from backup set

destination for restore of datafile 00004: /global/CEXOE/XYZDB/oradata/group1/XYZDB_tools01a.dbs

channel ORA_DISK_1: restored backup piece 1

piece handle=/global/CEXOE/XYZDB/RMAN_CENXCT_BK/RmanIncremental_06npu8a1_1_1_XYZDB_20121110 tag=TAG20121110T044444

channel ORA_DISK_1: restore complete, elapsed time: 00:00:47

channel ORA_DISK_1: starting incremental datafile backupset restore

piece handle=/global/CEXOE/XYZDB/RMAN_CENXCT_BK/RmanIncremental_05npu8a1_1_1_XYZDB_20121110 tag=TAG20121110T044444

channel ORA_DISK_1: restore complete, elapsed time: 00:00:37

channel ORA_DISK_1: starting incremental datafile backupset restore

channel ORA_DISK_1: specifying datafile(s) to restore from backup set

piece handle=/global/CEXOE/XYZDB/RMAN_CENXCT_BK/RmanIncremental_04npu8a0_1_1_XYZDB_20121110 tag=TAG20121110T044444

channel ORA_DISK_1: restore complete, elapsed time: 00:00:26

Finished recover at 10-NOV-12


Step 7:

 

Shutdown and startup mount standby database.


Standby Database:


RMAN> shutdown immediate

Oracle instance shut down


RMAN> startup mount


Note: List the files that have had nologging changes applied on the standby database


SQL> SELECT FILE#, FIRST_NONLOGGED_SCN FROM V$DATAFILE WHERE FIRST_NONLOGGED_SCN > 0;

no rows selected


SQL> !date

Saturday, November 10, 2012  7:41:28 AM EST


Step 8:


Standby Database:


SQL> select max(sequence#) from v$log_history;

MAX(SEQUENCE#)

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

268176


SQL> archive log list;

Database log mode              Archive Mode

Automatic archival             Enabled

Archive destination            /u01/apps/oracle/admin/XYZDB/arch

Oldest online log sequence     268173

Next log sequence to archive   268177

Current log sequence           268177


SQL> exit

Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options



Enable the  Manager Recover process


SQL> RECOVER AUTOMATIC STANDBY DATABASE;


SQL>alter database recover managed standby database dissconenct from session;


Step9:


 Primary Database:


SQL>  select max(sequence#) from v$log_history;


MAX(SEQUENCE#)

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

268207


Sql> alter system set log_archive_dest_2_state=enable scope=both;

 

Standby side


SQL> select max(sequence#) from v$log_history;

MAX(SEQUENCE#)

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

 268204

Add new mountpoint on your linux server

  Below are the steps to follow for adding any new mount on you linux machine. [root@oem ~]# fdisk -l Disk /dev/sdb: 53.7 GB, 53687091200 by...