Friday, November 9, 2012

Oracle RMAN backup script

                          Oracle RMAN back up script 

A detailed review 

Recovery Manager (RMAN) is an Oracle utility that can backup, restore and recover backup files.This will install along with oracle DB and communicates database server sessions to communicate each other for backup and restore. Let's look what is the benefits of RMAN than conventional backup mechanism .



Makes recovery time proportional to the amount of data recovered 
 Make backups proportional to the size of transitional changes, not to the size of the database. 
 Manage the complexity of backup and restore mechanism .
 Minimize the possibility of human errors .
 Make backups scalable  and reliable .
 Utilize all available hardware. 
There are two methods to take backups in oracle
a. Operating system commands to perform backup and SQL or SQL*Plus commands for recovery
b.Using RMAN to perform backup , restore and recovery.













RMAN Environment

a. RMAN executable

RMAN executable are automatically included with oracle installation and as default it is located at $ORACLE_HOME/bin .

b.RMAN target database

Target database is the database which you are going to take backup,restore.A single recovery cataloge* can be used to store metadata of multipe databases.

*Recovery cataloge is a set of tables and views used by RMAN to store the information about the target database and RMAN uses this cataloge to backup,restore and recover the database.

c. RMAN repository 

RMAN repository is the metadata which RMAN uses to store target database information and its backup and recovery operations .This will contain backup set pieces, target database schema, persistent configuration settings etc etc.

d. RMN media management Interface

This will be used for storing backup's in external media like tape drives. RMAN media interface will loads , unloads and labels backup media's .




RMAN Sample Script and its description 


















Connecting to the RMAN

We can use different methods to connect to RMAN. Here i am using connecting to the target database using OS credentials as default. You can see mounted target database given as MBDB.








As per the sample script RMAN backup activities are pointed to a log file (/home/oracle/logs/ARTLBK/rmanlogs/backupincremental_0.$BACKUP_DAY.log) where $BACKUP_DAY is the folder for each days back up which is exported as an environment variable initially with the script .

Allocating the channel 

RMAN channel represents a stream of data to a device type from an established server session and backup, restore and recovery operations will be carried out through this established channel. as per our script we are creating 2 channels for the backup channel c1 and channel c2 and mentioned as device type and disk ( we are taking the backup in local disk ) . Also i want the backup should be happen at non default location (/oradata/backup/$BACKUP_DAY) because of the space constrain, so mentioned the same along with channel allocation here .



Backup format 


%d- Name of the backup
%T-Year,Month,Day (YYYYMMDD)
%t-Back up set time stamp which is a 4 byte value
%s-Back up set number and which is used for unique identification of the back up sets.
%p-The piece number with in the back up set and same will be incremented by 1.

Type of backups 

Full Backup- Full back up will back up all data files in the DB as block by block . Using a full back up you can recover the entire at that point of time.

Level1 Backup- This will take the back up of the blocks which are changed from last parent backup (Level0 or Level1). (please check the sample script given above)

Level0 Backup- This is almost identical as full back up except the empty blocks . 

Differential Incremental Backup -  As per this backup RMAN looks for the changed data blocks since from either last  Level0 or Level1backups.

Cumulative Incremental Backup- Same as differential backup incremental backup also takes the changed data blocks from last Level0 or Level1 backup and the exception is if the last backup is an incremental backup, this will take the backup of changes of changes which will reduce the restoration timing.


Also to remove old incremental backups ( becouse i am planning to take full backup on weekly basis) command is given to delete all incremental backups older than 8 days .

Listing the Backups 


You can view the list of backups using 
RMAN> LIST BACKUP; command 












Listing the Archive logs 

Archive logs can be cross checked using 
RMAN> LIST COPY; command 











Configuring the back up script in crontab 


I have configured the back up script in crontab which will be run at 2:30 AM every day