---备份自动数据库脚本
10 10 * * 6 su - oracle -c "/nfsbk/script/rmbk.sh 0 oral /nfsbk/rmanfs 10 15 4"
10 10 * * 3 su - oracle -c "/nfsbk/script/rmbk.sh 1 oral /nfsbk/rmanfs 10 15 4"
通过传入参数,实现全库备份或增量备份,可定制日志和数据库备份保留策略;
<eof
<eof
<eof|$tee -a="" $logfile<eof
<eof|$tee -a="" $logfile<eof|$tee -a="" $logfile<eof|$tee -a="" $logfile<eof|$tee -a="" $logfile#!/bin/sh
# 编写: 刘建 2010.01.15(版本1.0)
# update:wcj 2018/5/25 10:11:10
# 功能: 源数据库根据事先定制的备份策略,定期执行数据备份任务。
# 调用方式:RmanBackup.sh 备份模式 ORACLE_SID 备份的目标文件夹 本地归档日志存放有效天数 备份数据有效天数 备份进程并行度
# 注:备份模式 0: 0级全备份 1: 1级增量备份
# 例如: ./RmanBackup.sh 0 orcl /opt/ora10g/backup 3 3 2
# 执行方式:(1)以oracle用户身份登录linux后手工执行
# (2)以oracle用户身份自动执行的crontab任务
#
#mkdir -p /backup/rmanfs
#mkdir -p /backup/script
#--上传备份脚本
#chown -R oracle:oinstall /backup
#测试
#./rmbk.sh 0 oral /backup/rmanfs 15 5 2
#计划任务调用
#crontab -e
#10 20 1,10,20 * * su - oracle -c "/backup/script/rmbk.sh 0 oral /backup/rmanfs 10 5 4"
#10 20 * * 5 su - oracle -c "/backup/script/rmbk.sh 0 oral /backup/rmanfs 8 8 8"
#10 20 * * 1,2,3,4,6,7 su - oracle -c "/backup/script/rmbk.sh 1 oral /backup/rmanfs 8 8 8"
##########################################################################################################################
###################################################
#开始脚本的各项初始化工作
#先核对命令行中,参数是否齐备
if [ $# -lt 6 ]
then
echo "==========================================================================================================="
echo "Usage:"
echo "RmanBackup.sh {BackupMode} {ORACLE_SID} {BackupDirectory} {ArchRetainDay} {BakFileRetainDay} {Parallelism} "
echo "RmanBackup.sh 备份模式0或1 ORACLE_SID 备份目录 归档日志保留天数 备份数据保留天数 备份并行度"
echo "请详细核对命令行中需要设置的6个参数是否完整:"
echo "==========================================================================================================="
exit 1
fi
#执行变量
source $HOME/.bash_profile
TEE=/usr/bin/tee
RMAN=$ORACLE_HOME/bin/rman
SQLPLUS=$ORACLE_HOME/bin/sqlplus
export ORACLE_SID=$2
#目录变量
BACKUPDEST=$3
if [ ! -d $BACKUPDEST ]
then
echo "请详细核对“备份的目标文件夹”参数是否正确,此文件夹无法正常访问!"
exit 1
fi
#日志文件
if [ ! -d $BACKUPDEST/backupLogs ]
then
mkdir $BACKUPDEST/backupLogs
fi
LOGFILE=$BACKUPDEST/backupLogs/rmanbackup_`date +%Y%m%d%H%M%S`.log
#检查数据库是否已经处于“归档模式”下
$SQLPLUS "/ as sysdba" <<EOF
spool AL.log
SELECT LOG_MODE FROM V\$DATABASE;
spool off
EXIT
EOF
STATUS=`grep -i "NOARCHIVELOG" AL.log|wc -l`
if [ $STATUS -eq 1 ]
then
echo "当前ORACLE数据库未运行在“归档模式”下,不能进行联机热备,请完善相关配置!"
exit 1
fi
###########################################################
#激活“块跟踪”功能,并设置块跟踪日志文件的文件名及其存放路径
if [ ! -d $ORACLE_BASE/blocktrack ]
then
mkdir $ORACLE_BASE/blocktrack
fi
#检查是否已经激活了“块跟踪”功能
$SQLPLUS "/ as sysdba" <<EOF
spool BCT.log
SELECT STATUS FROM V\$BLOCK_CHANGE_TRACKING;
spool off
EXIT
EOF
STATUS=`grep -i "ENABLED" BCT.log|wc -l`
if [ $STATUS -eq 0 ]
then
$SQLPLUS "/ as sysdba" <<EOF|$TEE -a $LOGFILE
ALTER DATABASE ENABLE BLOCK CHANGE TRACKING USING FILE '$ORACLE_BASE/blocktrack/blcokchangetrack.f';
EXIT
EOF
fi
###########################################################
#修改初始化参数---控制文件保存备份文件信息的天数,扩大为1年
$SQLPLUS "/ as sysdba" <<EOF
spool KD.log
SHOW PARAMETER control_file_record_keep_time
spool off
EXIT
EOF
STATUS=`grep -i "integer" KD.log| awk '{print $3}'`
if [ $STATUS -lt 365 ]
then
$SQLPLUS "/ as sysdba" <<EOF|$TEE -a $LOGFILE
ALTER SYSTEM SET CONTROL_FILE_RECORD_KEEP_TIME = 365 SCOPE=BOTH;
EXIT
EOF
fi
###########################################################
# 防止RMAN可能对部分过期备份文件漏删除,这里用OS的命令补做
#find $BACKUPDEST -name "20*_L0_*.bak" -mtime +$5 -exec rm {} ;
#find $BACKUPDEST -name "20*_L1_*.bak" -mtime +$5 -exec rm {} ;
#find $BACKUPDEST -name "20*_c-*" -mtime +$5 -exec rm {} ;
###########################################################
#开始执行备份工作
#开始0级备份
if [ $1 -eq 0 ]
then
$RMAN <<EOF|$TEE -a $LOGFILE
CONNECT TARGET
CONFIGURE BACKUP OPTIMIZATION ON;
#若开启CF的自动备份规则,那么RMAN会在每个数据文件发生备份时产生1个控制文件备份(仅第一个记录在案),
#这势必造成备份文件过多的问题,因此需要屏蔽此规则
#CONFIGURE CONTROLFILE AUTOBACKUP on;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$BACKUPDEST/%T_control_%F.bak';
#备份文件的保持策略(即保留天数,过期将会被delete noprompt obsolete语句自动删除)
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF $5 DAYS;
#并行度(决定开启并发的备份通道个数,多并发可以加快备份速度,但要和CPU核数、磁盘实际的吞吐能力匹配)
CONFIGURE DEVICE TYPE DISK PARALLELISM $6 BACKUP TYPE TO BACKUPSET;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
#0级全备份;控制文件(spfile也会包含)需同步备份,且每次backup仅备份产生1个控制文件备份。
BACKUP as compressed backupset INCREMENTAL LEVEL 0 FORMAT '$BACKUPDEST/%T_db_L0_%s_%p.bak' section size 30g TAG 'BackupL0' DATABASE INCLUDE CURRENT CONTROLFILE;
#因为在备份过程中,某些数据文件可能仍然被修改(已经备份的文件的SCN会比控制文件最后记录的SCN要小),仍然产生redoLog,
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
crosscheck archivelog all;
backup as compressed backupset archivelog all format '$BACKUPDEST/%T_arch_%s_%p.bak';
#备份完成后,自动清除处于失效期的本地归档日志(即仅保留 $4 天之内的)
DELETE NOPROMPT ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-$4';
#根据先前制定的备份保留策略,自动删除过期失效文件
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
DELETE NOPROMPT OBSOLETE;
EXIT;
EOF
else
####################################################################################################
#开始1级备份
$RMAN <<EOF|$TEE -a $LOGFILE
CONNECT TARGET
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$BACKUPDEST/%T_control_%F.bak';
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF $5 DAYS;
CONFIGURE DEVICE TYPE DISK PARALLELISM $6 BACKUP TYPE TO BACKUPSET;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
BACKUP as compressed backupset INCREMENTAL LEVEL 1 FORMAT '$BACKUPDEST/%T_db_L1_%s_%p.bak' section size 30g TAG 'BackupL1' DATABASE INCLUDE CURRENT CONTROLFILE;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
crosscheck archivelog all;
backup as compressed backupset archivelog all not backed up 1 times format '$BACKUPDEST/%T_arch_%s_%p.bak';
DELETE NOPROMPT ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-$4';
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
DELETE NOPROMPT OBSOLETE;
EXIT;
EOF
fi
#备份控制文件
#rm $ORACLE_BASE/new.ctl
#不要直接备份到$BACKUPDEST目录
#$SQLPLUS "/ as sysdba" <<EOF|$TEE -a $LOGFILE
#ALTER DATABASE BACKUP CONTROLFILE TO '$ORACLE_BASE/new.ctl';
#exit;
#EOF
#cp $ORACLE_BASE/new.ctl $BACKUPDEST/`date +%Y%m%d%H%M%S`.ctl
</eof
</eof
</eof
10 10 * * 6 su - oracle -c "/nfsbk/script/rmbk.sh 0 oral /nfsbk/rmanfs 10 15 4"
10 10 * * 3 su - oracle -c "/nfsbk/script/rmbk.sh 1 oral /nfsbk/rmanfs 10 15 4"
通过传入参数,实现全库备份或增量备份,可定制日志和数据库备份保留策略;
<eof
<eof
<eof|$tee -a="" $logfile<eof
<eof|$tee -a="" $logfile<eof|$tee -a="" $logfile<eof|$tee -a="" $logfile<eof|$tee -a="" $logfile#!/bin/sh
# 编写: 刘建 2010.01.15(版本1.0)
# update:wcj 2018/5/25 10:11:10
# 功能: 源数据库根据事先定制的备份策略,定期执行数据备份任务。
# 调用方式:RmanBackup.sh 备份模式 ORACLE_SID 备份的目标文件夹 本地归档日志存放有效天数 备份数据有效天数 备份进程并行度
# 注:备份模式 0: 0级全备份 1: 1级增量备份
# 例如: ./RmanBackup.sh 0 orcl /opt/ora10g/backup 3 3 2
# 执行方式:(1)以oracle用户身份登录linux后手工执行
# (2)以oracle用户身份自动执行的crontab任务
#
#mkdir -p /backup/rmanfs
#mkdir -p /backup/script
#--上传备份脚本
#chown -R oracle:oinstall /backup
#测试
#./rmbk.sh 0 oral /backup/rmanfs 15 5 2
#计划任务调用
#crontab -e
#10 20 1,10,20 * * su - oracle -c "/backup/script/rmbk.sh 0 oral /backup/rmanfs 10 5 4"
#10 20 * * 5 su - oracle -c "/backup/script/rmbk.sh 0 oral /backup/rmanfs 8 8 8"
#10 20 * * 1,2,3,4,6,7 su - oracle -c "/backup/script/rmbk.sh 1 oral /backup/rmanfs 8 8 8"
##########################################################################################################################
###################################################
#开始脚本的各项初始化工作
#先核对命令行中,参数是否齐备
if [ $# -lt 6 ]
then
echo "==========================================================================================================="
echo "Usage:"
echo "RmanBackup.sh {BackupMode} {ORACLE_SID} {BackupDirectory} {ArchRetainDay} {BakFileRetainDay} {Parallelism} "
echo "RmanBackup.sh 备份模式0或1 ORACLE_SID 备份目录 归档日志保留天数 备份数据保留天数 备份并行度"
echo "请详细核对命令行中需要设置的6个参数是否完整:"
echo "==========================================================================================================="
exit 1
fi
#执行变量
source $HOME/.bash_profile
TEE=/usr/bin/tee
RMAN=$ORACLE_HOME/bin/rman
SQLPLUS=$ORACLE_HOME/bin/sqlplus
export ORACLE_SID=$2
#目录变量
BACKUPDEST=$3
if [ ! -d $BACKUPDEST ]
then
echo "请详细核对“备份的目标文件夹”参数是否正确,此文件夹无法正常访问!"
exit 1
fi
#日志文件
if [ ! -d $BACKUPDEST/backupLogs ]
then
mkdir $BACKUPDEST/backupLogs
fi
LOGFILE=$BACKUPDEST/backupLogs/rmanbackup_`date +%Y%m%d%H%M%S`.log
#检查数据库是否已经处于“归档模式”下
$SQLPLUS "/ as sysdba" <<EOF
spool AL.log
SELECT LOG_MODE FROM V\$DATABASE;
spool off
EXIT
EOF
STATUS=`grep -i "NOARCHIVELOG" AL.log|wc -l`
if [ $STATUS -eq 1 ]
then
echo "当前ORACLE数据库未运行在“归档模式”下,不能进行联机热备,请完善相关配置!"
exit 1
fi
###########################################################
#激活“块跟踪”功能,并设置块跟踪日志文件的文件名及其存放路径
if [ ! -d $ORACLE_BASE/blocktrack ]
then
mkdir $ORACLE_BASE/blocktrack
fi
#检查是否已经激活了“块跟踪”功能
$SQLPLUS "/ as sysdba" <<EOF
spool BCT.log
SELECT STATUS FROM V\$BLOCK_CHANGE_TRACKING;
spool off
EXIT
EOF
STATUS=`grep -i "ENABLED" BCT.log|wc -l`
if [ $STATUS -eq 0 ]
then
$SQLPLUS "/ as sysdba" <<EOF|$TEE -a $LOGFILE
ALTER DATABASE ENABLE BLOCK CHANGE TRACKING USING FILE '$ORACLE_BASE/blocktrack/blcokchangetrack.f';
EXIT
EOF
fi
###########################################################
#修改初始化参数---控制文件保存备份文件信息的天数,扩大为1年
$SQLPLUS "/ as sysdba" <<EOF
spool KD.log
SHOW PARAMETER control_file_record_keep_time
spool off
EXIT
EOF
STATUS=`grep -i "integer" KD.log| awk '{print $3}'`
if [ $STATUS -lt 365 ]
then
$SQLPLUS "/ as sysdba" <<EOF|$TEE -a $LOGFILE
ALTER SYSTEM SET CONTROL_FILE_RECORD_KEEP_TIME = 365 SCOPE=BOTH;
EXIT
EOF
fi
###########################################################
# 防止RMAN可能对部分过期备份文件漏删除,这里用OS的命令补做
#find $BACKUPDEST -name "20*_L0_*.bak" -mtime +$5 -exec rm {} ;
#find $BACKUPDEST -name "20*_L1_*.bak" -mtime +$5 -exec rm {} ;
#find $BACKUPDEST -name "20*_c-*" -mtime +$5 -exec rm {} ;
###########################################################
#开始执行备份工作
#开始0级备份
if [ $1 -eq 0 ]
then
$RMAN <<EOF|$TEE -a $LOGFILE
CONNECT TARGET
CONFIGURE BACKUP OPTIMIZATION ON;
#若开启CF的自动备份规则,那么RMAN会在每个数据文件发生备份时产生1个控制文件备份(仅第一个记录在案),
#这势必造成备份文件过多的问题,因此需要屏蔽此规则
#CONFIGURE CONTROLFILE AUTOBACKUP on;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$BACKUPDEST/%T_control_%F.bak';
#备份文件的保持策略(即保留天数,过期将会被delete noprompt obsolete语句自动删除)
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF $5 DAYS;
#并行度(决定开启并发的备份通道个数,多并发可以加快备份速度,但要和CPU核数、磁盘实际的吞吐能力匹配)
CONFIGURE DEVICE TYPE DISK PARALLELISM $6 BACKUP TYPE TO BACKUPSET;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
#0级全备份;控制文件(spfile也会包含)需同步备份,且每次backup仅备份产生1个控制文件备份。
BACKUP as compressed backupset INCREMENTAL LEVEL 0 FORMAT '$BACKUPDEST/%T_db_L0_%s_%p.bak' section size 30g TAG 'BackupL0' DATABASE INCLUDE CURRENT CONTROLFILE;
#因为在备份过程中,某些数据文件可能仍然被修改(已经备份的文件的SCN会比控制文件最后记录的SCN要小),仍然产生redoLog,
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
crosscheck archivelog all;
backup as compressed backupset archivelog all format '$BACKUPDEST/%T_arch_%s_%p.bak';
#备份完成后,自动清除处于失效期的本地归档日志(即仅保留 $4 天之内的)
DELETE NOPROMPT ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-$4';
#根据先前制定的备份保留策略,自动删除过期失效文件
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
DELETE NOPROMPT OBSOLETE;
EXIT;
EOF
else
####################################################################################################
#开始1级备份
$RMAN <<EOF|$TEE -a $LOGFILE
CONNECT TARGET
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$BACKUPDEST/%T_control_%F.bak';
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF $5 DAYS;
CONFIGURE DEVICE TYPE DISK PARALLELISM $6 BACKUP TYPE TO BACKUPSET;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
BACKUP as compressed backupset INCREMENTAL LEVEL 1 FORMAT '$BACKUPDEST/%T_db_L1_%s_%p.bak' section size 30g TAG 'BackupL1' DATABASE INCLUDE CURRENT CONTROLFILE;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
crosscheck archivelog all;
backup as compressed backupset archivelog all not backed up 1 times format '$BACKUPDEST/%T_arch_%s_%p.bak';
DELETE NOPROMPT ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-$4';
CROSSCHECK BACKUP;
DELETE NOPROMPT EXPIRED BACKUP;
DELETE NOPROMPT OBSOLETE;
EXIT;
EOF
fi
#备份控制文件
#rm $ORACLE_BASE/new.ctl
#不要直接备份到$BACKUPDEST目录
#$SQLPLUS "/ as sysdba" <<EOF|$TEE -a $LOGFILE
#ALTER DATABASE BACKUP CONTROLFILE TO '$ORACLE_BASE/new.ctl';
#exit;
#EOF
#cp $ORACLE_BASE/new.ctl $BACKUPDEST/`date +%Y%m%d%H%M%S`.ctl
</eof
</eof
</eof
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29519108/viewspace-2154317/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29519108/viewspace-2154317/