Linux下完整的RMAN增量备份shell脚本

  此备份脚本整理自网络,再此只是添加了些注释。此脚本思路清晰,稍做更改便可应用到生产机上。

  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 EOF

106 fi

107 

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.log

109 

110 echo "------------------------------end------------------------------";date

 

你可能感兴趣的:(shell脚本)