一次使用duplicate建立測試資料庫的過程

us_yunleiwang發表於2013-12-04

由於管理不善,測試機的環境和生產機的環境已經相差甚遠了,於是一狠心重建測試機,重建的第一步,重建資料庫,我採用rman的duplicate命令從生產資料庫複製到測試資料庫的方式,雖


然過程不太順利,還是有一定的參考性的。


1。找出資料庫相關檔案的位置
spfile/pfile
我用的是spfile,如果您用的是pfile,過程類似,這裡就不熬了,先找到這個spfile,通常來說是 $ORACLE_HOME/dbs/spfile$ORACLE_SID 。


sys$ora8i@4.20 SQL> show parameter spfile
 
NAME              TYPE        VALUE
----------------- ----------- ------------------------------------
spfile            string      /u01/app/oracle/dbs/spfileora8i.ora
sys$ora8i@4.20 SQL> show parameter spfile
 
NAME              TYPE        VALUE
----------------- ----------- ------------------------------------
spfile            string      /u01/app/oracle/dbs/spfileora8i.ora
資料檔案、日誌檔案和控制檔案
sys$ora8i@4.20 SQL> select name from v$datafile
  2  union all
  3  select name from v$controlfile
  4  union all
  5  select member from v$logfile;
 
NAME
--------------------------------------------
/u02/oradata/ora8i/system01.dbf
/u02/oradata/ora8i/undotbs01.dbf
/u02/oradata/ora8i/sysaux01.dbf
/u02/oradata/ora8i/users01.dbf
/u02/oradata/ora8i/E3.dbf
/u02/oradata/ora8i/switch01.dbf
/u02/oradata/ora8i/PSS.dbf
/u02/oradata/ora8i/CTXSYS.dbf
/u02/oradata/ora8i/e3_02.dbf
/u02/oradata/ora8i/CMS_1.dbf
/u02/oradata/ora8i/control01.ctl
/u02/oradata/ora8i/control02.ctl
/u02/oradata/ora8i/control03.ctl
/u02/oradata/ora8i/redo01.log
/u02/oradata/ora8i/redo02.log
/u02/oradata/ora8i/redo03.log
/u02/oradata/ora8i/redo04.log
/u02/oradata/ora8i/redo05.log
/u02/oradata/ora8i/redo06.log
從以上資訊來看spfile放在/u01/app/oracle/dbs/ ,資料檔案、重做日誌檔案和控制檔案都放在/u02/oradata/ora8i/。


2。在測試機中建立對應的目錄
在測試機中檢查這些目錄,確保這些目錄都存在:
# 在測試機中確保這些目錄都存在: 
[oracle@test-server ora8i]$ ls  /u01/app/oracle/dbs/  
hc_ora8i.dat  initdw.ora  init.ora  lkORA8I  old  orapwora8i  spfileora8i.ora
[oracle@test-server ora8i]$ ls /u02/oradata/ora8i/
ls: /u02/oradata/ora8i/: 沒有那個檔案或目錄
# 不存在的目錄就建立它:
[root@test-server ~]# cd /
[root@test-server /]# mkdir u02
[root@test-server /]# chown oracle:dba /u02
[root@test-server /]# mkdir -p /u02/oradata/ora8i/ 
[root@test-server /]# chown -R oracle:dba /u02
3。清理好測試伺服器
測試伺服器裝好的時候還有個資料,所以 /u01/app/oracle/dbs/ 中還有檔案。 
在測試機上關閉資料庫,然後將相關的資料檔案都備份好,由於這個不是重點,這裡就不熬述了。


4。配置測試伺服器
將spfile和密碼檔案copy到測試機上面,目錄要對應好。
[oracle@localhost dbs]$ cd /u01/app/oracle/dbs
[oracle@localhost dbs]$ scp ./spfileora8i.ora ./orapwora8i  10.168.4.74:`pwd`
oracle@10.168.4.74's password: 
spfileora8i.ora                                                        100% 3584     3.5KB/s   00:00    
orapwora8i                                                             100% 1536     1.5KB/s   00:00
在測試機中可以這樣檢測這兩個檔案是否可用:


檢查spfile:將資料庫啟動到nomount;


由於測試機和生產機的存在硬體上的差異(主要是內容)要將SGA調小一點[跳過]:


SQL> show parameter sga;
 
NAME                                 TYPE                   VALUE
------------------------------------ ---------------------- ------------------------------
lock_sga                             boolean                FALSE
pre_page_sga                         boolean                FALSE
sga_max_size                         big integer            1600M
sga_target                           big integer            1600M
SQL> alter system set sga_target=400M;
 
alter system set sga_target=400M
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-00827: could not shrink sga_target to specified value
 
SQL> alter system set sga_target=400M scope=spfile;  
 
System altered.
 
SQL> alter system set sga_max_size=500M scope=spfile;  
 
System altered.
 
SQL> shutdown immediate;  
ORA-01507: database not mounted
 
ORACLE instance shut down.
SQL> startup nomount; 
ORA-00821: Specified value of sga_target 400M is too small, needs to be at least 788M
 
-- 沒辦法,建立pfile,改吧
SQL> create pfile from spfile; 
[oracle@test-server dbs]$ cd /u01/app/oracle/dbs 
[oracle@test-server dbs]$ vim initora8i.ora 
-- 將開頭那幾行ASMM動態修改的引數去掉,
-- 順便把 pga_aggregate_target,db_cache_size,shared_pool_size 也改小,
-- 總之就是改到能啟動Oracle 就可以了。
 
SQL> startup nomount pfile='/u01/app/oracle/dbs/initora8i.ora'
ORACLE instance started.
 
Total System Global Area  524288000 bytes
Fixed Size                  2072472 bytes
Variable Size             205521000 bytes
Database Buffers          310378496 bytes
Redo Buffers                6316032 bytes
SQL> shutdown immediate ;   
ORA-01507: database not mounted
 
 
ORACLE instance shut down.
 
-- 引數沒有問題後,可以建立spfile,以後啟動的時候可以使用spfile了
SQL> create spfile from pfile; 
 
File created.
 
SQL> startup ; 
ORACLE instance started.
 
Total System Global Area  524288000 bytes
Fixed Size                  2072472 bytes
Variable Size             205521000 bytes
Database Buffers          310378496 bytes
Redo Buffers                6316032 bytes
ORA-00205: error in identifying control file, check alert log for more info
-- 這個錯誤是肯定了,spfile只能把oracle帶到nomount階段。
檢查密碼檔案: 先確定listener啟動了,然後再用sys使用者遠端登入到測試機。


[oracle@test-server admin]$ sqlplus sys/oracle@ora8i_4_74  as sysdba
SQL*Plus: Release 10.2.0.2.0 - Production on Mon May 10 14:01:47 2010
Copyright (c) 1982, 2005, Oracle.  All Rights Reserved.
 
Connected to an idle instance.
 
SQL>
如果上述兩部有問題的話請認真除錯。


5。準備資料
在生產機上面做一個全備,由於機器比較空閒,壓一壓,要依據實際情況寫指令碼哦。


RMAN> run
2> {
3>   allocate channel c1 device type disk ;
4>   backup
5>   as compressed backupset
6>   tag 'build_test_db'
7>   database
8>   INCLUDE CURRENT CONTROLFILE
9>   format '/u01/app/backup/backup_4.20_%I_%T_%s' ;
10>   release channel c1 ;
11> }
....
....
....
rman> list backup ; 
BS Key  Type LV Size       Device Type Elapsed Time Completion Time    
------- ---- -- ---------- ----------- ------------ -------------------
856     Full    824.35M    DISK        00:06:14     2010-05-10 14:24:34
        BP Key: 856   Status: AVAILABLE  Compressed: YES  Tag: BUILD_TEST_DB
        Piece Name: /u01/app/backup/backup_4.20_57919146_20100510_888 *******************
  List of Datafiles in backup set 856
  File LV Type Ckp SCN    Ckp Time            Name
  ---- -- ---- ---------- ------------------- ----
  1       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/system01.dbf
  2       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/undotbs01.dbf
  3       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/sysaux01.dbf
  4       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/users01.dbf
  5       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/E3.dbf
  7       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/switch01.dbf
  8       Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/PSS.dbf
  11      Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/CTXSYS.dbf
  13      Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/e3_02.dbf
  16      Full 121784138  2010-05-10 14:18:20 /u02/oradata/ora8i/CMS_1.dbf
 
BS Key  Type LV Size       Device Type Elapsed Time Completion Time    
------- ---- -- ---------- ----------- ------------ -------------------
857     Full    1.13M      DISK        00:00:02     2010-05-10 14:24:37
        BP Key: 857   Status: AVAILABLE  Compressed: YES  Tag: BUILD_TEST_DB
        Piece Name: /u01/app/backup/backup_4.20_57919146_20100510_889 *******************
  Control File Included: Ckp SCN: 121785222    Ckp time: 2010-05-10 14:24:35
  SPFILE Included: Modification time: 2010-05-08 00:11:48
將這兩個新生成的備份集的檔案到copy到測試機對應的目錄中,當然做之前要檢查目錄是否存在:
[oracle@localhost backup]$ cd /u01/app/backup/
[oracle@localhost backup]$ scp /u01/app/backup/backup_4.20_57919146_20100510_888 10.168.4.74:`pwd` 
oracle@10.168.4.74's password: 
backup_4.20_57919146_20100510_888                                      100%  824MB  14.7MB/s   00:56    
[oracle@localhost backup]$ scp /u01/app/backup/backup_4.20_57919146_20100510_889 10.168.4.74:`pwd`
oracle@10.168.4.74's password: 
backup_4.20_57919146_20100510_889                                      100% 1168KB   1.1MB/s   00:00
6。開始duplicate
確定一下測試機(在rman的角度來說是auxiliary)處於nomount 階段,生產機(在rman的角度來說是target)處於mount或open階段。在生產機的rman中執行:
[oracle@localhost backup]$ rman target /  auxiliary sys/oracle@ora8i_4_74 
關鍵是這兩句,最後確定一下兩個資料的狀態:
connected to target database: ORA8I (DBID=57919146)
connected to auxiliary database: ORA8I (not mounted)
 
RMAN> DUPLICATE TARGET DATABASE TO ORA8I NOFILENAMECHECK;
我建議,此時在生產機上作一次日誌切換,並將從全備開始時到剛生成的所有日誌檔案複製到測試伺服器相對應的目錄中,否則有可能出現以下問題[跳過]


channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
.....
.....
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/backup/backup_4.20_57919146_20100510_886
ORA-19870: error reading backup piece /u01/app/backup/backup_4.20_57919146_20100510_886
ORA-19505: failed to identify file "/u01/app/backup/backup_4.20_57919146_20100510_886"
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
failover to previous backup
 
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
.....
.....
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/backup/backup_4.20_57919146_20100509_884
ORA-19870: error reading backup piece /u01/app/backup/backup_4.20_57919146_20100509_884
ORA-19505: failed to identify file "/u01/app/backup/backup_4.20_57919146_20100509_884"
ORA-27037: unable to obtain file status
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
failover to previous backup
 
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of Duplicate Db command at 05/10/2010 14:47:54
RMAN-03015: error occurred in stored script Memory Script
RMAN-06026: some targets not found - aborting restore
原因是不能在測試機(auxiliary)找不到備份集,仔細看一下rman是從“最近”的備份集開始找的,但就是跳過了最最新的,這是由於在歸檔模式下的備份是非一致性備份,還需要redo的信


息才能使之變成一致性的,而這些資訊暫時還在online redo log中,簡單來說就是還沒有出生(歸檔),我們可以切換一下日誌檔案,使之生成歸檔日誌檔案,並傳到測試伺服器上,對應的


目錄下面。


將剛才duplicate在測試機上生成的檔案統統刪掉,再次duplicate


RMAN> DUPLICATE TARGET DATABASE TO ORA8I NOFILENAMECHECK;
 
........
contents of Memory Script:
{
   Alter clone database open resetlogs;
}
executing Memory Script
rman停在這裡了。此時可以從測試機的alert檔案中找到這麼一句話:


Active process 6260 user 'oracle' program 'oracle@test-server (TNS V1-V3)'
SHUTDOWN: waiting for logins to complete.
現在只要登入到資料庫中就大功告成了。

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

相關文章