mercoledì 7 novembre 2012

Oracle Database: Schedule hot backups using RMAN

In this article I will schedule periodic hot backups of a running database instance using rman.

To perform hot backups using rman, first you need to check if your database is in ArchiveLog mode.

[oracle@db1 ~]$ sqlplus "/ as sysdba"

SQL> SELECT log_mode FROM v$database;

LOG_MODE
------------
NOARCHIVELOG


If not change it:

SQL> SHUTDOWN IMMEDIATE;

SQL> STARTUP MOUNT;

SQL> ALTER DATABASE ARCHIVELOG;

SQL> ALTER DATABASE OPEN;

SQL> SELECT log_mode FROM v$database;

LOG_MODE
------------
ARCHIVELOG


Once it's in ArchiveLog mode create the main rman script which will be called by crontab:

[oracle@db1 ~]$ vi /u01/rman_main

ORACLE_HOSTNAME=db1
export ORACLE_HOSTNAME
ORACLE_UNQNAME=orcl
export ORACLE_UNQNAME
ORACLE_BASE=/u01/app/oracle
export ORACLE_BASE
ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export ORACLE_HOME
ORACLE_SID=orcl
export ORACLE_SID

/u01/app/oracle/product/11.2.0/db_1/bin/rman target=/ @/u01/rman_script


Where ORACLE_HOSTNAME is your machine hostname and ORACLE_UNQNAME and ORACLE_SID is the SID of the instance you need to backup.

[oracle@db1 ~]$ chmod a+x /u01/rman_main
Then we create the rman script itself:

[oracle@db1 ~]$ vi /u01/rman_script

run {
  allocate channel ch1 type disk format '/u01/backup%d_DB_%u_%s_%p';
  backup database;
  backup archivelog all;
  release channel ch1;
}



This will backup the database creating a file under "/u01/" directory.
In the above script %d, %u, %s, %p are substitution variables for generating unique file name, if you wish to know more about these variables have a look HERE.

You could manually run rman_main to take hot backups, but if you like to run scheduled backups we need to add this to crontab:

[root@db1 ~]# crontab -e -u oracle

I will schedule backup everyday at 18:10 (6.10 PM)

10 18 * * * /u01/rman_main


The first five fields are:

minute (0-59)
hour (0-23)
day of the month(1-31)
month of the year (1-12)
day of the week (0-6 with 0 = Sunday)

For more info on crontab have a look at crontab.org

NOTE: At first I was unable to edit this file, so after a brief search I had to set EDITOR variable according to my preferred file editor.

In my case:

[root@db1 ~]# export EDITOR=nano

[root@db1 ~]# crontab -e -u oracle


That's all!!

1 commento: