[20160720]rman set newname for datafile

lfree發表於2016-07-20

[20160720]rman set newname for datafile本質.txt

--前幾天在恢復歷史資料庫,因為生產系統資料庫是asm,恢復必須要轉成檔案型別。
--一般指令碼執行如下:

run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
set until time '2015-03-01 18:00:00';
set newname for datafile 1   to '/u01/app/oracle/oradata/dbcn1/datafile/system.308.862160493';
....
set newname for datafile 43  to '/u01/app/oracle/oradata/dbcn1/datafile/portal_emrca01.dbf';
restore database;
switch datafile all;
recover database;
release channel c1;
release channel c2;
release channel c3;
}

--自己也在測試環境做一些探究,看看一些細節:

1.環境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

CREATE TABLESPACE TEA DATAFILE
  '/mnt/ramdisk/book/tea01.dbf' SIZE 5M AUTOEXTEND ON NEXT 1M MAXSIZE 1G
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT AUTO
FLASHBACK ON;

RMAN> list archivelog all ;
specification does not match any archived log in the repository

RMAN> list backupset ;
specification does not match any backup in the repository
--//清除archive log資訊以及備份集。

2.建立資料檔案7備份:
--//注:tea表空間的資料檔案僅僅1個,file#=7.

RMAN> backup datafile 7 format '/home/oracle/backup/datafile_7_%u.bak';

Starting backup at 2016-07-20 11:26:36
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00007 name=/mnt/ramdisk/book/tea01.dbf
channel ORA_DISK_1: starting piece 1 at 2016-07-20 11:26:36
channel ORA_DISK_1: finished piece 1 at 2016-07-20 11:26:37
piece handle=/home/oracle/backup/datafile_7_64rb5r3c.bak tag=TAG20160720T112636 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 2016-07-20 11:26:37

3.測試:
--僅僅恢復資料檔案7看看,建立指令碼如下:

run
{
set newname for datafile 7 to '/home/oracle/backup/tea01.dbf';
restore datafile 7;
}

executing command: SET NEWNAME
Starting restore at 2016-07-20 11:30:05
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=123 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=134 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=145 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 00007 to /home/oracle/backup/tea01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/backup/datafile_7_64rb5r3c.bak
channel ORA_DISK_1: piece handle=/home/oracle/backup/datafile_7_64rb5r3c.bak tag=TAG20160720T112636
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 2016-07-20 11:30:08
RMAN> **end-of-file**

RMAN> list copy of datafile 7;

List of Datafile Copies
=======================

Key     File S Completion Time     Ckp SCN    Ckp Time
------- ---- - ------------------- ---------- -------------------
20      7    A 2016-07-20 11:30:07 13244560062 2016-07-20 11:26:36
        Name: /home/oracle/backup/tea01.dbf

RMAN> list archivelog all ;
specification does not match any archived log in the repository

RMAN> list copy of datafile 7;
List of Datafile Copies
=======================

Key     File S Completion Time     Ckp SCN    Ckp Time
------- ---- - ------------------- ---------- -------------------
20      7    A 2016-07-20 11:30:07 13244560062 2016-07-20 11:26:36
        Name: /home/oracle/backup/tea01.dbf

--//從這裡可以看出set之後,加入restore相當於建立資料檔案的copy,並且這些資訊記錄在控制檔案中。
--//而指令碼中執行switch datafile all;相當於切換到複製的資料檔案作為資料檔案。
--//如果單獨切換資料檔案7可以寫成:
switch datafile number|name to copy;
switch datafile number|name ;

--//注:第2種語法僅僅執行在run內。第1種可以rman的提示符下使用,測試看看:

RMAN> switch datafile 7 to copy;

RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of switch to copy command at 07/20/2016 11:48:46
RMAN-06572: database is open and datafile 7 is not offline

--//注:資料檔案7必須offline.
RMAN> sql 'alter database datafile 7 offline';
sql statement: alter database datafile 7 offline

RMAN> switch datafile 7 to copy;
datafile 7 switched to datafile copy "/home/oracle/backup/tea01.dbf"

RMAN> list copy of datafile 7;

List of Datafile Copies
=======================

Key     File S Completion Time     Ckp SCN    Ckp Time
------- ---- - ------------------- ---------- -------------------
21      7    A 2016-07-20 11:49:35 13244560062 2016-07-20 11:26:36
        Name: /mnt/ramdisk/book/tea01.dbf

--這樣原來的資料檔案變成了資料檔案copy。再交換回來。

RMAN> switch datafile 7 to copy;
datafile 7 switched to datafile copy "/mnt/ramdisk/book/tea01.dbf"


RMAN> sql 'alter database datafile 7 online';
sql statement: alter database datafile 7 online
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03009: failure of sql command on default channel at 07/20/2016 11:51:01
RMAN-11003: failure during parse/execution of SQL statement: alter database datafile 7 online
ORA-01113: file 7 needs media recovery
ORA-01110: data file 7: '/mnt/ramdisk/book/tea01.dbf'
--//說明offline沒有寫檢查點,online要先恢復。

RMAN> recover datafile 7;
Starting recover at 2016-07-20 11:51:50
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
starting media recovery
media recovery complete, elapsed time: 00:00:00
Finished recover at 2016-07-20 11:51:50

RMAN> sql 'alter database datafile 7 online';
sql statement: alter database datafile 7 online

--總結:
--從以上測試可以發現set newname for datafile以後在restore,相當於建立了一個資料檔案複製。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2122270/,如需轉載,請註明出處,否則將追究法律責任。

相關文章