|---------------------------------------------------------------------------------|---------------------------------------------------------------------|
|
| RMAN Backup Shell Script Example for 10g onwards (Doc ID 1415373.1) |
Goal
RMAN Backup Shell Script Example
Solution
NOTE: In the images and/or the document content below, the user information and environment data used represents fictitious data from the Oracle sample schema(s), Public Documentation delivered with an Oracle database product or other training material. Any similarity to actual environments, actual persons, living or dead, is purely coincidental and not intended in any manner.
For the purposes of this document, the following fictitious environment is used as an example to describe the procedure:
RMAN schema username: rman
RMAN catalog TNS connection: rmancat
Example SID: v112
Backup directory for disk backup: /mnt_pt/bkup
Catalog_alias: rmancat
Script $outputfile: /tmp/<pid><sid>.bck
Script $logfile: /tmp/rman_<sid>.log
*******************
Requirements
-
Create a catalog database if one does not exist (With additional UNDO-, USERS- and TEMP-tablespaces)
-
Configure SQL*Net to handle the catalog database and the target database connections.
-
Create a user 'RMAN' in the catalog database.
SQL> create user rman identified by <password>
default tablespace USERS quota unlimited on users
temporary tablespace TEMP;
SQL> grant recovery_catalog_owner to rman;
- Create the RMAN-catalog in the catalog database.
$ rman catalog rman/<password>@rmancat
RMAN> create catalog;
- Register the target-database with catalog:
Set environment(ORACLE_HOME, ORACLE_SID and PATH) to target-database.
$ rman catalog rman/<password>@rmancat target /
RMAN> register database;
Access Privileges:
- User that is member of dba group.
- Permission to write to directory where backups will be stored when doing backup to disk.
Configuring
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
- Verify that catalog is running and you can connect as rman via sqlnet.
% sqlplus rman/rman@rmancat
- 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
- 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
- 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';
- Use above nls information to edit script to change charset parameter.
charset="LANGUAGE_TERRITORY.CHARACTERSET" # Characterset of the database
- 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
- Run script
NOTE: If instance is not mounted or open when running script it will only backup datafiles and not backup
archivelogs if they exist. In doing so the instance will be open to mount mode in order to do the backup and be left in that state unless the "Restore to original mode" code is uncommented.
Script
PROOFREAD THIS SCRIPT BEFORE USING IT! Due to differences in the way text
editors, e-mail packages, and operating systems handle text formatting (spaces,
tabs, and carriage returns), this script may not be in an executable state
when you first receive it. Check over the script to ensure that errors of
this type are corrected.The script will produce an output file named [outputfile].
This file can be viewed in a browser or uploaded for support analysis.
This script takes input of target instance sid to backup and device type to backup to. It takes the target instance sid and sets the environment using the Oracle supplied "oraenv" script. It then checks the status of the instance to see what mount condition it is in and if running in mount or open mode checks to see if it is running in archivelog mode and what log sequence it is at.
With this is information it creates a rman backup command file "/tmp/<pid><sid>.bck" to perform the backup. This command file will connect to the catalog and target instance and put the instance in mount mode if not already mounted or open. It will then set the snapshot controlfile location to "/tmp/<sid>_snapshot.ctl" and allocate 1 channel based on the device_type specified. The backup will name the datafile backups as "<backup_dir>/d%_t%t_s%s_p%p" if device type is disk or as "%d/d%_t%t_s%s_p%p" if device type is tape. The parameter filesperset is set to 4 so each backup set will have 4 datafiles. If in archivelog mode, a log switch is performed, and if device type is tape, all archive logs will be backed up to tape with name of "%d/%d_al_t%t_s%s_p%p" at 12 files per set. Also, regardless of device type, archive logs that have entries (first_time) older than 3 days will be backed up to disk, at 12 files per set, with name of "<backup_dir>/%d_al_t%t_s%s_p%p" and then deleted from archivelog destination. If no archivelog with entries older than 3 days are found then all archivelogs will be backed up to disk. The "%" variables have the following meaning:
%d = Database name
%t = Backup set timestamp
%s = Backup set number
%p = Backup piece number
Once the command file is created then rman utility is launched with syntax of "rman cmdfile=outputfile msglog=logfile" where outputfile is the command file "/tmp/\
Check the file "/tmp/rman_<sid>.log" for result of the backup. If any errors occur then this file will have following section at the bottom of the file followed by the errors that occurred.
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
#! /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 <sid> <backup_type>"
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" <<EOF
set heading off pages 0 feedback off
select status from v\\$instance;
exit
EOF
`
fi
if [ db_status = "MOUNTED" -o db_status = "OPEN" ]; then
Get the archivelog-mode of the database
archive_log=`sqlplus -s "/ as sysdba" <<EOF
set heading off pages 0 feedback off
select log_mode from v\\$database;
exit
EOF
`
fi
Create backup script with info from source-database.
Setup userid and connections
echo "#" > $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
DISCLAIMER
EXCEPT WHERE EXPRESSLY PROVIDED OTHERWISE, THE INFORMATION, SOFTWARE,
PROVIDED ON AN "AS IS" AND "AS AVAILABLE" BASIS. ORACLE EXPRESSLY DISCLAIMS
ALL WARRANTIES OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NON-INFRINGEMENT. ORACLE MAKES NO WARRANTY THAT: (A) THE RESULTS
THAT MAY BE OBTAINED FROM THE USE OF THE SOFTWARE WILL BE ACCURATE OR
RELIABLE; OR (B) THE INFORMATION, OR OTHER MATERIAL OBTAINED WILL MEET YOUR
EXPECTATIONS. ANY CONTENT, MATERIALS, INFORMATION OR SOFTWARE DOWNLOADED OR
OTHERWISE OBTAINED IS DONE AT YOUR OWN DISCRETION AND RISK. ORACLE SHALL HAVE
NO RESPONSIBILITY FOR ANY DAMAGE TO YOUR COMPUTER SYSTEM OR LOSS OF DATA THAT
RESULTS FROM THE DOWNLOAD OF ANY CONTENT, MATERIALS, INFORMATION OR SOFTWARE.
ORACLE RESERVES THE RIGHT TO MAKE CHANGES OR UPDATES TO THE SOFTWARE AT ANY
TIME WITHOUT NOTICE.
Limitation of Liability
IN NO EVENT SHALL ORACLE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL OR CONSEQUENTIAL DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE,
DATA OR USE, INCURRED BY YOU OR ANY THIRD PARTY, WHETHER IN AN ACTION IN
CONTRACT OR TORT, ARISING FROM YOUR ACCESS TO, OR USE OF, THE SOFTWARE.
SOME JURISDICTIONS DO NOT ALLOW THE LIMITATION OR EXCLUSION OF LIABILITY.
ACCORDINGLY, SOME OF THE ABOVE LIMITATIONS MAY NOT APPLY TO YOU.
Sample Output
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 '/<path>/%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: <dbname> (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=/<path>/<dbname>_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=/<path>/datafile/o1_mf_sysaux_6c0tltsv_.dbf
input datafile file number=00004 name=/<path>/datafile/o1_mf_undotbs1_6c0tohcs_.dbf
input datafile file number=00003 name=/<path>/datafile/o1_mf_sys_undo_6c0tly50_.dbf
input datafile file number=00006 name=/<path>/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=/<path>/<dbname>_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=/<path>/<dbname>_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=/<path>/<dbname>_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.