您好,登錄后才能下訂單哦!
使用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_area db_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_area db_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用戶,并授權:
[oracle@temp-test oracle]$ sqlplus /nolog SQL> connect/as sysdba Connected. SQL> create user rman identified by oracle; User created. SQL> grant resource,connect,dba to rman; Grant succeeded. SQL> quit
使用rman用戶登錄RMAN:
[oracle@temp-test oracle]$ rman target rman/oracle Recovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 16:59:27 2017 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. connected to target database: ORCL (DBID=1486802665) RMAN>
也可以使用如下方式登錄:
[oracle@temp-test oracle]$ rman Recovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 17:00:35 2017 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. RMAN> connect target system/rman connected to target database: ORCL (DBID=1486802665) RMAN> connect target rman/oracle
也可以使用操作系統認證連接RMAN:
[oracle@temp-test oracle]$ rman target / Recovery Manager: Release 11.2.0.1.0 - Production on Fri Nov 3 17:02:52 2017 Copyright (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 catalog RMAN configuration parameters for database with db_unique_name ORCL are: CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default CONFIGURE BACKUP OPTIMIZATION OFF; # default CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default CONFIGURE MAXSETSIZE TO UNLIMITED; # default CONFIGURE ENCRYPTION FOR DATABASE OFF; # default CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default CONFIGURE 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-17 using channel ORA_DISK_1 channel ORA_DISK_1: starting datafile copy copying current control file output file name=/data/app/oracle/recovery_area/ORCL/controlfile/o1_mf_TAG20171103T180113_dzrhm9vk_.ctl tag=TAG20171103T180113 RECID=3 STAMP=959104874 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01 Finished backup at 03-NOV-17
如果沒有指定快閃恢復區也可以用手動指定路徑:
將快閃恢復區改為test目錄
SQL> connect/as sysdba Connected. SQL> show parameter db_recovery_file_dest; NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_recovery_file_dest string /data/app/oracle/recovery_area db_recovery_file_dest_size big integer 2G SQL> 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/test db_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狀態。
[oracle@temp-test oracle]$ sqlplus /nolog SQL> connect/as sysdba Connected. SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount; ORACLE instance started. Total System Global Area 1586708480 bytes Fixed Size 2213736 bytes Variable Size 939526296 bytes Database Buffers 637534208 bytes Redo Buffers 7434240 bytes Database mounted. SQL> quit
進入RMAN,備份數據:
先查看數據備份的默認參數,備份方式(copy或backupset),和備份格式(disk 或其他)
RMAN> show all; using target database control file instead of recovery catalog RMAN configuration parameters for database with db_unique_name ORCL are: CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default CONFIGURE BACKUP OPTIMIZATION ON; CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COPY PARALLELISM 1; CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default CONFIGURE MAXSETSIZE TO UNLIMITED; # default CONFIGURE ENCRYPTION FOR DATABASE OFF; # default CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default CONFIGURE 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 Mode Automatic archival Disabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 22 Current 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 any instance
日志歸檔必須在mount模式,關閉數據庫啟動到mount模式,開啟歸檔模式:
SQL> shutdown immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount; ORACLE instance started. Total System Global Area 1586708480 bytes Fixed Size 2213736 bytes Variable Size 1040189592 bytes Database Buffers 536870912 bytes Redo Buffers 7434240 bytes Database mounted. SQL> alter database archivelog; Database altered. SQL> alter database open; Database altered. SQL> archive log list; Database log mode Archive Mode Automatic archival Enabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence 22 Next log sequence to archive 24 Current 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-17 using channel ORA_DISK_1 channel ORA_DISK_1: starting datafile copy input datafile file number=00004 name=/data/app/oracle/oradata/orcl/users01.dbf output file name=/data/app/oracle/test/ORCL/datafile/o1_mf_users_dzzxzmxv_.dbf tag=TAG20171106T134939 RECID=12 STAMP=959348980 channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01 Finished 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> }
每次執行這條命令,就可以使用映像的方式對數據庫進行增量備份。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。