1. 程式人生 > >RMAN備份計劃及指令碼

RMAN備份計劃及指令碼

最近新交接了一個專案,對專案的備份機制不太瞭解,所以找到備份指令碼研究了下,初學者,憑自己的理解加了註釋避免遺忘,貼出來如有不足的請多多指教微笑

$crontab -l               ##先檢視執行計劃

00 01 20 * * /u02/rman/scripts/bakdbfull.sh > /dev/null
00 02 * * * /u02/rman/scripts/baklog.sh > /dev/null
00 03 * * * /u02/rman/scripts/bak_to_dis_rec.sh > /dev/null
##當crontab呼叫時,錯誤和標準輸出會寫成mail通知你不需要記錄額外資訊的話,把標準輸出和錯誤重定向到/dev/null(可以理解為空檔案,輸到這裡就找不回來了)

rman備份指令碼
#####################baklog.sh
export ORACLE_HOME=/u01/orcl/product/10.2.0/db
export ORACLE_SID=CDBRWA1
/u01/orcl/product/10.2.0/db/bin/rman target / log=/u02/rman/backup/rwa/rman_arc_daily.log <<-EOF   ##寫日誌rman_arc_daily.log
run{
##備份歸檔日誌刪除所有輸入壓縮備份集,格式:%U%s.logbak
backup as compressed backupset archivelog all delete all input format '/u02/rman/backup/rwa/%U%s.logbak';
}
exit;
EOF
more /u02/rman/backup/rwa/rman_arc_daily.log >>/u02/rman/backup/rwa/rman_arc.log
exit

######################bak_to_dis_rec.sh 
export ORACLE_HOME=/u01/orcl/product/10.2.0/db
export ORACLE_SID=CDBRWA1
/u01/orcl/product/10.2.0/db/bin/rman target / log=/u02/rman/backup/rwa/rman_full_monthly.log <<-EOF
run{
##備份資料庫壓縮備份集,格式:%U%s.logbak
backup as compressed backupset database format '/u02/rman/backup/rwa/%U%s.bak';
}
exit;
EOF
more /u02/rman/backup/rwa/rman_full_monthly.log >>/u02/rman/backup/rwa/rman_full.log
exit

#######################bakdbfull.sh
back_time=`date+%Y%m%d%H%M`
back_file_path=/u02/rman/backup/rwa
#建立壓縮:查詢24小時(1天)以內檔名含有"*bak"的到目標資料夾
tar -cvf $back_file_path/$back_time.tar $(find $back_file_path/ -name "*bak" -mtime -1)
[ -f $back_file_path/$back_time.tar ]
if [ $? = 0] ; then
echo "$back_time backup informaion!" >> $back_file_path/bak_to_dis_rec.log
ftp -n<<EOF >> $back_file_path/bak_to_dis_rec.log ##-n限制FTP自動登入,<<EOF>>為輸入重定向,表示前面的ftp把後面的指令作為輸入的指令,直到遇到EOF
  open 10.68.xx.xx
  user rwa rwaadmin
  binary    ##使用二進位制的檔案傳輸方式(需要用這種方式傳輸的型別有:ISO檔案,可執行檔案,壓縮檔案,圖片等)
  lcd $back_time.tar   ##相當於cd ,命令定位本地機器目錄
  prompt  ##關閉傳輸確認提示
  mput $back_time.tar
  close
  bye
EOF
rm $back_file_path/$back_time.tar
else
  echo "There is something wrong with FTP of $back_time.\nPlease check the the ShellScript /u02/rman/scripts/bak_to_dis_rec.sh" >>$back_file_path/bak_to_dis_rec.log
fi

注:linux crontab命令基本格式 :
*  *  *  *  *  command
分  時  日  月  周  命令
注意,上述都是空格隔開的
第1列表示分鐘1~59 每分鐘用*或者 */1表示
第2列表示小時1~23(0表示0點)
第3列表示日期1~31
第4列表示月份1~12
第5列標識號星期0~6(0表示星期天)
第6列要執行的命令 注:格式化代表的意思
%c 備份片的拷貝數
%d 資料庫名稱
%D 位於該月中的第幾天 (DD)
%M 位於該年中的第幾月 (MM)
%F 一個基於DBID唯一的名稱,這個格式的形式為c-IIIIIIIIII-YYYYMMDD-QQ,其中IIIIIIIIII為該資料庫的DBID,YYYYMMDD為日期,QQ是一個1-256的序列
%n 資料庫名稱,向右填補到最大八個字元
%u 一個八個字元的名稱代表備份集與建立時間
%p 該備份集中的備份片號,從1開始到建立的檔案數
%U 一個唯一的檔名,代表%u_%p_%c
%s 備份集的號
%t 備份集時間戳
%T 年月日格式(YYYYMMDD)

寫日誌:
在使用rman的時候經常會碰到以下兩種場景,需要把rman的日誌輸出到檔案中; 
1、顯示的日誌太多,導致一個螢幕顯示不完,影響了問題的診斷,這時候需要把rman的log輸出到文字中,整個的診斷過程就相對容易了許多。
2、在使用自動備份的時候,需要把日誌輸出到文字中,便於第二天進行備份任務的檢查; 
 
一、簡單的日誌資料的指令碼 
rman target /   log   /u01/rman.log  
該指令碼直接把rman的日誌寫到/u01/,並命名為rman.log,但是使用這種方法的時候,螢幕不會顯示日誌的。 
二、輸出日誌的同時也在螢幕上列印日誌 
rman target / | tee /u01/rman.log 
該指令碼把rman日誌出到檔案/u01/rman.log,並列印到螢幕中; 
三、在進行自動備份的時候,往往需要需要產生的日誌自動增加備份的日期 
以linux系統為例,通過如下指令碼實現 
export Today=`date +%Y%m%d`  
rman target /   log /home/oracle/${Today}.log