在配置ADG环境的同时,wait4friend 整理了一下RMAN脚本,记录一下
###########################################################################################
backup.rman.sh
这个脚本自动判断Primary和Standby,进行不同的操作。如果是单机环境,备份的同时会删除归档。如果是Data Guard环境,那么不删除Archived Log,
DG环境删除归档的脚本见 这里
#!/bin/bash
##===========================================================
## backup.rman.sh
## created by McDull Zeng
## 2012-10-25
## usage: backup.rman.sh <$BACKUP_LEVEL>
## BACKUP_LEVEL:
## A: archived log
## F: full backup
## 0: level 0
## 1: level 1
##
##
## crontab under oracle user:
## 30 0 * * 0 /home/oracle/script/backup.rman.sh 0
## 30 0 * * 1,2,3,4,5,6 /home/oracle/script/backup.rman.sh 1
##============================================================
#======================================================================
# User specific environment
if [ -f ~/.bash_profile ];
then
. ~/.bash_profile
fi
RMAN_DIR=/u02/backupdata; export RMAN_DIR
TIMESTAMP=`date +%Y%m%d_%H%M`; export TIMESTAMP
DATE=`date +%Y%m%d`; export DATE
RETENTION=3; export RETENTION
export RMAN_LEVEL
# default level
if [ $# == 1 ];then
RMAN_LEVEL=${1}
else
RMAN_LEVEL="F"
fi
#======================================================================
# Check rman level
if [ "$RMAN_LEVEL" == "F" ];
then unset INCR_LVL
BACKUP_TYPE=full
else
INCR_LVL="INCREMENTAL LEVEL ${RMAN_LEVEL}"
BACKUP_TYPE=lev${RMAN_LEVEL}
fi
RMAN_FILE=${RMAN_DIR}/${TIMESTAMP}_${BACKUP_TYPE}; export RMAN_FILE
#======================================================================
#Check RMAN Backup Path
if ! test -d ${RMAN_DIR}
then
mkdir -p ${RMAN_DIR}
fi
#======================================================================
# query db role and whether it is a Data Guard enviroment
DB_INFO=`${ORACLE_HOME}/bin/sqlplus -s / as sysdba<<EOF
set pages 0
set head off
set feed off
select d.database_role || '|' || l.standby_count || '|' as db_info
from (select d.database_role from v\\$database d) d,
(select count(1) as standby_count
from v\\$archived_log l
where l.standby_dest = upper('YES')
and rownum = 1) l
where rownum = 1;
EOF`
DB_ROLE=`echo ${DB_INFO} | awk -F'|' '{print $1}'`
HAS_STANDBY=`echo ${DB_INFO} | awk -F'|' '{print $2}'`
# echo "DB ROLE = ${DB_ROLE}"
# echo "HAS Stamdby = ${HAS_STANDBY}"
# Configuration should only be set on Primary
#=============================================================================
if [ "${DB_ROLE}" = "PRIMARY" ];then
RMAN_CFG="
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF ${RETENTION} DAYS;
CONFIGURE BACKUP OPTIMIZATION ON;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '${RMAN_FILE}_ctl_%F';
"
else
RMAN_CFG="";
fi
# echo "RMAN Config = ${RMAN_CFG}"
# Delete Input Archived Log should only be set on a non Data Guard Primary
# Note: As for Data Guard, archived log is deleted by another script
#=============================================================================
if [ "${DB_ROLE}" = "PRIMARY" -a "${HAS_STANDBY}" = "0" ];then
DEL_INPUT="DELETE INPUT";
else
DEL_INPUT="";
fi
# echo "Del Input = ${DEL_INPUT}"
# RMAN Command
#=============================================================================
if [ "$RMAN_LEVEL" == "A" ]; then
RMAN_CMD="
BACKUP AS COMPRESSED BACKUPSET
ARCHIVELOG ALL FORMAT '${RMAN_FILE}_arc_%s_%p_%c'
${DEL_INPUT}
;
"
else
RMAN_CMD="
BACKUP AS COMPRESSED BACKUPSET
${INCR_LVL}
DATABASE FORMAT '${RMAN_FILE}_dat_%s_%p_%c'
PLUS ARCHIVELOG FORMAT '${RMAN_FILE}_arc_%s_%p_%c'
${DEL_INPUT}
;
"
fi
# echo "RMAN Command = ${RMAN_CMD}"
#Startup rman to backup
#=============================================================================
${ORACLE_HOME}/bin/rman log=${RMAN_FILE}.log <<EOF
connect target /
run {
${RMAN_CFG}
ALLOCATE CHANNEL 'ch1' TYPE DISK maxpiecesize=4G;
ALLOCATE CHANNEL 'ch2' TYPE DISK maxpiecesize=4G;
CROSSCHECK ARCHIVELOG ALL;
DELETE NOPROMPT EXPIRED ARCHIVELOG ALL;
${RMAN_CMD}
DELETE NOPROMPT OBSOLETE;
RELEASE CHANNEL ch1;
RELEASE CHANNEL ch2;
}
exit;
EOF
RC=$?
#
exit