中小型資料庫 RMAN CATALOG 備份恢復方案(二)

us_yunleiwang發表於2013-12-05

中小型資料庫呈現的是資料庫併發少,資料庫容量小,版本功能受限以及N多單例項等特點。儘管如此,資料庫的損失程度也會存在零丟失的情形。企業不願意花太多的錢又要保證資料庫的可靠穩定,可是苦煞了我這些搞DB的。接上一篇文章,中小型資料庫 RMAN CATALOG 備份恢復方案(一),我們繼續來給出基於中小型資料庫的恢復的指令碼與其部署。

 

1、RMAN還原shell指令碼

  1. --下面的shell指令碼用於實現資料庫的自動還原,還原成功後,資料庫被關閉。因為我們在Prod資料庫無異常的情形下,不需要bak 的備用庫open  
  2. --shell指令碼做還原時呼叫了catalog中的全域性指令碼global_restore  
  3. --在指令碼最尾部,我們將DB還原是否成功的狀態輸出到日誌檔案db_restore_rman.log,這樣做的好處是我們可以將多個DB的還原狀態集中,便於檢視  
  4. $ more db_restore_rman_catalog.sh  
  5. ##====================================================================  
  6. ##   File name: db_restore_rman_catalog.sh  
  7. ##   Usage: db_restore_rman_catalog.sh   
  8. ##   Desc:  
  9. ##        The script uses to restore database with level 0 backupset.  
  10. ##   Author: Robinson  
  11. ##   Blog  : http://blog.csdn.net/robinson_0612  
  12. ##====================================================================  
  13.   
  14. #!/bin/bash   
  15. # --------------------  
  16. # Define variable  
  17. # --------------------  
  18.   
  19. if [ -f ~/.bash_profile ]; then  
  20. . ~/.bash_profile  
  21. fi  
  22.   
  23. # --------------------------  
  24. #   Check SID  
  25. # --------------------------  
  26. if [ -z "${1}" ];then  
  27.     echo "Usage: "  
  28.     echo "      `basename $0` ORACLE_SID"  
  29.     exit 1  
  30. fi  
  31.   
  32. ORACLE_SID=${1};                              export ORACLE_SID  
  33. LOG_DIR=/u02/database/${ORACLE_SID}/backup;   export RMAN_DIR  
  34. TIMESTAMP=`date +%Y%m%d%H%M`                  export TIMESTAMP  
  35. RMAN_LOG=${LOG_DIR}/${ORACLE_SID}_restore_${TIMESTAMP}.log;          export RMAN_LOG  
  36. SSH_LOG=${LOG_DIR}/${ORACLE_SID}_restore_full_${TIMESTAMP}.log;      export SSH_LOG  
  37. RETENTION=5  
  38.   
  39. echo "----------------------------------------------------------------" >>${SSH_LOG}  
  40. echo "Start rman to backup at `date`."                                  >>${SSH_LOG}  
  41. echo "----------------------------------------------------------------" >>${SSH_LOG}  
  42.   
  43. $ORACLE_HOME/bin/rman target / catalog rman_user/xxx@catadb log=${RMAN_LOG} <
  44. startup nomount;  
  45. run{execute global script global_restore;}  
  46. exit;  
  47. EOF  
  48. RV=$?  
  49.   
  50. cat ${RMAN_LOG}>>${SSH_LOG}  
  51. echo ""        >>${SSH_LOG}  
  52. echo "----------------------------------------------------------------" >>${SSH_LOG}  
  53. echo "MSG1: RMAN restore end at `date`."                                >>${SSH_LOG}  
  54. echo "----------------------------------------------------------------" >>${SSH_LOG}  
  55.   
  56. if [ $RV -ne "0" ]; then  
  57.     echo "----------------------------------------------------------------" >>${SSH_LOG}      
  58.     echo "MSG2: RMAN restore error at `date`."                              >>${SSH_LOG}  
  59.     echo "----------------------------------------------------------------" >>${SSH_LOG}  
  60.     RMAN_STAT='FAILED'  
  61.     mail -s "Failed RMAN restore for $ORACLE_SID on `hostname`." dba@12306.com 
  62. else  
  63.     echo "----------------------------------------------------------------" >>${SSH_LOG}  
  64.     echo "MSG2: No error found for RMAN restore at `date`."                 >>${SSH_LOG}  
  65.     echo "----------------------------------------------------------------" >>${SSH_LOG}  
  66.     RMAN_STAT='SUCCEED'  
  67.     rm -rf ${RMAN_LOG} 2>/dev/null  
  68. fi  
  69.   
  70. echo "`date '+%F %X'` --  $0 $1 $RMAN_STAT ">> /u01/comm_scripts/db_restore_rman.log  
  71.   
  72. exit  

2、檢測還原狀態shell指令碼

  1. --我們用一個shell指令碼來檢測多個DB當天最終的還原狀態成功與否,並將當前的所有記錄輸出到ck_restore.log日誌  
  2. --指令碼尾部傳送郵件列出當天所有進行restore之後的所有狀態,是一個多個DB restore 的summary report.  
  3. $ more ck_restore.sh  
  4. ##====================================================================  
  5. ##   File name: ck_restore.sh  
  6. ##   Usage: ck_restore.sh  
  7. ##   Desc:  
  8. ##        The script uses to check RMAN restore log for current day  
  9. ##          and send mail to DBA  
  10. ##   Author: Robinson  
  11. ##   Blog  : http://blog.csdn.net/robinson_0612  
  12. ##====================================================================  
  13. #!/bin/bash  
  14. if [ -f ~/.bash_profile ];  
  15. then  
  16. . ~/.bash_profile  
  17. fi  
  18.   
  19. REV_DIR=/u01/comm_scripts  
  20. dt=`date '+%F'`  
  21. cat /dev/null >${REV_DIR}/ck_restore.log  
  22. cat ${REV_DIR}/db_restore_rman.log | grep "${dt}" >>${REV_DIR}/ck_restore.log  
  23. total=`cat ${REV_DIR}/ck_restore.log |wc -l`  
  24. suc=`grep SUCCEED ${REV_DIR}/ck_restore.log |wc -l`  
  25. fail=`grep FAILED ${REV_DIR}/ck_restore.log |wc -l`  
  26. echo "">>ck_restore.log  
  27. echo -e "The total DB of current recovery is $total in `hostname` \n">>${REV_DIR}/ck_restore.log  
  28. echo -e "The number of succee is : ${suc} \n">>${REV_DIR}/ck_restore.log  
  29. echo -e "The number of fail is : ${fail} \n">>${REV_DIR}/ck_restore.log  
  30. mail -s "RMAN restore summary for `hostname` at `date +'%a %b %d %Y'`" dba@12306.com 

3、部署還原shell指令碼到crontab

  1. --首先將多個需要自動restore的DB封裝到一個單獨的檔案,如下:  
  2. --最後呼叫ck_restore.sh 腳步檢測所有DB restore狀態併傳送RMAN summary report郵件  
  3. $ more full_resotre_by_rman.sh  
  4. #!/bin/bash  
  5. /u01/comm_scripts/db_restore_rman_catalog.sh BC1200  
  6. /u01/comm_scripts/db_restore_rman_catalog.sh AF2630  
  7.   
  8. /u01/comm_scripts/ck_restore.sh  
  9.   
  10. --部署到crontab  
  11. --注,無論是備份還是恢復指令碼,我們都是透過Bak server的crontab來部署以減輕Prod的壓力  
  12. #Rman restore database  
  13. 0 3 * * 1-6 /u01/comm_scripts/full_resotre_by_rman.sh    

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

相關文章