RMAN備份Shel指令碼示例

husthxd發表於2005-04-01

RMAN備份Shel指令碼示例

From : metalink.oracle.com


#! /bin/sh

# 使用者定義引數

charset="AMERICAN_AMERICA.ZHS16GBK" # 資料庫字符集
backup_dir=/mnt_pt/bkup # 磁碟備份路徑
rman_id=rman # RMAN使用者
rman_pw=rman # RMAN密碼
catalog_alias=rmancat #tnsnames.ora中的恢復目錄連線串

#
# 初始化引數
#

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

#
# 主體指令碼
#

outputfile=/tmp/$$$sid.bck

#
# 設定源資料庫的環境變數

#
#
ORACLE_SID=$sid;export ORACLE_SID
ORAENV_ASK=NO;export ORAENV_ASK
. oraenv
unset ORAENV_ASK

#
# 設定字符集

#
NLS_LANG=$charset;export NLS_LANG

#
# 初始化變數
#
db_status="CLOSED"
archive_log="NOARCHIVELOG"
log_seq=0

#
# 檢查資料庫狀態
#
pmon=`ps -ef | egrep pmon_$ORACLE_SID | grep -v grep`

if [ "$pmon" = "" ]; then
db_status="CLOSED"
else
db_status=`sqlplus -s "/ as sysdba" <set heading off
set feedback off
set verify off
select status from v$instance;
exit
EOF
`
fi

if [ $db_status = "MOUNTED" -o $db_status = "OPEN" ]; then
#
# 獲取資料庫的歸檔狀態
#
archive_log=`sqlplus -s "/ as sysdba" <set heading off
set feedback off
set verify off
select log_mode from v$database;
exit
EOF
`

#
# 獲取log sequence(三天前的歸檔日誌) 
#
log_seq=`sqlplus -s "/ as sysdba" <set heading off
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

#
# 建立備份指令碼

#

#
# 設定登陸使用者
#
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

 

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

相關文章