RAC RMAN定期備份及刪除腳本
1.環境說明:
os--linux
oracle--11.2.03
一臺服務器上兩個庫orcl prod
RAC雙節點環境,采用節點1執行定制備份及刪除腳本
2.備份策略說明:
1)周1 周4 周6 執行全庫備份,加上備份產生的歸檔,周2 周3 周日執行歸檔日誌備份
備註:因為數據庫為10g以下的小庫,采用全庫備份;如果是大庫而且變化較小的庫,采用增量備份;
2)備份保存15天,15天以前的備份刪除
3)oracle數據庫的歸檔保存30天
舉例以一臺服務器上多個庫(orcl 和prod兩個庫的情況下的腳本,如果一個庫可以適當的去掉循環)
3.數據庫配置
3.1更改控制文件記錄的備份時長
備份及刪除腳本上有配置configure retention policy to recovery window of 15 days;
備註:
CONTROL_FILE_RECORD_KEEP_TIME參數默認保留7天。
建議CONTROL_FILE_RECORD_KEEP_TIME參數值設置不小於選中數據庫的備份保存周期的值。
理論上CONTROL_FILE_RECORD_KEEP_TIME的天數必須大於等於CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF xx DAYS
4.備份及刪除腳本如下:
4.1全庫備份腳本:
使用oracle用戶創建如下腳本
vi /home/oracle/full_bak.sh
#! /bin/bash
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
current_day=`date +%Y%m%d`
basdir=/home/oracle/rmanbackup
for i in {orcl,prod}
export ORACLE_SID=${i}"1"
Rdir=$basdir/$i
if [ ! -d $Rdir/$current_day ]
then
mkdir -p $Rdir/$current_day
rman target / <<EOF
run{
allocate channel d1 type disk maxpiecesize=30G;
allocate channel d2 type disk maxpiecesize=30G;
backup as compressed backupset database format '$Rdir/$current_day/full_%d_%T_%u.bkp';
sql 'alter system archive log current';
sql 'alter system archive log current';
sql 'alter system archive log current';
backup archivelog all not backed up 1 times format '$Rdir/$current_day/arclog_%d_%T_%u.bkp';
backup spfile format '$Rdir/$current_day/spfile_%d_%T_%u.bkp';
backup current controlfile format '$Rdir/$current_day/controlfile_%d_%T_%u.bkp';
crosscheck backup;
delete noprompt expired backup;
configure retention policy to recovery window of 15 days;
delete noprompt obsolete;
release channel d1;
release channel d2;
}
EOF
else
echo "$i's full db backup on $current_day has been completed;"
fi
done
4.2歸檔日誌備份腳本:
vi /home/oracle/arclog_bak.sh
#! /bin/bash
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
current_day=`date +%Y%m%d`
basdir=/home/oracle/rmanbackup
for i in {orcl,prod}
do
export ORACLE_SID=${i}"1"
Rdir=$basdir/$i
if [ ! -d $Rdir/$current_day ]
then
mkdir -p $Rdir/$current_day
rman target / <<EOF
run{
allocate channel d1 type disk maxpiecesize=30G;
allocate channel d2 type disk maxpiecesize=30G;
sql 'alter system archive log current';
backup archivelog all not backed up 1 times format '$Rdir/$current_day/arclog_%d_%T_%u.bkp';
backup spfile format '$Rdir/$current_day/spfile_%d_%T_%u.bkp';
backup current controlfile format '$Rdir/$current_day/controlfile_%d_%T_%u.bkp';
crosscheck backup;
delete noprompt expired backup;
configure retention policy to recovery window of 15 days;
delete noprompt obsolete;
release channel d1;
release channel d2;
}
EOF
else
echo "$i's archivelog backup on $current_day has been completed;"
fi
done
說明:
%d:Oracle數據庫名稱
%T:當前時間的年月日格式(YYYYMMDD)
%u:是一個由備份集編號和建立時間壓縮後組成的8字符名稱。利用%u可以為每個備份集生成一個唯一的名稱
4.3日誌刪除腳本:
vi /home/oracle/del_arclog_bak.sh
#! /bin/bash
export NLS_LANG='AMERICAN_AMERICA.AL32UTF8'
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin
basdir=/home/oracle/rmanbackup
current_day=`date +%Y%m%d`
for i in {orcl,prod}
do
export ORACLE_SID=${i}"1"
rman target / <<EOF
run{
delete noprompt archivelog all completed before 'sysdate-30';
}
EOF
done
4.4備份空文刪腳本:
vi /home/oracle/del_db_bak.sh
#!/bin/bash
removedir=/home/oracle/rmanbackup
dt=`date +%Y%m%d -d "15 day ago"`
current_day=`date +%Y%m%d`
for i in {orcl,prod}
do
Rdir=$removedir/$i
for subdir in `ls $Rdir`;
do
if [ "${subdir}" -lt "${dt}" ];
then
rm -rf $Rdir/$subdir >/dev/null
echo "on $current_day the directory $Rdir/$subdir has been removed."
fi
done
done
5.調試腳本
5.1更改腳本具有可執行權限
su - oracle
chmod u+x full_bak.sh
chmod u+x arclog_bak.sh
chmod u+x del_arclog_bak.sh
chmod u+x del_db_bak.sh
5.2設置定時任務前調試腳本
於腳本當前路徑下執行,測試腳本
./full_bak.sh >> full_bak.log 2>&1
./arclog_bak.sh >> arclog_bak.log 2>&1
./del_arclog_bak.sh >> del_arclog.log 2>&1
./del_db_bak.sh >> del_bak.log 2>&1
6.配置定時任務執行腳本
在節點1上執行:
=========================
輸入crontab -e
按下a鍵進入到編輯模式
輸入如下
10 0 * * 1,4,6 /home/oracle/full_bak.sh >> /home/oracle/rmanbackup/backup.log 2>&1
10 0 * * 2,3,5,0 /home/oracle/arclog_bak.sh >> /home/oracle/rmanbackup/backup.log 2>&1
10 2 * * * /home/oracle/del_arclog_bak.sh >> /home/oracle/rmanbackup/del.log 2>&1
10 3 * * * /home/oracle/del_db_bak.sh >> /home/oracle/rmanbackup/del.log 2>&1
同時按下ctrl+c退出編輯模式
按下shift+: 輸入wq 退出 crontab
crontab -l 查看已經建立的定時任務
RAC RMAN定期備份及刪除腳本