RMAN备份异机恢复并创建新DBID
测试平台信息:
Oracle:10gR2
操作系统:Redhat 4.5
Target DB:ORCL
几点说明:
(1)RMAN 异机恢复的时候,db_name必须相同。 如果说要想改成其他的实例名,可以在恢复成功后,用nid命令修改。 实例名的信息会记录到控制文件里,所以如果在恢复的时候,如果实例名不一致,恢复的时候会报错。
(2)如果恢复的路径和源库不一致,就需要在restore时用set 命令指定新位置。 并且使用switch datafile all将信息更新的到控制文件。
在做duplicate的时候,RMAN 会自动根据pfile中的log_file_name_convert和db_file_name_convert来进行set 的转换。 手工restore时,只能只只能使用set 命令。
(3)异机恢复对相同目录和不同目录都做了说明。
(4)最后测试了NID 修改DBID 和DBNAME.
一. Target 库准备工作:
1. 查询DBID
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
ORCL 1230508166
2. 备份DB
RMAN 的shell 备份脚本:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u03/RMAN/auto_c_%F';
CONFIGURE DEVICE TYPE DISK PARALLELISM 2 BACKUP TYPE TO COMPRESSED BACKUPSET;
run {
crosscheck archivelog all;
delete expired archivelog all;
crosscheck backup;
delete expired backup;
allocate channel c1 type disk;
backup incremental level 0 database format '/u03/RMAN/db0%u_%s_%p.bak' include current controlfile;
sql 'alter system archive log current';
backup filesperset 1 format '/u03/RMAN/arch%u_%s_%p.bak'
backup spfile tag='spfile' format='/u03/RMAN/ORCL_spfile_%U_%T';
archivelog all delete input;
release channel c1;
}
exit;
~
备份出来的文件如下:
[oracle@rhel133 RMAN]$ ls -lth
total 5.8G
-rw-r----- 1 oracle oinstall 6.9M Apr 9 14:30 auto_c_c-1230508166-20130409-04
-rw-r----- 1 oracle oinstall 96K Apr 9 14:30 ORCL_spfile_0so6lccp_1_1_20130409
-rw-r----- 1 oracle oinstall 6.9M Apr 9 14:27 auto_c_c-1230508166-20130409-03
-rw-r----- 1 oracle oinstall 6.8M Apr 9 12:20 auto_c_c-1230508166-20130409-02
-rw-r----- 1 oracle oinstall 6.8M Apr 9 11:32 auto_c_c-1230508166-20130409-00
-rw-r----- 1 oracle oinstall 6.8M Apr 9 09:37 auto_c_c-1230508166-20130409-01
-rw-r----- 1 oracle oinstall 2.5K Apr 9 09:37 arch0io6kr7a_18_1.bak
-rw-r----- 1 oracle oinstall 2.5K Apr 9 09:37 arch0ho6kr78_17_1.bak
-rw-r----- 1 oracle oinstall 4.4M Apr 9 09:37 arch0go6kr75_16_1.bak
-rw-r----- 1 oracle oinstall 22M Apr 9 09:37 arch0fo6kr6t_15_1.bak
-rw-r----- 1 oracle oinstall 135M Apr 9 09:37 arch0eo6kr63_14_1.bak
-rw-r----- 1 oracle oinstall 161M Apr 9 09:37 arch0do6kr59_13_1.bak
-rw-r----- 1 oracle oinstall 1.1M Apr 9 09:36 db00bo6kr52_11_1.bak
-rw-r----- 1 oracle oinstall 5.4G Apr 9 09:36 db00ao6kq0u_10_1.bak
检查数据文件所在位置,在Auxiliary库需将所有的数据文件从/u02改变到/u01下:
SQL> select file_id,file_name from dba_data_files order by file_id;
FILE_ID FILE_NAME
---------- --------------------------------------------------
1 /u02/oradata/orcl/system01.dbf
2 /u02/oradata/orcl/undotbs01.dbf
3 /u02/oradata/orcl/sysaux01.dbf
4 /u02/oradata/orcl/DEALSPACE_002.dbf
5 /u02/oradata/orcl/DEALSPACE_001.dbf
6 /u02/oradata/orcl/l5mspace01.dbf
7 /u02/oradata/orcl/RCFSPACE_002.dbf
8 /u02/oradata/orcl/RCFSPACE_001.dbf
9 /u02/oradata/orcl/DTSPACE_001.dbf
9 rows selected.
SQL> select file_id,file_name from dba_temp_files;
FILE_ID FILE_NAME
---------- --------------------------------------------------
1 /u01/oradata/orcl/temp01.dbf
二. Auxiliary库准备工作:
由于我是在同一台机器上做测试,所以将所有target库相关的文件全部清除。
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
[oracle@rhel133 orcl]$ ls
control01.ctl control02.ctl control03.ctl redo01.log redo02.log redo03.log temp01.dbf
[oracle@rhel133 orcl]$ pwd
/u01/oradata/orcl
[oracle@rhel133 orcl]$ rm -rf *
[oracle@rhel133 orcl]$ cd /u02/oradata/orcl/
[oracle@rhel133 orcl]$ ls
DEALSPACE_001.dbf DTSPACE_001.dbf RCFSPACE_001.dbf sysaux01.dbf undotbs01.dbf
DEALSPACE_002.dbf l5mspace01.dbf RCFSPACE_002.dbf system01.dbf
[oracle@rhel133 orcl]$ rm *
[oracle@rhel133 dbs]$ ls
cntrlorcl.dbf hc_orcl.dat initorcl.ora lkDUMMY lkORCL snapcf_orcl.f spfileorcl.ora
[oracle@rhel133 dbs]$ rm *
1. 创建口令文件
[oracle@rhel133 dbs]$ orapwd file=?/dbs/orapworcl password=oracle
2. 创建相关的目录
[oracle@rhel133 u01]$ mkdir oradata
[oracle@rhel133 oradata]$ pwd
/u01/oradata
3. 创建初始化参数
如果要恢复rman备份的spfile,数据库必须先启动到mount状态,所以,得从其它的数据库先找个临时的pfile启动到mount状态,然后再通过rman来恢复rman备份的spfile.
[oracle@rhel133 dbs]$ export ORACLE_SID=orcl
[oracle@rhel133 dbs]$ rman target /
Recovery Manager: Release 10.2.0.4.0 - Production on Tue Apr 9 15:01:01 2013
Copyright (c) 1982, 2007, Oracle. All rights reserved.
connected to target database (not started)
RMAN> startup nomount;
Oracle instance started
Total System Global Area 1224736768 bytes
Fixed Size 1266416 bytes
Variable Size 683674896 bytes
Database Buffers 524288000 bytes
Redo Buffers 15507456 bytes
RMAN> restore spfile to pfile '/u01/app/oracle/product/10201/dbs/initorcl.ora' from '/u03/RMAN/ORCL_spfile_0so6lccp_1_1_20130409';
Starting restore at 09-APR-13
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=541 devtype=DISK
channel ORA_DISK_1: autobackup found: /u03/RMAN/ORCL_spfile_0so6lccp_1_1_20130409
channel ORA_DISK_1: SPFILE restore from autobackup complete
Finished restore at 09-APR-13
4. 将用pfile将Auxiliary库启动到nomout状态
SQL> startup nomount pfile=?/dbs/initorcl.ora
ORACLE instance started.
Total System Global Area 1325400064 bytes
Fixed Size 1267236 bytes
Variable Size 1256196572 bytes
Database Buffers 52428800 bytes
Redo Buffers 15507456 bytes
5. 恢复控制文件
RMAN> restore controlfile from '/u03/RMAN/auto_c_c-1230508166-20130409-04';
Starting restore at 09-APR-13
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=101 devtype=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
output filename=/u01/oradata/orcl/control01.ctl
output filename=/u01/oradata/orcl/control02.ctl
output filename=/u01/oradata/orcl/control03.ctl
Finished restore at 09-APR-13
6. 将DB启动到mout状态
RMAN> alter database mount;
database mounted
released channel: ORA_DISK_1
7. restore 数据库
因为我们的路径不同,所以我们需要使用set 命令转换一下路径。
注意,restore 的时候不会对temp 表空间进行restore。所以等restore 之后,我们需要手工创建temp表空间。
RMAN> run
2> {
3> set newname for datafile 1 to "/u01/oradata/orcl/system01.dbf";
4> set newname for datafile 2 to "/u01/oradata/orcl/undotbs01.dbf";
5> set newname for datafile 3 to "/u01/oradata/orcl/sysaux01.dbf";
6> set newname for datafile 4 to "/u01/oradata/orcl/DEALSPACE_002.dbf";
7> set newname for datafile 5 to "/u01/oradata/orcl/DEALSPACE_001.dbf";
8> set newname for datafile 6 to "/u01/oradata/orcl/l5mspace01.dbf";
9> set newname for datafile 7 to "/u01/oradata/orcl/RCFSPACE_002.dbf";
10> set newname for datafile 8 to "/u01/oradata/orcl/RCFSPACE_001.dbf";
11> set newname for datafile 9 to "/u01/oradata/orcl/DTSPACE_001.dbf";
12> restore database;
13> switch datafile all;
14> }
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting restore at 09-APR-13
Starting implicit crosscheck backup at 09-APR-13
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=101 devtype=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: sid=100 devtype=DISK
Crosschecked 12 objects
Finished implicit crosscheck backup at 09-APR-13
Starting implicit crosscheck copy at 09-APR-13
using channel ORA_DISK_1
using channel ORA_DISK_2
Finished implicit crosscheck copy at 09-APR-13
searching for all files in the recovery area
cataloging files...
no files cataloged
using channel ORA_DISK_1
using channel ORA_DISK_2
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /u01/oradata/orcl/system01.dbf
restoring datafile 00002 to /u01/oradata/orcl/undotbs01.dbf
restoring datafile 00003 to /u01/oradata/orcl/sysaux01.dbf
restoring datafile 00004 to /u01/oradata/orcl/DEALSPACE_002.dbf
restoring datafile 00005 to /u01/oradata/orcl/DEALSPACE_001.dbf
restoring datafile 00006 to /u01/oradata/orcl/l5mspace01.dbf
restoring datafile 00007 to /u01/oradata/orcl/RCFSPACE_002.dbf
restoring datafile 00008 to /u01/oradata/orcl/RCFSPACE_001.dbf
restoring datafile 00009 to /u01/oradata/orcl/DTSPACE_001.dbf
channel ORA_DISK_1: reading from backup piece /u03/RMAN/db00ao6kq0u_10_1.bak
channel ORA_DISK_1: restored backup piece 1
piece handle=/u03/RMAN/db00ao6kq0u_10_1.bak tag=TAG20130409T091718
channel ORA_DISK_1: restore complete, elapsed time: 00:17:57
Finished restore at 09-APR-13
datafile 1 switched to datafile copy
input datafile copy recid=37 stamp=812304874 filename=/u01/oradata/orcl/system01.dbf
datafile 2 switched to datafile copy
input datafile copy recid=38 stamp=812304874 filename=/u01/oradata/orcl/undotbs01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=39 stamp=812304874 filename=/u01/oradata/orcl/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=40 stamp=812304874 filename=/u01/oradata/orcl/DEALSPACE_002.dbf
datafile 5 switched to datafile copy
input datafile copy recid=41 stamp=812304874 filename=/u01/oradata/orcl/DEALSPACE_001.dbf
datafile 6 switched to datafile copy
input datafile copy recid=42 stamp=812304874 filename=/u01/oradata/orcl/l5mspace01.dbf
datafile 7 switched to datafile copy
input datafile copy recid=43 stamp=812304874 filename=/u01/oradata/orcl/RCFSPACE_002.dbf
datafile 8 switched to datafile copy
input datafile copy recid=44 stamp=812304874 filename=/u01/oradata/orcl/RCFSPACE_001.dbf
datafile 9 switched to datafile copy
input datafile copy recid=45 stamp=812304874 filename=/u01/oradata/orcl/DTSPACE_001.dbf
如果不需要更改文件目录的话,
就简单了
RMAN> restore database;
8. recover DB
RMAN> recover database;
Starting recover at 09-APR-13
using channel ORA_DISK_1
using channel ORA_DISK_2
starting media recovery
channel ORA_DISK_1: starting archive log restore to default destination
channel ORA_DISK_2: starting archive log restore to default destination
channel ORA_DISK_1: restoring archive log
archive log thread=1 sequence=5
channel ORA_DISK_1: reading from backup piece /u03/RMAN/arch0fo6kr6t_15_1.bak
channel ORA_DISK_2: restoring archive log
archive log thread=1 sequence=6
channel ORA_DISK_2: reading from backup piece /u03/RMAN/arch0io6kr7a_18_1.bak
channel ORA_DISK_2: restored backup piece 1
piece handle=/u03/RMAN/arch0io6kr7a_18_1.bak tag=TAG20130409T093640
channel ORA_DISK_2: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: restored backup piece 1
piece handle=/u03/RMAN/arch0fo6kr6t_15_1.bak tag=TAG20130409T093640
channel ORA_DISK_1: restore complete, elapsed time: 00:00:04
archive log filename=/u01/app/oracle/flash_recovery_area/ORCL/archivelog/2013_04_09/o1_mf_1_5_8p7mmykf_.arc thread=1 sequence=5
channel default: deleting archive log(s)
archive log filename=/u01/app/oracle/flash_recovery_area/ORCL/archivelog/2013_04_09/o1_mf_1_5_8p7mmykf_.arc recid=17 stamp=812305026
archive log filename=/u01/app/oracle/flash_recovery_area/ORCL/archivelog/2013_04_09/o1_mf_1_6_8p7mmykb_.arc thread=1 sequence=6
unable to find archive log
archive log thread=1 sequence=1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 04/09/2013 16:17:20
RMAN-06054: media recovery requesting unknown log: thread 1 seq 1 lowscn 3524945668
RMAN> list backupset;
其中的这么一个archivelog的备份文件,/u03/RMAN/arch0io6kr7a_18_1.bak的最大SCN是3524945667,系统recover到这个文件后找不到下一次SCN,就会报这个错,这是正常的,如不想出现这个错误的话,则需要用recover database until scn 3524945667 来不完全恢复。
如需要全完恢复,则需将备份之后产生的所有archivelog。
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
14 2.00K DISK 00:00:01 09-APR-13
BP Key: 14 Status: AVAILABLE Compressed: YES Tag: TAG20130409T093640
Piece Name: /u03/RMAN/arch0io6kr7a_18_1.bak
List of Archived Logs in backup set 14
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 6 3524945662 09-APR-13 3524945667 09-APR-13
9. 用open resetlogs打开数据库
RMAN> alter database open resetlogs;
database opened
在open resetlogs 之后,自动在原来默认的路径创建了temp表空间和3组redo文件。
如果恢复目录和原来相同,就不用修改。
如果目录不同,我们就需要把这些文件移到我们现在的 data 目录。
10. NID 修改DBID和DBNAME
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
ORCL 1230508166
几点说明:
(1)在修改DBID期间仍然可能会遇到不可恢复的错误。所以修改之前备份数据库,特使是控制文件。因为nid 会修改控制文件中的信息。
(2)需要将DB 启动到mount 状态才能修改。
SQL> shutdown immediate
SQL> startup mount;
在执行NID 命令之前:一定要关闭所有的session连接。
nid 命令到执行的最后会关闭数据库,如果有session 连接,就会阻止这个操作,修改dbid就会被挂死。如果中断这个操作。修改就会失败,数据库就不能mount。需要恢复。
(1)只改DBID,不改db_name
[oracle@rhel133 orcl]$ nid target=sys/oracle
DBNEWID: Release 10.2.0.4.0 - Production on Tue Apr 9 16:48:51 2013
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to database ORCL (DBID=1230508166)
Connected to server version 10.2.0
Control Files in database:
/u01/oradata/orcl/control01.ctl
/u01/oradata/orcl/control02.ctl
/u01/oradata/orcl/control03.ctl
Change database ID of database ORCL? (Y/[N]) => y
Proceeding with operation
Changing database ID from 1230508166 to 1340095604
Control File /u01/oradata/orcl/control01.ctl - modified
Control File /u01/oradata/orcl/control02.ctl - modified
Control File /u01/oradata/orcl/control03.ctl - modified
Datafile /u01/oradata/orcl/system01.dbf - dbid changed
Datafile /u01/oradata/orcl/undotbs01.dbf - dbid changed
Datafile /u01/oradata/orcl/sysaux01.dbf - dbid changed
Datafile /u01/oradata/orcl/DEALSPACE_002.dbf - dbid changed
Datafile /u01/oradata/orcl/DEALSPACE_001.dbf - dbid changed
Datafile /u01/oradata/orcl/l5mspace01.dbf - dbid changed
Datafile /u01/oradata/orcl/RCFSPACE_002.dbf - dbid changed
Datafile /u01/oradata/orcl/RCFSPACE_001.dbf - dbid changed
Datafile /u01/oradata/orcl/DTSPACE_001.dbf - dbid changed
Datafile /u01/oradata/orcl/temp01.dbf - dbid changed
Control File /u01/oradata/orcl/control01.ctl - dbid changed
Control File /u01/oradata/orcl/control02.ctl - dbid changed
Control File /u01/oradata/orcl/control03.ctl - dbid changed
Instance shut down
Database ID for database ORCL changed to 1340095604.
All previous backups and archived redo logs for this database are unusable.
Database is not aware of previous backups and archived logs in Recovery Area.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database ID.
DBNEWID - Completed succesfully.
SQL> startup
ORACLE instance started.
Total System Global Area 1325400064 bytes
Fixed Size 1267236 bytes
Variable Size 1256196572 bytes
Database Buffers 52428800 bytes
Redo Buffers 15507456 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
SQL> alter database open resetlogs;
Database altered.
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
ORCL 1340095604
已成功更改dbid。
(2)修改DBID和DB_NAME
注意一点,修改DB_NAME 之前,要将spfile创建成pfile,因为修改dbname之后,原来的参数文件就没用了。所以要保证最新的参数。 还有修改DB_NAME的值为最新值。 修改完之后,然后用这个新参数启动DB.
SQL> shutdown immediate
SQL> startup mount;
[oracle@rhel133 dbs]$ nid target=sys/oracle dbname=test64
DBNEWID: Release 10.2.0.4.0 - Production on Tue Apr 9 16:59:05 2013
Copyright (c) 1982, 2007, Oracle. All rights reserved.
Connected to database ORCL (DBID=1340095604)
Connected to server version 10.2.0
Control Files in database:
/u01/oradata/orcl/control01.ctl
/u01/oradata/orcl/control02.ctl
/u01/oradata/orcl/control03.ctl
Change database ID and database name ORCL to TEST64? (Y/[N]) => yes
Proceeding with operation
Changing database ID from 1340095604 to 380502617
Changing database name from ORCL to TEST64
Control File /u01/oradata/orcl/control01.ctl - modified
Control File /u01/oradata/orcl/control02.ctl - modified
Control File /u01/oradata/orcl/control03.ctl - modified
Datafile /u01/oradata/orcl/system01.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/undotbs01.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/sysaux01.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/DEALSPACE_002.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/DEALSPACE_001.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/l5mspace01.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/RCFSPACE_002.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/RCFSPACE_001.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/DTSPACE_001.dbf - dbid changed, wrote new name
Datafile /u01/oradata/orcl/temp01.dbf - dbid changed, wrote new name
Control File /u01/oradata/orcl/control01.ctl - dbid changed, wrote new name
Control File /u01/oradata/orcl/control02.ctl - dbid changed, wrote new name
Control File /u01/oradata/orcl/control03.ctl - dbid changed, wrote new name
Instance shut down
Database name changed to TEST64.
Modify parameter file and generate a new password file before restarting.
Database ID for database TEST64 changed to 380502617.
All previous backups and archived redo logs for this database are unusable.
Database is not aware of previous backups and archived logs in Recovery Area.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.
[oracle@rhel133 dbs]$ vi initorcl.ora 修改dbname为test64
[oracle@rhel133 dbs]$ sqlplus / as sysdba
SQL*Plus: Release 10.2.0.4.0 - Production on Tue Apr 9 17:01:06 2013
Copyright (c) 1982, 2007, Oracle. All Rights Reserved.
Connected to an idle instance.
SQL> startup
ORACLE instance started.
Total System Global Area 1325400064 bytes
Fixed Size 1267236 bytes
Variable Size 1256196572 bytes
Database Buffers 52428800 bytes
Redo Buffers 15507456 bytes
Database mounted.
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
SQL> alter database open resetlogs;
Database altered.
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
TEST64 380502617
另外sid也可以在这个时候进行修改: