ITPub博客

首页 > Linux操作系统 > Linux操作系统 > RMAN备份Shel脚本示例

RMAN备份Shel脚本示例

原创 Linux操作系统 作者:husthxd 时间:2005-04-01 00:00:00 0 删除 编辑

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/,如需转载,请注明出处,否则将追究法律责任。

上一篇: AskTom笔记(1)
下一篇: AskTom笔记(2)
请登录后发表评论 登录
全部评论
ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。现就职于广州云图数据技术有限公司,系统架构师。

注册时间:2007-12-28

  • 博文量
    1393
  • 访问量
    3840458