標籤:oracle 11g
使用Oracle內建的備份恢複工具RMAN(Recovery Manager)具有以下幾種優勢:
支援增量備份
能自動管理備份檔案
自動化備份與恢複
備份的檔案有效性檢查
配置快閃恢複區(flash recovery area)
快閃恢複區是儲存備份和恢複資料檔案以及相關資訊的儲存區。需要設定兩個參數:db_recovery_file_dest_size(快閃恢複區的最大容量),db_recovery_file_dest(快閃恢複區所在的路徑)。
查看快閃恢複區資訊:
SQL> show parameter db_recovery_file_dest;NAME TYPE VALUE------------------------------------ ----------- ------------------------------db_recovery_file_dest string /data/app/oracle/recovery_areadb_recovery_file_dest_size big integer 3882M
修改快閃恢複區大小:
SQL> alter system set db_recovery_file_dest_size=2g;System altered.SQL> show parameter db_recovery_file_dest;NAME TYPE VALUE------------------------------------ ----------- ------------------------------db_recovery_file_dest string /data/app/oracle/recovery_areadb_recovery_file_dest_size big integer 2G
查看使用方式:
SQL> select name,space_limit,space_used,number_of_files from v$recovery_file_dest;NAME--------------------------------------------------------------------------------SPACE_LIMIT SPACE_USED NUMBER_OF_FILES----------- ---------- ---------------/data/app/oracle/recovery_area 2147483648 0 0
顯示沒有使用。
也可以使用此命令查看詳細資料:
SQL> select file_type,percent_space_used,percent_space_reclaimable,number_of_files from v$flash_recovery_area_usage;
建立RMAN到資料庫的串連
建立rman使用者,並授權:
[[email protected] oracle]$ sqlplus /nologSQL> connect/as sysdbaConnected.SQL> create user rman identified by oracle;User created.SQL> grant resource,connect,dba to rman;Grant succeeded.SQL> quit
使用rman使用者登入RMAN:
[[email protected] oracle]$ rman target rman/oracleRecovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 16:59:27 2017Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.connected to target database: ORCL (DBID=1486802665)RMAN>
也可以使用如下方式登入:
[[email protected] oracle]$ rmanRecovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 17:00:35 2017Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.RMAN> connect target system/rmanconnected to target database: ORCL (DBID=1486802665)RMAN> connect target rman/oracle
也可以使用作業系統認證串連RMAN:
[[email protected] oracle]$ rman target /Recovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 17:02:52 2017Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.connected to target database: ORCL (DBID=1486802665)RMAN>
配置RMAN
查看RMAN的配置參數:
RMAN> show all;using target database control file instead of recovery catalogRMAN configuration parameters for database with db_unique_name ORCL are:CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # defaultCONFIGURE BACKUP OPTIMIZATION OFF; # defaultCONFIGURE DEFAULT DEVICE TYPE TO DISK; # defaultCONFIGURE CONTROLFILE AUTOBACKUP OFF; # defaultCONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ‘%F‘; # defaultCONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # defaultCONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # defaultCONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # defaultCONFIGURE MAXSETSIZE TO UNLIMITED; # defaultCONFIGURE ENCRYPTION FOR DATABASE OFF; # defaultCONFIGURE ENCRYPTION ALGORITHM ‘AES128‘; # defaultCONFIGURE COMPRESSION ALGORITHM ‘BASIC‘ AS OF RELEASE ‘DEFAULT‘ OPTIMIZE FOR LOAD TRUE ; # defaultCONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # defaultCONFIGURE SNAPSHOT CONTROLFILE NAME TO ‘/data/app/oracle/product/11.2.0/dbhome_1/dbs/snapcf_orcl.f‘; # default
參數含義:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1
保留的備份副本數量,表示只保留一個資料檔案的副本,並且保留最新的備份副本。
CONFIGURE DEFAULT DEVICE TYPE TO DISK
資料檔案預設備份到磁碟上,也可以是其他裝置如磁帶(sbt)
CONFIGURE BACKUP OPTIMIZATION OFF
不使用備份最佳化,啟用備份最佳化的作用是如果已經備份了某個檔案的相同版本,則不會再備份該檔案。,只保留一份備份檔案。
CONFIGURE CONTROLFILE AUTOBACKUP OFF
不啟動控制檔案的自動備份,修改為ON之後,如果資料庫結構發生變化或者在備份資料庫過程中,控制檔案會自動再備份到指定目錄下
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET
備份通道數量為1,預設使用備份組的方式。通道數量越多,執行任務時間越短。
如果要修改參數,可以執行如下命令:
開啟備份最佳化:
RMAN> CONFIGURE BACKUP OPTIMIZATION ON;new RMAN configuration parameters:CONFIGURE BACKUP OPTIMIZATION ON;new RMAN configuration parameters are successfully stored
設定備份類型為Copy,也就是映像模式:
RMAN> CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COPY; new RMAN configuration parameters:CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COPY PARALLELISM 1;new RMAN configuration parameters are successfully stored
備份控制檔案到快閃恢複區:
RMAN> backup current controlfile;Starting backup at 03-NOV-17using channel ORA_DISK_1channel ORA_DISK_1: starting datafile copycopying current control fileoutput file name=/data/app/oracle/recovery_area/ORCL/controlfile/o1_mf_TAG20171103T180113_dzrhm9vk_.ctl tag=TAG20171103T180113 RECID=3 STAMP=959104874channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01Finished backup at 03-NOV-17
如果沒有指定快閃恢複區也可以用手動指定路徑:
將快閃恢複區改為test目錄
SQL> connect/as sysdbaConnected.SQL> show parameter db_recovery_file_dest;NAME TYPE VALUE------------------------------------ ----------- ------------------------------db_recovery_file_dest string /data/app/oracle/recovery_areadb_recovery_file_dest_size big integer 2GSQL> alter system set db_recovery_file_dest=‘/data/app/oracle/test‘;System altered.SQL> show parameter db_recovery_file_dest;NAME TYPE VALUE------------------------------------ ----------- ------------------------------db_recovery_file_dest string /data/app/oracle/testdb_recovery_file_dest_size big integer 2G
配置控制檔案備份的磁碟類型和備份目錄,路徑以%F結尾:
RMAN> configure controlfile autobackup format for device type disk to ‘/data/app/oracle/test/%F‘;new RMAN configuration parameters:CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ‘/data/app/oracle/test/%F‘;new RMAN configuration parameters are successfully stored
RMAN離線備份
RMAN離線備份需要關閉資料庫,啟動資料庫到mount狀態。
[[email protected] oracle]$ sqlplus /nologSQL> connect/as sysdbaConnected.SQL> shutdown immediate;Database closed.Database dismounted.ORACLE instance shut down.SQL> startup mount;ORACLE instance started.Total System Global Area 1586708480 bytesFixed Size 2213736 bytesVariable Size 939526296 bytesDatabase Buffers 637534208 bytesRedo Buffers 7434240 bytesDatabase mounted.SQL> quit
進入RMAN,備份資料:
先查看資料備份的預設參數,備份方式(copy或backupset),和備份格式(disk 或其他)
RMAN> show all;using target database control file instead of recovery catalogRMAN configuration parameters for database with db_unique_name ORCL are:CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # defaultCONFIGURE BACKUP OPTIMIZATION ON;CONFIGURE DEFAULT DEVICE TYPE TO DISK; # defaultCONFIGURE CONTROLFILE AUTOBACKUP OFF; # defaultCONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ‘%F‘; # defaultCONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COPY PARALLELISM 1;CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # defaultCONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # defaultCONFIGURE MAXSETSIZE TO UNLIMITED; # defaultCONFIGURE ENCRYPTION FOR DATABASE OFF; # defaultCONFIGURE ENCRYPTION ALGORITHM ‘AES128‘; # defaultCONFIGURE COMPRESSION ALGORITHM ‘BASIC‘ AS OF RELEASE ‘DEFAULT‘ OPTIMIZE FOR LOAD TRUE ; # defaultCONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # defaultCONFIGURE SNAPSHOT CONTROLFILE NAME TO ‘/data/app/oracle/product/11.2.0/dbhome_1/dbs/snapcf_orcl.f‘; # default
執行備份命令使用copy方式,映像複製整個庫:
RMAN> backup as copy database;
如果是使用預設的backupset模式,可以使用:
RMAN> backup as compressed backupset database;
備份完成後,開啟資料庫:
RMAN> sql ‘alter database open‘;sql statement: alter database open
RMAN聯機備份整個資料庫
聯機備份必須要設定快閃恢複區,快閃恢複區要足夠大,並且資料庫要置于歸檔模式。在資料庫進行備份的同時,可以進行DML操作,可以正常讀取,新的DML操作記錄會在重做記錄檔中,如果備份時間很長,而且在這個期間產生了大量的資料變化,重做日誌會切換從而將這些變化的資料寫到歸檔記錄檔中。這裡的歸檔日誌就類似於mysql的binlog,在出現介質故障的時候,使用備份和歸檔日誌記錄和完成資料的全部恢複。
查看資料庫歸檔模式是否開啟,當前屬於關閉狀態:
SQL> archive log list;Database log mode No Archive ModeAutomatic archival DisabledArchive destination USE_DB_RECOVERY_FILE_DESTOldest online log sequence 22Current log sequence 24
開啟日誌歸檔:
SQL> alter database archivelog;alter database archivelog*ERROR at line 1:ORA-01126: database must be mounted in this instance and not open in anyinstance
日誌歸檔必須在mount模式,關閉資料庫啟動到mount模式,開啟歸檔模式:
SQL> shutdown immediate;Database closed.Database dismounted.ORACLE instance shut down.SQL> startup mount;ORACLE instance started.Total System Global Area 1586708480 bytesFixed Size 2213736 bytesVariable Size 1040189592 bytesDatabase Buffers 536870912 bytesRedo Buffers 7434240 bytesDatabase mounted.SQL> alter database archivelog;Database altered.SQL> alter database open;Database altered.SQL> archive log list;Database log mode Archive ModeAutomatic archival EnabledArchive destination USE_DB_RECOVERY_FILE_DESTOldest online log sequence 22Next log sequence to archive 24Current log sequence 24
使用RMAN聯機備份資料庫:
RMAN> backup as compressed backupset database plus archivelog delete all input;
使用備份組格式進行備份,並採用壓縮的方式,壓縮比例大概為5:1。備份資料檔案,同時也備份歸檔記錄檔,備份完成之後會將已經備份的歸檔記錄檔從儲存目錄中刪除,清除歸檔空間。
備份資料表空間:
RMAN> backup tablespace users;Starting backup at 06-NOV-17using channel ORA_DISK_1channel ORA_DISK_1: starting datafile copyinput datafile file number=00004 name=/data/app/oracle/oradata/orcl/users01.dbfoutput file name=/data/app/oracle/test/ORCL/datafile/o1_mf_users_dzzxzmxv_.dbf tag=TAG20171106T134939 RECID=12 STAMP=959348980channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01Finished backup at 06-NOV-17
使用壓縮:
RMAN> backup as compressed backupset tablespace users;
使用RMAN備份資料檔案,%U表示自動分配一個唯一的命名:
RMAN> backup as backupset datafile 1 format ‘/data/app/backup/datafile_1_%U‘;
清除命令,慎用
清除舊的備份檔案set格式:
RMAN> delete obsolete; RMAN> crosscheck backupset;RMAN> delete expired backupset;
清楚舊的備份檔案和所有備份,copy模式:
RMAN> list datafilecopy all;RMAN> delete expired backupcopy;RMAN> crosscheck datafilecopy all;RMAN> delete expired backupcopy;
如果是由於快閃恢複區容量限制,可以修改快閃恢複區的路徑和容量:
SQL> alter system set db_recovery_file_dest=‘/data/app/oracle/test‘;
SQL> alter system set db_recovery_file_dest_size=10g;
RMAN增量備份
使用backup database時,都是全庫備份,每次這樣備份很耗時也佔用磁碟空間,RMAN的增量備份就能解決這些問題。
這裡有兩種備份層級,層級0備份為全庫備份,層級1的備份為增量備份,一般情況下需要對資料進行0層級的備份之後在進行層級1的增量備份。
RMAN> backup incremental level 0 database;
層級1的差異增量備份:
RMAN> backup incremental level 1 database;
對映像副本進行增量備份
RMAN> run { 2> backup incremental level 1 for recover of copy with tag ‘incr_copy_backup‘ database;3> recover copy of database with tag ‘incr_copy_backup‘;4> }
每次執行這條命令,就可以使用映像的方式對資料庫進行增量備份。
本文出自 “Trying” 部落格,請務必保留此出處http://tryingstuff.blog.51cto.com/4603492/1979382
Oracle 11g 使用RMAN備份資料庫