应用场景:服务器A为正常运行的生产环境,需要在服务器B上部署一套相同环境做测试。
数据库环境:RHEL6.4 + Oracle 11.2.0.4.7
一、 服务器A备份数据库
二、 服务器B恢复数据库
一、 服务器A备份数据库
1.1 在线备份(数据库运行在归档模式)
nohup /bin/bash backup_all.sh &
备份完将所有备份介质传到服务器B(如果两台机器是内连网络,可以考虑结合NFS服务从一开始就备份到服务器B上)。
1.2 备份脚本内容
#!/bin/bash
#ScriptName:backup_all.sh
#Usage: backup all files in oracle user environment.
#ex: nohup /bin/bash backup_all.sh > backup.log &
#Author: Alfred Zhao
#Creation: 2015-09-11
#Version: 1.0.0
#Define variable <You may need to change the value of basedir.>
basedir=/u01/orabak
date=`date +%Y%m%d`
#Create pfile
sqlplus / as sysdba <<EOF
create pfile='$basedir/pfile$date.ora' from spfile;
EOF
#RMAN BACKUP
rman target / log=$basedir/backup_all_$date.log <<EOF
run{
allocate channel c1 device type disk;
allocate channel c2 device type disk;
backup database filesperset 4 format '$basedir/full_%d_%T_%s_%p';
sql 'alter system archive log current';
sql 'alter system archive log current';
sql 'alter system archive log current';
sql 'alter system archive log current';
backup archivelog all format '$basedir/arch_%d_%T_%s_%p' delete input;
backup current controlfile format '$basedir/ctl_%d_%T_%s_%p';
release channel c1;
release channel c2;
}
EOF
2. 服务器B恢复数据库
2.1 服务器B安装数据库软件
参考http://www.cnblogs.com/jyzhao/p/3891769.html安装数据库软件及之前的相关配置。
然后创建密码文件:
orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle force=y entries=5
2.2 启动实例到nomount状态(指定pfile文件)
将$basedir/pfile$date.ora
文件复制到$ORACLE_HOME/dbs/init$ORACLE_SID.ora
,根据实际服务器B的情况修改。然后启动实例到nomount状态(指定pfile文件)。
startup nomount pfile='$ORACLE_HOME/dbs/init$ORACLE_SID.ora'
2.3 RMAN恢复控制文件,确定备份集有效性
2.3.1 RMAN恢复控制文件,启动数据库到mount状态,确定备份集有效性
backupControlFile=/u01/orabak/backup/ctl_JINGYU_20150911_46_1
rman target / > crosscheck.log <<EOF
restore controlfile from '$backupControlFile';
alter database mount;
crosscheck backupset;
EOF
2.3.2 如果备份集无效,删除无效备份集,手工注册备份集
一般是两台主机的备份目录不一样,控制文件记录的备份集路径找不到对应的备份集,状态为'EXPIRED',此时应该删除这些过期的备份集,catalog新的备份集,再次确认备份集有效性。
rman target / > catalog.log <<EOF
catalog backuppiece '/u01/orabak/backup/full_JINGYU_20150911_39_1';
catalog backuppiece '/u01/orabak/backup/full_JINGYU_20150911_40_1';
catalog backuppiece '/u01/orabak/backup/full_JINGYU_20150911_41_1';
catalog backuppiece '/u01/orabak/backup/full_JINGYU_20150911_42_1';
catalog backuppiece '/u01/orabak/backup/arch_JINGYU_20150911_43_1';
catalog backuppiece '/u01/orabak/backup/arch_JINGYU_20150911_44_1';
catalog backuppiece '/u01/orabak/backup/arch_JINGYU_20150911_45_1';
crosscheck backupset;
delete noprompt expired backupset;
EOF
2.4 RMAN恢复数据库
2.4.1 restore数据文件
如果数据文件存放目录已经更改,需要重命名还原。
rman target / log=restore.log <<EOF
run {
allocate channel c1 device type disk;
allocate channel c2 device type disk;
set newname for datafile 1 to '/u01/oradata02/o1_mf_system_01.dbf';
set newname for datafile 2 to '/u01/oradata02/o1_mf_sysaux_01.dbf';
set newname for datafile 3 to '/u01/oradata02/o1_mf_undotbs1_01.dbf';
set newname for datafile 4 to '/u01/oradata02/o1_mf_users_01.dbf';
set newname for datafile 5 to '/u01/oradata02/o1_mf_dbs_d_ji_01.dbf';
set newname for datafile 6 to '/u01/oradata02/o1_mf_dbs_d_lu_01.dbf';
set newname for datafile 7 to '/u01/oradata02/o1_mf_dbs_d_xx_01.dbf';
set newname for datafile 8 to '/u01/oradata02/o1_mf_dbs_i_xx_01.dbf';
restore database;
switch datafile all;
release channel c1;
release channel c2;
}
EOF
2.4.2 recover数据文件
2.4.2.1 recover database;
rman target / log=recover.log <<EOF
recover database;
EOF
2.4.2.2 recover database until scn xxxxxxx;
scn=1463689
rman target / log=recover$scn.log <<EOF
recover database until scn $scn;
EOF
这个scn根据上一步的日志信息获取。
2.4.3 修改日志文件路径
new_dest=/usr3/oradata/sysdata/redo_file
sqlplus / as sysdba > logfile.log<<EOF
set linesize 180 pagesize 100
select 'alter database rename file '''||member||''' to ''$new_dest/redoXXX.log'';' from v\$logfile;
EOF
注意:可以用UE列编辑模式快速处理下新的redo文件名字(即redoXXX.log改为对应的实际值)。
2.4.4 修改临时文件路径
new_dest=/usr3/oradata/sysdata
sqlplus / as sysdba > tempfile.log<<EOF
set linesize 180 pagesize 100
select 'alter database rename file '''||name||''' to ''$new_dest/tempXXX.dbf'';' from v\$tempfile;
EOF
注意:同样处理下新的temp文件名字(即tempXXX.dbf改为对应的实际值)。
2.4.5 打开数据库(resetlogs)
sqlplus / as sysdba > dbopen.log<<EOF
alter database open resetlogs;
EOF
启动过程中会自动创建redo文件,temp文件。
2.5 恢复后操作
2.5.1 查看数据库基本信息
查看数据库实例和库的状态,数据文件、临时文件、日志文件、控制文件、参数文件路径信息。
sqlplus / as sysdba > dbstatus.log <<EOF
select instance_name, status from v\$instance;
select dbid, open_mode from v\$database;
select file_name from dba_data_files;
select file_name from dba_temp_files;
select member from v\$logfile;
show parameter control
show parameter pfile
EOF
2.5.2 创建spfile文件,重启数据库
创建spfile文件
sqlplus / as sysdba > createSpfile.log <<EOF
create spfile from pfile;
EOF
关闭数据库
sqlplus / as sysdba > shutdownDB.log <<EOF
shutdown immediate
EOF
启动数据库
sqlplus / as sysdba > startDB.log <<EOF
startup
EOF
2.5.3 监听的配置
修改监听配置文件:$ORACLE_HOME/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = JY-DB02)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
ADR_BASE_LISTENER = /u01/app/oracle
对应服务器主机名相关的3处修改:
1. hostname显示正确主机名
2. /etc/sysconfig/network中HOSTNAME配置
3. /etc/hosts中IP地址和主机名的对应关系
启动监听及查看监听状态:
lsnrctl start
lsnrctl status