Chinaunix首页 | 论坛 | 博客
  • 博客访问: 827244
  • 博文数量: 101
  • 博客积分: 1311
  • 博客等级: 中尉
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-25 12:15
文章分类

全部博文(101)

文章存档

2012年(101)

分类: Oracle

2012-06-12 16:59:39


点击(此处)折叠或打开

  1. #!/bin/bash
  2. ###################################################################
  3. # Usage:
  4. # backup_rman_v1.1.sh [all|repeat|sync]
  5. # all: list all backup file.
  6. # repeat: repeat level-1 increment backup.
  7. # sync:    sync backup file to target database.
  8. ###################################################################
  9. export ORACLE_HOME=/u01/app/11.2.0
  10. export ORACLE_SID=health
  11. export LD_LIBRARY_PATH=${ORACLE_HOME}/lib:${ORACLE_HOME}/ctx/lib
  12. export PATH=$PATH:${ORACLE_HOME}/bin
  13. #export NLS_LANG="American_america.zhs16gbk"
  14. export NLS_LANG="American_america.al32utf8"
  15. export ORACLE_OWNER=oracle
  16. export backup_dir=/sda/backup
  17. export log=/sda/backup/log
  18. rsync_dir=$backup_dir #sync dir
  19. controlfile_dir=$backup_dir/controlfile
  20. username=oracle #target OS ,oracle user
  21. password=go2north #target oracle user password
  22. target_host=61.158.131.94
  23. today_backup=`date +'%Y-%m-%d'`
  24. mail_to_admin="zhaorupeng@yeah.net"
  25. ########set display color#########
  26. white=$(echo -e "\e[39;40m")
  27. green=$(echo -e "\e[36;40m")
  28. red=$(echo -e "\e[31;40m")
  29. purple=$(echo -e "\e[35;40m")
  30. yellow=$(echo -e "\e[33;40m")
  31. blue=$(echo -e "\e[34;40m")
  32. ########color set end ############

  33. # data backup status.
  34. # 0: backup failed.
  35. # 2: default
  36. # 9: success
  37. backup_status=2

  38. #database status check ,If it's not turn on,the value is 0,or else 1
  39. ora_stat=`ps -ef | grep -i 'ora_smon_*' |grep -v grep| wc -l`

  40. #database mode check,If it's archive mode,that value is 1,or else 0;
  41. arch=`ps -ef | grep -i 'ora_arc_*' | grep -v grep | wc -l`

  42. function open_database()
  43. {
  44. if [ "$ora_stat" = 0 ]; then
  45. cat << EOF | $ORACLE_HOME/bin/sqlplus '/as sysdba'
  46. shutdown immediate;
  47. startup;
  48. quit;
  49. EOF
  50. backup_status=2
  51.  if [ "$?" = 1 ]; then
  52.  echo "database unable strtup!"
  53.  backup_status=0
  54.  exit 1
  55.  fi
  56. fi
  57. }
  58. function open_archive_mode()
  59. {
  60. if [ "$arch" = 0 ]; then #if arch=1,nothing,because it was already on archive mode
  61. echo "****************open archive mode*************"
  62. cat << EOF | $ORACLE_HOME/bin/sqlplus '/as sysdba'
  63. shutdown immediate;
  64. startup mount;
  65. alter database archivelog;
  66. alter database open;
  67. quit;
  68. EOF
  69. fi
  70. }
  71. function return_initaliztion_mode()
  72. {
  73. if [ "$arch" = 0 -a "$backup_status" > 0 ]; then
  74. #if arch=1,nothing,because initialization mode is archive mode
  75. echo "********* return initialization database mode**********"
  76. cat << EOF | $ORACLE_HOME/bin/sqlplus '/as sysdba'
  77. shutdown immediate;
  78. startup mount;
  79. alter database noarchivelog;
  80. alter database open;
  81. quit;
  82. EOF
  83. fi
  84. if [ "$?" = 0 ]; then
  85.     echo "return initalization database successfully."
  86. fi
  87. echo "************return initialization database mode *********" ;
  88. }
  89. function increment_backup_level_1() # incremental level-1 backup
  90. {
  91. #open_database
  92. #open_archive_mode
  93. echo "******** `date +'%Y%m%d'` Do level-1 increment backup....*********************" 2>&1;
  94. cat << EOF | $ORACLE_HOME/bin/rman target / |tee $log/rman_increment_db_`date +'%y%m%d%H%M'`.log
  95. configure maxsetsize to 20g;
  96. configure controlfile autobackup on;
  97. configure controlfile autobackup format for device type disk to '$controlfile_dir/%F';
  98. run {
  99. allocate channel c01 type disk;
  100. backup incremental level 1 database format '$backup_dir/increment_db_%d_%s_%t_%p' tag="increment_db_`date +'%y%m%d%H%M'`";
  101. release channel c01;
  102. }
  103. configure controlfile autobackup off;
  104. crosscheck backup of database;
  105. crosscheck archivelog all;
  106. delete noprompt obsolete ;
  107. delete noprompt expired backup;
  108. delete noprompt backup completed before 'sysdate-30';
  109. delete noprompt archivelog until time 'sysdate-14';
  110. EOF
  111. if [ "$?" = 0 ];then
  112.     echo "*******************level-1 backup completed!************************"
  113.         backup_status=9
  114. else
  115.     echo "*****************level-1 backup databae failed,please contact oracle dba*******"
  116.     backup_status=0
  117. fi
  118. return $backup_status
  119. #return_initaliztion_mode
  120. }
  121. function level_0_backup_database()
  122. {
  123. #open_database
  124. #open_archive_mode
  125. echo "************* Do level-0 backup ****************"
  126. cat << EOF | $ORACLE_HOME/bin/rman target / |tee $log/rman_full_db_`date +'%y%m%d%H%M'`.log
  127. configure retention policy to redundancy 7;
  128. configure maxsetsize to 20g;
  129. configure controlfile autobackup on;
  130. configure controlfile autobackup format for device type disk to '$controlfile_dir/%F';
  131. crosscheck backup of database;
  132. crosscheck archivelog all;
  133. delete noprompt obsolete ;
  134. delete noprompt expired backup;
  135. delete noprompt backup completed before 'sysdate-7';
  136. delete noprompt archivelog until time 'sysdate-7';
  137. run {
  138. allocate channel c1 type disk;
  139. backup incremental level 0 database format '$backup_dir/full_db_%d_%s_%t_%p' tag="full_db_`date +'%y%m%d%H%M'`";
  140. release channel c1 ;
  141. }
  142. configure controlfile autobackup off;
  143. quit;
  144. EOF
  145. if [ "$?" = 0 ];then
  146.     echo "*******************level-0 backup completed!************************"
  147.         backup_status=9
  148. else
  149.     echo "******************level-0 backup databae failed,please contact oracle dba*******"
  150.     backup_status=0
  151. fi
  152. return $backup_status
  153. #return_initaliztion_mode
  154. }
  155. function repeat_increment_backup()
  156. {    
  157.     if [ "$#" = 0 ]; then
  158.      exit 0
  159.     else

  160.          if [ "$1" = "repeat" ]; then
  161.             echo "************do database increment backup again**************"
  162.             increment_backup_level_1 $ORACLE_HOME $log $backup_dir
  163.             echo "************repeat increment backup completed!**************"
  164.          else
  165.             echo "command error,please use parameter 'repeat'"
  166.             exit 0
  167.          fi
  168.     fi
  169. }

  170. # sync target database backup files #
  171. function sync()
  172. {
  173. ping $target_host -c 1 > /dev/null # test network link #
  174. if [ $? != 0 ] ; then
  175.     echo "sync host:$red $target_host $white link failed!,please check network."
  176.     exit 1
  177. fi
  178. if [ -f /usr/bin/rsync ]; then
  179. #check resync command #
  180.    cat << EOF > sync
  181. #!/usr/bin/expect
  182. spawn /usr/bin/rsync -avzu $rsync_dir/ $username@$target_host:$rsync_dir
  183. expect "password:"
  184. send "$password\n";
  185. send "quit\n";
  186. interact
  187. EOF
  188. echo "********copy backup files to target database********"
  189.   if [ -f sync -a -f /usr/bin/expect ]; then
  190.     chmod +x sync
  191.     ./sync
  192.     rm -rf ./sync
  193. #list sync files
  194. backup_file=`ls -ltR --full-time $backup_dir/ | egrep -i "increment_|c-" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`
  195.     echo "sync files:"
  196.     echo "$blue"
  197.     j=0
  198.     for i in $backup_file
  199.     do
  200.         ((j++))
  201.         a[$j]=$i
  202.         if [ $j = 3 ]; then
  203.             echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} ${a[$j]}"
  204.              j=0
  205.         fi
  206.     done
  207.     echo "$white"
  208.     echo " transtion has succeed.please check the backup files on target database."
  209.     exit 0
  210.    else
  211.       echo "command expect not found, please install Tcl/expect"
  212.       exit 1
  213.    fi
  214. else
  215.     echo "command rsync not found,please install!"
  216.     exit 1
  217. fi
  218. }
  219. if [ -f $log/autobak_`date +'%Y%m%d'`.log ]; then
  220.     rm -rf $log/autobak_`date +'%Y%m%d'`.log
  221. fi    
  222. (
  223. ##### backup level #####
  224. level_0_backup_status=`find $backup_dir/ -name 'full_db_*'| grep -i full_db |grep -v grep | wc -l` 2>&1
  225. level_1_backup_status=`ls -l --full-time $backup_dir/ |grep -i 'increment_db_*'| grep -i $today_backup|grep -v grep | wc -l` 2>&1

  226. #Always perform 0 backup

  227. level_0_backup_status=0

  228. ##########################
  229. if [ $level_0_backup_status = 0 -a $backup_status = 2 ]; then
  230.     level_0_backup_database
  231.     backup_status=$?
  232. fi
  233. if [ $level_1_backup_status = 0 -a $backup_status = 2 ]; then
  234.     increment_backup_level_1
  235.     backup_status=$?
  236. fi
  237. ##### backup level end ######

  238. # ############Today's database backup information##########
  239. # check today's backup status #
  240. check_backup=`ls -l --full-time $backup_dir/ | egrep -i "increment_db_|full_db_" | awk '{print $6}' | grep -i $today_backup | wc -l`
  241. # check today's controlfile backup information #
  242. control_file=`ls -lt --full-time $controlfile_dir/ | grep -i "c-*" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`
  243. # check today's increment backup information #
  244. backup_file_info=`ls -lt --full-time $backup_dir/ | egrep -i "increment_db_|full_db_" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`
  245. log_file_info=`ls -lt --full-time $log/ | egrep -i "increment_db_|full_db_" | grep -i $today_backup | awk '{print $6 " " substr($7,1,8) " " $9}'`
  246. if [ "$1" = "all" ] ; then
  247.     backup_file_info=`ls -lt --full-time $backup_dir/ | egrep -i "increment_db_|full_db" | awk '{print $6 " " substr($7,1,8) " " $9}'`
  248.     control_file=`ls -lt --full-time $controlfile_dir/ | grep -i "c-*"| awk '{print $6 " " substr($7,1,8) " " $9}'`
  249. fi
  250.     
  251. # print today's backup information including controlfile and log information #
  252. if [ $check_backup -ge 0 ]; then

  253.     if [ "$1" = "repeat" ] ; then
  254.         repeat_increment_backup $1
  255.     else
  256.                 echo " ############Today's database backup information########## "
  257.          if [ "$1" = "all" ]; then
  258.     
  259.      today_backup=`ls -l --full-time $backup_dir/ | grep -i full_db_* | awk '{print $6}'`
  260.             
  261.             echo "List date $purple ${today_backup[0]} $white level-0 backup database after file information"
  262.          else    
  263.                 echo "Date $purple $today_backup $white database backup is completed."
  264.                 fi
  265.             echo "backup file directory: $backup_dir"
  266.             echo "backup file information: $green"
  267.         echo ""
  268.         j=0
  269.             for i in $backup_file_info
  270.         do
  271.             ((j++))
  272.             a[$j]=$i
  273.             if [ $j = 3 ]; then
  274.                 echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} $backup_dir/${a[$j]}"
  275.                  j=0
  276.             fi
  277.         done
  278.             echo "$white"
  279.                 echo "Controlfile information:$yellow"
  280.         echo ""
  281.         j=0
  282.         for p in $control_file;do
  283.             ((j++))
  284.             a[$j]=$p
  285.             if [ $j = 3 ] ; then
  286.                     echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} $controlfile_dir/${a[$j]}"
  287.                     j=0
  288.             fi
  289.          done
  290.         echo "$white"
  291.                 echo "log information:$blue"
  292.         echo ""
  293.         j=0
  294.         for p in $log_file_info;do
  295.             ((j++))
  296.             a[$j]=$p
  297.             if [ $j = 3 ] ; then
  298.                     echo "${a[`expr $j - 2`]} ${a[`expr $j - 1`]} $log/${a[$j]}"
  299.                     j=0
  300.             fi
  301.          done
  302.         echo "$white"
  303.             echo "If you want increment backup database again,please use \"repeat\" parameter"
  304.                 echo " ############Today database backup information the end ########## "
  305.         fi
  306. fi
  307. # end print backup information #
  308. # copy backup file #
  309. # if [ "$1" = "sync" ] ; then
  310. #    backup_status=9    
  311. # fi
  312. # if [ "$backup_status" = 9 ]; then
  313. #    sync
  314. # else
  315. # echo "Today's Backup file is synced. please check whether it's in the target database."
  316. # echo "If you want to sync again,please use \"sync\" parameter."
  317. # exit 0
  318. # fi
  319. echo "If you want to view all backup information,Please use \"all\" parameter."
  320. ) | tee -a $log/autobak_`date +'%Y%m%d'`.log
  321. mail -s "`date +'%Y%m%d'` database backup information" $mail_to_admin < $log/autobak_`date +'%Y%m%d'`.log

阅读(6751) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~