Oracle 11g R2 RAC RMAN備份指令碼樣本
一、將RAC切換成歸檔模式
1. 修改資料庫的歸檔模式,通常在安裝RAC的時候都會配置歸檔並且使用閃回區,已經配置過歸檔下面的方式可以略過。
SQL> alter system set cluster_database=false scope=spfile sid='*';
2. 關閉所有執行個體(兩邊都要shutdown)
SQL> shutdown immediate
或直接關閉所有執行個體
$ srvctl stop database -d orcl
3. 在任意一個執行個體上將資料庫啟動到mount狀態,修改資料庫歸檔模式
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter system set cluster_database=true scope=spfile sid='*';
SQL> shutdown immediate;
4. 啟動所有執行個體
$ srvctl start database -d orcl
5. 驗證歸檔模式開啟
SQL> archive log list;
二、配置NFS共用目錄,用於儲存RMAN備份資料
1. 配置nfs服務,確保備份目錄兩邊都可以訪問,這裡測試,使用節點1提供nfs服務。
node1配置:
# cat /etc/exports
#說明節點二同時也掛載節點的RMAN備份目錄。
/data/rman_bak db2(rw,no_root_squash)
2. node2配置,節點2做為nfs用戶端掛載,兩目錄保持一致。
# 手動掛載
# mount -t nfs db1:/data/rman_bak /data/rman_bak
# 配置開機自動掛載
# cat /etc/fstab
db1:/data/rman_bak /data/rman_bak nfs defaults 0 0
三、Oracle 11g RAC資料庫參數修改
1. 修改控制檔案的儲存備份時間
說明:預設7天,根據情況修改。
SQL> show parameter control;
SQL> alter system set control_file_record_keep_time=40 scope=both;
2. RMAN 配置參數
開啟控制檔案的自動備份,開啟之後在Database Backup或者資料檔案(比如添加資料檔案)有修改的時候都會自動備份控制檔案和spfile檔案。
RMAN> configure controlfile autobackup on;
RMAN> configure retention policy to recovery window of 30 days;
3. 備份策略
確定資料庫運行在歸檔模式,一天一次對資料庫做一次備份,可以根據備份伺服器上磁碟空間以及自定策略來決定儲存備份的時間。
每周日 做 Level 0 級備份
每周一,二,三,四,五,六 做 Level 1 級備份
四、 RMAN備份指令碼配置
1. 準備目錄
# su – oracle
說明:可以根據實際情況建立相關目錄,注意目錄許可權。
$ mkdir -p /u01/app/oracle/rman_bak/scripts
$ mkdir -p /data/rman_bak/data
$ mkdir -p /data/rman_bak/logs
說明: RAC的備份如果採用ASM的話,備份只在一個節點進行就可以了。也就是說這個備份儲存到了其中的一個節點上了,其它節點如果採用NFS的方式也是可以實現恢複的。
2. 建立指令碼內容
$ vi /u01/app/oracle/rman_bak/scripts/rman_bak.sh
$ chmod u+x /u01/app/oracle/rman_bak/scripts/rman_bak.sh
3. 添加計劃任務
$ crontab -e
# RMAN
00 1 * * 0 /u01/app/oracle/rman_bak/scripts/rman_bak.sh 0
00 1 * * 1,2,3,4,5,6 /u01/app/oracle/rman_bak/scripts/rman_bak.sh 1
4. 手動測試通道
手動添加db1,db2本地服務名配置,11g rac預設只有scan ip的伺服器,本地服務名需要手動添加如下:
# su - oracle
$ vi /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = scan-cluster)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db1-vip)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
(INSTANCE_NAME = orcl1)
)
)
ORCL2 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = db2-vip)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
(INSTANCE_NAME = orcl2)
)
)
手動測試通道
rman target /
configure channel 1 device type disk connect 'sys/oracle123@orcl1';
configure channel 2 device type disk connect 'sys/oracle123@orcl2';
4. 指令碼步署方案
$ vi /u01/app/oracle/rman_bak/scripts/rman_bak.sh
#!/bin/bash
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export ORACLE_SID=orcl1
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
export PATH=$ORACLE_HOME/bin:$PATH
LEVEL=$@
DATE=`date +%w`
DATE_2=`date +%Y%m%d`
BACKUP_PATH="/data/rman_bak"
BIN=$ORACLE_HOME/bin
if [ $# != 1 ]; then
echo "usage: rman_bak.sh n
where n is the rman backup level(Only 0,1 is permitted)."
exit 1
fi
if [ $@ -ne 0 -a $@ -ne 1 ]; then
echo "usage: rman_bak.sh n
where n is the rman backup level(Only 0,1 is permitted)."
exit 2
fi
if [[ $LEVEL = 0 ]]; then
$BIN/rman log $BACKUP_PATH/logs/level.$ORACLE_SID.$LEVEL.$DATE_2.log <<EOF
connect target /;
run{
allocate channel c1 device type disk connect 'sys/oracle@orcl1';
allocate channel c2 device type disk connect 'sys/oracle@orcl2';
crosscheck backupset of archivelog all;
backup archivelog all format '$BACKUP_PATH/data/archlog.%d.level.$LEVEL.%U_%T' delete all input;
delete noprompt expired backupset of archivelog all;
release channel c1;
release channel c2;
}
run{
allocate channel c1 device type disk connect 'sys/oracle@orcl1';
allocate channel c2 device type disk connect 'sys/oracle@orcl2';
crosscheck backupset of database;
backup incremental level $LEVEL database format '$BACKUP_PATH/data/data.%d.level.$LEVEL.%U_%T';
backup spfile tag='spfile' format '$BACKUP_PATH/data/spfile_%U_%T';
backup current controlfile tag='control' format='$BACKUP_PATH/data/control_%U_%T';
delete noprompt expired backupset of database;
delete noprompt obsolete;
release channel c1;
release channel c2;
}
exit;
EOF
else
$BIN/rman log $BACKUP_PATH/logs/level.$ORACLE_SID.$LEVEL.$DATE_2.log <<EOF
connect target /;
run{
allocate channel c1 device type disk connect 'sys/oracle@orcl1';
allocate channel c2 device type disk connect 'sys/oracle@orcl2';
crosscheck backupset of archivelog all;
backup archivelog all format '$BACKUP_PATH/data/archlog.%d.level.$LEVEL.%U_%T' delete all input;
delete noprompt expired backupset of archivelog all;
release channel c1;
release channel c2;
}
run{
allocate channel c1 device type disk connect 'sys/oracle@orcl1';
allocate channel c2 device type disk connect 'sys/oracle@orcl2';
crosscheck backupset of database ;
backup incremental level $LEVEL database format '$BACKUP_PATH/data/data.%d.level.$LEVEL.%U_%T';
backup spfile tag='spfile' format '$BACKUP_PATH/data/spfile_%U_%T';
backup current controlfile tag='control' format='$BACKUP_PATH/data/control_%U_%T';
delete noprompt expired backupset of database ;
delete noprompt obsolete ;
release channel c1;
release channel c2;
}
exit;
EOF
fi
5. 手動執行測試
$ /u01/app/oracle/rman_bak/scripts/rman_bak.sh 0
測試注意細節:
1)觀察rman log日誌有無異常錯誤。
2)觀察archivelog是否有備份與刪除。
Oracle資料庫rman備份計劃及恢複
Oracle RMAN實現“一鍵式”資料表空間TSPITR
Oracle 11gR2利用RMAN複製整庫記錄
RMAN 配置歸檔日誌刪除策略
Oracle基礎教程之通過RMAN複製資料庫
RMAN備份策略制定參考內容
RMAN備份學習筆記
OracleDatabase Backup加密 RMAN加密