資料庫檔案的遷移

kingsql發表於2014-09-11
關於資料庫中的檔案遷移,需要考慮普通資料檔案,redo日誌檔案(還需要考慮是否為current狀態),undo表空間,臨時表空間,system,sysaux表空間。
檔案遷移可以參考下面的虛擬碼:

move_non_system_tablespace ,需要在open狀態
                    {
                    alter tablespace xxxx offline;
                    cp datafiles
                    alter tablespace xxx rename sourcexxxxx   to targetxxxxx;
                    alter tablespace xxxxx online;
                    }

move_system_tablespace(system,sysaux,undo),需要在mount狀態
                    {
                    cp datafiles
                    alter database rename  file  sourcexxxxx   to targetxxxxx;
                    alter database open;
                    }
move_redo_logs
                    {
                   1. for current/active logfiles, need to in mount state
                        cp logfiles
                        alter database rename  file  sourcexxxxx   to targetxxxxx;
                   2. for inactive/unused logfiles,can change online
                        cp logfiles
                        alter database rename  file  sourcexxxxx   to targetxxxxx;
                    }
move_temp_files
                    {
                    cp datafiles
                    alter database rename  file  sourcexxxxx   to targetxxxxx;
                    alter database tempfile xxxx online;
                    }

示例指令碼如下:

##### normal tablespace  ####

alter tablespace TEST_DATA offline;
!cp '/oravl08/oradata/TESTDB/TEST_DATA_1.dbf' '/oravl01/oracle/TEST_DATA_1.dbf';
alter tablespace TEST_DATA rename datafile '/oravl08/oradata/TESTDB/TEST_DATA_1.dbf' to '/oravl01/oracle/TEST_DATA_1.dbf';
alter tablespace TEST_DATA online;

alter tablespace TEST_IX offline;
!cp '/oravl08/oradata/TESTDB/TEST_IX_1.dbf' '/oravl01/oracle/TEST_IX_1.dbf';
alter tablespace TEST_IX rename datafile '/oravl08/oradata/TESTDB/TEST_IX_1.dbf' to '/oravl01/oracle/TEST_IX_1.dbf';
alter tablespace TEST_IX online;


##### temp files ####
alter database tempfile '/oravl08/oradata/TESTDB/TEMP_1.dbf' offline;
!cp '/oravl08/oradata/TESTDB/TEMP_1.dbf' '/oravl01/oracle/TEMP_1.dbf'
alter database  rename file '/oravl08/oradata/TESTDB/TEMP_1.dbf' to '/oravl01/oracle/TEMP_1.dbf';
alter database tempfile '/oravl01/oracle/TEMP_1.dbf' online;

#### system,sysaux,undo ####
!cp '/oravl08/oradata/TESTDB/SYSTEM_1.dbf' '/oravl01/oracle/SYSTEM_1.dbf'
alter database  rename file '/oravl08/oradata/TESTDB/SYSTEM_1.dbf' to '/oravl01/oracle/SYSTEM_1.dbf';

!cp '/oravl08/oradata/TESTDB/SYSAUX_1.dbf' '/oravl01/oracle/SYSAUX_1.dbf'
alter database  rename file '/oravl08/oradata/TESTDB/SYSAUX_1.dbf' to '/oravl01/oracle/SYSAUX_1.dbf';


!cp '/oravl08/oradata/TESTDB/UNDOTBS_1.dbf' '/oravl01/oracle/UNDOTBS_1.dbf'
alter database  rename file '/oravl08/oradata/TESTDB/UNDOTBS_1.dbf' to '/oravl01/oracle/UNDOTBS_1.dbf';

#### logfiles ####
!cp '/oravl03/oradata/TESTDB/redo_g1_m1.dbf' '/oravl01/oracle/redo_g1_m1.dbf'
alter database  rename file '/oravl03/oradata/TESTDB/redo_g1_m1.dbf' to '/oravl01/oracle/redo_g1_m1.dbf';

!cp '/oravl03/oradata/TESTDB/redo_g1_m2.dbf' '/oravl01/oracle/redo_g1_m2.dbf'
alter database  rename file '/oravl03/oradata/TESTDB/redo_g1_m2.dbf' to '/oravl01/oracle/redo_g1_m2.dbf';


最後帶給大家一些福利,關於檔案的遷移,可以參考如下的指令碼。
if [[ -d $1 ]]; then
    echo 'Directory has found :)'
else
    echo "Directory doesn't exists! :("
    exit
fi
#used for 11g
#select regexp_count(file_name,'/') from dba_data_files
#used for 10g,9i..
#select (length(file_name)-length(replace(file_name,'/',null)))/(length('/')) from dba_data_files
#get file_name
# select substr(file_name,instr(file_name,'/',1,regexp_count(file_name,'/'))) from dba_data_files;
sqlplus -s / as sysdba <
set linesize 200
set pages 0
prompt ########## Scripts as below #######
select 'DB is now in '||status||' status'  from gv\$instance;
select
 'alter tablespace '||tablespace_name||' offline; '||chr(10)
||'!cp '||chr(39)||file_name||chr(39)||' ' ||chr(39)||'$1'||substr(file_name,instr(file_name,'/',1,regexp_count(file_name,'/')))||chr(39)||'; '||chr(10)
||'alter tablespace '||tablespace_name||' rename datafile '||chr(39)||file_name||chr(39)||' to '||chr(39)||'$1'||substr(file_name,instr(file_name,'/',1,regexp_count(file_name,'/')))||chr(39)||'; '||chr(10)
|| 'alter tablespace '||tablespace_name||' online; '
 from dba_data_files where tablespace_name not in ('SYSTEM','SYSAUX') and tablespace_name not like 'UNDO%';

prompt ##### temp files ####
select 'alter database tempfile '||chr(39)||name||chr(39)||' offline;'||chr(10)
||'!cp '||chr(39)||name||chr(39)||' ' ||chr(39)||'$1'||substr(name,instr(name,'/',1,regexp_count(name,'/')))||chr(39)||'  '||chr(10) 
||'alter database  rename file '||chr(39)||name||chr(39)||' to '||chr(39)||'$1'||substr(name,instr(name,'/',1,regexp_count(name,'/')))||chr(39)||'; '||chr(10)
|| 'alter database tempfile '||chr(39)||'$1'||substr(name,instr(name,'/',1,regexp_count(name,'/')))||chr(39)||' online; ' 
 from v\$tempfile;

prompt #### system,sysaux,undo ####
select
'!cp '||chr(39)||f.name||chr(39)||' ' ||chr(39)||'$1'||substr(f.name,instr(f.name,'/',1,regexp_count(f.name,'/')))||chr(39)||' '||chr(10)
||'alter database  rename file '||chr(39)||f.name||chr(39)||' to '||chr(39)||'$1'||substr(f.name,instr(f.name,'/',1,regexp_count(f.name,'/')))||chr(39)||'; '
 from v\$datafile f,v\$tablespace t where t.ts#=f.ts# and (t.name  in ('SYSTEM','SYSAUX') or t.name like 'UNDO%'); 

prompt #### logfiles ####
select
'!cp '||chr(39)||member||chr(39)||' ' ||chr(39)||'$1'||substr(member ,instr(member ,'/',1,regexp_count(member ,'/')))||chr(39)||'  '||chr(10) 
||'alter database  rename file '||chr(39)||member ||chr(39)||' to '||chr(39)||'$1'||substr(member ,instr(member ,'/',1,regexp_count(member ,'/')))||chr(39)||'; ' 
 from v\$logfile f,v\$log l where f.group#=l.group# and l.status!='CURRENT';

EOF
exit


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

相關文章