如果要说DBA工作中最重要的职责(没有之一),那无疑就是保证客户数据的安全和完整,可以看到几乎任何一本Oracle DBA的技术书籍一定都会把大篇幅来介绍数据库的备份与恢复,从中也可以看到备份和恢复的重要程度。eygle曾经总结了DBA生存之四大守则的第一条就是:备份重于一切
我们必需知道,系统总是要崩溃的,没有有效的备份只是等哪一天死!我经常开玩笑的说,唯一会使DBA在梦中惊醒的就是,没有有效的备份.

RMAN概览

RMAN是Oracle数据库软件自带的备份/恢复工具。RMAN只能用于9i或更高的版本中。它能够备份整个数据库或数据库部件,如表空间、数据文件、控制文件、归档文件以及Spfile参数文件。通过RMAN的方式无论是要备份还是要恢复,都必须先启动实例并加载数据库。

1
From Wiki:RMAN (Recovery Manager) is a backup and recovery manager supplied for Oracle databases (from version 8) created by the Oracle Corporation. It provides database backup, restore, and recovery capabilities addressing high availability and disaster recovery concerns. Oracle Corporation recommends RMAN as its preferred method for backup and recovery and has written command-line and graphical (via Oracle Enterprise Manager) interfaces for the product.

RMAN之备份

连接数据库
很简单,进入到命令提示符界面:

1
2
3
4
5
[oracle@orcl ~]$ rman target /
Recovery Manager: Release 11.2.0.4.0 - Production on Tue Feb 28 10:51:34 2017
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1455780274)
RMAN>

也可以先启动RMAN,然后再通过CONNECT命令来连接目标数据库:

1
2
3
4
5
6
[oracle@orcl ~]$ rman
Recovery Manager: Release 11.2.0.4.0 - Production on Tue Feb 28 10:52:45 2017
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
RMAN> connect target /
connected to target database: ORCL (DBID=1455780274)
RMAN>

使用RMAN来做各种类型的备份

  1. 整库备份
    整库备份的命令很简单,就是backup database
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    RMAN> backup database;
    Starting backup at 28-FEB-17
    using target database control file instead of recovery catalog
    allocated channel: ORA_DISK_1
    channel ORA_DISK_1: SID=1717 device type=DISK
    channel ORA_DISK_1: starting full datafile backup set
    channel ORA_DISK_1: specifying datafile(s) in backup set
    input datafile file number=00001 name=/u01/data/orcl/system01.dbf
    input datafile file number=00002 name=/u01/data/orcl/sysaux01.dbf
    input datafile file number=00003 name=/u01/data/orcl/undotbs01.dbf
    input datafile file number=00004 name=/u01/data/orcl/users01.dbf
    input datafile file number=00007 name=/u01/data/orcl/dev_paydb.dbf
    input datafile file number=00008 name=/u01/data/orcl/dev_payboxdb.dbf
    input datafile file number=00009 name=/u01/data/orcl/ticket.dbf
    input datafile file number=00005 name=/u01/data/orcl/PAYBOX_FG_PRO.dbf
    input datafile file number=00006 name=/u01/data/orcl/PAYBOX_TEST2SP.dbf
    channel ORA_DISK_1: starting piece 1 at 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/01rtn1md_1_1 tag=TAG20170228T105613 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:40:26
    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 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/02rtn428_1_1 tag=TAG20170228T105613 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
    Finished backup at 28-FEB-17
    RMAN>

整库备份集生成了两个备份片段:一个存储数据文件,另一个存储控制文件和SPFILE(服务器端初始化参数文件),都被保存到Oracle软件的安装目录下,这是因为没有为备份集指定存储路径,默认情况下就会存储到Oracle软件的安装目录中

真正的备份操作,你肯定希望能够指定备份集的存储位置,没问题,最简单的方式是在执行BACKUP命令时,指定FORMAT参数来自定义备份片段的路径和命令规则

1
RMAN> BACKUP DATABASE FORMAT '/u01/data/db_bk/bak_%U';

查看创建的全库备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
RMAN> LIST BACKUP OF DATABASE;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1 Full 4.99G DISK 00:40:17 28-FEB-17
BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20170228T105613
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/01rtn1md_1_1
List of Datafiles in backup set 1
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- --------- ----
1 Full 37643780 28-FEB-17 /u01/data/orcl/system01.dbf
2 Full 37643780 28-FEB-17 /u01/data/orcl/sysaux01.dbf
3 Full 37643780 28-FEB-17 /u01/data/orcl/undotbs01.dbf
4 Full 37643780 28-FEB-17 /u01/data/orcl/users01.dbf
5 Full 37643780 28-FEB-17 /u01/data/orcl/PAYBOX_FG_PRO.dbf
6 Full 37643780 28-FEB-17 /u01/data/orcl/PAYBOX_TEST2SP.dbf
7 Full 37643780 28-FEB-17 /u01/data/orcl/dev_paydb.dbf
8 Full 37643780 28-FEB-17 /u01/data/orcl/dev_payboxdb.dbf
9 Full 37643780 28-FEB-17 /u01/data/orcl/ticket.dbf
RMAN>

  1. 表空间的备份
    rman还可以针对表空间备份
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    RMAN> BACKUP TABLESPACE USERS;
    Starting backup at 28-FEB-17
    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=00004 name=/u01/data/orcl/users01.dbf
    channel ORA_DISK_1: starting piece 1 at 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/03rtn5li_1_1 tag=TAG20170228T120402 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:01:05
    Finished backup at 28-FEB-17
    RMAN>

查看表空间的备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
RMAN> LIST BACKUP OF TABLESPACE USERS;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1 Full 4.99G DISK 00:40:17 28-FEB-17
BP Key: 1 Status: AVAILABLE Compressed: NO Tag: TAG20170228T105613
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/01rtn1md_1_1
List of Datafiles in backup set 1
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- --------- ----
4 Full 37643780 28-FEB-17 /u01/data/orcl/users01.dbf
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
3 Full 415.72M DISK 00:00:59 28-FEB-17
BP Key: 3 Status: AVAILABLE Compressed: NO Tag: TAG20170228T120402
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/03rtn5li_1_1
List of Datafiles in backup set 3
File LV Type Ckp SCN Ckp Time Name
---- -- ---- ---------- --------- ----
4 Full 37647157 28-FEB-17 /u01/data/orcl/users01.dbf
RMAN>

这里发现USER表空间存在2份备份(因为之前做过一次全量备份)

删除指定的BACKUPSET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
RMAN> DELETE BACKUPSET 3;
using channel ORA_DISK_1
List of Backup Pieces
BP Key BS Key Pc# Cp# Status Device Type Piece Name
------- ------- --- --- ----------- ----------- ----------
3 3 1 1 AVAILABLE DISK /u01/app/oracle/product/11.2.0.4/db_1/dbs/03rtn5li_1_1
Do you really want to delete the above objects (enter YES or NO)? YES
deleted backup piece
backup piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/03rtn5li_1_1 RECID=3 STAMP=937137843
Deleted 1 objects
RMAN>

  1. 数据文件的备份
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    SQL> select file#,name from v$datafile;
    FILE# NAME
    ---------- --------------------------------------------------
    1 /u01/data/orcl/system01.dbf
    2 /u01/data/orcl/sysaux01.dbf
    3 /u01/data/orcl/undotbs01.dbf
    4 /u01/data/orcl/users01.dbf
    5 /u01/data/orcl/PAYBOX_FG_PRO.dbf
    6 /u01/data/orcl/PAYBOX_TEST2SP.dbf
    7 /u01/data/orcl/dev_paydb.dbf
    8 /u01/data/orcl/dev_payboxdb.dbf
    9 /u01/data/orcl/ticket.dbf
    9 rows selected.
    SQL>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
RMAN> BACKUP DATAFILE '/u01/data/orcl/users01.dbf';
Starting backup at 28-FEB-17
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=00004 name=/u01/data/orcl/users01.dbf
channel ORA_DISK_1: starting piece 1 at 28-FEB-17
channel ORA_DISK_1: finished piece 1 at 28-FEB-17
piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/04rtn83l_1_1 tag=TAG20170228T124540 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
Finished backup at 28-FEB-17
RMAN>
  1. 控制文件备份
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    RMAN> BACKUP CURRENT CONTROLFILE;
    Starting backup at 28-FEB-17
    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 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/05rtn873_1_1 tag=TAG20170228T124731 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
    Finished backup at 28-FEB-17
    RMAN>

查看控制文件备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
RMAN> LIST BACKUP OF CONTROLFILE;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
2 Full 9.39M DISK 00:00:05 28-FEB-17
BP Key: 2 Status: AVAILABLE Compressed: NO Tag: TAG20170228T105613
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/02rtn428_1_1
Control File Included: Ckp SCN: 37645877 Ckp time: 28-FEB-17
BS Key Type LV Size Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
5 Full 9.36M DISK 00:00:01 28-FEB-17
BP Key: 5 Status: AVAILABLE Compressed: NO Tag: TAG20170228T124731
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/05rtn873_1_1
Control File Included: Ckp SCN: 37648397 Ckp time: 28-FEB-17
RMAN>

  1. 归档文件的备份
    归档日志对于数据库介质恢复相当关键,只要拥有相应的归档日志文件,就能确保我们将数据库恢复到备份之后的任意时刻。在RMAN中备份归档日志有以下两种方式:
  • 利用BACKUP ARCHIVELOG命令备份
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    RMAN> BACKUP ARCHIVELOG ALL;
    Starting backup at 28-FEB-17
    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=868 RECID=1 STAMP=937050974
    input archived log thread=1 sequence=871 RECID=2 STAMP=937050978
    input archived log thread=1 sequence=872 RECID=3 STAMP=937050981
    input archived log thread=1 sequence=873 RECID=4 STAMP=937052764
    input archived log thread=1 sequence=874 RECID=5 STAMP=937052771
    input archived log thread=1 sequence=875 RECID=6 STAMP=937073029
    input archived log thread=1 sequence=876 RECID=7 STAMP=937079244
    input archived log thread=1 sequence=877 RECID=8 STAMP=937087288
    input archived log thread=1 sequence=878 RECID=9 STAMP=937108830
    input archived log thread=1 sequence=879 RECID=10 STAMP=937124801
    input archived log thread=1 sequence=880 RECID=11 STAMP=937140632
    channel ORA_DISK_1: starting piece 1 at 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/06rtn8cp_1_1 tag=TAG20170228T125032 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
    Finished backup at 28-FEB-17
    RMAN>

BACKUP ARCHIVELOG命令比较灵活,ALL是指备份当前所有可访问到的归档文件,你还可以通过UNTIL、SCN、TIME、SEQUENCE等参数灵活指定要备份的归档区间。

  • 执行BACKUP命令时指定PLUS ARCHIVELOG子句
    在备份控制文件之前首先对所有归档文件进行备份,BACKUP…PLUS ARCHIVELOG命令在备份过程中会依次执行下列步骤:
  1. 运行ALTER SYSTEM ARCHIVE LOG CURRENT语句对当前 Redolog 进行归档
  2. 执行BACKUP ARCHIVELOG ALL命令备份所有已归档日志
  3. 执行BACKUP命令对指定项进行备份
  4. 再次运行ALTER SYSTEM ARCHIVE LOG CURRENT对当前 Redolog 归档
  5. 对新生成的尚未备份的归档文件进行备份
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    RMAN> BACKUP CURRENT CONTROLFILE PLUS ARCHIVELOG;
    Starting backup at 28-FEB-17
    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=868 RECID=1 STAMP=937050974
    input archived log thread=1 sequence=871 RECID=2 STAMP=937050978
    input archived log thread=1 sequence=872 RECID=3 STAMP=937050981
    input archived log thread=1 sequence=873 RECID=4 STAMP=937052764
    input archived log thread=1 sequence=874 RECID=5 STAMP=937052771
    input archived log thread=1 sequence=875 RECID=6 STAMP=937073029
    input archived log thread=1 sequence=876 RECID=7 STAMP=937079244
    input archived log thread=1 sequence=877 RECID=8 STAMP=937087288
    input archived log thread=1 sequence=878 RECID=9 STAMP=937108830
    input archived log thread=1 sequence=879 RECID=10 STAMP=937124801
    input archived log thread=1 sequence=880 RECID=11 STAMP=937140632
    input archived log thread=1 sequence=881 RECID=12 STAMP=937140870
    channel ORA_DISK_1: starting piece 1 at 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/07rtn8k6_1_1 tag=TAG20170228T125430 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
    Finished backup at 28-FEB-17
    Starting backup at 28-FEB-17
    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 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/08rtn8ka_1_1 tag=TAG20170228T125434 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
    Finished backup at 28-FEB-17
    Starting backup at 28-FEB-17
    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=882 RECID=13 STAMP=937140878
    channel ORA_DISK_1: starting piece 1 at 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/09rtn8ke_1_1 tag=TAG20170228T125438 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
    Finished backup at 28-FEB-17
    RMAN>

完成备份之后,可以通过下列命令查看已备份的归档日志片段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
RMAN> LIST BACKUP OF ARCHIVELOG ALL;
List of Backup Sets
===================
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
6 319.98M DISK 00:00:07 28-FEB-17
BP Key: 6 Status: AVAILABLE Compressed: NO Tag: TAG20170228T125032
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/06rtn8cp_1_1
List of Archived Logs in backup set 6
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 868 37498935 24-FEB-17 37505736 24-FEB-17
1 871 37537046 25-FEB-17 37548296 25-FEB-17
1 872 37548296 25-FEB-17 37574822 27-FEB-17
1 873 37574822 27-FEB-17 37577191 27-FEB-17
1 874 37577191 27-FEB-17 37577232 27-FEB-17
1 875 37577232 27-FEB-17 37592150 27-FEB-17
1 876 37592150 27-FEB-17 37596510 27-FEB-17
1 877 37596510 27-FEB-17 37607071 27-FEB-17
1 878 37607071 27-FEB-17 37631135 28-FEB-17
1 879 37631135 28-FEB-17 37638288 28-FEB-17
1 880 37638288 28-FEB-17 37648486 28-FEB-17
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
7 319.98M DISK 00:00:03 28-FEB-17
BP Key: 7 Status: AVAILABLE Compressed: NO Tag: TAG20170228T125430
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/07rtn8k6_1_1
List of Archived Logs in backup set 7
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 868 37498935 24-FEB-17 37505736 24-FEB-17
1 871 37537046 25-FEB-17 37548296 25-FEB-17
1 872 37548296 25-FEB-17 37574822 27-FEB-17
1 873 37574822 27-FEB-17 37577191 27-FEB-17
1 874 37577191 27-FEB-17 37577232 27-FEB-17
1 875 37577232 27-FEB-17 37592150 27-FEB-17
1 876 37592150 27-FEB-17 37596510 27-FEB-17
1 877 37596510 27-FEB-17 37607071 27-FEB-17
1 878 37607071 27-FEB-17 37631135 28-FEB-17
1 879 37631135 28-FEB-17 37638288 28-FEB-17
1 880 37638288 28-FEB-17 37648486 28-FEB-17
1 881 37648486 28-FEB-17 37648591 28-FEB-17
BS Key Size Device Type Elapsed Time Completion Time
------- ---------- ----------- ------------ ---------------
9 2.50K DISK 00:00:00 28-FEB-17
BP Key: 9 Status: AVAILABLE Compressed: NO Tag: TAG20170228T125438
Piece Name: /u01/app/oracle/product/11.2.0.4/db_1/dbs/09rtn8ke_1_1
List of Archived Logs in backup set 9
Thrd Seq Low SCN Low Time Next SCN Next Time
---- ------- ---------- --------- ---------- ---------
1 882 37648591 28-FEB-17 37648602 28-FEB-17
RMAN>

  1. 初始化参数文件的备份

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    RMAN> BACKUP SPFILE;
    Starting backup at 28-FEB-17
    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 28-FEB-17
    channel ORA_DISK_1: finished piece 1 at 28-FEB-17
    piece handle=/u01/app/oracle/product/11.2.0.4/db_1/dbs/0artn91l_1_1 tag=TAG20170228T130141 comment=NONE
    channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
    Finished backup at 28-FEB-17
    RMAN>
  2. 对备份集备份

  • 备份所有备份集

    1
    RMAN> BACKUP BACKUPSET ALL;
  • 备份指定的备份集

    1
    2
    #n=备份集ID,可以同时指定多个,相互间以逗号分隔即可
    RMAN> BACKUP BACKUPSET n;

参考:http://blog.csdn.net/pan_tian/article/details/46766985


本文出自”Jack Wang Blog”:http://www.yfshare.vip/2017/02/28/Oracle数据库备份与恢复-RMAN备份/