Thursday, March 3, 2011

Backups using RMAN


How to take backup of the entire database?  i.e database files and the control files

 RMAN won't take backup of log files.
 RMAN>backup database;

If you want to take a complete backup as a compressed backup and also specify the location with control file, then use the following syntax


RMAN> backup as compressed backupset format 'Desired location' include current controlfile;

    It will backup entire database files along with control file.

How to take backup of the entire tablespace?

RMAN>backup tablespace tablespacename;
eg: RMAN>backup tablespace SYSTEM;

It will take backup of all data files belong to SYSTEM tablespace.
Assume system contains 3 datafiles it will take all dbf files
How to take backup of a particular datafile?

RMAN>backup datafile '/home/sales/userdata.dbf';
This will take backup of only specified file.             

How to take backup of control file using RMAN?

sql>conn /as sysdba
sql>select cotnrolfile_type from v$database;

 CURRENT
--------------------------------------------------
RMAN>backup current cotnrolfile;

Note: if the target database is running using spfile.
Then only we can take spfile backup.

RMAN>backup spfile;

How to take backup of archive logs?

RMAN>backup archivelog all;

How to take the backup of database plus archive log files?

RMAN>backup database plus archivelog;
 This will take datafiles, controlfiles and arhivelogs.

Practical demo
----------------
RMAN>connect catalog rman/rman@to_sales;
RMAN>connect target sys/prd@to_sales;
RMAN>backup database;
------------------------------------------------------------------
Note: cp command will simply copy whether the block is corrupted
      or not. RMAN won't take the backup in the corrupted  blocks.     
------------------------------------------------------------------
go to serverside and check it.
]$cd $ORACLE_HOME/dbs
dbs]$ls
Here we can find backup files.
------------------------------------------------------------
Take backup of tablespace
------------------------
RMAN>backup tablespace system;
RMAN>report schema;

Take backup of datafile
-----------------------
rman>backup datafile 'path';
rman>list backup;
(we can check whether the backups are available or not)


Detailed information
rman>list backup summary;

How to delete rman backups?

RMAN>list backup summary;
RMAN>delete backupset backupsetkey;
eg: RMAN>delete backupset 51;
    (enter yes/no)?yes

You can check
RMAN>list backup summary;

It is displaying all

How to delete all?

If we don't want confirmation
RMAN>delete noprompt backupset backupsetno;
eg: RMAN>delete noprompt backupset 52;

RMAN>delte backup; 
The above command will delete everything.

RMAN>list backup summary;
20090907
--------------------------------------------------------------
RMAN>backup database;
RMAN>list backup summary;
RMAN>delete force backup;
RMAN>list backup;
--------------------------------------------------------
How to take backup at desired location?
 With the help of format command we can keep the backup files in particular location.

DEMO
----
On the target (server side) go to opt directory
opt]$mkdir backup
login as root user and give the permissions

opt]$ls

backup

These format specifiers are used with format command

 %d - It indicates database name
 %u - 8 character unique id
 %T --Current system date

 backupfilename_%d_%T20090907

RMAN>backup tablespace userdata format '/opt/backup/rman_%d_%T';
------------------------------------------------------



Take complete backup for recovery purposes
-------------------------------------------
RMAN> backup database;

Starting backup at 29-AUG-09
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/home/sales/system.dbf
input datafile fno=00002 name=/home/sales/undosales.dbf
input datafile fno=00003 name=/home/sales/sysaux.dbf
input datafile fno=00004 name=/home/sales/userdata.dbf
channel ORA_DISK_1: starting piece 1 at 29-AUG-09
channel ORA_DISK_1: finished piece 1 at 29-AUG-09
piece handle=/home/oracle/product/10.2.0/db_1/dbs/11knu0kf_1_1 tag=TAG20090829T1 75942 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
channel ORA_DISK_1: starting piece 1 at 29-AUG-09
channel ORA_DISK_1: finished piece 1 at 29-AUG-09
piece handle=/home/oracle/product/10.2.0/db_1/dbs/12knu0km_1_1 tag=TAG20090829T1 75942 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 29-AUG-09
----------------------------------------


RMAN> 

RMAN Concepts


 RMAN is a oracle utility (tool) that is used to take backup, or 
do restore database or perform recovery.

Features of RMAN
--------------
i) It is free, comes with server software.
ii) It provides faster and safest backup.
iii) It provides faster recovery.
iv) It provides centralized backup restore and recovers the database.
v) Only with the help of rman we can take physical incremental   backup.

Some basic terms
-----------------
TARGET DATABASE:   Database that rman uses to take backup, recovery operations.
CATALOG DATABASE SCHEMA:  Is a schema where rman stores set of tables to keep information of the target database.

          RMAN                           PROD

        machine1        -------->       machine2 

        Client                           Server

RMAN REPOSITORY
---------------
 It is a collection of metadata of the target database that includes database name, database id, complete database schema information.

    machine1 (UK)        machine2 (USA)      machine3 (Singapore)

Configure the listener

|       register             |                          |
|                            |                          |                               
                     Machine (RMAN client)
                (Catalog database information)

 tnsnames.ora must be configured on the client side. Here you have to install oracle server software (client side)                         
RMAN keeps repository information in the target database control file itself. Optionally it is stored in catalog database schema.
The backups are stored in the server only.
 First preference goes to flash_recovery_area(default backup location is FRA)

RMAN BACKUP LOCATION:
--------------------
 If you have not given any instructions to store the backup files.Then in this case the target database side server process initially it will look for the flash_recovery_area (FRA),  if it is not configured it will keep all backup files  in this location $ORACLE_HOME/dbs

How to configure server for RMAN
--------------------------------
CATALOG                                          TARGET                                                                                                      

                                                SYS user
 machine1                                       machine2
PRD (client)                                   SALES (server)

Client side: tnsnames.ora required    Server side: listener.ora       
                                                   required and      
                                                   configure 
                                                   password file.
                                                  Target database  
                                                   must be in  
                                                 Archive Log Mode.


i)                    Create one user rman, here schema name is rman
ii)                  Go to OS level
$cd $ORACLE_HOME/bin
 ]$./rman
RMAN> connect catalog rman/rman;
 RMAN> create catalog;
---------------------------------------------------------------
    Next we have to connect to the server to perform administrative tasks on the remote server
     We have to create a password file the target (server) side.

      ]$cd $ORACLE_HOME/dbs
 Syntax:
      ]$orapwd file=ora<sid> password=<password>
      ]$orapwd file=orapwsales password=sales
-----------------------------------------------------------------
 Syntax for remote server connection

         RMAN>connect target sys/password@connectstring;
         RMAN>connect target sys/sales@to_sales;
         here to_sales is a connect string.

       RMAN>register database;
        I am registering target database info on the client side.
    -------------------------------------------------------- 
       sql>connect rman/rman;
        sql>select * from rc_database;
     here u will find how many databases are registered with the client.
  ---------------------------------------------------------------
Practical Demo:
---------------
  Server side

]$sqlplus '/as sysdba'
sql>startup
sql> select name from v$database;
sql> select name from v$controlfile;
sql>archive log list;
       Archive log mode
sql>host

]$cd /home/oracle/product/10.2.0/db_1/network/admin
admin]$netca
        listener name is listener
        use default portno i.e 1521
( listener is a server process to handle the client requests)
admin]$lsnrctl stop
          stop the listener
admin]$ls
admin]$vi listener.ora
 --------------------------------------------

SID_LIST_LISTENER=
     (SID_LIST=
        (SID_DESC=
           (SID_NAME= sales)
            (ORACLE_HOME=/home/oracle/product/10.2.0/db_1)
  )
)

LISTENER =
  (DESCRIPTION_LIST=
(DESCRIPTION=
      (ADDRESS=(PROTOCOL=TCP)(HOST=192.168.100.2)(PORT=1521)
   )
)
]$lsnrctl start

Creating password file on the serverside
 ]$cd $ORACLE_HOME/dbs
dbs]$orapwd file=orapwsales password=sales
dbs]$
              client side (catalog side)
            ----------------------------------
configure tnsnames.ora on the client side
]$cd $ORACLE_HOME/network/admin
admin]$netca
         - select local netservice name configuration
         - service name: sales  (server database name)
         - hostname: 192.168.100.2
        - don't test it
        - Net service name: to_sales (connect string)
         - Next àNext  à Finish

admin]$vi tnsnames.ora
-----------------------------------------
TO_SALES =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.2)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = sales)
    )
  )
------------------------------------------
Syntax for connectivity
------------------------
admin]$tnsping connectstring
admin]$tnsping to_sales
[oracle@dbaschool admin]$ tnsping to_sales

TNS Ping Utility for Linux: Version 10.2.0.1.0 - Production on 29-AUG-2009 17:11:27
Copyright (c) 1997, 2005, Oracle.  All rights reserved.
Used parameter files:
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.2)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = sales)))
OK (0 msec)

[oracle@dbaschool admin]$
]$sqlplus '/as sysdba'
sql>startup
sql>select * from all_users;
create one tablespace on the client side
sql>create tablespace catlg datafile '/home/prd/catlg.dbf' size  200m (min 30mb required);
sql> create user rman identified by rman default tablespace catlg;
sql>grant connect,resource , recovery_catalog_owner to rman;
sql>select * from all_users;

]$cd $ORACLE_HOME/BIN
BIN]$./rman
RMAN>connect catalog rman/rman@to_sales;
RMAN>create catalog;
recovery catalog created
RMAN>connect target sys/sales@to_sales
--------------------------------------------------------------------------
     RMAN> connect target sys/sales@to_sales

RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-06004: ORACLE error from recovery catalog database: RMAN-20001: target data base not found in recovery catalog

RMAN>
---------------------------------------------------------------------------    
         bcz we didn't register
-------------------------------------------------------------------
sql>select * from rman.rc_database;
      no database is registered
----------------------------------
RMAN>register database;
------------------------------
sql>select * from rc_database;
       Database is registered
----------------------------------
RMAN>report schema;
for checking backups
---------------------
RMAN>list backup;
        Nothing

RMAN>
 BACKUP: Backup command is used to take the backup of the database.
 RESTORE:  RESTORE command is used to take restore the backup file. (Physically copied into the respective locations)

 RECOVER: RECOVER command is used to recover the database.

Oracle Database & Applications R12.2 Log file locations

Many times, we forget the path for the log files since there are too many.  Below is the list of frequently used log files which can help us...