使用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> }
每次执行这条命令,就可以使用映像的方式对数据库进行增量备份。