利用DBMS_FILE_TRANSFER包實現遠端備份

yangtingkun發表於2009-05-08

Oraclerman一直沒有提供備份到其他伺服器上的功能,不過從10g開始,可以利用DBMS_FILE_TRANSFER包來實現這個功能。

 

 

由於DBMS_FILE_TRANSFER包可以實現PUT_FILEGET_FILE的功能,因此可以在源資料庫透過PUT_FILE的方式進行備份,也可以在遠端站點透過GET_FILE的方式進行備份,不過考慮到使用DBMS_FILE_TRANSFER需要保證資料庫處於開啟的狀態,因此需要將表空間處於備份狀態,或者將表空間或資料庫處於只讀狀態。顯然後面一種方法對系統可用性的影響更大,而採用前一種需要執行ALTER TABLESPACE命令,因此還是選擇在源資料庫使用DBMS_FILE_TRANSFER.PUT_FILE的方式更方便一點。

如果只是在遠端儲存備份,那麼沒有太多的限制,只是要求源資料庫和目標伺服器上資料庫版本都在10g以上,且資料庫都處於開啟狀態。另外需要當前使用者配置了遠端資料庫的資料庫鏈,且資料庫鏈使用者可以訪問備份目錄的DIRECTORY,且擁有寫許可權,而當前使用者擁有DBMS_FILE_TRANSFER包的執行許可權,且擁有資料檔案所在目錄的DIRECTORY的訪問許可權。

首先在目標伺服器的所在資料庫做好準備工作:

[oracle@yans3 backup]$ sqlplus "/ as sysdba"

SQL*Plus: Release 10.2.0.3.0 - Production on Fri May 8 15:38:18 2009

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> create directory d_test10g as '/data/backup/test10g';

Directory created.

SQL> grant read, write on directory d_test10g to test;

Grant succeeded.

SQL> host ls -l /data/backup/test10g
total 0

下面在源資料庫檢查控制檔案、資料檔案的位置,並建立DIRECTORYDATABASE LINK

[oracle@bjtest ~]$ sqlplus "/ as sysdba"

SQL*Plus: Release 10.2.0.3.0 - Production on 星期六 5 9 00:30:28 2009

Copyright (c) 1982, 2006, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options

SQL> set pages 100 lines 120
SQL> select file_name from dba_data_files;

FILE_NAME
-------------------------------------------------------------------
/data/oradata/test10g/system01.dbf
/data/oradata/test10g/undotbs01.dbf
/data/oradata/test10g/sysaux01.dbf
/data/oradata/test10g/users01.dbf

SQL> show parameter control_files

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
control_files                        string      /data/oradata/test10g/control0
                                                 1.ctl, /data/oradata/test10g/c
                                                 ontrol02.ctl, /data/oradata/te
                                                 st10g/control03.ctl
SQL> create directory d_test10g as '/data/oradata/test10g';

Directory created.

SQL> create database link kaifa     
  2  connect to test
  3  identified by test
  4  using '172.25.13.227/kaifa';

Database link created.

下面可以將表空間置於備份狀態,並開始備份:

SQL> alter tablespace system begin backup;

Tablespace altered.

SQL> alter tablespace sysaux begin backup;

Tablespace altered.

SQL> alter tablespace undotbs1 begin backup;

Tablespace altered.

SQL> alter tablespace users begin backup;

Tablespace altered.

SQL> exec dbms_file_transfer.put_file('D_TEST10G', 'system01.dbf', 'D_TEST10G', 'system01.dbf', 'KAIFA')

PL/SQL procedure successfully completed.

SQL> exec dbms_file_transfer.put_file('D_TEST10G', 'undotbs01.dbf', 'D_TEST10G', 'undotbs01.dbf', 'KAIFA')

PL/SQL procedure successfully completed.

SQL> exec dbms_file_transfer.put_file('D_TEST10G', 'sysaux01.dbf', 'D_TEST10G', 'sysaux01.dbf', 'KAIFA')

PL/SQL procedure successfully completed.

SQL> exec dbms_file_transfer.put_file('D_TEST10G', 'users01.dbf', 'D_TEST10G', 'users01.dbf', 'KAIFA')

PL/SQL procedure successfully completed.

SQL> alter tablespace system end backup;

Tablespace altered.

SQL> alter tablespace sysaux end backup;

Tablespace altered.

SQL> alter tablespace undotbs1 end backup;

Tablespace altered.

SQL> alter tablespace users end backup;

Tablespace altered.

SQL> alter system switch logfile;

System altered.

SQL> alter database backup controlfile to '/data/oradata/test10g/controlfile.bak';

Database altered.

SQL> exec dbms_file_transfer.put_file('D_TEST10G', 'controlfile.bak', 'D_TEST10G', 'control01.ctl', 'KAIFA')

PL/SQL procedure successfully completed.

SQL> select name from v$archived_log; 

NAME
--------------------------------------------------------------------------
/data/oradata/test10g/archivelog/1_10_686362609.dbf

SQL> create directory d_arch as '/data/oradata/test10g/archivelog';

Directory created.

SQL> exec dbms_file_transfer.put_file('D_ARCH', '1_10_686362609.dbf', 'D_TEST10G', '1_10_686362609.dbf', 'KAIFA')

PL/SQL procedure successfully completed.

至此,所有資料檔案、控制檔案和歸檔日誌的檔案的備份全部完成。利用類似的方法還可以備份SPFILE,不過DBMS_FILE_TRANSFER不支援文字格式的pfile的備份。

SQL> select global_name from global_name;

GLOBAL_NAME
-----------------------------------------------------------------
KAIFA

SQL> host ls -l /data/backup/test10g
total 753244
-rw-r-----  1 oracle oinstall   8442368 May  8 16:31 1_10_686362609.dbf
-rw-r-----  1 oracle oinstall   7061504 May  8 16:30 control01.ctl
-rw-r-----  1 oracle oinstall 125837312 May  8 16:19 sysaux01.dbf
-rw-r-----  1 oracle oinstall 314580992 May  8 16:17 system01.dbf
-rw-r-----  1 oracle oinstall 209723392 May  8 16:18 undotbs01.dbf
-rw-r-----  1 oracle oinstall 104865792 May  8 16:21 users01.dbf

 

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

相關文章