[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,相当于建立了一个数据文件拷贝。