很多时候,我们DBA需要定时对生产环境进行整体备份,并恢复到测试环境,供开发人员调试或测试,如果数据量比较大的情况下,RMAN方式比较高效省时,下面策略是在生产环境部署备份任务,并在测试模拟环境进行定时还原的脚本。

生产库源端每周日全备:

#/bin/sh

source ~/.bash_profile

export BACKUP_DATE=`date +%Y%m%d`

export BACKUP_PATH=/orabak

echo `mkdir -p ${BACKUP_PATH}/${BACKUP_DATE}/backupset`

echo `mkdir -p ${BACKUP_PATH}/${BACKUP_DATE}/archivelog`

echo `mkdir -p ${BACKUP_PATH}/${BACKUP_DATE}/controlfile`

rman target / nocatalog msglog=$BACKUP_PATH/${BACKUP_DATE}/bak_0_$BACKUP_DATE.log << EOF

set encryption on identified by "*******" only;

run{

allocate channel c1 type disk;

allocate channel c2 type disk;

allocate channel c3 type disk;

backup incremental level 0 database tag='level_0' format '$BACKUP_PATH/${BACKUP_DATE}/backupset/level_0_%d_%T_%s_%U';

sql 'alter system archive log current';

backup archivelog from time "to_date(to_char(sysdate,'yyyy-mm-dd')||' 00:50:00','yyyy-mm-dd hh24:mi:ss')" until time "sysdate" format '$BACKUP_PATH/${BACKUP_DATE}/archivelog/arch_level_0_%d_%T_%s_%U';

release channel c1;

release channel c2;

release channel c3;

}

backup current controlfile tag='bak_ctlfile' format '$BACKUP_PATH/${BACKUP_DATE}/controlfile/ctl_file_%U_%T';

crosscheck backup;

delete noprompt expired backup;

crosscheck archivelog all;

report obsolete;

delete noprompt obsolete;

exit

EOF

cd /orabak

#备份后并通过rsync方式传输至源端的测试环境:

rsync -av $BACKUP_DATE --password-file=/etc/.rsync.pass oracle@10.0.32.15::orabak

生产库源端每日增量备份:

#/bin/sh

source ~/.bash_profile

export BACKUP_DATE=`date +%Y%m%d`

export BACKUP_PATH=/orabak

echo `mkdir -p ${BACKUP_PATH}/${BACKUP_DATE}/backupset`

echo `mkdir -p ${BACKUP_PATH}/${BACKUP_DATE}/archivelog`

echo `mkdir -p ${BACKUP_PATH}/${BACKUP_DATE}/controlfile`

rman target / nocatalog msglog=$BACKUP_PATH/${BACKUP_DATE}/bak_1_$BACKUP_DATE.log << EOF

set encryption on identified by "*******" only;

run{

allocate channel c1 type disk;

allocate channel c2 type disk;

allocate channel c3 type disk;

backup incremental level 1 database tag='level_1' format '$BACKUP_PATH/${BACKUP_DATE}/backupset/level_1_%d_%T_%s_%U';

sql 'alter system archive log current';

backup archivelog from time "to_date(to_char(sysdate,'yyyy-mm-dd')||' 00:50:00','yyyy-mm-dd hh24:mi:ss')" until time "sysdate" format '$BACKUP_PATH/${BACKUP_DATE}/archivelog/arch_level_0_%d_%T_%s_%U';

backup current controlfile tag='bak_ctlfile' format '$BACKUP_PATH/${BACKUP_DATE}/controlfile/ctl_file_%U_%T';

release channel c1;

release channel c2;

release channel c3;

}

crosscheck backup;

delete noprompt expired backup;

crosscheck archivelog all;

report obsolete;

delete noprompt obsolete;

exit

EOF

cd /orabak

#备份后并通过rsync方式传输至源端的测试环境:

rsync -av $BACKUP_DATE --password-file=/etc/.rsync.pass oracle@10.0.32.15::orabak

linux定时调用备份任务:

[oracle@oracle03 rman_script]$ crontab -l

30 05 * * 1,2,3,4,5,6 /orabak/rman_script/lev_1_bak.sh

30 05 * * 0 /orabak/rman_script/full_bak.sh

目标端部署定时还原脚本:

[oracle@test rman_script]$ crontab -l

30 07 * * * /orabak/rman_script/recover_db.sh

测试环境还原脚本设计:

[oracle@gzrmzdata2006 rman_script]$ more recover_db.sh


#/bin/sh

source ~/.bash_profile

export BACKUP_DATE=`date +%Y%m%d`

export OLD_BACKUP_DATE=`date +%Y%m%d -d "-8days"`

/opt/app/oracle/product/11.2.0/db_1/bin/sqlplus "/as sysdba" << EOF

spool /orabak/${BACKUP_DATE}/reboot_db.log

shutdown immediate

startup nomount

spool off

exit

--rman离线高级复制恢复

rman auxiliary / msglog=/orabak/${BACKUP_DATE}/recover_$BACKUP_DATE.log << EOF

set decryption identified by "oraba_K0415";

run{

allocate auxiliary channel c1 type disk;

allocate auxiliary channel c2 type disk;

allocate auxiliary channel c3 type disk;

DUPLICATE DATABASE TO erprac BACKUP LOCATION '/orabak/';

}

至此,测试环境定时RMAN恢复任务还原成功!