RMAN备份策略与异机恢复一例

时间:2022-05-15 07:48:06

实验环境:
A机器(生产用途):RHEL 6.5 + Oracle 11.2.0.4 + IP Address 192.168.1.11
B机器(备机用途):RHEL 6.5 + Oracle 11.2.0.4 + IP Address 192.168.1.61

需求:
A机器RMAN备份完成后自动传输备份文件到B机器。防止A机器本身硬件Crash无法恢复造成核心数据库无法恢复的窘境。而且在B机器可以将数据异机恢复到任意时间点。

注意:因为要求备份完毕后直接脚本命令自动传输到异机备份目录,所以需要预先配置好到B机器的SSH无密码登录。

1.定时任务:

  • 每周日1点数据库全备
  • 每天8点备份归档
[ora11204@OEL-ASM scripts]$ crontab -l
0 1 * * 0 /u01/orabak/scripts/backup.sh /u01/orabak
0 8 * * * /u01/orabak/scripts/backuparch.sh /u01/orabak

2.脚本内容:

  • 全备脚本:vi backup.sh
#!/bin/bash
#ENV
export ORACLE_SID=orcl
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH

#backup RMAN 
if [ ! -d "$1" ]; then
        echo "You have input no dir for \$1"
        exit 1
fi

echo "=================================================================================" >>${1}/backup_full.log
echo "Begin backup at : `date`" >> ${1}/backup_full.log
rman target / <<EOF >> ${1}/backup_full.log
run {
CONFIGURE RETENTION POLICY TO REDUNDANCY = 2;
CONFIGURE DEVICE TYPE DISK PARALLELISM 6;
CONFIGURE DEFAULT DEVICE TYPE TO DISK;
backup as compressed backupset database format '${1}/FULLBAK_%d_%T_%s_%p.DBFILE'; 
backup current controlfile format '${1}/%d.%s.%p.%T.CTL';
backup spfile format '${1}/%d.%s.%p.%T.SPFILE';
crosscheck backup;
crosscheck copy;
sql "alter system archive log current";
backup as COMPRESSED backupset archivelog all not backed up format '${1}/%d.%s.%p.%T.ARC';
crosscheck archivelog all;
delete noprompt archivelog all completed before 'sysdate-10';
delete noprompt expired backup;
delete noprompt obsolete;
}
exit
EOF
echo "End backup at : `date`" >>${1}/backup_full.log

echo "Begin scp at : `date`" >>${1}/backup_full.log
scp ${1}/*`date +%Y%m%d`* oracle@192.168.1.61:/tmp/
echo "End scp at : `date`" >>${1}/backup_full.log
echo "=================================================================================" >>${1}/backup_full.log

exit 0
  • 归档备份脚本:vi backuparch.sh
    这里只是去掉全备的命令,其他保留。
#!/bin/bash
#ENV
export ORACLE_SID=orcl
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH

#backup RMAN 
if [ ! -d "$1" ]; then
        echo "You have input no dir for \$1"
        exit 1
fi

echo "=================================================================================" >>${1}/backup_arch.log
echo "Begin backup at : `date`" >> ${1}/backup_arch.log
rman target / <<EOF >> ${1}/backup_arch.log
run {
CONFIGURE RETENTION POLICY TO REDUNDANCY = 2;
CONFIGURE DEVICE TYPE DISK PARALLELISM 6;
CONFIGURE DEFAULT DEVICE TYPE TO DISK;
backup current controlfile format '${1}/%d.%s.%p.%T.CTL';
backup spfile format '${1}/%d.%s.%p.%T.SPFILE';
crosscheck backup;
crosscheck copy;
sql "alter system archive log current";
backup as COMPRESSED backupset archivelog all not backed up format '${1}/%d.%s.%p.%T.ARC';
crosscheck archivelog all;
delete noprompt archivelog all completed before 'sysdate-10';
delete noprompt expired backup;
delete noprompt obsolete;
}
exit
EOF
echo "End backup at : `date`" >>${1}/backup_arch.log

echo "Begin scp at : `date`" >>${1}/backup_arch.log
scp ${1}/*`date +%Y%m%d`*.{CTL,SPFILE,ARC} oracle@192.168.1.61:/tmp/
echo "End scp at : `date`" >>${1}/backup_arch.log
echo "=================================================================================" >>${1}/backup_arch.log

exit 0

注意:这里的通配只是满足去掉全备的备份传输,对于归档日志,控制文件和参数文件的备份并没有做进一步区分(如果你的归档文件很大,还需要进一步设计细分)

3.异机恢复:

异机恢复核心步骤:
3.1 参数文件:

$ strings ORCL.24.1.20180619.SPFILE

[oracle@jystdrac1 orcl]$ pwd
/u01/oradata/orcl
[oracle@jystdrac1 orcl]$ vi pfile.ora
*.audit_file_dest='/u01/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/oradata/orcl/control01.ctl','/u01/oradata/orcl/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='orcl'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
*.log_archive_dest_1='LOCATION=/u01/arch'
*.memory_target=1234173952
*.open_cursors=300
*.processes=150
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'


SQL> startup nomount pfile='/u01/oradata/orcl/pfile.ora';
ORACLE instance started.

3.2 控制文件:

RMAN> restore controlfile from '/tmp/ORCL.23.1.20180619.CTL';
RMAN> alter database mount;
RMAN> catalog start with '/tmp/';
RMAN> crosscheck backup;

3.3 转储文件并恢复到指定时间点:

RMAN> restore database;
RMAN> sql "alter session set nls_date_format=''yyyy-mm-dd hh24:mi:ss''";
RMAN> recover database until time '2018-06-19 17:50:00';

--先只读打开确认数据是否符合要求
SQL> alter database open read only; 

--确认没问题后重新以resetlogs方式打开
SQL> create spfile from pfile='/u01/oradata/orcl/pfile.ora';
SQL> startup mount
SQL> alter database open resetlogs; 

至此,RMAN备份策略与异机恢复实例演示完成。