系統環境:
系統環境先放在前面,是為了後面說明概念時,可以加以標示註解,以便更加瞭解 RMAN 的各個環節。
1. Oracle DB 11g on Windows:記錄 RMAN 所有備份的記錄,在 RMAN 稱為 RMAN Repository (RMAN 恢復目錄資料庫)。
ORACLE_SID:RMANDB
執行記錄 recovery catalog 的帳號/密碼:RMAN_USER/RMAN_PWD
2. Taget Database (要備份的目標資料庫):
Oracle DB 12c on Linux:正式資料庫,日常的交易要作備份。
ORACLE_SID:orcl
執行備份的帳號/密碼:sys/oracle
3. 兩者的 tnsnames.ora 均要有雙方連線的資訊:
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/12.1.0/db_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools.
ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = oradb12clinux.domain)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) )
RMANDB = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = rmandb.domain)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = rmandb.domain) ) )
|
RMAN 概念:
Oracle Database 的備份,一般建議是利用 RMAN 來作業,一來可以由系統自行管理,二來也可以省掉自已下一堆的指令。在建立 RMAN 的機制前,下面幾個概念要有:
1. Target Database:(目標資料庫):本實例為 ORACLE_SID: orcl
就是需要RMAN對其進行備份與恢復的資料庫,RMAN 可以備份資料檔案,控制檔,歸檔日誌,spfile.(注意:RMAN不能用於備份連線日誌、初始化參數檔:pfile 和命令檔)
2. recovery catalog(恢復目錄):
用來保存備份與恢復資訊的一個資料庫,不建議建在目標資料庫上。RMAN 利用恢復目錄記載的資訊去判斷如何執行需要的備份恢復操作。
如果不採用恢復目錄(nocatalog),備份資訊可以存在於目標資料庫的 control file 中。如果存放在目標資料庫的 control file 中,控制項檔會不斷增長,不能保存 RMAN 的Script. CONTROL_FILE_RECORD_KEEP_TIME (default=7):控制項檔中 RMAN 資訊保存的最短時間。
使用恢復目錄(catalog)的優勢: 可以存儲腳本,並記載較長時間的備份恢復操作。
3. RMAN Repository(RMAN 恢復目錄資料庫): 本實例為 ORACLE_SID: rmandb
存放 recovery catalog(恢復目錄)的資料庫。建議為 recovery catalog(恢復目錄)資料庫創建一個單獨的資料庫。
設置 RMAN ( Configuration RMAN )實作:
Step 01: 在資料庫 ORACLE_SID: orcl 建立 RMAN 所需的環境
1.) 連上資料庫:rmandb
C:\> sqlplus /nolog SQL> connect / as sysdba |
2.) 建立 tablespace:RMAN_TS
CREATE TABLESPACE RMAN_TS DATAFILE 'C:\oradb11g\oradata\rmandb\RMAN_DATA.DBF' SIZE 100M AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL AUTOALLOCATE LOGGING ONLINE SEGMENT SPACE MANAGEMENT AUTO; |
3.) 建立執行記錄 recovery catalog 的帳號:RMAN_USER,並給 CONNECT,RESOURCE,RECOVERY_CATALOG_OWNER 這三個權限
CREATE USER RMAN_USER IDENTIFIED BY RMAN_PWD DEFAULT TABLESPACE RMAN_TS TEMPORARY TABLESPACE TEMP ACCOUNT UNLOCK ; GRANT CONNECT,RESOURCE,RECOVERY_CATALOG_OWNER TO RMAN_USER; |
註:Oracle Database 11g 以後,帳號/密碼 有分大小寫,(To Enable/ Disable set SEC_CASE_SENSITIVE_LOGON to TRUE/ FALSE ,預設:TRUE ) 若要關閉需用指令 alter system set sec_case_sensitive_logon=false scope=both;
4.) 利用帳號:RMAN_USER 來建立 recovery catalog(恢復目錄)
C:\> rman catalog RMAN_USER/RMAN_PWD@rmandb RMAN> create catalog;
recovery catalog created |
Step 02: 連到目標資料庫 Connect to the target database
1.a ) 若是在 ORACLE_SID:rmandb 資料庫主機上作業
C:\> rman catalog RMAN_USER/RMAN_PWD@rmandb
RMAN> connect target sys/oracle@orcl
己連線到目標資料庫: ORCL (DBID=1357434876) |
1.b ) 若是在 ORACLE_SID: orcl (目標資料庫)資料庫主機上作業
Step 03: 註冊目標資料庫 Register the database
RMAN> register database;
database registered in recovery catalog starting full resync of recovery catalog full resync complete |
Step04: 驗證是否有註冊成功
RMAN> list incarnation of database;
List of Database Incarnations DB Key Inc Key DB Name DB ID STATUS Reset SCN Reset Time ------- ------- -------- ---------------- ------------ -------------- ---------- 1 2 ORCL 1357434876 CURRENT 1720082 24-OCT-13
|
PS: 也可以用SQL: select * from rc_database;
RMAN 備份實作:
0.) 先將 ORACLE_SID: orcl 改為 archive log mode
$ sqlplus / as sysdba #查看現在資料庫的備份狀況 SQL> archive log list; Database log mode No Archive Mode Automatic archival Disabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 30 Current log sequence 32
#查看 archive log 預設備份的目錄 SQL> show parameter DB_RECOVERY_FILE_DEST
NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_recovery_file_dest string /u01/app/oracle/fast_recovery_area db_recovery_file_dest_size big integer 4800M
#為了方便觀察, 改變備份目錄, 並關閉資料庫 SQL> alter system set db_recovery_file_dest='/u01/arclog' scope=both; SQL> shutdown immediate; # 設定資料庫的模式為 archive log mode SQL> startup mount; SQL> alter database archivelog; SQL> alter database open;
SQL> archive log list; Database log mode Archive Mode Automatic archival Enabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 30 Next log sequence to archive 32 Current log sequence 32
#強制產生 archive log file SQL> alter system switch logfile; System altered.
#到 OS 去查看是否有 archive log file 產生 $ pwd /u01/arclog/ORCL/archivelog/2013_11_07 $ ls -l -rw-r-----. 1 oracle oinstall 25397248 Nov 7 13:44 o1_mf_1_32_97pb5qnt_.arc |
註: log_archive_format 的參數設定請參考:
http://docs.oracle.com/cd/B19306_01/server.102/b14237/initparams103.htm
1.) 建立一個全備份的 script (Script 會存在RC_STORED_SCRIPT及RC_STORED_SCRIPT_LINE的table中): fullbackup
$ rman catalog RMAN_USER/RMAN_PWD@rmandb RMAN> connect target; connected to target database: ORCL (DBID=1357434876)
# 先將之前的 script 刪除 (若有的話) RMAN> delete script fullbackup; deleted script: fullbackup # 建立一個資料庫全備份的 script (含control file & spfile) RMAN> create script fullbackup { allocate channel d1 type disk; backup incremental level 0 format '/u01/backdb/%d_%s_%p' filesperset 5 (database include current controlfile); sql 'alter system archive log current'; release channel d1;} |
2.) 查看 RMAN 的備份 script
RMAN> connect target; connected to target database: ORCL (DBID=1357434876) RMAN> print script fullbackup; 或 $ sqlplus RMAN_USER/RMAN_PWD@rmandb SQL> select * from RC_STORED_SCRIPT; SQL> SQL> select * from RC_STORED_SCRIPT_LINE;
|
3.) 執行備份 script
RMAN> run {execute script fullbackup;}
executing script: fullbackup
allocated channel: d1 channel d1: SID=416 device type=DISK
Starting backup at 07-NOV-13 channel d1: starting incremental level 0 datafile backup set channel d1: specifying datafile(s) in backup set input datafile file number=00003 name=/u01/app/oracle/oradata/orcl/sysaux01.dbf input datafile file number=00001 name=/u01/app/oracle/oradata/orcl/system01.dbf input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf input datafile file number=00006 name=/u01/app/oracle/oradata/orcl/users01.dbf channel d1: starting piece 1 at 07-NOV-13 channel d1: finished piece 1 at 07-NOV-13 piece handle=/u01/backdb/ORCL_7_1 tag=TAG20131107T145348 comment=NONE => 此為RMAN備份的檔名: /u01/backdb/ORCL_7_1
channel d1: backup set complete, elapsed time: 00:00:45 channel d1: starting incremental level 0 datafile backup set channel d1: specifying datafile(s) in backup set input datafile file number=00009 name=/u01/app/oracle/oradata/orcl/pdborcl/sysaux01.dbf input datafile file number=00011 name=/u01/app/oracle/oradata/orcl/pdborcl/example01.dbf input datafile file number=00008 name=/u01/app/oracle/oradata/orcl/pdborcl/system01.dbf input datafile file number=00010 name=/u01/app/oracle/oradata/orcl/pdborcl/SAMPLE_SCHEMA_users01.dbf channel d1: starting piece 1 at 07-NOV-13 channel d1: finished piece 1 at 07-NOV-13 piece handle=/u01/backdb/ORCL_8_1 tag=TAG20131107T145348 comment=NONE => 此為RMAN備份的檔名:/u01/backdb/ORCL_8_1
channel d1: backup set complete, elapsed time: 00:00:35 channel d1: starting incremental level 0 datafile backup set channel d1: specifying datafile(s) in backup set input datafile file number=00007 name=/u01/app/oracle/oradata/orcl/pdbseed/sysaux01.dbf input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/pdbseed/system01.dbf channel d1: starting piece 1 at 07-NOV-13 channel d1: finished piece 1 at 07-NOV-13 piece handle=/u01/backdb/ORCL_9_1 tag=TAG20131107T145348 comment=NONE => 此為RMAN備份的檔名:/u01/backdb/ORCL_9_1
channel d1: backup set complete, elapsed time: 00:00:25 channel d1: starting incremental level 0 datafile backup set channel d1: specifying datafile(s) in backup set including current control file in backup set channel d1: starting piece 1 at 07-NOV-13 channel d1: finished piece 1 at 07-NOV-13 piece handle=/u01/backdb/ORCL_10_1 tag=TAG20131107T145348 comment=NONE channel d1: backup set complete, elapsed time: 00:00:01 Finished backup at 07-NOV-13
Starting Control File and SPFILE Autobackup at 07-NOV-13 piece handle=/u01/arclog/ORCL/autobackup/2013_11_07/o1_mf_s_830876136_97pgc919_.bkp comment=NONE => 此為RMAN 備份 Control File and SPFILE file :/u01/arclog/ORCL/autobackup/2013_11_07/o1_mf_s_830876136_97pgc919_.bkp Finished Control File and SPFILE Autobackup at 07-NOV-13
sql statement: alter system archive log current
released channel: d1
|
基本上 Oracle Database 資料庫在 RMAN 的備份,ORACLE_SID: orcl (目標資料庫)應該是處於 Archive log mode 才可以線上備份。以上是 Oracle RMAN 的基本概念與資料庫全備份實作
附註:
若您是使用 Oracle XE 版本的資料庫,應該會出現下列的訊息,代表資料庫 ORACLE_SID:xe 是11g,但目標資料庫 ORACLE_SID:orcl 是 12c ,package RMAN_XE.DBMS_RCVCAT 不相容。所以不能用 oracle XE 版本的資料庫來作。
在資料庫 ORACLE_SID:xe 是11g 看到的訊息: database registered in recovery catalog starting full resync of recovery catalog RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03008: error while performing automatic resync of recovery catalog RMAN-20036: invalid record order 在資料庫 ORACLE_SID:orcl 是 12c 看到的訊息:
PL/SQL package RMAN_XE.DBMS_RCVCAT version 11.02.00.02 in RCVCAT database is too old PL/SQL package RMAN_XE.DBMS_RCVCAT version 11.02.00.02 in RCVCAT database is too old RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-03002: failure of list command at 11/01/2013 14:38:44 RMAN-06429: RCVCAT database is not compatible with this version of RMAN
|
就算用指令UPGRADE CATALOG; 來升級 catalog,雖然會成功,也不能使用
RMAN> UPGRADE CATALOG;
recovery catalog owner is RMAN_XE enter UPGRADE CATALOG command again to confirm catalog upgrade
RMAN> UPGRADE CATALOG;
recovery catalog upgraded to version 11.02.00.02 DBMS_RCVMAN package upgraded to version 11.02.00.02 DBMS_RCVCAT package upgraded to version 11.02.00.02 |
參考:
http://fecbob.pixnet.net/blog/post/38195291-oracle-rman%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5
原文出處:Oracle RMAN 的基本概念與資料庫全備份實作 @ MISTECH 技術手抄本 :: 痞客邦 PIXNET ::