Menu Close

使用恢复目录管理Oracle备份

1. 什么是恢复目录

恢复目录(RMAN Recovery Catalog)存储RMAN备份恢复相关信息的数据库(在物理形式上可以对应成Oracle中的一个SCHEMA)。当没有恢复目录时,RMAN相关的备份信息,比如归档文件路径、备份集路径等均存储在目标数据库的控制文件中,不过考虑到控制文件并不能无限增长,而且控制文件也不仅仅是用来存储与备份相关的信息,因此RMAN也有一个专门的备份信息存储地,这就是恢复目录了,即:目录数据库。当待备份的数据库注册到恢复目录之后,RMAN相关的信息除了保存在控制文件中外(控制文件实际上只保存一部分),更加详细的信息就都被存储在恢复目录中。

2. 使用恢复目录的优势

  • 比控制文件存储更多额外的元数据历史记录
  • 允许您使用RMAN存储脚本
  • 允许您为所有注册目标创建自定义报告
  • 允许您使用备份命令中的“永久保留”子句
  • 允许您列出在某一特定的时间内存储于目标数据库中的数据文件和表空间的元数据
  • 简化了用于保留RMAN存储库元数据的控制文件丢失后的还原和恢复

3. 如何创建恢复目录?

  1. 配置恢复目录数据库。
  2. 创建表空间以容纳对象。
  3. 通过recovery_catalog_owner创建恢复目录所有者角色。
  4. 创建恢复目录。

4. 示例

4.1. 创建一个PDB数据库作为目录数据库

SQL> show pdbs;

    CON_ID CON_NAME   OPEN MODE  RESTRICTED
---------- ----------- ---------- ----------
  2        PDB$SEED     READ ONLY  NO
  3        EMCCDB       READ WRITE NO

SQL> create pluggable database catpdb
     admin user pdbadmin identified by "Hz571!xy00yz" roles=(dba)
     file_name_convert=('/oradata/CATALOGDB/pdbseed','/oradata/CATALOGDB/catpdb');

Pluggable database created.

SQL> show pdbs;

    CON_ID CON_NAME      OPEN MODE  RESTRICTED
---------- ----------- ---------- ----------
   2        PDB$SEED     READ ONLY  NO
   3        EMCCDB       READ WRITE NO
   4        CATPDB       MOUNTED
SQL>
SQL> alter pluggable database catpdb open;

Pluggable database altered.

SQL> alter pluggable database catpdb save state;

Pluggable database altered.

SQL> show pdbs;

    CON_ID CON_NAME       OPEN MODE  RESTRICTED
---------- -------------  ---------- ----------
     2      PDB$SEED      READ ONLY  NO
     3      EMCCDB        READ WRITE NO
     4      CATPDB        READ WRITE NO

4.2. 创建表空间与用户用

SQL> alter session set container=catpdb;

Session altered.

SQL> create tablespace tbs_cat datafile
    '/oradata/CATALOGDB/catpdb/tbs_cat01.dbf' size 1g autoextend on next 500m maxsize 30g,
    '/oradata/CATALOGDB/catpdb/tbs_cat02.dbf' size 1g autoextend on next 500m maxsize 30g,
    '/oradata/CATALOGDB/catpdb/tbs_cat03.dbf' size 1g autoextend on next 500m maxsize 30g;

Tablespace created.

SQL> create user rco identified by "Hz571!xy00yz"
     default tablespace tbs_cat
     quota unlimited on tbs_cat;

User created.

SQL> grant recovery_catalog_owner to rco;

Grant succeeded.

SQL> exit;

4.3. 创建恢复目录

启动 RMAN 并运行以下 RMAN 命令以作为 rco 连接到恢复目录数据库并创建恢复目录。

~]$ rman catalog 'rco/"Hz571!xy00yz"'@127.0.0.1:1521/catpdb

Recovery Manager: Release 19.0.0.0.0 - Production on Tue Sep 21 22:51:59 2021
Version 19.3.0.0.0

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

connected to recovery catalog database

RMAN> create catalog;

recovery catalog created

5. RMAN连接恢复目录

前面已经创建好了恢复目录,在我们使用RMAN进行备份时,要连接到恢复目录才能够进行数据备份。连接到恢复目录的方式也是在命令行上就可以直接操作。

RMAN连接恢复目录语法:

rman target user/password@servicename catalog user/password@serviecename

语法解析:

  • target指定的是目标数据库(既是要进行备份的数据库)。
  • catalog后面指定的是恢复目录的数据库。

5.1. 将目标数据库注册到恢复目录

将目标数据库(即要执行备份的数据库)注册到目录数据库有两种连接方式,

  • 方式一、从恢复目录连接目标数据库
    首先在目标数据让创建一个具有sysbackup权限的用户,用于备份整个CDB以及其他所有PDB数据库。
SQL> show con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL>
SQL> create user c##backup identified by "Hz571!xy00yz";

User created.

SQL> grant sysbackup,create session to c##backup;

Grant succeeded.

备份整个CDB数据库及其下面的所有PDB类似于非CDB数据库方法相同,使用具有SYSDBA或SYSBACKUP权限用户连接到CDB的root环境下面,然后执行 backup database 命令即可完成整个CDB的备份。

~]$  rman target '"c##backup/Hz571!xy00yz@172.16.xxx.xxx:1521/xxxdb as sysbackup"'  catalog 'rco/"Hz571!xy00yz"'@172.16.xxx.xxx:1521/catpdb

Recovery Manager: Release 19.0.0.0.0 - Production on Tue Sep 21 23:42:53 2021
Version 19.3.0.0.0

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

connected to target database: XXXDB (DBID=4267076118)
connected to recovery catalog database
  • 方式二、从目标数据库连接恢复目录
~]$ rman target / catalog 'rco/"Hz571!xy00yz"'@172.16.xxx.xxx:1521/catpdb

Recovery Manager: Release 19.0.0.0.0 - Production on Tue Sep 21 23:01:37 2021
Version 19.10.0.0.0

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

connected to target database: xxxDB (DBID=4267076118)
connected to recovery catalog database
PL/SQL package RCO.DBMS_RCVCAT version 19.03.00.00. in RCVCAT database is not current
PL/SQL package RCO.DBMS_RCVMAN version 19.03.00.00 in RCVCAT database is not current

5.2. 在恢复目录上注册目标数据库

RMAN> register database;

database registered in recovery catalog
starting full resync of recovery catalog
full resync complete

5.3. 同步备份数据

在同时连接到目标db和catalog执行rman命令时,不论是局部同步还是全局同步,同步操作都能够由rman执行相关命令(backup、configure)时自动判断、自动发起。完全同步也可以通过 resync catalog 手动发起,一般没有必要这样做,但以下3种情况除外

  1. 使用rman执行了需要同步的操作(比如configure命令备份某个表空间),但是在执行此类命令时由于种种原因rman没有连接catalog,只连接了目标db
  2. dba备份db的频率过低,很久不用一次rman,这样,由于control_file_record_keep_time参数的设置,catalog可能会遗忘某些归档日志信息
  3. 在db物理结构发生变化之后,比如:添加数据文件,新建表空间等
RMAN> resync catalog;

starting full resync of recovery catalog
full resync complete

6. 备份数据库

6.1. 创建一个脚本

create script bak_0{
  crosscheck backup;
  crosscheck archivelog all;
  allocate channel c1 type disk maxpiecesize = 200000m;
  sql 'alter system archive log current';
  backup incremental level 0 skip inaccessible database tag 'inc_0' format '/oralce/ora_rman/bak_0%p_%s_%t.bak' filesperset 4
  include current controlfile plus archivelog tag 'arc_0' format '/oracle/ora_rman/bak_arc%U.bak' filesperset 2
  delete all input;
  delete obsolete;
  release channel c1;
}

6.2. 备份脚本内容

~]# cat dbfullbak.rman
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '+DATAC1';
CONFIGURE DEVICE TYPE DISK PARALLELISM 12 BACKUP TYPE TO BACKUPSET;
run {
backup full  tag 'db_full_bak' format '+DATAC1' database;
sql 'alter system archive log current';
backup format '+DATAC1' archivelog all ;
delete noprompt  obsolete;
delete noprompt expired backup;
}

6.3. 将脚本内容存储到恢复目录

RMAN> create script backup_exadata from file '/dbData/OracleBack/rmanbak/dbfullbak.rman';

script commands will be loaded from file /dbData/OracleBack/rmanbak/dbfullbak.rman
created script backup_exadata

6.4. 查看恢复目录中的脚本名称

RMAN> list script names;

List of Stored Scripts in Recovery Catalog

    Scripts of Target Database XXXDB

       Script Name
       Description
       -----------------------------------------------------------------------
       backup_exadata

6.5. 查看脚本具体内容

RMAN> print script backup_exadata;

printing stored script: backup_exadata
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '+DATAC1';
CONFIGURE DEVICE TYPE DISK PARALLELISM 12 BACKUP TYPE TO BACKUPSET;
run {
backup full  tag 'db_full_bak' format '+DATAC1' database;
sql 'alter system archive log current';
backup format '+DATAC1' archivelog all ;
delete noprompt  obsolete;
delete noprompt expired backup;
}

6.6. 运行脚本

使用rman连接目标库和catalog后,执行run {execute script ...}

RMAN> run {execute script backup_exadata;}

6.7. 删除脚本

RMAN> DELETE SCRIPT backup_exadata;

deleted script: backup_exadata

参考:Oracle官方文档:
https://docs.oracle.com/cd/B12037_01/server.101/b10770/toc.htm
https://www.cnblogs.com/yhq1314/p/11040852.html