此備份指令碼整理自網路,再此只是添加了些注釋。此指令碼思路清晰,稍做更改便可應用到生產機上。
1 #!/bin/bash 2 ####################################################################### 3 # 4 # 備份策略: 5 # 每周日淩晨做0級備份, 6 # 每周一,四做一級備份, 7 # 每周二,三,五,六做二級備份, 8 # 00 4 * * 0 /u01/app/oracle/rman_bak/scripts/rman_bak.sh 0 9 # 00 4 * * 1,4 /u01/app/oracle/rman_bak/scripts/rman_bak.sh 1 10 # 00 4 * * 2,3,5,6 /u01/app/oracle/rman_bak/scripts/rman_bak.sh 2 11 # 12 ####################################################################### 13 #備份指令碼: 14 #set env 15 #環境變數根據 實際情況自行修改 16 export ORACLE_BASE=/u01/app/oracle 17 export ORACLE_HOME=$ORACLE_BASE/db10g 18 export ORACLE_SID=emar 19 export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK 20 export PATH=$ORACLE_HOME/bin:$PATH 21 22 DATE=`date +%w` 23 DATE_2=`date +%Y%m%d` 24 BACKUP_PATH=/u01/app/oracle/rman_bak 25 #傳進來的參數:備份層級 26 LEVEL=$@ 27 TARGET_SID=emar 28 RMAN_SID=emar.emar 29 BIN=/u01/app/oracle/db10g/bin 30 31 # Delete the data backuped last time 此處根據保留備份時間自行修改 32 #rm -rf $BACKUP_PATH/data/$DATE/* 33 34 if [ $# != 1 ]; then 35 echo "usage: rman_bak.sh n 36 where n is the rman backup level(0,1,2 is permitted)." 37 exit 1 38 fi 39 40 if [ $@ -ne 0 -a $@ -ne 1 -a $@ -ne 2 ]; then 41 echo "usage: rman_bak.sh n 42 where n is the rman backup level(Only 0,1,2 is permitted)." 43 exit 2 44 fi 45 46 echo "-----------------------------start-----------------------------";date 47 48 #0級備份,調用rman時利用了here document 49 if [ $LEVEL = 0 ]; then 50 51 $BIN/rman log $BACKUP_PATH/log/level.$TARGET_SID.$LEVEL.$DATE_2.log <<EOF 52 connect target sys/sys202; 53 #串連catalog 54 connect catalog rman/rman@$RMAN_SID; 55 resync catalog; 56 run{ 57 allocate channel c1 type disk ; 58 #交叉查對資料庫中的歸檔日誌 59 crosscheck backupset of archivelog all ; 60 #備份7天以內的歸檔日誌;format格式根據公司規範自行更改 61 backup filesperset 3 format '$BACKUP_PATH/data/$DATE/arch.%d.live.$LEVEL.%t'(archivelog from time 'sysdate-7' all delete input) ; 62 #刪除狀態為expired的歸檔日誌,noprompt為不提示 63 delete noprompt expired backupset of archivelog all ; 64 release channel c1 ; 65 } 66 67 run{ 68 allocate channel c2 type disk ; 69 crosscheck backupset of database ; 70 backup incremental level $LEVEL filesperset 3 format '$BACKUP_PATH/data/$DATE/data.%d.live.$LEVEL.%t'(database include current controlfile) ; 71 --sql 'alter system archive log current'; 72 delete noprompt expired backupset of database ; 73 delete noprompt obsolete ; 74 release channel c2 ; 75 } 76 exit; 77 EOF 78 79 #根據調用時間及傳進的參數進行1,2級備份 80 else 81 $BIN/rman log $BACKUP_PATH/log/level.$TARGET_SID.$LEVEL.$DATE_2.log <<EOF 82 connect target sys/sys202; 83 connect catalog rman/rman@$RMAN_SID; 84 resync catalog; 85 run{ 86 allocate channel c1 type disk ; 87 --sql 'alter system archive log current'; 88 crosscheck backupset of archivelog all ; 89 backup filesperset 3 format '$BACKUP_PATH/data/$DATE/arch.%d.live.$LEVEL.%t' (archivelog from time 'sysdate-1' all) ; 90 delete noprompt expired backupset of archivelog all ; 91 release channel c1 ; 92 } 93 94 run{ 95 allocate channel c2 type disk ; 96 crosscheck backupset of database ; 97 backup incremental level $LEVEL filesperset 3 format '$BACKUP_PATH/data/$DATE/data.%d.live.$LEVEL.%t' (database include current controlfile) ; 98 --sql 'alter system archive log current'; 99 delete noprompt expired backupset of database ;100 delete noprompt obsolete ;101 release channel c2 ;102 }103 104 exit;105 EOF106 fi107 108 $BIN/exp rman/rman@$RMAN_SID file=$BACKUP_PATH/data/$DATE/live.rman.$DATE_2.dmp log=$BACKUP_PATH/log/$RMAN_SID.rman.$DATE_2.log109 110 echo "------------------------------end------------------------------";date