Pre-requisites
1. Create a catalog database (rmancat) if one does not exist. (With additional UNDO-, USERS- and TEMP-tablespaces)
2. Configure SQL*Net to handle the catalog (rmancat) and the ‘target’ database connections.
3. Create a user ‘RMAN’ in the catalog database.
SQL> create user rman identified by rman default tablespace USERS quota unlimited on users temporary tablespace TEMP;
SQL> grant recovery_catalog_owner to rman;
4. Create the RMAN-catalog in the catalog database.
$ rman catalog rman/rman@rmancat RMAN> create catalog;
5. Register the target-database with catalog. Set environment(ORACLE_HOME, ORACLE_SID and PATH) to target-database.
$ rman catalog rman/rman@rmancat target / RMAN> register database;
6. Access Privileges:
– User that is member of dba group.
– Permission to write to directory where backups will be stored when doing backup to disk.
Script Usage
Usage: backup_database [sid] [backup_type] where, sid = ORACLE_SID of target instance to be backed up backup_type = disk | tape
Example:
% backup_database v112 tape #### Backup database v112 to tape
or
% backup_database v112 disk #### Backup database v112 to disk
Instructions
1. Verify that catalog is running and you can connect as rman via sqlnet.
% sqlplus rman/rman@rmancat
2. Using the information to connect to catalog via sqlplus, set following parameters in script.
rman_id=rman # RMAN userid rman_pw=rman # RMAN password catalog_alias=rmancat # Catalog connect script from tnsnames.ora
3. Determine which instance to backup (target). The target instance to backup must be listed in the following file
For Solaris : /var/opt/oracle/oratab
For AIX, HPUX, Linux, HP Tru64 : /etc/oratab
4. Determine target instance nls setting for nls_lang parameter.
SQL> select value from v$nls_parameters where parameter = 'NLS_LANGUAGE'; SQL> select value from v$nls_parameters where parameter = 'NLS_TERRITORY'; SQL> select value from v$nls_parameters where parameter = 'NLS_CHARACTERSET';
5. Use above nls information to edit script to change charset parameter.
charset="LANGUAGE_TERRITORY.CHARACTERSET" # Characterset of the database
6. Determine if backup will be to disk or tape.
– If to disk
a) Determine location that backup will go to and verify you have permission to write to the directory.
b) Edit script to change “backup_dir” variable to reflect this directory
– If to tape
a) Verify that tape is mounted.
b) Run following command to verify that oracle can communicate with tape media management layer
% $ORACLE_HOME/bin/sbttest test
7. Run script.
The RMAN script
#! /bin/sh # User defined parameters charset="AMERICAN_AMERICA.WE8ISO8859P15" # Characterset of the database backup_dir=/mnt_pt/bkup # Back directory for disk backup rman_id=rman # RMAN userid rman_pw=rman # RMAN password catalog_alias=rmancat # Catalog connect script from tnsnames.ora # # Initialization parameters # if [ -z "$1" -o -z "$2" ]; then echo "" echo " ERROR : Invalid number of arguments" echo " Usage : backup_database" exit fi sid=$1 # Source database backup_type=$2 # Backuptype : disk or tape # # Main # outputfile=/tmp/$$$sid.bck # # Setup environment to the source-database # Requires : oraenv => must be found as executable # /var/opt/oracle/oratab or /etc/oratab => Must contain the source database # ORACLE_SID=$sid;export ORACLE_SID ORAENV_ASK=NO;export ORAENV_ASK . oraenv unset ORAENV_ASK # # Set the charaterset to the characterset of the database : # for suppressing error-messages of RMAN regarding this issue # NLS_LANG=$charset;export NLS_LANG # # Initialize variables # db_status="CLOSED" archive_log="NOARCHIVELOG" log_seq=0 # # Check status of database # pmon=`ps -ef | egrep pmon_$ORACLE_SID | grep -v grep` if [ "$pmon" = "" ]; then db_status="CLOSED" else db_status=`sqlplus -s "/ as sysdba" < $outputfile echo "connect catalog $rman_id/$rman_pw@$catalog_alias" >> $outputfile echo "connect target /" >> $outputfile # # Instance must be running in mount mode to do backups. Mount instance # if it is closed or in nomount mode. # echo "" >> $outputfile if [ $db_status = "CLOSED" ]; then echo "startup mount;" >> $outputfile fi echo "" >> $outputfile if [ $db_status = "STARTED" ]; then echo "alter database mount;" >> $outputfile fi # # Begin backup process # echo "run " >> $outputfile echo "{ " >> $outputfile # # If the database does not run in ARCHIVELOG, it must # be a cold-backup, so a shutdown of the database is required. # Uncomment the following code to add code to do this. # - Begin of shutdown code #if [ $archive_log = "NOARCHIVELOG" -a $db_status = "OPEN" ]; then # echo " shutdown immediate;" >> $outputfile # echo " startup mount;" >> $outputfile # echo "" >> $outputfile #fi # - End of shutdown code # Otherwise the program will be terminated so user can manually # shutdown instance when desired. If the above code to automate # shutdown is used then comment out the termination section below. # - Begin termination code if [ $archive_log = "NOARCHIVELOG" -a $db_status = "OPEN" ]; then echo "The database is running in NOARCHIVELOG mode and must be" echo "shutdown first to do a cold backup. Terminating backup_database." rm $outputfile exit fi # - End termination code # # Depending the backup_type-argument : # Choose the desired channel allocation # if [ $backup_type = "tape" ]; then echo " allocate channel ch1 type 'sbt_tape';" >> $outputfile else echo " allocate channel ch1 type disk;" >> $outputfile fi echo "" >> $outputfile echo "" >> $outputfile echo " backup" >> $outputfile # # Depending the backup_type-argument : # Choose the desired format. # For the backup on disk : it's the path where the backup will # be put. # if [ $backup_type = "disk" ]; then echo " format '$backup_dir/%d_t%t_s%s_p%p'" >> $outputfile else echo " format '%d/%d_t%t_s%s_p%p'" >> $outputfile fi echo " filesperset=4" >> $outputfile # # backup the archivelogs always to disk and if requested to tape. # if [ $archive_log = "ARCHIVELOG" ]; then echo " database plus archivelog filesperset 12;" >> $outputfile echo "" >> $outputfile if [ $backup_type = "tape" ]; then echo " release channel ch1;" >> $outputfile echo " allocate channel ch1 type disk;" >> $outputfile echo " backup" >> $outputfile echo " format '$backup_dir/%d_al_t%t_s%s_p%p'" >> $outputfile echo " filesperset=12" >> $outputfile echo " (archivelog until time 'sysdate - 3'" >> $outputfile echo " delete input);" >> $outputfile echo "" >> $outputfile else echo " delete archivelog until time 'sysdate - 3' backed up 1 times to device type disk;" >> $outputfile fi else echo " database;" >> $outputfile echo "" >> $outputfile fi # # Restore instance to original mode. # Uncomment following code to return instance to close or nomount mode. # - Begin restore code #if [ $db_status = "CLOSED" -o $db_status = "STARTED" ]; then # echo "shutdown immediate;" >> $outputfile # if [ $db_status = "STARTED" ]; then # echo "startup nomount;" >> $outputfile # fi #fi # - End restore code echo "" >> $outputfile echo " release channel ch1;" >> $outputfile echo "}" >> $outputfile # # Execute created script with rman. # logfile=/tmp/rman_$sid.log rm $logfile 2>/dev/null rman cmdfile=$outputfile msglog=$logfile echo "" echo "The rman backup for $sid has completed with results written to file $logfile." echo "" rm $outputfile
Sample Output
Here is a sample output from the script execution:
Recovery Manager: Release 11.2.0.2.0 - Production on Thu Mar 15 16:59:21 2012 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. RMAN> # 2> connect catalog * 3> connect target * 4> 5> 6> run 7> { 8> allocate channel ch1 type disk; 9> 10> 11> backup 12> format '/u01/oradata/rzy/%d_t%t_s%s_p%p' 13> filesperset=4 14> database plus archivelog filesperset 12; 15> 16> delete archivelog until time 'sysdate - 3' backed up 1 times to device type disk; 17> 18> release channel ch1; 19> } 20> connected to recovery catalog database connected to target database: V1122 (DBID=4126790627) allocated channel: ch1 channel ch1: SID=146 device type=DISK Starting backup at 15-MAR-2012 16:59:25 current log archived channel ch1: starting archived log backup set channel ch1: specifying archived log(s) in backup set input archived log thread=1 sequence=4900 RECID=5511 STAMP=772265173 .... input archived log thread=1 sequence=4910 RECID=5532 STAMP=772534947 channel ch1: starting piece 1 at 15-MAR-2012 16:59:32 channel ch1: finished piece 1 at 15-MAR-2012 17:02:28 piece handle=/u01/oradata/rzy/V1122_t778006893_s1186_p1 tag=TAG20120315T165930 comment=NONE channel ch1: backup set complete, elapsed time: 00:00:55 Finished backup at 15-MAR-2012 17:02:28 Starting backup at 15-MAR-2012 17:17:59 channel ch1: starting full datafile backup set channel ch1: specifying datafile(s) in backup set input datafile file number=00002 name=/u01/oradata/v1122/V1122/datafile/o1_mf_sysaux_6c0tltsv_.dbf input datafile file number=00004 name=/u01/oradata/v1122/V1122/datafile/o1_mf_undotbs1_6c0tohcs_.dbf input datafile file number=00003 name=/u01/oradata/v1122/V1122/datafile/o1_mf_sys_undo_6c0tly50_.dbf input datafile file number=00006 name=/u01/oradata/v1122/V1122/datafile/o1_mf_users_6jycwgkf_.dbf channel ch1: starting piece 1 at 15-MAR-2012 17:18:02 .... channel ch1: finished piece 1 at 15-MAR-2012 17:24:02 piece handle=/u01/oradata/rzy/V1122_t778008017_s1190_p1 tag=TAG20120315T171759 comment=NONE channel ch1: backup set complete, elapsed time: 00:03:46 channel ch1: starting full datafile backup set channel ch1: specifying datafile(s) in backup set including current control file in backup set including current SPFILE in backup set channel ch1: starting piece 1 at 15-MAR-2012 17:24:06 channel ch1: finished piece 1 at 15-MAR-2012 17:24:11 piece handle=/u01/oradata/rzy/V1122_t778008243_s1191_p1 tag=TAG20120315T171759 comment=NONE channel ch1: backup set complete, elapsed time: 00:00:05 Finished backup at 15-MAR-2012 17:24:11 Starting backup at 15-MAR-2012 17:24:12 current log archived channel ch1: starting archived log backup set channel ch1: specifying archived log(s) in backup set input archived log thread=1 sequence=4926 RECID=5548 STAMP=778008252 channel ch1: starting piece 1 at 15-MAR-2012 17:24:17 channel ch1: finished piece 1 at 15-MAR-2012 17:24:18 piece handle=/u01/oradata/rzy/V1122_t778008257_s1192_p1 tag=TAG20120315T172415 comment=NONE channel ch1: backup set complete, elapsed time: 00:00:01 Finished backup at 15-MAR-2012 17:24:18 specification does not match any archived log in the repository released channel: ch1 Recovery Manager complete.