測試平臺信息:
Oracle:11gR2
操作系統:Redhat 5.5
Target DB:dave
幾點說明:
(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
--------- ----------
DAVE 808234286
2. 備份DB
關於RMAN 的shell 備份腳本,參考:
Nocatalog 下的RMAN 增量備份 shell腳本
http://blog.csdn.net/tianlesoftware/archive/2011/01/26/6164931.aspx
在這裏我用0級和1級備份了下DB。
[oracle@qs-dmm-rh1 backup]$ ls
arch_0pm6qt8q_1_1_20110309 dave_lev0_0jm6qt77_1_1_20110309 dave_lev1_0um6qtcq_1_1_20110309
arch_0qm6qt8q_1_1_20110309 dave_lev0_0km6qt77_1_1_20110309 dave_lev1_0vm6qtcq_1_1_20110309
arch_13m6qtda_1_1_20110309 dave_lev0_0lm6qt77_1_1_20110309 dave_lev1_11m6qtd7_1_1_20110309
arch_14m6qtda_1_1_20110309 dave_lev0_0nm6qt7c_1_1_20110309 dave_spfile_16m6qtde_1_1_20110309
ctl_file_15m6qtdc_1_1_20110309 dave_lev1_0tm6qtcq_1_1_20110309
二. Auxiliary庫準備工作:
1. 創建口令文件
[oracle@qs-dmm-rh2 trace]$ orapwd file=?/dbs/orapwdave password=oracle
2. 創建相關的目錄
[oracle@qs-dmm-rh2 u01]$ mkdir oradata
[oracle@qs-dmm-rh2 oradata]$ pwd
/u01/oradata
3. 創建初始化參數
將Target 庫的pfile 文件copy過來。
也可以使用RMAN 從我們Target庫的備份集中恢復,因爲我們之前備份過spfile。不過使用RMAN, DB要先啓動到nomout 狀態。 這個可以用默認的init.ora 來啓動。
[oracle@qs-dmm-rh2 backup]$ export ORACLE_SID=dave
[oracle@qs-dmm-rh2 backup]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Fri Mar 11 15:11:31 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: DAVE (not mounted)
RMAN> restore spfile to pfile '/u01/app/oracle/product/11.2.0/dbhome_1/dbs/initdave2.ora' from '/u01/backup/dave_spfile_16m6qtde_1_1_20110309';
Starting restore at 11-MAR-11
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=19 device type=DISK
channel ORA_DISK_1: restoring spfile from AUTOBACKUP /u01/backup/dave_spfile_16m6qtde_1_1_20110309
channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete
Finished restore at 11-MAR-11
如果修改數據文件保存的位置,那麼要修改控制文件的相關的參數:
*.control_files='/u01/oradata/control01.ctl','/u01/oradata/control02.ctl','/u01/oradata/control03.ctl'
*.db_name='dave'
4. 將用pfile將Auxiliary庫啓動到nomout 狀態
SQL> startup nomount pfile=?/dbs/initdave.ora
5. 恢復控制文件
[oracle@qs-dmm-rh2 dbs]$ export ORACLE_SID=dave
[oracle@qs-dmm-rh2 dbs]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Fri Mar 11 15:25:55 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: DAVE (not mounted)
RMAN> restore controlfile from '/u01/backup/ctl_file_15m6qtdc_1_1_20110309';
Starting restore at 11-MAR-11
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/u01/oradata/control01.ctl
output file name=/u01/oradata/control02.ctl
output file name=/u01/oradata/control03.ctl
Finished restore at 11-MAR-11
restore 的時候需要控制文件,控制文件恢復的位置,是我們在pfile中的control_files參數控制的。
6. 將DB 啓動到mout狀態
RMAN> alter database mount;
database mounted
released channel: ORA_DISK_1
7. restore 數據庫
7.1 恢復目錄不同的情況:
因爲我們的路徑不同,所以我們需要使用set 命令轉換一下路徑。
到Target 庫查詢一下:
SQL> select file_id,file_name from dba_data_files;
FILE_ID FILE_NAME
---------- -------------------------------------------
4 /u01/app/oracle/oradata/dave/users01.dbf
3 /u01/app/oracle/oradata/dave/undotbs01.dbf
2 /u01/app/oracle/oradata/dave/sysaux01.dbf
1 /u01/app/oracle/oradata/dave/system01.dbf
SQL> select file_id,file_name from dba_temp_files;
FILE_ID FILE_NAME
---------- ------------------------------------------
1 /u01/app/oracle/oradata/dave/temp01.dbf
注意,restore 的時候不會對temp 表空間進行restore。所以等restore 之後,我們需要手工創建temp表空間。
RMAN> run
2> {
set newname for datafile 1 to "/u01/oradata/system01.dbf";
3> 4> set newname for datafile 2 to "/u01/oradata/sysaux01.dbf";
5> set newname for datafile 3 to "/u01/oradata/undotbs01.dbf";
6> set newname for datafile 4 to "/u01/oradata/users01.dbf";
7> restore database;
8> switch datafile all;
9> }
對switch datafile all的說明:
--對於nocatalog 模式下,rman備份的信息是保存在控制文件裏的,包括文件的路徑信息。 這裏的switch datafile all的作用,就是更新控制文件裏的信息。
executing command: SET NEWNAME
released channel: ORA_DISK_1
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting restore at 11-MAR-11
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=20 device type=DISK
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00003 to /u01/oradata/undotbs01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev0_0lm6qt77_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev0_0lm6qt77_1_1_20110309 tag=DAVE_LEV0
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00004 to /u01/oradata/users01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev0_0nm6qt7c_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev0_0nm6qt7c_1_1_20110309 tag=DAVE_LEV0
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00002 to /u01/oradata/sysaux01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev0_0km6qt77_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev0_0km6qt77_1_1_20110309 tag=DAVE_LEV0
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00001 to /u01/oradata/system01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev0_0jm6qt77_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev0_0jm6qt77_1_1_20110309 tag=DAVE_LEV0
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:25
Finished restore at 11-MAR-11
datafile 1 switched to datafile copy
input datafile copy RECID=5 STAMP=745522150 file name=/u01/oradata/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=6 STAMP=745522150 file name=/u01/oradata/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=7 STAMP=745522150 file name=/u01/oradata/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=8 STAMP=745522150 file name=/u01/oradata/users01.dbf
7.2 恢復目錄相同
這種情況比較簡單,直接:
RMAN> restore database;
8. recover DB
RMAN> recover database;
Starting recover at 11-MAR-11
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
destination for restore of datafile 00001: /u01/oradata/system01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev1_0tm6qtcq_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev1_0tm6qtcq_1_1_20110309 tag=DAVE_LEV1
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
destination for restore of datafile 00002: /u01/oradata/sysaux01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev1_0um6qtcq_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev1_0um6qtcq_1_1_20110309 tag=DAVE_LEV1
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
destination for restore of datafile 00003: /u01/oradata/undotbs01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev1_0vm6qtcq_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev1_0vm6qtcq_1_1_20110309 tag=DAVE_LEV1
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting incremental datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
destination for restore of datafile 00004: /u01/oradata/users01.dbf
channel ORA_DISK_1: reading from backup piece /u01/backup/dave_lev1_11m6qtd7_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/dave_lev1_11m6qtd7_1_1_20110309 tag=DAVE_LEV1
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
starting media recovery
channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=7
channel ORA_DISK_1: reading from backup piece /u01/backup/arch_13m6qtda_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/arch_13m6qtda_1_1_20110309 tag=ARC_BAK
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_7_745352047.dbf thread=1 sequence=7
channel ORA_DISK_1: starting archived log restore to default destination
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=8
channel ORA_DISK_1: reading from backup piece /u01/backup/arch_14m6qtda_1_1_20110309
channel ORA_DISK_1: piece handle=/u01/backup/arch_14m6qtda_1_1_20110309 tag=ARC_BAK
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch1_8_745352047.dbf thread=1 sequence=8
unable to find archived log
archived log thread=1 sequence=9
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 03/11/2011 17:32:00
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 9 and starting SCN of 823627
RMAN>
之後會報一個錯誤:
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence 9 and starting SCN of 823627
這裏是提醒恢復到一個未知的scn號。在alter database mount之後,通過set until scn或者set until time命令設置恢復到的scn號或時間。就可以避免這個錯誤。
9. 用open resetlogs 打開數據庫
SQL> alter database open resetlogs;
Database altered.
我測試的平臺是11gR2的版本,在open resetlogs 之後,自動在原來默認的路徑創建了temp 表空間和3組redo 文件。
如果恢復目錄和原來相同,就不用修改。
如果目錄不同,我們就需要把這些文件移到我們現在的data目錄。
源目錄:
[oracle@qs-dmm-rh2 dave]$ pwd
/u01/app/oracle/oradata/dave
[oracle@qs-dmm-rh2 dave]$ ls
redo01.log redo02.log redo03.log temp01.dbf
現在的目錄:
[oracle@qs-dmm-rh2 dave]$ cd /u01/oradata/
[oracle@qs-dmm-rh2 oradata]$ ls
control01.ctl control02.ctl control03.ctl sysaux01.dbf system01.dbf undotbs01.dbf users01.dbf
(1)處理online redo log
SQL> select group#,bytes/1024/1024||'M',status from v$log;
GROUP# BYTES/1024/1024||'M' STATUS
---------- ----------------------------------------- ----------------
1 50M INACTIVE
2 50M CURRENT
3 50M UNUSED
SQL> select group#,member from v$logfile;
GROUP# MEMBER
---------- ---------------------------------------------------------------------
3 /u01/app/oracle/oradata/dave/redo03.log
2 /u01/app/oracle/oradata/dave/redo02.log
1 /u01/app/oracle/oradata/dave/redo01.log
oracle 至少有2組redo log。 所以我們可以將已經完成歸檔的redo drop掉, 重新創建。
SQL> alter database drop logfile group 3;
Database altered.
SQL> alter database add logfile group 3 ('/u01/oradata/redo03.log') size 50m;
Database altered.
SQL> alter system switch logfile;
System altered.
SQL> select group#,bytes/1024/1024||'M',status from v$log;
GROUP# BYTES/1024/1024||'M' STATUS
---------- ----------------------------------------- ----------------
1 50M INACTIVE
2 50M ACTIVE
3 50M CURRENT
SQL> alter database drop logfile group 1;
Database altered.
SQL> alter database add logfile group 1 ('/u01/oradata/redo01.log') size 50m;
Database altered.
SQL> alter system switch logfile;
System altered.
SQL> alter database drop logfile group 2;
Database altered.
SQL> alter database add logfile group 2 ('/u01/oradata/redo02.log') size 50m;
Database altered.
SQL> select group#,member from v$logfile;
GROUP# MEMBER
---------- ---------------------------------------------------------------------
3 /u01/oradata/redo03.log
2 /u01/oradata/redo02.log
1 /u01/oradata/redo01.log
(2)處理temp 臨時表空間
SQL> select name from v$tempfile;
NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/dave/temp01.dbf
--表空間offline
SQL> alter database tempfile '/u01/app/oracle/oradata/dave/temp01.dbf' offline;
Database altered.
--在OS 級別移動temp 的數據文件
SQL> !mv /u01/app/oracle/oradata/dave/temp01.dbf /u01/oradata/temp01.dbf
--修改控制文件中temp文件的信息
SQL> alter database rename file '/u01/app/oracle/oradata/dave/temp01.dbf' to '/u01/oradata/temp01.dbf';
Database altered.
--temp 表空間online
SQL> alter database tempfile '/u01/oradata/temp01.dbf' online;
Database altered.
--驗證
SQL> select name from v$tempfile;
NAME
--------------------------------------------------------------------------------
/u01/oradata/temp01.dbf
10. NID 修改DBID和DBNAME
先查看,這個和我們之前的一樣
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
DAVE 808234286
幾點說明:
(1)在修改DBID期間仍然可能會遇到不可恢復的錯誤。所以修改之前備份數據庫,特使是控制文件。因爲nid 會修改控制文件中的信息。
(2)需要將DB 啓動到mount 狀態才能修改。
SQL> shutdown immediate
SQL> startup mount;
在執行NID 命令之前:一定要關閉所有的session連接。
nid 命令到執行的最後會關閉數據庫,如果有session 連接,就會阻止這個操作,修改dbid就會被掛死。如果中斷這個操作。修改就會失敗,數據庫就不能mount。需要恢復。
(1)只改DBID, 不改db_name
[oracle@qs-dmm-rh2 trace]$ nid target=sys/oracle
DBNEWID: Release 11.2.0.1.0 - Production on Fri Mar 11 18:36:33 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to database DAVE (DBID=808234286)
Connected to server version 11.2.0
Control Files in database:
/u01/oradata/control01.ctl
/u01/oradata/control02.ctl
/u01/oradata/control03.ctl
Change database ID of database DAVE? (Y/[N]) => y
Proceeding with operation
Changing database ID from 808234286 to 808418162
Control File /u01/oradata/control01.ctl - modified
Control File /u01/oradata/control02.ctl - modified
Control File /u01/oradata/control03.ctl - modified
Datafile /u01/app/oracle/oradata/dave/system01.db - dbid changed
Datafile /u01/app/oracle/oradata/dave/sysaux01.db - dbid changed
Datafile /u01/app/oracle/oradata/dave/undotbs01.db - dbid changed
Datafile /u01/app/oracle/oradata/dave/users01.db - dbid changed
Datafile /u01/app/oracle/oradata/dave/temp01.db - dbid changed
Control File /u01/oradata/control01.ctl - dbid changed
Control File /u01/oradata/control02.ctl - dbid changed
Control File /u01/oradata/control03.ctl - dbid changed
Instance shut down
Database ID for database DAVE changed to 808418162.
All previous backups and archived redo logs for this database are unusable.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database ID.
DBNEWID - Completed succesfully.
重啓打開數據庫:
[oracle@qs-dmm-rh2 ~]$ export ORACLE_SID=dave
[oracle@qs-dmm-rh2 ~]$ sqlplus / as sysdba;
SQL*Plus: Release 11.2.0.1.0 Production on Fri Mar 11 18:37:57 2011
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to an idle instance.
SQL> startup
ORACLE instance started.
Total System Global Area 196681728 bytes
Fixed Size 1335444 bytes
Variable Size 83890028 bytes
Database Buffers 109051904 bytes
Redo Buffers 2404352 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
--------- ----------
DAVE 808418162
SQL>
(2)修改DBID 和DB_NAME
注意一點,修改DB_NAME 之前,要將spfile 創建成pfile,因爲修改dbname之後,原來的參數文件就沒用了。所以要保證最新的參數。 還有修改DB_NAME的值爲最新值。 修改完之後,然後用這個新參數啓動DB.
[oracle@qs-dmm-rh2 trace]$ nid target=sys/oracle dbname=anqing
DBNEWID: Release 11.2.0.1.0 - Production on Fri Mar 11 18:41:01 2011
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to database DAVE (DBID=808418162)
Connected to server version 11.2.0
Control Files in database:
/u01/oradata/control01.ctl
/u01/oradata/control02.ctl
/u01/oradata/control03.ctl
Change database ID and database name DAVE to DBA? (Y/[N]) => y
Proceeding with operation
Changing database ID from 808418162 to 1204743549
Changing database name from DAVE to DBA
Control File /u01/oradata/control01.ctl - modified
Control File /u01/oradata/control02.ctl - modified
Control File /u01/oradata/control03.ctl - modified
Datafile /u01/app/oracle/oradata/dave/system01.db - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/dave/sysaux01.db - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/dave/undotbs01.db - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/dave/users01.db - dbid changed, wrote new name
Datafile /u01/app/oracle/oradata/dave/temp01.db - dbid changed, wrote new name
Control File /u01/oradata/control01.ctl - dbid changed, wrote new name
Control File /u01/oradata/control02.ctl - dbid changed, wrote new name
Control File /u01/oradata/control03.ctl - dbid changed, wrote new name
Instance shut down
Database name changed to DBA.
Modify parameter file and generate a new password file before restarting.
Database ID for database DBA changed to 1204743549.
All previous backups and archived redo logs for this database are unusable.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.
驗證:
SQL> select name,dbid from v$database;
NAME DBID
--------- ----------
ANQING 676458969