标签:
迁移数据库的方法有多种,较为常用的则是使用RMAN来迁移。使用RMAN迁移数据库属于数据库的物理备份与恢复范畴,整个过程中数据库的相关信息是完整地镜像。因此,基于此种方式还原恢复的数据库用于测试会使得与真实的生产环境差异相对较小。本文描述了使用RMAN来还原Oracle 10g数据库的过程。
一、主要步骤
1、备份数据库
2、ftp备份到目的服务器
3、为目标数据库创建项目目录
4、为目标数据库创建pfile或spfile(使用RMAN还原或复制原pfile到目的服务器)
5、还原控制文件
6、还原数据文件
7、OPEN 数据库
其实,这几个步骤比较好理解,整个实质是对数据库体系结构以及数据库整个启动过程的理解。
关于体系结构和启动过程可以参考
Oracle实例和Oracle数据库(Oracle体系结构)
Oracle 数据库实例启动关闭过程
下面对此进行一下描述
a、首先需要为实例的运行环境创建相应的目录,如dump位置,datafile位置,以及archive位置等等
如果是恢复到不同的路径,则后续RMAN时需要使用set newname for datafile 方式更新到控制文件
b、实例需要pfile来启动,因此需要恢复pfile或spfile,然后将数据库切换到nomount状态
c、接下来的一步是通过控制文件将数据库切换到mount状态,因此需要恢复控制文件,然后再mount
d、mount之后就可以对数据库进行还原(restore)操作
e、还原完毕之后是对数据库进行恢复(recovery)操作(restore和recover需要基于控制文件的备份信息或恢复目录)
f、最后是open数据库
二、迁移演示
1、备份原数据库 此处演示的源数据库与目标数据库使用相同的版本为Oracle 10g R2(10.2.0.3),操作系统都为suse 10 +Sp3 备份过程略,有关备份脚本,请参考:linux 下RMAN备份shell脚本 http://blog.csdn.net/robinson_0612/article/details/8029245 2、ftp所有的备份文件到需要恢复的服务器上 打包整个备份集ftp到目标服务器或scp到目标服务器 下面是ftp到目标服务器解压后包含的文件 oracle@2go-devDB01uv:/u02/database/SY5221_RMAN/20121013> ls -hltr total 9.4G -rw-r--r-- 1 oracle oinstall 3.0K 2012-10-16 09:48 initSY5221.ora -rw-r--r-- 1 oracle oinstall 2.7G 2012-10-16 10:02 SY5221_lev0_201210130630_4unnkjvi_1_1 -rw-r--r-- 1 oracle oinstall 3.1G 2012-10-16 10:04 SY5221_lev0_201210130630_4vnnkjvi_1_1 -rw-r--r-- 1 oracle oinstall 938M 2012-10-16 10:07 SY5221_lev0_201210130630_arc_51nnkk2h_1_1 -rw-r--r-- 1 oracle oinstall 935M 2012-10-16 10:08 SY5221_lev0_201210130630_arc_52nnkk2h_1_1 -rw-r--r-- 1 oracle oinstall 930M 2012-10-16 10:10 SY5221_lev0_201210130630_arc_53nnkk31_1_1 -rw-r--r-- 1 oracle oinstall 15M 2012-10-16 10:10 SY5221_lev0_cntl_bak_c-1468911009-20121013-00 -rw-r--r-- 1 oracle oinstall 15M 2012-10-16 10:10 SY5221_lev0_cntl_bak_c-1468911009-20121013-01 -rw-r--r-- 1 oracle oinstall 910M 2012-10-16 10:11 SY5221_lev0_201210130630_arc_54nnkk32_1_1 3、创建所需的目录(使用oracle用户) oracle@2go-devDB01uv:~> more mkdir_sy5221.sh mkdir -p /u02/database/SY5221/flash_recovery_area mkdir -p /u02/database/SY5221 mkdir -p /u02/database/SY5221/archive mkdir -p /u02/database/SY5221/backup mkdir -p /u02/database/SY5221/bdump mkdir -p /u02/database/SY5221/cdump mkdir -p /u02/database/SY5221/udump mkdir -p /u02/database/SY5221/controlf mkdir -p /u02/database/SY5221/oradata mkdir -p /u02/database/SY5221/redolog mkdir -p /u02/database/SY5221/undo mkdir -p /u02/database/SY5221/temp mkdir -p /u02/database/SY5221/ref_data mkdir -p /u02/database/SY5221/BNR mkdir -p /u02/database/SY5221/BNR/full mkdir -p /u02/database/SY5221/BNR/dump mkdir -p /u02/database/SY5221/dbcreatelogs ORACLE_SID=SY5221; export ORACLE_SID oracle@2go-devDB01uv:~> chmod u+x mkdir_sy5221.sh oracle@2go-devDB01uv:~> ./mkdir_sy5221.sh 4、创建密码文件 oracle@2go-devDB01uv:~> cd $ORACLE_HOME/dbs oracle@2go-devDB01uv:~/OraHome10g/dbs> orapwd file=orapwSY5221 password=oracle force=y entries=10 # Author : Robinson # Blog : http://blog.csdn.net/robinson_0612 3、使用pfile启动实例到nomount状态 oracle@2go-devDB01uv:~> cp /u02/database/SY5221_RMAN/20121013/initSY5221.ora $ORACLE_HOME/dbs oracle@2go-devDB01uv:~> export ORACLE_SID=SY5221 oracle@2go-devDB01uv:~> sqlplus / as sysdba SQL> startup nomount; 4、恢复控制文件并切换到mount状态 #再开一个session oracle@2go-devDB01uv:~> export ORACLE_SID=SY5221 oracle@2go-devDB01uv:~> $ORACLE_HOME/bin/rman target / RMAN> restore controlfile from ‘/u02/database/SY5221_RMAN/20121013/SY5221_lev0_cntl.bak_c-1468911009-20121013-01‘; RMAN> alter database mount; 5、指定备份文件所在目录 RMAN> catalog start with ‘/u02/database/SY5221_RMAN/20121013‘; #此命令用于扫描整个目录的备份片或者归档日志文件等 6、还原数据库 RMAN> restore database; 7、恢复数据库 RMAN> recover database; unable to find archive log archive log thread=1 sequence=143388 RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03002: failure of recover command at 10/16/2012 11:32:54 RMAN-06054: media recovery requesting unknown log: thread 1 seq 143388 lowscn 608805162 #由于未复制联机日志文件,此时提示需要seqence 143388,scn 608805162 #在sqlplus提示符下继续介质恢复 SQL> conn / as sysdba Connected. idle> recover database until cancel; ORA-00283: recovery session canceled due to errors ORA-01610: recovery using the BACKUP CONTROLFILE option must be done SQL> recover database using backup controlfile until cancel; ORA-00279: change 608805162 generated at 10/13/2012 06:31:44 needed for thread 1 ORA-00289: suggestion : /u02/database/SY5221/archive/arch_668881377_1_143388.arc ORA-00280: change 608805162 for thread 1 is in sequence #143388 Specify log: {<RET>=suggested | filename | AUTO | CANCEL} cancel #输入cancel,完成介质恢复 Media recovery cancelled. 8、open 数据库 #如果open 不成功,请尝试shutdown 之后再次open,如果仍然不成功使用隐藏参数_allow_resetlogs_corruption打开数据库 SQL> alter database open resetlogs; Database altered. 9、一致性关闭数据库并重启数据库 SQL> shutdown immediate; SQL> startup
恢复到异机不同目录的情形(补充内容@20130906)
步骤基本上与使用的相同目录差不多,有些需要注意的,下面一一列出
1、备份数据及ftp到目的服务器,同之前
2、创建相应的目录
此处的目录有别于原目录,如下面的示例
原目录: /u02/database/SY5221 原数据库名: SY5221 (主机: Linux1, ORACLE_SID=SY5221)
新目录: /u02/database/SY5223 新数据库名:SY5223 (主机: Linux2, ORACLE_SID=SY5223)
3、创建pfile文件并启动实例
从原库提取参数文件到目的服务器,根据需要修改相应的参数,如增加sga大小等
其次修改pfile文件中的相关路径使之指向新的位置,即参数文件中所有含SY5221目录的应修改为SY5223
注意db_name参数不变,为原来的db_name,待恢复完成后使用nid修改
使用pfile文件启动到nomount状态
3、还原控制文件
RMAN> restore controlfile from ‘/u02/database/bak/SY5221cntl.bak‘;
--切换到mount状态,注,db_name务必保持原db_name,否则切换到mount时提示
--ORA-01103: database name ‘SY5221‘ in control file is not ‘SY5223‘
RMAN> alter database mount;
4、使用catalog start with指定备份文件位置
RMAN> catalog start with ‘/u02/database/bak‘;
5、还原及恢复数据库,由于使用了不同的位置,因此我们需要使用set newname 子句,如下面的示例
run{
set newname for datafile 1 to ‘/u02/database/SY5223/oradata/sysSY5223.dbf‘;
set newname for datafile 2 to ‘/u02/database/SY5223/undo/undotbsSY5223.dbf‘;
set newname for datafile 3 to ‘/u02/database/SY5223/oradata/sysauxSY5223.dbf‘;
set newname for datafile 4 to ‘/u02/database/SY5223/undo/undotbsSY52232.dbf‘;
set newname for datafile 5 to ‘/u02/database/SY5223/oradata/SY5223_system_tbl.dbf‘;
set newname for datafile 6 to ‘/u02/database/SY5223/oradata/SY5223_account_tbl.dbf‘;
set newname for datafile 7 to ‘/u02/database/SY5223/oradata/SY5223_stock_tbl.dbf‘;
set newname for datafile 8 to ‘/u02/database/SY5223/oradata/SY5223_stock_l_tbl.dbf‘;
restore database;
switch datafile all;
recover database;}
--Recover过程中会收到如下错误提示
RMAN-03002: failure of recover command at 09/06/2013 22:40:54
RMAN-06054: media recovery requesting unknown log: thread 1 seq 2872 lowscn 18896302
--我们使用until子句来再次recover
RMAN>run{
2> set until sequence 2872;
3> recover database;
4> alter database open resetlogs;
5> }
executing command: SET until clause
Starting recover at 06-SEP-13
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:03
Finished recover at 06-SEP-13
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 09/06/2013 22:52:04
ORA-00344: unable to re-create online log ‘/u02/database/SY5221/redolog/log1aSY5221.log‘
ORA-27040: file create error, unable to create file
Linux-x86_64 Error: 2: No such file or directory
--上面又收到了错误提示,是由于无法创建日志文件,因为路径与原来的数据库路径不一致
RMAN> exit
6、修改日志文件位置及open数据库
$ sqlplus / as sysdba
SQL> select name,open_mode from v$database;
NAME OPEN_MODE
--------- ----------
SY5221 MOUNTED
SQL> set linesize 190
SQL> col member format a60
SQL> select * from v$logfile;
GROUP# STATUS TYPE MEMBER IS_
---------- ------- ------- ------------------------------------------------------------ ---
1 ONLINE /u02/database/SY5221/redolog/log1aSY5221.log NO
1 ONLINE /u02/database/SY5221/redolog/log1bSY5221.log NO
2 ONLINE /u02/database/SY5221/redolog/log2aSY5221.log NO
2 ONLINE /u02/database/SY5221/redolog/log2bSY5221.log NO
SQL> set heading off;
SQL> select ‘alter database rename file ‘‘‘||member||‘‘‘ to ‘‘‘||replace(member,‘SY5221‘,‘SY5223‘)||‘‘‘;‘ from v$logfile;
alter database rename file ‘/u02/database/SY5221/redolog/log1aSY5221.log‘ to ‘/u02/database/SY5223/redolog/log1aSY5223.log‘;
alter database rename file ‘/u02/database/SY5221/redolog/log1bSY5221.log‘ to ‘/u02/database/SY5223/redolog/log1bSY5223.log‘;
alter database rename file ‘/u02/database/SY5221/redolog/log2aSY5221.log‘ to ‘/u02/database/SY5223/redolog/log2aSY5223.log‘;
alter database rename file ‘/u02/database/SY5221/redolog/log2bSY5221.log‘ to ‘/u02/database/SY5223/redolog/log2bSY5223.log‘;
SQL> alter database rename file ‘/u02/database/SY5221/redolog/log5bSY5221.log‘ to ‘/u02/database/SY5223/redolog/log5bSY5223.log‘;
SQL> alter database rename file ‘/u02/database/SY5221/redolog/log6aSY5221.log‘ to ‘/u02/database/SY5223/redolog/log6aSY5223.log‘;
SQL> alter database rename file ‘/u02/database/SY5221/redolog/log6bSY5221.log‘ to ‘/u02/database/SY5223/redolog/log6bSY5223.log‘;
SQL> alter database rename file ‘/u02/database/SY5221/redolog/log7aSY5221.log‘ to ‘/u02/database/SY5223/redolog/log7aSY5223.log‘;
SQL> set heading on;
SQL> alter database open resetlogs;
Database altered.
7、修正相应的临时数据文件
SQL> select * from dba_temp_files;
select * from dba_temp_files
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 201 - see DBWR trace file
ORA-01110: data file 201: ‘/u02/database/SY5221/temp/tempSY5221.dbf‘
SQL> select name from v$tempfile;
NAME
------------------------------------------------------------
/u02/database/SY5221/temp/tempSY5221.dbf
/u02/database/SY5221/temp/SY5221_tempSY5221.dbf
SQL> alter tablespace temp add tempfile ‘/u02/database/SY5223/temp/tempSY5223.dbf‘ size 50m;
SQL> alter tablespace goex_temp add tempfile ‘/u02/database/SY5223/temp/SY5223_tempSY5223.dbf‘ size 50m;
SQL> alter tablespace temp drop tempfile ‘/u02/database/SY5221/temp/tempSY5221.dbf‘;
SQL> alter tablespace goex_temp drop tempfile ‘/u02/database/SY5221/temp/SY5221_tempSY5221.dbf‘;
SQL> alter database tempfile ‘/u02/database/SY5223/temp/tempSY5223.dbf‘ autoextend on;
SQL> alter database tempfile ‘/u02/database/SY5223/temp/SY5223_tempSY5223.dbf‘ autoextend on;
SQL> select count(*) from dba_temp_files;
COUNT(*)
----------
2
SQL> shutdown immediate;
8、使用nid修改数据库名字
$ export ORACLE_SID=SY5223
$ sqlplus / as sysdba
SQL> startup mount pfile=/u02/database/SY5223/initSY5223.ora;
$ nid target=sys/oracle dbname=SY5223 setname=yes #nid命令用法可参考:使用nid命令修改 db name 及 dbid
修改完毕后,修改pfile文件中的db_name参数为SY5223,启动数据库,生成spfile;同时创建相应的密码文件
SQL> create spfile from pfile;
$orapwd file=orapwSY5223 password=oracle force=y entries=10
9、配置listener及tnsnames,根据需要全备数据库
10、后记
上面描述的主要是Oracle 10g,无法连接到目标数据库,也就是不使用duplicate方式的异机恢复方法
转:http://lib.csdn.net/article/oracle/43644
标签:
原文地址:http://www.cnblogs.com/andy6/p/5894937.html