有些时候需要将Oracle的多个数据文件以及日志文件重定位或者迁移到新的分区或新的位置,比如磁盘空间不足,或因为特殊需求。对于这种情形可以采取批量迁移的方式将多个数据文件或者日志文件实现一次性迁移。当然备份恢复也是其中的方式之一。本文主要描述如何使用批量方式来迁移数据文件,日志文件。如需要也可以将整个数据库迁移到新的位置以及重命名数据库。
- 1、环境及需求
- robin@SZDB:~> cat /etc/issue
- Welcome to SUSE Linux Enterprise Server 10 SP3 (x86_64) - Kernel \r (\l).
- robin@SZDB:~> sqlplus -v
- SQL*Plus: Release 10.2.0.3.0 - Production
- 下面的迁移主要是将数据库/u02/database/SYBO2SZ下的所有文件迁移到一个新的目录/u02/database/SY5221BK下面。
- 源路径:数据库SYBO2SZ所有的数据文件,日志文件,控制文件全部位于SYBO2SZ下的相应子目录。
- 新路径:数据库SYBO2SZ所有的数据文件,日志文件,控制文件全部移动到SY5221BK相应的子目录下。
- 2、当前数据库文件位置(来源于数据字典)
- sys@SYBO2SZ> @dba_files_all_2.sql
- Tablespace Name / File Class Filename File Size Auto
- ----------------------------- ------------------------------------------------------- --------------- ----
- GOEX_ACCOUNT_IDX /u02/database/SYBO2SZ/oradata/SYBO2SZ_account_idx.dbf 16,777,216 YES
- GOEX_ACCOUNT_TBL /u02/database/SYBO2SZ/oradata/SYBO2SZ_account_tbl.dbf 25,165,824 YES
- GOEX_ARCHIVE_IDX /u02/database/SYBO2SZ/oradata/SYBO2SZ_archive_idx.dbf 20,971,520 YES
- -- ......... .........................
- SOE /u02/database/SYBO2SZ/oradata/soe.dbf 934,043,648 YES
- SOEINDEX /u02/database/SYBO2SZ/oradata/soeindex.dbf 713,031,680 YES
- SYSAUX /u02/database/SYBO2SZ/oradata/sysauxSYBO2SZ.dbf 325,058,560 YES
- SYSTEM /u02/database/SYBO2SZ/oradata/sysSYBO2SZ.dbf 524,288,000 YES
- TBST /u02/database/SYBO2SZ/oradata/tbst.dbf 10,485,760 YES
- TEMP /u02/database/SYBO2SZ/temp/tempSYBO2SZ.dbf 432,013,312 YES
- UNDOTBS1 /u02/database/SYBO2SZ/undo/undotbsSYBO2SZ.dbf 429,916,160 YES
- UNDOTBS2 /u02/database/SYBO2SZ/undo/undotbsSYBO2SZ2.dbf 314,572,800 YES
- [ ONLINE REDO LOG ] /u02/database/SYBO2SZ/redolog/log3aSYBO2SZ.log 20,971,520
- [ ONLINE REDO LOG ] /u02/database/SYBO2SZ/redolog/log3bSYBO2SZ.log 20,971,520
- [ ONLINE REDO LOG ] /u02/database/SYBO2SZ/redolog/log4aSYBO2SZ.log 20,971,520
- [ ONLINE REDO LOG ] /u02/database/SYBO2SZ/redolog/log4bSYBO2SZ.log 20,971,520
- ---------------
- sum 5,107,376,128
- 41 rows selected.
- 3、创建相应的目录
- oracle@SZDB:/u02/database/SYBO2SZ> more mkdir_SY5221BK.sh
- #!/bin/sh
- rm -rf /u02/database/SY5221BK/archive
- rm -rf /u02/database/SY5221BK/backup
- rm -rf /u02/database/SY5221BK/bdump
- rm -rf /u02/database/SY5221BK/cdump
- rm -rf /u02/database/SY5221BK/udump
- rm -rf /u02/database/SY5221BK/controlf
- rm -rf /u02/database/SY5221BK/oradata
- rm -rf /u02/database/SY5221BK/redolog
- rm -rf /u02/database/SY5221BK/undo
- rm -rf /u02/database/SY5221BK/temp
- rm -rf /u02/database/SY5221BK/ref_data
- rm -rf /u02/database/SY5221BK/BNR
- rm -rf /u02/database/SY5221BK/BNR/full
- rm -rf /u02/database/SY5221BK/BNR/dump
- rm -rf /u02/database/SY5221BK/dbcreatelogs
- mkdir -p /u02/database/SY5221BK/flash_recovery_area
- mkdir -p /u02/database/SY5221BK
- mkdir -p /u02/database/SY5221BK/archive
- mkdir -p /u02/database/SY5221BK/backup
- mkdir -p /u02/database/SY5221BK/bdump
- mkdir -p /u02/database/SY5221BK/cdump
- mkdir -p /u02/database/SY5221BK/udump
- mkdir -p /u02/database/SY5221BK/controlf
- mkdir -p /u02/database/SY5221BK/oradata
- mkdir -p /u02/database/SY5221BK/redolog
- mkdir -p /u02/database/SY5221BK/undo
- mkdir -p /u02/database/SY5221BK/temp
- mkdir -p /u02/database/SY5221BK/ref_data
- mkdir -p /u02/database/SY5221BK/BNR
- mkdir -p /u02/database/SY5221BK/BNR/full
- mkdir -p /u02/database/SY5221BK/BNR/dump
- mkdir -p /u02/database/SY5221BK/dbcreatelogs
- oracle@SZDB:/u02/database/SYBO2SZ> ./mkdir_SY5221BK.sh
- 4、实施迁移
- sys@SYBO2SZ> startup mount force; --->切换数据库到mount状态
- ORACLE instance started.
- Database mounted.
- sys@SYBO2SZ> @/users/robin/dba_scripts/custom/sql/transfer_db_files -->调用脚本进行数据及日志文件的迁移
- Step 1, Coping file to destination from source
- ============================================
- Step 2, updating files to control file
- ============================================
- sys@SYBO2SZ> alter database open; -->切换数据库到open状态
- Database altered.
- sys@SYBO2SZ> @dba_files_all_2.sql -->验证切换结果
- Tablespace Name / File Class Filename File Size Auto
- ----------------------------- ---------------------------------------------------------------------- ----
- GOEX_ACCOUNT_IDX /u02/database/SY5221BK/oradata/SY5221BK_account_idx.dbf 16,777,216 YES
- --.......... .................
- TEMP /u02/database/SY5221BK/temp/tempSY5221BK.dbf 432,013,312 YES
- UNDOTBS1 /u02/database/SY5221BK/undo/undotbsSY5221BK.dbf 429,916,160 YES
- UNDOTBS2 /u02/database/SY5221BK/undo/undotbsSY5221BK2.dbf 314,572,800 YES
- [ ONLINE REDO LOG ] /u02/database/SY5221BK/redolog/log3aSY5221BK.log 20,971,520
- [ ONLINE REDO LOG ] /u02/database/SY5221BK/redolog/log3bSY5221BK.log 20,971,520
- [ ONLINE REDO LOG ] /u02/database/SY5221BK/redolog/log4aSY5221BK.log 20,971,520
- [ ONLINE REDO LOG ] /u02/database/SY5221BK/redolog/log4bSY5221BK.log 20,971,520
- -------------
- sum 5,107,376,128
- 41 rows selected.
- --如果仅仅是迁移数据文件以及日志文件则上述步骤完成即可
- --如果需要修改相关的参数文件以及迁移控制文件则继续下面的步骤
- --由于控制文件的在mount状态下被校验,因此我们在nomount状态下来处理
- sys@SYBO2SZ> shutdown immediate;
- sys@SYBO2SZ> startup nomount;
- -->修改参数文件之前先备份spfile
- sys@SYBO2SZ> create pfile='/users/oracle/OraHome10g/dbs/initSYBO2SZ_bak.ora' from spfile;
- File created.
- -->下面对相关的参数文件的路径进行修改,如果不需要修改参数,则跳过下面的步骤
- sys@SYBO2SZ> show parameter dump
- NAME TYPE VALUE
- ------------------------------------ ----------- ------------------------------
- background_core_dump string partial
- background_dump_dest string /u02/database/SYBO2SZ/bdump
- core_dump_dest string /u02/database/SYBO2SZ/cdump
- max_dump_file_size string UNLIMITED
- shadow_core_dump string partial
- user_dump_dest string /u02/database/SYBO2SZ/udump
- sys@SYBO2SZ> alter system set background_dump_dest='/u02/database/SY5221BK/bdump' scope=both sid='*';
- System altered.
- sys@SYBO2SZ> alter system set core_dump_dest='/u02/database/SY5221BK/cdump' scope=both sid='*';
- System altered.
- sys@SYBO2SZ> alter system set user_dump_dest='/u02/database/SY5221BK/udump' scope=both sid='*';
- System altered.
- sys@SYBO2SZ> show parameter db_recovery_file_dest
- NAME TYPE VALUE
- ------------------------------------ ----------- ------------------------------
- db_recovery_file_dest string /u02/database/SYBO2SZ/flash_re
- covery_area
- db_recovery_file_dest_size big integer 1G
- sys@SYBO2SZ> alter system set db_recovery_file_dest='/u02/database/SY5221BK/flash_recovery_area' scope=both;
- System altered.
- sys@SYBO2SZ> show parameter LOG_ARCHIVE_DEST_1
- NAME TYPE VALUE
- ------------------------------------ ----------- ------------------------------
- log_archive_dest_1 string LOCATION=/u02/database/SYBO2SZ
- /archive/
- log_archive_dest_10 string
- sys@SYBO2SZ> alter system set log_archive_dest_1='LOCATION=/u02/database/SY5221BK/archive' scope=both;
- System altered.
- sys@SYBO2SZ> show parameter UTL_FILE_DIR
- NAME TYPE VALUE
- ------------------------------------ ----------- ------------------------------
- utl_file_dir string /u02/database/SYBO2SZ/udump
- sys@SYBO2SZ> alter system set UTL_FILE_DIR='/u02/database/SY5221BK/udump' scope=both;
- alter system set UTL_FILE_DIR='/u02/database/SY5221BK/udump' scope=both
- *
- ERROR at line 1:
- ORA-02095: specified initialization parameter cannot be modified -->该参数不能修改内存值
- sys@SYBO2SZ> alter system set UTL_FILE_DIR='/u02/database/SY5221BK/udump' scope=spfile;
- System altered.
- -->下面对控制文件位置进行修改
- sys@SYBO2SZ> show parameter control_f
- NAME TYPE VALUE
- ------------------------------------ ----------- ------------------------------
- control_file_record_keep_time integer 7
- control_files string /u02/database/SYBO2SZ/controlf
- /cntl1SYBO2SZ.ctl, /u02/databa
- se/SYBO2SZ/controlf/cntl2SYBO2
- SZ.ctl, /u02/database/SYBO2SZ/
- controlf/cntl3SYBO2SZ.ctl
- -->将控制文件复制到新位置
- sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/controlf/cntl1SYBO2SZ.ctl /u02/database/SY5221BK/controlf/cntl1SY5221BK.ctl
- sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/controlf/cntl1SYBO2SZ.ctl /u02/database/SY5221BK/controlf/cntl2SY5221BK.ctl
- sys@SYBO2SZ> ho cp /u02/database/SYBO2SZ/controlf/cntl1SYBO2SZ.ctl /u02/database/SY5221BK/controlf/cntl3SY5221BK.ctl
- -->Author : Robinson Cheng
- -->Blog : http://blog.csdn.net/robinson_0612
- -->通过修改control_files参数来修改控制文件位置
- sys@SYBO2SZ> alter system set control_files='/u02/database/SY5221BK/controlf/cntl1SY5221BK.ctl',
- 2 '/u02/database/SY5221BK/controlf/cntl2SY5221BK.ctl','/u02/database/SY5221BK/controlf/cntl3SY5221BK.ctl'
- 3 scope=spfile;
- System altered.
- sys@SYBO2SZ> shutdown immediate;
- sys@SYBO2SZ> startup mount;
- SQL> show parameter control_f
- NAME TYPE VALUE
- ------------------------------------ ----------- ------------------------------
- control_file_record_keep_time integer 7
- control_files string /u02/database/SY5221BK/control
- f/cntl1SY5221BK.ctl, /u02/data
- base/SY5221BK/controlf/cntl2SY
- 5221BK.ctl, /u02/database/SY52
- 21BK/controlf/cntl3SY5221BK.ct
- l
- sys@SYBO2SZ> show parameter dump
- background_core_dump string partial
- background_dump_dest string /u02/database/SY5221BK/bdump
- core_dump_dest string /u02/database/SY5221BK/cdump
- max_dump_file_size string UNLIMITED
- shadow_core_dump string partial
- user_dump_dest string /u02/database/SY5221BK/udump
- sys@SYBO2SZ> alter database open;
- Database altered.
- 5、迁移脚本
- sys@SYBO2SZ> ho more /users/robin/dba_scripts/custom/sql/transfer_db_files.sql
- Prompt
- Prompt Step 1, Coping file to destination from source
- Prompt ============================================
- Prompt
- set linesize 200
- set heading off verify off feedback off termout off pagesize 999
- define src_dir='SYBO2SZ'
- define tar_dir='SY5221BK'
- spool /tmp/cp_files.sql
- SELECT 'ho cp ' || name || ' ' || REPLACE (name, '&src_dir', '&tar_dir')
- FROM v$datafile
- UNION ALL
- SELECT 'ho cp ' || name || ' ' || REPLACE (name, '&src_dir', '&tar_dir')
- FROM v$tempfile
- UNION ALL
- SELECT 'ho cp ' || MEMBER || ' ' || REPLACE (MEMBER, '&src_dir', '&tar_dir') FROM v$logfile;
- spool off;
- @/tmp/cp_files.sql
- set termout on
- Prompt
- Prompt Step 2, updating files to control file
- Prompt ============================================
- Prompt
- set termout off
- spool /tmp/update_cntl.sql
- SELECT 'alter database rename file '''
- || name
- || ''' to '''
- || REPLACE (name, '&src_dir', '&tar_dir')
- || ''''
- || ';'
- FROM v$datafile
- UNION ALL
- SELECT 'alter database rename file '''
- || name
- || ''' to '''
- || REPLACE (name, '&src_dir', '&tar_dir')
- || ''''
- || ';'
- FROM v$tempfile
- UNION ALL
- SELECT 'alter database rename file '''
- || MEMBER
- || ''' to '''
- || REPLACE (MEMBER, '&src_dir', '&tar_dir')
- || ''''
- || ';'
- FROM v$logfile;
- spool off;
- set termout on;
- @/tmp/update_cntl.sql
- set heading on verify on feedback on termout on
- 6、后记
- a、数据迁移前建议先备份数据库
- b、我们在迁移中使用了复制(cp)方式,实际上可以直接使用移动方式(mv)
- c、需要理解数据库的启动的几个阶段。即nomount状态时不加载控制文件,mount状态时不加载数据文件及日志文件
- d、对于数据库启动阶段的深刻理解,有助于弄清楚什么状态下我们能做什么,不能做什么
- e、对于控制文件位置以及参数中相关dump文件位置可以直接通过编辑pfile文件来完成。上例使用的是修改spfile文件
- f、迁移脚本可以根据需要进行相应的修改,注意我们定义了src_dir与tar_dir
- g、可将数据库源文件夹重命名,重启数据库(open),open会校验所有文件,以防止迁移中的部分文件丢失,无误后可删除源文件夹
- h、如果需要修改数据库名,则可以通过使用nid命令修改 db name 及 dbid来完成,相当于生成了一个新的数据库
- 转:http://blog.csdn.net/leshami/article/details/8553650