使用RMAN進行同名資料庫異地環境搭建實驗

realkid4發表於2014-03-04

 

隨著版本不斷地提升,RMAN作為標準備份工具的作用地位是不斷的被增強的。從過去傳統的備份還原工具,到現在新環境遷移、DG搭建,我們都可以看到RMAN工具增強的身影。

本篇就以RMAN為工具,利用RMAN備份在新主機上搭建服務名相同的資料庫。由於環境所限,筆者採用的方法是:先進行備份獲取,之後刪除掉原資料庫,最後使用備份重新搭建(包括引數、控制檔案和資料檔案恢復)。

 

1、環境介紹

 

我們選擇Linux環境下的10.2.0.1進行實驗。

 

SQL> select * from v$version;

BANNER

---------------------------------

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

PL/SQL Release 10.2.0.1.0 - Production

CORE    10.2.0.1.0      Production

TNS for Linux: Version 10.2.0.1.0 - Production

NLSRTL Version 10.2.0.1.0 - Production

 

當前處在歸檔模式下。

 

SQL> archive log list;

Database log mode              Archive Mode

Automatic archival             Enabled

Archive destination            USE_DB_RECOVERY_FILE_DEST

Oldest online log sequence     2

Next log sequence to archive   4

Current log sequence           4

 

2、備份獲取

 

RMAN支援onlineoffline兩種備份模式。如果選擇online備份模式,Oracle就不需要因為備份操作停機,但是恢復過程需要配合redo logapply過程。如果選擇offline備份模式,Oracle需要在備份過程中停機,但是理論上是不需要archive redo log進行支援的。

如果Oracle執行在archived模式下,RMAN可以進行onlineoffline模式。如果是noarchived模式,RMAN就只能在offline模式備份了。

當前環境變數:

 

[oracle@SimpleLinuxUp ~]$ env | grep ORA

ORACLE_SID=oratest

ORACLE_BASE=/u01/app/oracle

ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1

 

筆者對onlineoffline備份,都進行保留。首先建立駐留目錄:

 

[root@SimpleLinuxUp ~]# cd /

[root@SimpleLinuxUp /]# mkdir onlinebk

[root@SimpleLinuxUp /]# mkdir oflinebk

[root@SimpleLinuxUp /]# chown oracle:oinstall onlinebk/

[root@SimpleLinuxUp /]# chown oracle:oinstall oflinebk/

[root@SimpleLinuxUp /]# ls -l | grep linebk

drwxr-xr-x   2 oracle oinstall  4096 Mar  3 16:46 oflinebk

drwxr-xr-x   2 oracle oinstall  4096 Mar  3 16:46 onlinebk

 

如果進行online備份,資料庫不需要關閉。備份範圍為:引數檔案、資料檔案、控制檔案。

 

online backup

RMAN> connect target /

 

connected to target database: ORATEST (DBID=3370560176)

using target database control file instead of recovery catalog

 

RMAN> backup database format '/onlinebk/%U'; --資料庫

Starting backup at 03-MAR-14

using channel ORA_DISK_1

channel ORA_DISK_1: starting full datafile backupset

(篇幅原因,有省略……)

piece handle=/onlinebk/04p28ubo_1_1 tag=TAG20140303T165227 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02

Finished backup at 03-MAR-14

 

RMAN> backup archivelog all format '/onlinebk/%U'; --已經歸檔日誌

 

Starting backup at 03-MAR-14

current log archived

using channel ORA_DISK_1

(篇幅原因,有省略……)

piece handle=/onlinebk/05p28uds_1_1 tag=TAG20140303T165420 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02

Finished backup at 03-MAR-14

 

 

RMAN> backup current controlfile format '/onlinebk/control.bks';

 

Starting backup at 03-MAR-14

using channel ORA_DISK_1

(篇幅原因,有省略……)

piece handle=/onlinebk/control.bks tag=TAG20140303T165457 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 03-MAR-14

 

RMAN> backup spfile format '/onlinebk/spfile.bks';

 

Starting backup at 03-MAR-14

using channel ORA_DISK_1

(篇幅原因,有省略……)

piece handle=/onlinebk/spfile.bks tag=TAG20140303T165518 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 03-MAR-14

 

如果進行offline備份,則需要將資料庫完整關閉(非abort關閉)。也不需要進行歸檔檔案備份。

 

RMAN> shutdown immediate;

database closed

database dismounted

Oracle instance shut down

 

RMAN> startup mount;

 

connected to target database (not started)

Oracle instance started

database mounted

 

進行備份。

 

 

RMAN> backup database format '/oflinebk/%U';

 

Starting backup at 03-MAR-14

allocated channel: ORA_DISK_1

(篇幅原因,有省略……

piece handle=/oflinebk/09p28ukd_1_1 tag=TAG20140303T165713 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02

Finished backup at 03-MAR-14

 

 

RMAN> backup current controlfile format '/oflinebk/control.bks';

 

Starting backup at 03-MAR-14

using channel ORA_DISK_1

channel ORA_DISK_1: starting full datafile backupset

(篇幅原因,有省略……

piece handle=/oflinebk/control.bks tag=TAG20140303T165824 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 03-MAR-14

 

RMAN> backup spfile format '/oflinebk/spfile.bks';

 

Starting backup at 03-MAR-14

using channel ORA_DISK_1

(篇幅原因,有省略……

piece handle=/oflinebk/spfile.bks tag=TAG20140303T165841 comment=NONE

channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01

Finished backup at 03-MAR-14

 

必要的備份元素已經具備。注意:如果從完善的角度,對TNS網路引數三檔案:tnsnames.oralistener.orasqlnet.ora最好也進行備份。

 

 

3、資料庫刪除

 

配置XWindows工具呼叫dbca將原資料庫刪除。

 

[oracle@SimpleLinuxUp oflinebk]$ export DISPLAY=192.168.0.1:0.0

[oracle@SimpleLinuxUp oflinebk]$ xclock

Warning: Missing charsets in String to FontSet conversion

 

呼叫dbca刪除資料庫。

 

使用RMAN進行同名資料庫異地環境搭建實驗


使用RMAN進行同名資料庫異地環境搭建實驗

刪除資料庫確認。

 

SQL> conn / as sysdba 

Connected to an idle instance.

SQL> startup

ORA-01078: failure in processing system parameters

LRM-00109: could not open parameter file '/u01/app/oracle/product/10.2.0/db_1/dbs/initoratest.ora'

 

注意:使用dbca刪除資料庫後,根據OFA原則建立的資料庫目錄大都被刪除。相關檔案也被刪除。

下面就可以進行恢復過程。

 

4、引數檔案恢復

 

引數檔案是確保資料庫進行nomount狀態、例項建立、控制檔案獲取的重要檔案。由於RMAN是工作在mount狀態以上,所以需要先進行spfile的恢復。如果使用RMAN,是可以在沒有引數檔案的情況下,進入一個dummy資料庫的。

 

RMAN> connect target /

connected to target database (not started)

MAN> startup nomount force;

 

startup failed: ORA-01078: failure in processing system parameters

LRM-00109: could not open parameter file '/u01/app/oracle/product/10.2.0/db_1/dbs/initoratest.ora'

starting Oracle instance without parameter file for retrival of spfile

Oracle instance started

 

Total System Global Area     159383552 bytes

 

Fixed Size                     1218268 bytes

Variable Size                 54528292 bytes

Database Buffers             100663296 bytes

Redo Buffers                   2973696 bytes

 

恢復引數檔案,使用restore spfile from ‘<目錄>’方法。

 

RMAN> restore spfile from '/onlinebk/spfile.bks';

Starting restore at 03-MAR-14

using channel ORA_DISK_1

 

channel ORA_DISK_1: autobackup found: /onlinebk/spfile.bks

channel ORA_DISK_1: SPFILE restore from autobackup complete

Finished restore at 03-MAR-14

 

注意:這個時候還不能啟動到nomount

 

SQL> conn / as sysdba

Connected.

SQL> shutdown immediate

ORA-01507: database not mounted

 

ORACLE instance shut down.

SQL> startup nomount

ORA-09925: Unable to create audit trail file

Linux Error: 2: No such file or directory

Additional information: 9925

 

這個問題的原因在於:我們刪除資料庫之後,原有的很多與ORATEST相關的目錄都被刪除。解決的方法是利用文字格式的pfile確定目錄,之後手工建立出來。

 

SQL> create pfile from spfile;

File created.

 

[oracle@SimpleLinuxUp ~]$ cd /u01/app/oracle/product/10.2.0/db_1/dbs/

[oracle@SimpleLinuxUp dbs]$ ls

hc_oratest.dat  initdw.ora  init.ora  initoratest.ora  snapcf_oratest.f  spfileoratest.ora

 

引數檔案initoratest.ora中,定義了很多目錄結構,如control_filesrecovery_file_dest等。內容較多,就不一一列舉。手工使用mkdir進行建立。

 

[root@SimpleLinuxUp u01]# cd /

 [root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/admin/oratest/adump

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/admin/oratest/bdump

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/oradata/ORATEST/controlfile/

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/flash_recovery_area/ORATEST/controlfile

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/flash_recovery_area/ORATEST/controlfile

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/admin/oratest/cdump

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/oradata

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/flash_recovery_area

[root@SimpleLinuxUp /]# mkdir -p /u01/app/oracle/admin/oratest/udump

[root@SimpleLinuxUp /]# chown -R oracle:oinstall /u01

[root@SimpleLinuxUp /]#

 

再嘗試啟動到nomount,成功。

 

SQL> conn / as sysdba

Connected to an idle instance.

SQL> startup nomount

ORACLE instance started.

 

Total System Global Area  247463936 bytes

Fixed Size                  1218772 bytes

Variable Size              83887916 bytes

Database Buffers          159383552 bytes

Redo Buffers                2973696 bytes

 

5、控制檔案恢復

 

使用restore controlfile from的方法進行控制檔案恢復。

 

RMAN> restore controlfile from '/onlinebk/control.bks';

 

Starting restore at 03-MAR-14

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=431 devtype=DISK

 

channel ORA_DISK_1: restoring control file

channel ORA_DISK_1: restore complete, elapsed time: 00:00:02

output filename=/u01/app/oracle/oradata/ORATEST/controlfile/o1_mf_9k8lh412_.ctl

output filename=/u01/app/oracle/flash_recovery_area/ORATEST/controlfile/o1_mf_9k8lh45x_.ctl

Finished restore at 03-MAR-14

 

載入入control file之後,作為備份資訊就可以讀取到。

 

RMAN> report schema;

 

Starting implicit crosscheck backup at 03-MAR-14

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=431 devtype=DISK

Crosschecked 3 objects

Finished implicit crosscheck backup at 03-MAR-14

 

Starting implicit crosscheck copy at 03-MAR-14

using channel ORA_DISK_1

Finished implicit crosscheck copy at 03-MAR-14

 

searching for all files in the recovery area

cataloging files...

no files cataloged

 

RMAN-06139: WARNING: control file is not current for REPORT SCHEMA

Report of database schema

 

List of Permanent Datafiles

===========================

File Size(MB) Tablespace           RB segs Datafile Name

---- -------- -------------------- ------- ------------------------

(篇幅原因,有省略……

List of Temporary Files

=======================

File Size(MB) Tablespace           Maxsize(MB) Tempfile Name

---- -------- -------------------- ----------- --------------------

1    0        TEMP                 32767       /u01/app/oracle/oradata/ORATEST/datafile/o1_mf_temp_9cq625jt_.tmp

 

下面要講/onlinebk目錄中的備份載入進去,使用rmancatalog命令就可以實現這個功能。

 

 

RMAN> catalog start with '/onlinebk/';

 

searching for all files that match the pattern /onlinebk/

 

List of Files Unknown to the Database

=====================================

File Name: /onlinebk/spfile.bks

File Name: /onlinebk/control.bks

File Name: /onlinebk/02p28u13_1_1

 

Do you really want to catalog the above files (enter YES or NO)? yes

cataloging files...

cataloging done

 

List of Cataloged Files

=======================

File Name: /onlinebk/spfile.bks

File Name: /onlinebk/control.bks

File Name: /onlinebk/02p28u13_1_1

 

6、恢復資料檔案

 

我們使用備份資料檔案、歸檔檔案和引數檔案,在online狀態下,是不能進行完全恢復的。因為執行狀態下,當前active的日誌資訊是拿不到的。所以是需要進行until scn

使用restore database preview可以檢視當前可以做到什麼程度。

 

RMAN> restore database preview ;

 

Starting restore at 03-MAR-14

using channel ORA_DISK_1

 

List of Backup Sets

===================

BS Key  Type LV Size       Device Type Elapsed Time Completion Time

------- ---- -- ---------- ----------- ------------ ---------------

(篇幅原因,有省略……)

Key     Thrd Seq     S Low Time  Name

------- ---- ------- - --------- ----

2       1    5       A 03-MAR-14 /u01/app/oracle/flash_recovery_area/ORATEST/archivelog/2014_03_03/o1_mf_1_5_9k8jsw0m_.arc

Media recovery start SCN is 556011

Recovery must be done beyond SCN 556011 to clear data files fuzziness

Finished restore at 03-MAR-14

 

進行資料庫restore過程。

 

 

RMAN> restore database;

 

Starting restore at 03-MAR-14

allocated channel: ORA_DISK_1

channel ORA_DISK_1: sid=434 devtype=DISK

 

channel ORA_DISK_1: starting datafile backupset restore

channel ORA_DISK_1: specifying datafile(s) to restore from backup set

(篇幅原因,有省略……)

 

channel ORA_DISK_1: restored backup piece 1

piece handle=/onlinebk/03p28uab_1_1 tag=TAG20140303T165227

channel ORA_DISK_1: restore complete, elapsed time: 00:00:25

Finished restore at 03-MAR-14

 

嘗試完全恢復。

 

RMAN> recover database;

Starting recover at 03-MAR-14

using channel ORA_DISK_1

 

starting media recovery

 

archive log thread 1 sequence 5 is already on disk as file /u01/app/oracle/flash_recovery_area/ORATEST/archivelog/2014_03_03/o1_mf_1_5_9k8jsw0m_.arc

archive log filename=/u01/app/oracle/flash_recovery_area/ORATEST/archivelog/2014_03_03/o1_mf_1_5_9k8jsw0m_.arc thread=1 sequence=5

unable to find archive log

archive log thread=1 sequence=6

RMAN-00571: ===========================================================

RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============

RMAN-00571: ===========================================================

RMAN-03002: failure of recover command at 03/03/2014 17:34:43

RMAN-06054: media recovery requesting unknown log: thread 1 seq 6 lowscn 556089

 

恢復失敗,只能選擇恢復到有日誌的SCN點。

 

 

RMAN> recover database until scn 556011;

Starting recover at 03-MAR-14

using channel ORA_DISK_1

 

starting media recovery

 

archive log thread 1 sequence 5 is already on disk as file /u01/app/oracle/flash_recovery_area/ORATEST/archivelog/2014_03_03/o1_mf_1_5_9k8jsw0m_.arc

archive log filename=/u01/app/oracle/flash_recovery_area/ORATEST/archivelog/2014_03_03/o1_mf_1_5_9k8jsw0m_.arc thread=1 sequence=5

media recovery complete, elapsed time: 00:00:00

Finished recover at 03-MAR-14

 

啟動資料庫,由於online redo log的缺失和指定SCN恢復,所以必須要進行resetlogs操作open資料庫。

 

SQL> alter database open resetlogs;

Database altered.

 

[oracle@SimpleLinuxUp ~]$ ps -ef | grep pmon

oracle    3512     1  0 17:21 ?        00:00:00 ora_pmon_oratest

oracle    3639  3481  0 17:44 pts/0    00:00:00 grep pmon

 

7、結論和擴充

 

同名資料庫同結構恢復是比較容易的。如果需要進行資料庫名稱修改,或者檔案目錄的修改,則需要進行額外的操作和步驟。但是無論是手工進行,還是虛擬搭建RMAN auxiliary資料庫,這都是可以實現的。


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

相關文章