备份脚本说明:
(1) 环境:HP-UX B.11.11+Oracle 9.2.0.6
(2) 主机空间要求:脚本导出的文件为一个owner级的数据库备份为一个文件,必须保证空间有足够的剩余量;必须保证主机支持大文件系统,以防止大于2G的文件导出失败。
(3) 部署:
$ su – oracle
oracle@gd_rdb01:/oracle$ mkdir -p /rpt_bak/gd_noarch_db_bk/bin –备份文件存放主目录
oracle@gd_rdb01:/oracle$ mkdir -p /rpt_bak/gd_noarch_db_bk/exp_file/last_exp_file –上次exp备份集的路径
oracle@gd_rdb01:/oracle$ mkdir -p /rpt_bak/gd_noarch_db_bk/exp_file/this_exp_file –本次exp备份集的路径
oracle@gd_rdb01:/oracle$ mkdir -p /rpt_bak/gd_noarch_db_bk/exp_log –备份log的路径
oracle@gd_rdb01:/oracle$ mkdir -p /rpt_bak/gd_noarch_db_bk/mail_result –发送邮件的邮件内容路径
oracle@gd_rdb01:/rpt_bak/gd_noarch_db_bk/bin$chmod +x gd_noarch_db_bk.sh — 给脚本赋予执行权限
部署crontab,每天凌晨0点10运行
oracle@gd_rdb01:/rpt_bak/gd_noarch_db_bk/mail_result$ crontab -l
10 0 * * * /rpt_bak/gd_noarch_db_bk/bin/gd_noarch_db_bk.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
#!/bin/sh ######################################################################### # GuangDong noarchive database exp solution # # This script must be used in large filesystem # # 2007-03-15 writen by jimmyhe1981@gmail.com # ######################################################################### . /oracle/.bash_profile # Path Define exp_path=/oradata/ora_bak exp_log=${exp_path}/exp_log exp_file=${exp_path}/exp_file this_exp_file=${exp_file}/this_exp_file last_exp_file=${exp_file}/last_exp_file mail_result=${exp_path}/mail_result main_bak_path=`echo ${exp_path} |awk -F/ '{print $2}'` cd ${exp_path} # SENDMAIL Define DBALIST="jimmyhe1981@gmail.com" #DB_backup principle #weekly backup:backup every Monday and every Thursday--bk_principle=2 #daily backup:backup every day--bk_principle<=2 sqlplus -s "/ as sysdba"<<EOF>/dev/null set feedback off set pages 0 set head off set echo off spool bk_principle.tmp1 SELECT to_char(SYSDATE,'yyyymmdd')-to_char(SYSDATE,'yyyymmdd') FROM dual; spool off spool bk_principle.tmp2 SELECT to_char(SYSDATE,'yyyymmdd')-to_char(trunc(SYSDATE,'iw'),'yyyymmdd') FROM dual; spool off spool bk_principle.tmp3 SELECT to_char(SYSDATE,'yyyymmdd')-to_char(trunc(SYSDATE,'iw')+3,'yyyymmdd') FROM dual; spool off exit EOF read bk_principle1<bk_principle.tmp1 read bk_principle2<bk_principle.tmp2 read bk_principle3<bk_principle.tmp3 echo $bk_principle1>bk_principle.tmp echo $bk_principle2>>bk_principle.tmp echo $bk_principle3>>bk_principle.tmp cat bk_principle.tmp |grep 0|wc -l>bk_principle_all.tmp read bk_principle<bk_principle_all.tmp rm *.tmp* #Sys_freespace_percent threshold Define sys_free_thre=85 #Noarch_db_info Define #newsims_db_info newsimsuser=newsims newsimspwd=newsims newsimssid=gdsims #date_format_yyyymmdd date_yyyymmdd=`date '+%Y%m%d'` cd ${exp_path} rm *.log ############## NEWSIMS FULL DB BACKUP BEGIN ##################################################### #-------daily backup,so bk_principle must less or equal to 2-------- if [ ${bk_principle} -le 2 ] then #-------check system free space-------- sys_freespace=`df -k |grep ${main_bak_path} |awk '{print $4}'` sys_freespace_percent=`df |grep ${main_bak_path} |awk '{print $5}' |awk -F% '{print $1}'` sqlplus -s ${newsimsuser}/${newsimspwd}@${newsimssid}<<EOF>/dev/null set feedback off set pages 0 set head off spool newsims_segment.tmp select sum(bytes)/1024 from user_segments; spool exit EOF read newsims_segment < newsims_segment.tmp rm newsims_segment.tmp if [ ${newsims_segment} -lt ${sys_freespace} ] && [ ${sys_freespace_percent} -lt ${sys_free_thre} ] then echo "newsims_full_db backup start at:" `date` >>newsims_exp_${date_yyyymmdd}.result exp ${newsimsuser}/${newsimspwd}@${newsimssid} owner=${newsimsuser} file=${exp_path}/${newsimsuser}_full_${date_yyyymmdd}.dmp log=${exp_path}/${newsimsuser}_full_${date_yyyymmdd}.log buffer=60000000; succ_flag=`cat ${exp_path}/${newsimsuser}_full_${date_yyyymmdd}.log |grep "Export terminated successfully without warnings" |wc -l` if [ $succ_flag -ne 1 ] then echo "newsims_full_db backup failed,pls check the exp_log:'"${newsimsuser}_full_${date_yyyymmdd}.log"'!">>newsims_exp_${date_yyyymmdd}.result echo "newsims_full_db backup end at:" `date` >>newsims_exp_${date_yyyymmdd}.result else echo "newsims_full_db backup succeed!" >>newsims_exp_${date_yyyymmdd}.result echo "newsims_full_db backup end at:" `date` >>newsims_exp_${date_yyyymmdd}.result #-------if backup succeed,delete last_bk_file,mv the bk_file to this_exp_file----------- rm ${last_exp_file}/${newsimsuser}_full*.dmp mv ${this_exp_file}/${newsimsuser}_full*.dmp ${last_exp_file} mv ${exp_path}/${newsimsuser}_full_${date_yyyymmdd}.dmp ${this_exp_file} fi else echo "newsims_full_db backup failed because of not enough space!">>newsims_exp_${date_yyyymmdd}.result echo "newsims_full_db backup end at:" `date` >>newsims_exp_${date_yyyymmdd}.result fi echo '--------------------------------------'>>newsims_exp_${date_yyyymmdd}.result mv *.log ${exp_log} fi ############## NEWSIMS FULL DB BACKUP END ###################################################### #------send the result to dba----------------- echo "From:dba@gd_noarch_db_bk">all2mail_${date_yyyymmdd}.result echo "To: jimmyhe1981@gmail.com">>all2mail_${date_yyyymmdd}.result ls -rt newsims_exp_${date_yyyymmdd}.result >list.txt for file in `cat list.txt` do cat $file>>all2mail_temp.result done fail_result=`cat all2mail_temp.result |grep failed |wc -l` if [ $fail_result -ge 1 ] then echo "Subject:GD_NEWSIMS_DB_BK_SITUATION(HAVE FAILED.)">>all2mail_${date_yyyymmdd}.result else echo "Subject:GD_NEWSIMS_DB_BK_SITUATION(ALL SUCCED.)">>all2mail_${date_yyyymmdd}.result fi echo "================= GD_NEWSIMS_DB_BK_SITUATION REPORT =================">>all2mail_${date_yyyymmdd}.result cat all2mail_temp.result>>all2mail_${date_yyyymmdd}.result echo "============================ REPORT END ============================">>all2mail_${date_yyyymmdd}.result sendmail jimmyhe1981@gmail.com < all2mail_${date_yyyymmdd}.result rm list.txt mv all2mail_${date_yyyymmdd}.result ${mail_result} rm *.result |
一条评论
不错啊!很多实际问题/实用技巧。建议将平时学习心得,实验过程写下来