rman备份恢复命令之switch
一 switch 命令
1 switch命令用途
更新数据文件名为rman下镜像拷贝时指定的数据文件名
更新数据文件名为 set newname 命令指定的名字,
rman备份恢复命令之switch
。2 switch 命令使用前提条件
rman 必须连接到目标数据库
当switch tablespaces、datafiles、tempfiles时,这些文件必须离线
当switch 整个数据库时,数据库不能open
3 注意事项
switch....to copy 命令用于rman命令提示符下
switch没有to copy的命令用于run语句块中
4 语法
第一组switch dtabase to copy;switch datafile number|name to copy;switch tablespace name to copy;第二组switch datafile all;switch datafile number|name;switch tempfile all;二 举例如下:例一:用switch datafile number to copy 更新数据文件位置和名字RMAN> backup as copy datafile 8 format '/oracle/CRM/test.dbf';Starting backup at 2013-02-22 10:06:21using channel ORA_DISK_1using channel ORA_DISK_2channel ORA_DISK_1: starting datafile copyinput datafile file number=00008 name=/backup/test.dbfoutput file name=/oracle/CRM/test.dbf tag=TAG20130222T100621 RECID=14 STAMP=808049181channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01Finished backup at 2013-02-22 10:06:22Starting Control File and SPFILE Autobackup at 2013-02-22 10:06:22piece handle=/backup/c-3599153036-20130222-01 comment=NONEFinished Control File and SPFILE Autobackup at 2013-02-22 10:06:25RMAN> sql 'alter tablespace test offline';sql statement: alter tablespace test offlineRMAN> switch datafile 8 to copy;datafile 8 switched to datafile copy "/oracle/CRM/test.dbf"RMAN> report schema;Report of database schema for database with db_unique_name CRMList of Permanent Datafiles===========================File Size(MB) Tablespace RB segs Datafile Name---- -------- -------------------- ------- ------------------------1 750 SYSTEM *** /oracle/CRM/system01.dbf2 540 SYSAUX *** /oracle/CRM/sysaux01.dbf3 100 UNDOTBS3 *** /oracle/CRM/undotbs3.dbf4 1742 USERS *** /backup/users01.dbf5 500 POS *** /oracle/CRM/pos.dbf6 100 ERP *** /oracle/CRM/erp.dbf7 5 USER01 *** /oracle/CRM/user01.dbf8 0 TEST *** /oracle/CRM/test.dbfList of Temporary Files=======================File Size(MB) Tablespace Maxsize(MB) Tempfile Name---- -------- -------------------- ----------- --------------------1 218 TEMP 32767 /oracle/CRM/temp01.dbf2 3072 MYNEWTEMP 40960 /oracle/CRM/newtemp.dbf例二 用switch tablespace name to copy更新表空间所有数据文件位置和名字RMAN> backup as copy tablespace pos format '/oracle/%N%f.dbf';(%N为表空间名、%f为数据文件绝对文件号)Starting backup at 2013-02-22 10:55:49using channel ORA_DISK_1using channel ORA_DISK_2channel ORA_DISK_1: starting datafile copyinput datafile file number=00005 name=/oracle/CRM/pos.dbfchannel ORA_DISK_2: starting datafile copyinput datafile file number=00009 name=/oracle/CRM/pos2.dbfoutput file name=/oracle/POS9.dbf tag=TAG20130222T105549 RECID=18 STAMP=808052153channel ORA_DISK_2: datafile copy complete, elapsed time: 00:00:07output file name=/oracle/POS5.dbf tag=TAG20130222T105549 RECID=19 STAMP=808052179channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:36Finished backup at 2013-02-22 10:56:25Starting Control File and SPFILE Autobackup at 2013-02-22 10:56:25piece handle=/backup/c-3599153036-20130222-03 comment=NONEFinished Control File and SPFILE Autobackup at 2013-02-22 10:56:28RMAN> sql 'alter tablespace pos offline';sql statement: alter tablespace pos offlineRMAN> switch tablespace pos to copy;datafile 5 switched to datafile copy "/oracle/POS5.dbf"datafile 9 switched to datafile copy "/oracle/POS9.dbf"RMAN> report schema;Report of database schema for database with db_unique_name CRMList of Permanent Datafiles===========================File Size(MB) Tablespace RB segs Datafile Name---- -------- -------------------- ------- ------------------------1 750 SYSTEM *** /oracle/CRM/system01.dbf2 540 SYSAUX *** /oracle/CRM/sysaux01.dbf3 100 UNDOTBS3 *** /oracle/CRM/undotbs3.dbf4 1742 USERS *** /backup/users01.dbf5 0 POS *** /oracle/POS5.dbf6 100 ERP *** /oracle/CRM/erp.dbf7 5 USER01 *** /oracle/CRM/user01.dbf8 0 TEST *** /oracle/CRM/test.dbf9 0 POS *** /oracle/POS9.dbfList of Temporary Files=======================File Size(MB) Tablespace Maxsize(MB) Tempfile Name---- -------- -------------------- ----------- --------------------1 218 TEMP 32767 /oracle/CRM/temp01.dbf2 3072 MYNEWTEMP 40960 /oracle/CRM/newtemp.dbf例三 用switch dtabase to copy更新数据库所有数据文件位置和名字RMAN> backup as copy database format '/oracle/CRM/test/%N%f.dbf';Starting backup at 2013-02-22 11:26:52allocated channel: ORA_DISK_1channel ORA_DISK_1: SID=129 device type=DISKallocated channel: ORA_DISK_2channel ORA_DISK_2: SID=192 device type=DISKchannel ORA_DISK_1: starting datafile copyinput datafile file number=00004 name=/backup/users01.dbfchannel ORA_DISK_2: starting datafile copyinput datafile file number=00001 name=/oracle/CRM/system01.dbfoutput file name=/oracle/CRM/test/SYSTEM1.dbf tag=TAG20130222T112653 RECID=52 STAMP=808054107channel ORA_DISK_2: datafile copy complete, elapsed time: 00:01:41channel ORA_DISK_2: starting datafile copyinput datafile file number=00002 name=/oracle/CRM/sysaux01.dbfoutput file name=/oracle/CRM/test/USERS4.dbf tag=TAG20130222T112653 RECID=53 STAMP=808054182channel ORA_DISK_1: datafile copy complete, elapsed time: 00:02:54channel ORA_DISK_1: starting datafile copyinput datafile file number=00005 name=/oracle/POS5.dbfoutput file name=/oracle/CRM/test/SYSAUX2.dbf tag=TAG20130222T112653 RECID=54 STAMP=808054193channel ORA_DISK_2: datafile copy complete, elapsed time: 00:01:11channel ORA_DISK_2: starting datafile copyinput datafile file number=00003 name=/oracle/CRM/undotbs3.dbfoutput file name=/oracle/CRM/test/UNDOTBS33.dbf tag=TAG20130222T112653 RECID=55 STAMP=808054203channel ORA_DISK_2: datafile copy complete, elapsed time: 00:00:16channel ORA_DISK_2: starting datafile copyinput datafile file number=00006 name=/oracle/CRM/erp.dbfoutput file name=/oracle/CRM/test/ERP6.dbf tag=TAG20130222T112653 RECID=56 STAMP=808054230channel ORA_DISK_2: datafile copy complete, elapsed time: 00:00:25channel ORA_DISK_2: starting datafile copyinput datafile file number=00009 name=/oracle/POS9.dbfoutput file name=/oracle/CRM/test/POS5.dbf tag=TAG20130222T112653 RECID=57 STAMP=808054247channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:58channel ORA_DISK_1: starting datafile copyinput datafile file number=00007 name=/oracle/CRM/user01.dbfoutput file name=/oracle/CRM/test/POS9.dbf tag=TAG20130222T112653 RECID=58 STAMP=808054247channel ORA_DISK_2: datafile copy complete, elapsed time: 00:00:08channel ORA_DISK_2: starting datafile copyinput datafile file number=00008 name=/oracle/CRM/test.dbfoutput file name=/oracle/CRM/test/USER017.dbf tag=TAG20130222T112653 RECID=59 STAMP=808054250channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:00output file name=/oracle/CRM/test/TEST8.dbf tag=TAG20130222T112653 RECID=60 STAMP=808054250channel ORA_DISK_2: datafile copy complete, elapsed time: 00:00:01Finished backup at 2013-02-22 11:30:51Starting Control File and SPFILE Autobackup at 2013-02-22 11:30:51piece handle=/backup/c-3599153036-20130222-06 comment=NONEFinished Control File and SPFILE Autobackup at 2013-02-22 11:30:59RMAN> switch database to copy;datafile 1 switched to datafile copy "/oracle/CRM/test/SYSTEM1.dbf"datafile 2 switched to datafile copy "/oracle/CRM/test/SYSAUX2.dbf"datafile 3 switched to datafile copy "/oracle/CRM/test/UNDOTBS33.dbf"datafile 4 switched to datafile copy "/oracle/CRM/test/USERS4.dbf"datafile 5 switched to datafile copy "/oracle/CRM/test/POS5.dbf"datafile 6 switched to datafile copy "/oracle/CRM/test/ERP6.dbf"datafile 7 switched to datafile copy "/oracle/CRM/test/USER017.dbf"datafile 8 switched to datafile copy "/oracle/CRM/test/TEST8.dbf"datafile 9 switched to datafile copy "/oracle/CRM/test/POS9.dbf"RMAN> sql 'alter database open';sql statement: alter database openSQL> select file#,name,status from v$datafile;FILE# NAME STATUS---------- ---------------------------------------- -------1 /oracle/CRM/test/SYSTEM1.dbf SYSTEM2 /oracle/CRM/test/SYSAUX2.dbf ONLINE3 /oracle/CRM/test/UNDOTBS33.dbf ONLINE4 /oracle/CRM/test/USERS4.dbf ONLINE5 /oracle/CRM/test/POS5.dbf ONLINE6 /oracle/CRM/test/ERP6.dbf ONLINE7 /oracle/CRM/test/USER017.dbf ONLINE8 /oracle/CRM/test/TEST8.dbf ONLINE9 /oracle/CRM/test/POS9.dbf ONLINE例四 用switch tempfile all更新所有临时数据文件位置和名字SQL> select file#,name,status from v$tempfile;FILE# NAME STATUS---------- ---------------------------------------- -------1 /oracle/CRM/temp01.dbf ONLINE2 /oracle/CRM/newtemp.dbf ONLINE语句如下:startup force mountrun{set newname for tempfile 1 to '/oracle/CRM/test/temp01.dbf';set newname for tempfile 2 to '/oracle/CRM/test/temp02.dbf';switch tempfile all;alter database open;}执行过程如下:RMAN> startup force mountrun{set newname for tempfile 1 to '/oracle/CRM/test/temp01.dbf';set newname for tempfile 2 to '/oracle/CRM/test/temp02.dbf';switch tempfile all;alter database open;}Oracle instance starteddatabase mountedTotal System Global Area 1252663296 bytesFixed Size 2226072 bytesVariable Size 1006635112 bytesDatabase Buffers 234881024 bytesRedo Buffers 8921088 bytesRMAN> 2> 3> 4> 5> 6>executing command: SET NEWNAMEexecuting command: SET NEWNAMErenamed tempfile 1 to /oracle/CRM/test/temp01.dbf in control filerenamed tempfile 2 to /oracle/CRM/test/temp02.dbf in control filedatabase openedRMAN> report schema;Report of database schema for database with db_unique_name CRMList of Permanent Datafiles===========================File Size(MB) Tablespace RB segs Datafile Name---- -------- -------------------- ------- ------------------------1 750 SYSTEM *** /oracle/CRM/test/SYSTEM1.dbf2 540 SYSAUX *** /oracle/CRM/test/SYSAUX2.dbf3 100 UNDOTBS3 *** /oracle/CRM/test/UNDOTBS33.dbf4 1742 USERS *** /oracle/CRM/test/USERS4.dbf5 500 POS *** /oracle/CRM/test/POS5.dbf6 100 ERP *** /oracle/CRM/test/ERP6.dbf7 5 USER01 *** /oracle/CRM/test/USER017.dbf8 1 TEST *** /oracle/CRM/test/TEST8.dbf9 10 POS *** /oracle/CRM/test/POS9.dbfList of Temporary Files=======================File Size(MB) Tablespace Maxsize(MB) Tempfile Name---- -------- -------------------- ----------- --------------------1 218 TEMP 32767 /oracle/CRM/test/temp01.dbf2 3072 MYNEWTEMP 40960 /oracle/CRM/test/temp02.dbf例五 用switch datafile all更新表空间所有数据位置和名字语句:run{sql 'alter tablespace pos offline immediate';set newname for datafile '/oracle/CRM/test/POS5.dbf' to '/oracle/CRM/pos1.dbf';set newname for datafile '/oracle/CRM/test/POS9.dbf' to '/oracle/CRM/pos2.dbf';restore tablespace pos;switch datafile all;recover tablespace pos;sql 'alter tablespace pos online';}执行过程如下:RMAN> run{2> sql 'alter tablespace pos offline immediate';3> set newname for datafile '/oracle/CRM/test/POS5.dbf' to '/oracle/CRM/pos1.dbf';4> set newname for datafile '/oracle/CRM/test/POS9.dbf' to '/oracle/CRM/pos2.dbf';5> restore tablespace pos;6> switch datafile all;7> recover tablespace pos;8> sql 'alter tablespace pos online';9> }sql statement: alter tablespace pos offline immediateexecuting command: SET NEWNAMEexecuting command: SET NEWNAMEStarting restore at 2013-02-22 15:18:37using channel ORA_DISK_1using channel ORA_DISK_2channel ORA_DISK_1: starting datafile backup set restorechannel ORA_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_DISK_1: restoring datafile 00005 to /oracle/CRM/pos1.dbfchannel ORA_DISK_1: restoring datafile 00009 to /oracle/CRM/pos2.dbfchannel ORA_DISK_1: reading from backup piece /backup/20130222_hho2k7jn_1_1channel ORA_DISK_1: piece handle=/backup/20130222_hho2k7jn_1_1 tag=TAG20130222T145756channel ORA_DISK_1: restored backup piece 1channel ORA_DISK_1: restore complete, elapsed time: 00:00:25Finished restore at 2013-02-22 15:19:03datafile 5 switched to datafile copyinput datafile copy RECID=72 STAMP=808067944 file name=/oracle/CRM/pos1.dbfdatafile 9 switched to datafile copyinput datafile copy RECID=73 STAMP=808067944 file name=/oracle/CRM/pos2.dbfStarting recover at 2013-02-22 15:19:04using channel ORA_DISK_1using channel ORA_DISK_2starting media recoverymedia recovery complete, elapsed time: 00:00:00Finished recover at 2013-02-22 15:19:05sql statement: alter tablespace pos onlineRMAN> report schema;Report of database schema for database with db_unique_name CRMList of Permanent Datafiles===========================File Size(MB) Tablespace RB segs Datafile Name---- -------- -------------------- ------- ------------------------1 750 SYSTEM *** /oracle/CRM/test/SYSTEM1.dbf2 540 SYSAUX *** /oracle/CRM/test/SYSAUX2.dbf3 100 UNDOTBS3 *** /oracle/CRM/test/UNDOTBS33.dbf4 1742 USERS *** /oracle/CRM/test/USERS4.dbf5 500 POS *** /oracle/CRM/pos1.dbf6 100 ERP *** /oracle/CRM/test/ERP6.dbf7 5 USER01 *** /oracle/CRM/test/USER017.dbf8 1 TEST *** /oracle/CRM/test/TEST8.dbf9 10 POS *** /oracle/CRM/pos2.dbf