Thursday, 14 September 2017

Oracle Home and GRID backup command



Create a directory to the target where you wish to take the backup as 

mkdir abc
cd abc
mkdir backup
cd backup

pwd

/abc/backup

Now set the ASM environment and execute as root user for GRID backup

tar -cvpf /abc/backup/abc_GRIDBKP.tar $ORACLE_HOME 

and for RDBMS set any DB environment and as a oracle user 

tar -cvpf /abc/backup/abc_RDBMSBKP.tar $ORACLE_HOME  $ORACLE_HOME 

Thursday, 7 September 2017

How to unregister the database from catalog

Steps - 

1)
SQL> conn rman@RMANCAT (CATALOG SERVER DATABASE) 
Enter password:
Connected.

2)
SQL> select NAME,DBID,DB_KEY from rc_database where name='ABC';

NAME           DBID     DB_KEY
-------- ---------- ----------
ABC      2311686309    5021072

3)
SQL> EXECUTE rman.dbms_rcvcat.unregisterdatabase(5021072,2311686309);

PL/SQL procedure successfully completed.

4)
SQL> select NAME,DBID,DB_KEY from rc_database where name='ABC';


no rows selected

Script to moving objects from one tablespace to another tablespace

There are many occasions where we will move segments from one tablespace to another like reclaim unused space in the segments after deleting rows.
We can create scripts to move different type of segments from one tablespace to another using following SQL commands.
Here the scripts will be created to move objects (actually segments) from the tablespace USERS to USERS_TEMP. 
You can change the tablespace names accordingly.

Moving Tables and Un-partitioned Indexes

set lines 200
set pages 0
set feedback off
spool move.sql
select
'alter ' || segment_type || ' ' || owner || '.' || segment_name || decode( segment_type, 'TABLE', ' move', ' rebuild' ) ||
' tablespace USERS_TEMP' || ';'
from dba_segments
where tablespace_name='USERS' 
and segment_type in('TABLE','INDEX') order by owner, segment_type desc, segment_name;
spool off

Now edit the file move.sql to remove first few lines which will be the command above and run it to move all tables from one tablespace to another and to rebuild all indexes from one partition to another.

@move.sql

Moving index partitions

set lines 200
set pages 0
set feedback off
spool move.sql
select 'alter index '|| owner||'.'||segment_name || ' rebuild partition '|| partition_name ||' tablespace USERS_TEMP;'
from dba_segments where segment_type ='INDEX PARTITION' and tablespace_name='USERS';
spool off

Now edit the file move.sql to remove first few lines which will be the command above and run it to move or rebuild all index partitions from one tablespace to another

@move.sql

Moving index sub-partitions

set lines 200
set pages 0
set feedback off
spool move.sql
select 'alter index '||owner||'.'||segment_name ||' rebuild subpartition ' || partition_name ||' tablespace USERS_TEMP;'      
from dba_segments where tablespace_name='USERS' and segment_type = 'INDEX SUBPARTITION';
spool off

Now edit the file move.sql to remove first few lines which will be the command above and run it to move or rebuild all index subpartition segments from one tablespace to another

@move.sql

Moving IOT index segments (When only IOT indexes left on the tablespace)

set lines 200
set pages 0
set feedback off
spool move.sql
select 'alter table '||owner||'.'|| table_name || ' move tablespace USERS_TEMP;' from dba_indexes where index_name in (
select  segment_name from dba_segments
where tablespace_name='USERS' and segment_type='INDEX');
spool off

Now edit the file move.sql to remove first few lines which will be the command above and run it to move all IOT segments form one tablespace to another

@move.sql

Moving LOB segments

set lines 200
set pages 0
set feedback off
spool move.sql
select 'alter table '|| owner || '.' ||table_name|| ' move LOB ('||COLUMN_NAME ||') STORE AS (tablespace USERS_TEMP);'
from DBA_LOBS where tablespace_name='USERS';
spool off

Now edit the file move.sql to remove first few lines which will be the command above and run it to move all LOB segments from one tablespace to another

@move.sql

------------------------------------------------------------------------------------------------
reference -  http://jporacledb.blogspot.in/2014/12/script-to-moving-objects-from-one.html

Add Data file

 ************************************************** Database status Tablespace Usage ASM Disk Group Usage(RAC) Backup Filesystem Usage Alert...