!/bin/sh
##############################################################
# The script is used for oracle autobackup. #
# Purpose:Backup the full database every month. #
# Backup the database for level 1 every week. #
# Backup the database for level 2 every day. #
# Script. Name: backupdb.sh #
# Author: yan shou peng #
# Date: 2010-03-25 #
##############################################################
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_BASE=/u01/app
export ORACLE_HOME=$ORACLE_BASE/oracle
export ORACLE_SID=racdb1
export ORACLE_TERM=xterm
export PATH=/usr/sbin:$PATH
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/usr/X11R6/lib64/
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export LD_ASSUME_KERNEL=2.6.9
--取出一个月中的第几天
CURRENTDAY=`date '+%d'`
--取出一个月中的最后一天
LASTDAY=`cal | awk 'NR == 7{print $NF}'`
--备份级别
BACKUPLEVEL=0
--是否已到第7天 0为是。
ISUPTOWEEK=`expr $CURRENTDAY % 7`
--备份文件目的地
BACKUPDEST='/orabak'
--RMAN日志文件
RMANLOG="$BACKUPDEST/rman.log"
--远程备份目的地
--当前日期
DATESTR=`date '+%Y-%m-%d'
--判断是否已到第7天,如果是设置备份级别为1 否则设置备份级别为2
if [ $ISUPTOWEEK -eq 0 ]; then
BACKUPLEVEL=1
else
BACKUPLEVEL=2
fi
--判断当前日期是否是本月最后一天,如果是重设备份级别为0
if [ $CURRENTDAY -eq $LASTDAY ]; then
BACKUPLEVEL=0
fi
--备份数据库
echo "************************************************************************" >> $RMANLOG
echo "Begin backup the database at `date '+%Y-%m-%d %T'` for LEVEL $BACKUPLEVEL.">> $RMANLOG
echo "************************************************************************" >> $RMANLOG
rman target / log=$RMANLOG append <RUN{
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 31 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
ALLOCATE CHANNEL CH1 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH2 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH3 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH4 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH5 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH6 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH7 DEVICE TYPE DISK MAXPIECESIZE 5G;
ALLOCATE CHANNEL CH8 DEVICE TYPE DISK MAXPIECESIZE 5G;
BACKUP
AS COMPRESSED BACKUPSET
INCREMENTAL LEVEL $BACKUPLEVEL
FORMAT "$BACKUPDEST/DATAFILE_$DATESTR_%U.bak"
TAG "LEVEL$BACKUPLEVEL"
DATABASE;
SQL 'ALTER SYSTEM ARCHIVE LOG CURRENT';
BACKUP
AS COMPRESSED BACKUPSET
FORMAT "$BACKUPDEST/ARCHIVELOG_$DATESTR_%U.bak"
TAG "ARCHIVELOG BACKUP"
ARCHIVELOG
FROM TIME 'SYSDATE-2' UNTIL TIME 'SYSDATE'
DELETE ALL INPUT;
CROSSCHECK BACKUPSET;
DELETE NOPROMPT EXPIRED BACKUP;
DELETE NOPROMPT OBSOLETE;
RELEASE CHANNEL CH1;
RELEASE CHANNEL CH2;
RELEASE CHANNEL CH3;
RELEASE CHANNEL CH4;
RELEASE CHANNEL CH5;
RELEASE CHANNEL CH6;
RELEASE CHANNEL CH7;
RELEASE CHANNEL CH8;
}
EXIT
EOF
echo "************************************************************************" >> $RMANLOG
echo "End backup the database at `date '+%Y-%m-%d %T'` for LEVEL $BACKUPLEVEL." >> $RMANLOG
echo "************************************************************************" >> $RMANLOG
--列出当前数据库的备份文件
echo "All of the following files is the backupset of database. " >> $RMANLOG
echo "************************************************************************" >> $RMANLOG
ls -lh $BACKUPDEST | grep "bak$" >> $RMANLOG 2>&1
echo "************************************************************************" >> $RMANLOG
----列出当前需要传送的备份文件
echo "Now begin copy the following files to 192.168.0.200 at `date '+%Y-%m-%d %T'`." >> $RMANLOG
echo "************************************************************************" >> $RMANLOG
ls -lh $BACKUPDEST | grep "$DATESTR" >> $RMANLOG 2>&1
echo "************************************************************************" >> $RMANLOG
--开始COPY文件
if scp *_$DATESTR_*.bak "$REMOTE_DEST" >> $RMANLOG 2>&1 ; then
echo "Successfuly copy the file from localhost to remote at `date '+%Y-%m-%d %T'`." >>$RMANLOG
else
echo "Encounter errors when copy the file from localhost to remote.please check." >>$RMANLOG
fi
echo " ">> $RMANLOG
echo " ">> $RMANLOG
echo "************************************************END*************************************">> $RMANLOG
阅读(307) | 评论(0) | 转发(0) |