RMAN Backup Shell Script Example (Doc ID 137181.1)

rongshiyuan發表於2014-05-13

RMAN Backup Shell Script Example (Doc ID 137181.1)


Abstract

RMAN Backup Shell Script Example

 

Product Name, Product Version

RMAN, 08.01.XX to 09.02.XX

Platform

Platform Independent

Date Created

31-Dec-2002

 

Instructions

Checked for relevance on 24-MAR-2012.
 
Execution Environment:

  Execution Environment:

    

   1) Create a catalog database (rmancat) if one does not exist (With additional

RBS-, 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;

 Access Privileges:

        User that is member of dba group.

        Permission to write to directory where backups will be stored when doing 

backup to disk.

    

    

Usage:

        backup_database  

        

where           

sid = ORACLE_SID of target instance to be backed up       

backup_type =  disk | tape

Ex:
backup_database v817 tape <= Backup database v817 to tape
or
backup_database v817 disk <= Backup database v817 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, DG, HP, Linux, Sequent, SGI, 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

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.

 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.

 

Description

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/.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/_snapshot.ctl" and allocate 1 channel based on the device_type specified. The backup will name the datafile backups as "/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 "/%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/.bck" and $logfile is the rman log "/tmp/rman_.log". The command file is then removed.

Check the file "/tmp/rman_.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: ===========================================================

 

 

References

Oracle8i Recovery Manager User's Guide and Reference

    

Note:76736.1 : "RMAN FAQ: Recovery Manager -- Frequently Asked Questions"

    

Note:164521.1 : "NLS Setting Using RMAN"

    

Note:121112.1 : "Setting Default Location of RMAN Snapshot Control File"

 

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" < set feedback off
set verify 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" < set feedback off
set verify off
select log_mode from v\\$database;
exit
EOF
`

#
# Get the logsequence meeting the archivelog-criteria
#
log_seq=`sqlplus -s "/ as sysdba" < set feedback off
set verify off
select min(sequence#) from v\\$archived_log
where deleted='NO' and archived = 'YES'
and first_time <= (sysdate - 3);
exit
EOF
`

if [ "$log_seq" = "" ]; then
log_seq=0
fi

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

#
# Set controlfile snapshot location
#
echo "" >> $outputfile
echo "set snapshot controlfile name to '/tmp/"$sid"_snapshot.ctl';" >> $outputfile
echo "" >> $outputfile

#
# 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
echo " database;" >> $outputfile
echo "" >> $outputfile

#
# backup the archivelogs always to disk and if requested to tape.
#

if [ $archive_log = "ARCHIVELOG" ]; then
if [ $db_status = "OPEN" ]; then
echo " sql 'alter system archive log current';" >> $outputfile
echo "" >> $outputfile
fi

if [ $backup_type = "tape" ]; then
echo " backup" >> $outputfile
echo " format '%d/%d_al_t%t_s%s_p%p'" >> $outputfile
echo " filesperset=12" >> $outputfile
echo " (archivelog all);" >> $outputfile
echo "" >> $outputfile
fi

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

#
# If no archive meet the criteria : (sysdate - 3) then backup all
# available archives. This way RMAN won't fail with an error message.
#
if [ $log_seq -gt 0 ]; then
echo " (archivelog until time 'sysdate - 3'" >> $outputfile
echo " delete input);" >> $outputfile
else
echo " (archivelog all); " >> $outputfile
fi
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.

 

 

 

 

References

NOTE:121112.1 - Setting Default Location of RMAN Snapshot Control File
NOTE:137181.1 - RMAN Backup Shell Script Example
NOTE:164521.1 - NLS Setting Using RMAN
NOTE:76736.1 - RMAN FAQ: Recovery Manager -- Frequently Asked Questions

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17252115/viewspace-1160643/,如需轉載,請註明出處,否則將追究法律責任。

相關文章