Rman備份的shell指令碼

season0891發表於2009-12-18

#Set oracle path
#source $HOME/.keychain/${HOSTNAME}-sh
export SSH_AUTH_SOCK
echo $SSH_AUTH_SOCK
export PATH
unset USERNAME
export LD_ASSUME_KERNEL=2.4.1
export ORACLE_BASE=/opt/oracle9i
export ORACLE_HOME=$ORACLE_BASE/product/9.2.0
export ORACLE_SID=back1
export ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
#echo "-----------資料庫環境設定完成-----------"
# Set shell search paths
export PATH=$ORACLE_HOME/bin:$PATH
export NLS_LANG=american_america.ZHS16GBK
export ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data
#echo "----------指令碼搜尋環境設定完成----------"
# set the backup script. enviroment variable.
#export USERPASS=/nolog
export BACKUP_BASE=/home/oracle/backup
export BACKUP_TARGET=${BACKUP_BASE}/data
#export TABLESPACE_TARGET=${BACKUP_TARGET}/tablespace
export DATABASE_TARGET=${BACKUP_TARGET}/database
export CTRFILE_TARGET=${BACKUP_TARGET}/ctrfile
#export ARCLOG_TARGET=${BACKUP_TARGET}/arclog
export LOG_TARGET=${BACKUP_BASE}/log
#echo "--------備份指令碼環境變數設定完成---------"
#當前時間
CurDate=`date +%Y-%m-%d_%H:%M:%S`
LogDate=`date +%Y-%m-%d`
#ORACLE使用者的使用者名稱
OraUser=oracle
#要備份的表空間變數與登陸資料庫的使用者
#TableSpace=shiyihai #只在備份表空間時需要
User=sys
Psw=sys
DataBase=back2
#恢復目錄使用者,表空間與資料庫名
RmanTs=rman_ts
RmanUser=rman
RmanPsw=rman
RmanDB=test2
#echo "-----------備份伺服器設定完成-------------"
#引數校驗
if [ "p"$1 = "p/?" ]
then
echo "Usage:rman_db.sh Command"
echo "------------------------------------------------"
echo "Command : backup (0,1,2,3,4) (增量備份的基礎策略備份)"
echo "Parameter - 0 (全量備份)"
echo "Parameter - 1 (一級增量備份)"
echo "Parameter - 2 (二級增量備份)"
echo "Parameter - 3 (三級增量備份)"
echo "Parameter - 4 (四級增量備份)"
echo "------------------------------------------------"
echo "Command : full (備份整個資料庫)"
echo "------------------------------------------------"
echo "Command : listts (顯示錶空間備份歷史記錄)"
echo "------------------------------------------------"
echo "Command : listdb (顯示資料庫備份歷史記錄)"
echo "------------------------------------------------"
#echo "Command : arclog (備份歸檔日誌並刪除)"
#echo "------------------------------------------------"
# echo "Command : recoverts (恢復表空間)"
exit
fi
if [ "p"$1 = "p" -o "p"$1$2 = "pbackupts" -o "p"$1$2 = "pbackupdb" ]
then
echo "Usage:rman_db.sh Command"
echo "------------------------------------------------"
echo "Command : backup (0,1,2,3,4) (增量備份的基礎策略備份)"
echo "Parameter - 0 (全量備份)"
echo "Parameter - 1 (一級增量備份)"
echo "Parameter - 2 (二級增量備份)"
echo "Parameter - 3 (三級增量備份)"
echo "Parameter - 4 (四級增量備份)"
echo "------------------------------------------------"
echo "Command : full (備份整個資料庫)"
echo "------------------------------------------------"
echo "Command : listts (顯示錶空間備份歷史記錄)"
echo "------------------------------------------------"
echo "Command : listdb (顯示資料庫備份歷史記錄)"
echo "------------------------------------------------"
# echo "Command : arclog (備份歸檔日誌並刪除)"
# echo "------------------------------------------------"
# echo "Command : recoverts (恢復表空間)"
exit
fi
if [ "p"$1$2 != "pbackup0" -a "p"$1$2 != "pbackup1" -a "p"$1$2 != "pbackup2" -a "p"$1$2 != "pbackup3" -a "p"$1$2 != "pbackup4" -a "p"$1 != "pfull" -a "p"$1 != "plistdb" ]
then
echo "Usage:rman_db.sh Command"
echo "------------------------------------------------"
echo "Command : backup (0,1,2,3,4)(備份資料庫)"
echo "Parameter - 0 (增量備份的基礎策略備份)"
echo "Parameter - 1 (一級增量備份)"
echo "Parameter - 2 (二級增量備份)"
echo "Parameter - 3 (三級增量備份)"
echo "Parameter - 4 (四級增量備份)"
echo "------------------------------------------------"
echo "Command : full (備份整個資料庫)"
echo "------------------------------------------------"
echo "Command : listts (顯示錶空間備份歷史記錄)"
echo "------------------------------------------------"
echo "Command : listdb (顯示資料庫備份歷史記錄)"
echo "------------------------------------------------"
# echo "Command : arclog (備份歸檔日誌並刪除)"
# echo "------------------------------------------------"
# echo "Command : recoverts (恢復表空間)"
exit
fi
echo "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH" >>${LOG_TARGET}/${LogDate}.log
#----------------------------------------------------------------------------------
#測試主備份目錄是否可寫
if ! test -w ${BACKUP_BASE}
then
echo "沒有對${BACKUP_BASE}資料夾的寫許可權" >>${LOG_TARGET}/${LogDate}.log
echo "`date +%Y.%m.%d.%H.%M.%S`備份終止" >>${LOG_TARGET}/${LogDate}.log
echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT"
exit 1
fi
#------------------------------------------------------------------------------------
#檢查資料夾是否存在
if ! test -d ${BACKUP_TARGET}
then
echo "-----沒有備份目錄,自動建立-----" >>${LOG_TARGET}/${LogDate}.log
mkdir ${BACKUP_TARGET}
fi
#if ! test -d ${TABLESPACE_TARGET}
#then
# echo "-----沒有備份目錄,自動建立-----" >>${LOG_TARGET}/${LogDate}.log
# mkdir ${TABLESPACE_TARGET}
#fi
#if ! test -d ${DATABASE_TARGET}
#then
# echo "-----沒有備份目錄,自動建立-----" >>${LOG_TARGET}/${LogDate}.log
# mkdir ${DATABASE_TARGET}
#fi
#if ! test -d ${CTRFILE_TARGET}
#then
# echo "-----沒有備份目錄,自動建立-----" >>${LOG_TARGET}/${LogDate}.log
# mkdir ${CTRFILE_TARGET}
#fi
#if ! test -d ${ARCLOG_TARGET}
#then
# echo "-----沒有備份目錄,自動建立-----" >>${LOG_TARGET}/${LogDate}.log
# mkdir ${ARCLOG_TARGET}
#fi
#------------------------------------------------------------------------------------
if ! test -d ${LOG_TARGET}
then
echo "-----沒有日誌目錄,自動建立-----" >>${LOG_TARGET}/${LogDate}.log
mkdir ${LOG_TARGET}
fi
#----------------------------------------------------------------------------------
#echo "$CurDate 備份開始"
#-----------------------------------------------------------------------------------
#顯示相關目錄
#echo "基礎備份目錄 : " ${BACKUP_BASE}
#echo "備份資料檔案目錄 : " ${BACKUP_TARGET}
#echo "表空間備份目錄 : " ${TABLESPACE_TARGET}
#echo "資料庫備份目錄 : " ${DATABASE_TARGET}
#echo "歸檔日誌備份目錄 : " ${ARCLOG_TARGET}
#echo "日誌目錄 : " ${LOG_TARGET}
#------------------------------------------------------------------------------------
#表空間備份
#if [ $1 = "backupts" ]
#then
#echo "`date +%Y-%m-%d_%H:%M:%S`開始${TABLESPACE}表空間備份" >>${LOG_TARGET}/${LogDate}.log
#rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <>${LOG_TARGET}/${LogDate}.log
# run {
# allocate channel dev1 type disk;
# backup
# incremental level $2
# filesperset 4
# format "${TABLESPACE_TARGET}/${TableSpace}_level${2}_s%s_t%t_p%p.bak"
# (tablespace ${TableSpace});
# }
# exit;
#EOF
# echo "`date +%Y-%m-%d_%H:%M:%S`${TABLESPACE}表空間備份結束" >>${LOG_TARGET}/${LogDate}.log
# echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" >>${LOG_TARGET}/${LogDate}.log
#fi
#資料庫備份
if [ $1 = "backup" ]
then
echo "`date +%Y-%m-%d_%H:%M:%S`開始${DataBase}資料庫備份" >>${LOG_TARGET}/${LogDate}.log
rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <>${LOG_TARGET}/${LogDate}.log
run {
allocate channel dev1 type disk;
backup
incremental level $2
filesperset 4
format "${BACKUP_TARGET}/%d_level${2}_s%s_t%t_p%p.bak"
(database);
}
exit;
EOF
echo "`date +%Y-%m-%d_%H:%M:%S`${DataBase}資料庫備份結束" >>${LOG_TARGET}/${LogDate}.log
echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" >>${LOG_TARGET}/${LogDate}.log
fi
#顯示錶空間備份歷史記錄
if [ $1 = "listts" ]
then
rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <list backupset of tablespace $TableSpace;
exit;
EOF
fi
#顯示資料庫備份歷史記錄
if [ $1 = "listdb" ]
then
rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <list backupset of database database;
exit;
EOF
fi
#備份整個資料庫
if [ $1 = "full" ]
then
echo "`date +%Y-%m-%d_%H:%M:%S`開始備份${DataBase}資料庫" >>${LOG_TARGET}/${LogDate}.log
rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <>${LOG_TARGET}/${LogDate}.log
run {
allocate channel dev1 type disk;
backup
full
format "${BACKUP_TARGET}/%d_Full_s%s_t%t_p%p.bak"
(database);
}
exit;
EOF
echo "`date +%Y-%m-%d_%H:%M:%S`${DataBase}資料庫備份結束" >>${LOG_TARGET}/${LogDate}.log
echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" >>${LOG_TARGET}/${LogDate}.log
fi
#備份歸檔日誌並刪除
#if [ $1 = "arclog" ]
#then
#echo "`date +%Y-%m-%d_%H:%M:%S`開始備份歸檔日誌" >>${LOG_TARGET}/${LogDate}.log
#rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <>${LOG_TARGET}/${LogDate}.log
# run {
# allocate channel dev1 type disk;
# backup
# format "${ARCLOG_TARGET}/ARCLOG_s%s_t%t_p%p.bak"
# (archivelog all delete input);
# }
# exit;
#EOF
# echo "`date +%Y-%m-%d_%H:%M:%S`備份歸檔日誌結束" >>${LOG_TARGET}/${LogDate}.log
# echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" >>${LOG_TARGET}/${LogDate}.log
#fi
#恢復表空間
#if [ $1 = "recoverts" ]
#then
#echo "`date +%Y-%m-%d_%H:%M:%S`開始恢復表空間" >>${LOG_TARGET}/${LogDate}.log
#rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <>${LOG_TARGET}/${LogDate}.log
# run {
# allocate channel dev1 type disk;
# sql "alter tablespace ${TABLESPACE} offline immediate";
# restore tablespace ${TABLESPACE};
# recover tablespace ${TABLESPACE};
# sql "alter tablespace ${TABLESPACE} online";
# }
# exit;
#EOF
# echo "`date +%Y-%m-%d_%H:%M:%S`恢復表空間結束" >>${LOG_TARGET}/${LogDate}.log
# echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" >>${LOG_TARGET}/${LogDate}.log
#fi
#恢復資料庫
#if [ $1 = "recoverdb" ]
#then
#echo "`date +%Y-%m-%d_%H:%M:%S`開始恢復資料庫" >>${LOG_TARGET}/${LogDate}.log
#rman target $User/$Psw@$DataBase catalog $RmanUser/$RmanPsw@$RmanDB <>${LOG_TARGET}/${LogDate}.log
# run {
# allocate channel dev1 type disk;
# sql "alter tablespace ${TABLESPACE} offline immediate";
# restore tablespace ${TABLESPACE}
# recover tablespace ${TABLESPACE}
# sql "alter tablespace ${TABLESPACE} online";
# }
# exit;
#EOF
# echo "`date +%Y-%m-%d_%H:%M:%S`恢復資料庫結束" >>${LOG_TARGET}/${LogDate}.log
# echo "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT" >>${LOG_TARGET}/${LogDate}.log
#fi

本文轉自:

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

相關文章