相比exp,expdp,imp,impdp导入导出工具,作为运维人员,很有必要学习RMAN的备份与恢复。

一、备份整个数据库

[oracle@test11g ~]$ rman target /

Recovery Manager: Release 11.2.0.1.0 - Production on Fri Jul 12 09:12:29 2019

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

connected to target database: ORCL (DBID=1530400561)

%s指定备份集,%p指定碎片编号,%t指定时间戳

RMAN> backup database format '/home/oracle/rman_back_%s_%p_%t';

Starting backup at 12-JUL-19
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00006 name=/u01/app/oraData/orcl/CMS69_DATA.dbf
input datafile file number=00001 name=/u01/app/oraData/orcl/system01.dbf
input datafile file number=00002 name=/u01/app/oraData/orcl/sysaux01.dbf
input datafile file number=00003 name=/u01/app/oraData/orcl/undotbs01.dbf
input datafile file number=00007 name=/u01/app/oraData/undoTbs/hbk_undotbs.dbf
input datafile file number=00005 name=/u01/app/oraData/bak/hbk_data.dbf
input datafile file number=00008 name=/u01/app/oraData/undoTbs/hbk_undotbs_auto
input datafile file number=00004 name=/u01/app/oraData/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/home/oracle/rman_back_2_1_1013419522 tag=TAG20190712T092522 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:13:06
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/home/oracle/rman_back_3_1_1013420308 tag=TAG20190712T092522 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 12-JUL-19

可以看到已经成功备份完成

[root@test11g oracle]# ll -sh  /home/oracle/rman_back_*
2.2G -rw-r----- 1 oracle oinstall 2.2G 7月  12 09:38 /home/oracle/rman_back_2_1_1013419522
9.5M -rw-r----- 1 oracle oinstall 9.5M 7月  12 09:38 /home/oracle/rman_back_3_1_1013420308

二、备份单个表空间

先讲解下sqlplus的备份方式,然后对比下RMAN,让你体会RMAN是多么简单。
2.1 sqlplus备份单个表空间
由提供的表空间名,比如HBK_DATA

SQL> select tablespace_name from dba_tablespaces;

TABLESPACE_NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
HBK_TEMP
HBK_DATA
CMS69_DATA
MYTEMP1
MYTEMP2
HBK_UNDOTBS

TABLESPACE_NAME
------------------------------
HBK_UNDOTBS_AUTO

12 rows selected.

需要知道HBK_DATA表空间由哪些数据文件组成

SQL>  select tablespace_name,file_name from sys.dba_data_files where tablespace_name = 'HBK_DATA';

TABLESPACE_NAME
------------------------------------------------------------------------------------------------------------------------------------------------------
FILE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HBK_DATA
/u01/app/oraData/bak/hbk_data.dbf

可知HBK_DATA有一个数据文件组成,把表空间置于备份模式

SQL> alter tablespace HBK_DATA begin backup;

Tablespace altered.

然后使用操作系统命令把数据库文件/u01/app/oraData/bak/hbk_data.dbf复制到备份路径中。
并退出备份模式,回到正常状态

SQL> alter tablespace HBK_DATA end backup;

Tablespace altered.

执行手工归档

SQL> alter system archive log current;

System altered.

这样才算备份单个表空间完成,是不是感觉比较多的步骤。
2.2RMAN备份单个表空间

RMAN> backup tablespace HBK_DATA format '/home/oracle/tbs_hbk_data_%s_%p_%t';

Starting backup at 12-JUL-19
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00005 name=/u01/app/oraData/bak/hbk_data.dbf
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/home/oracle/tbs_hbk_data_4_1_1013422052 tag=TAG20190712T100731 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 12-JUL-19

RMAN>

一条命令搞定手工。

三、备份单个数据文件

知道了数据文件的名称的情况下,使用RMAN备份数据文件也很简单

SQL> select file_name from dba_data_files;

FILE_NAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/u01/app/oraData/orcl/users01.dbf
/u01/app/oraData/orcl/undotbs01.dbf
/u01/app/oraData/orcl/sysaux01.dbf
/u01/app/oraData/orcl/system01.dbf
/u01/app/oraData/bak/hbk_data.dbf
/u01/app/oraData/orcl/CMS69_DATA.dbf
/u01/app/oraData/undoTbs/hbk_undotbs.dbf
/u01/app/oraData/undoTbs/hbk_undotbs_auto

8 rows selected.

比如我们备份/u01/app/oraData/bak/hbk_data.dbf数据文件,其实备份数据文件是备份表空间的一个分支,因为一个表空间有可能由多个数据文件组成,有的情况下,只需要备份单个数据文件即可。

RMAN> backup datafile '/u01/app/oraData/bak/hbk_data.dbf' format '/home/oracle/data_file_hbk_data_%s_%p_%t';

Starting backup at 12-JUL-19
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00005 name=/u01/app/oraData/bak/hbk_data.dbf
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/home/oracle/data_file_hbk_data_5_1_1013422543 tag=TAG20190712T101543 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 12-JUL-19

RMAN> 

可以看到此次备份的数据文件跟上面备份表空间的大小一样,因为HBK_DATA表空间只有一个数据文件/u01/app/oraData/bak/hbk_data.dbf组成

[root@test11g oracle]# ll -h |grep hbk
-rw-r----- 1 oracle oinstall 1.7M 7月  12 10:15 data_file_hbk_data_5_1_1013422543
-rw-r----- 1 oracle oinstall 1.7M 7月  12 10:07 tbs_hbk_data_4_1_1013422052

四、备份归档日志文件

4.1手工备份归档日志文件(不推荐,还是RMAN好用)
使用如下命令查看数据库有哪些归档日志文件,并使用操作系统命令进行复制备份

select thread#,sequence#,name from v$archived_log;

4.2RMAN备份归档日志文件

RMAN> backup archivelog all format '/home/oracle/log_%U';

Starting backup at 12-JUL-19
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set
input archived log thread=1 sequence=66 RECID=1 STAMP=1008772006
input archived log thread=1 sequence=67 RECID=2 STAMP=1008784813
input archived log thread=1 sequence=68 RECID=3 STAMP=1008799278
input archived log thread=1 sequence=69 RECID=4 STAMP=1008824423
input archived log thread=1 sequence=70 RECID=5 STAMP=1008842431
input archived log thread=1 sequence=71 RECID=6 STAMP=1008853214
input archived log thread=1 sequence=72 RECID=7 STAMP=1008885662
input archived log thread=1 sequence=73 RECID=8 STAMP=1008891317
input archived log thread=1 sequence=74 RECID=9 STAMP=1008930478
input archived log thread=1 sequence=75 RECID=10 STAMP=1008939662
.......中间省略了好多
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/home/oracle/log_06u6f72v_1_1 tag=TAG20190712T102630 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:18:53
Finished backup at 12-JUL-19

RMAN>

%U表示将产生唯一文件名

五、备份控制文件

备份控制文件有好多方法,但是我还是推荐RMAN,哈哈
5.1使用sqlplus

SQL> alter database backup controlfile to '/home/oracle/conf.bak';

5.2也可以备份到跟踪文件中,在跟踪文件中将生成创建控制文件的脚本

SQL> alter database backup controlfile to trace;

5.3RMAN备份控制文件(强力推荐)
也可以指定文件位置

RMAN> backup current controlfile;                                       

Starting backup at 12-JUL-19
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=37 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2019_07_12/o1_mf_ncnnf_TAG20190712T113853_glj06l08_.bkp tag=TAG20190712T113853 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:09
Finished backup at 12-JUL-19

RMAN> backup current controlfile format '/home/oracle/conf.bak';

Starting backup at 12-JUL-19
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current control file in backup set
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/home/oracle/conf.bak tag=TAG20190712T114001 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 12-JUL-19

5.4启用控制文件的自动备份,当使用RMAN执行备份的时候,RMAN会自动备份控制文件(也会自动备份SPFILE)

RMAN> configure controlfile autobackup on;

new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored

六、备份spfile

有可以加format指定具体备份文件位置

RMAN> backup spfile ;

Starting backup at 12-JUL-19
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
including current SPFILE in backup set
channel ORA_DISK_1: starting piece 1 at 12-JUL-19
channel ORA_DISK_1: finished piece 1 at 12-JUL-19
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/backupset/2019_07_12/o1_mf_nnsnf_TAG20190712T114424_glj0jrgr_.bkp tag=TAG20190712T114424 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 12-JUL-19

Starting Control File and SPFILE Autobackup at 12-JUL-19
piece handle=/u01/app/oracle/flash_recovery_area/ORCL/autobackup/2019_07_12/o1_mf_s_1013427865_glj0jspl_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 12-JUL-19

后面的控制文件备份是因为设置了configure controlfile autobackup on;