---
title: "RMAN备份基本用法"
author: "Openhearted"
date: "2010-07-20T17:53:55Z"
tags: []
categories: ["数据库"]
slug: "rman"
draft: false
lastmod: "2010-07-20T17:53:55Z"
id: 717204
url: "https://openhearted.iteye.com/blog/717204"
---

一. 创建RMAN CATALOG  
      RMAN 可以之用控制文件来存储RMAN的备份metadata, 也可以配置CATALOG来存储RMAN的备份信息. 一般来说最好在另一数据库上配置CATALOG来管理RMAN备份信息,这样当所有的控制文件都无效的时候还可以保证用CATALOG来重建控制文件和数据库.  
    1).SQL&gt; create tablespace rmantbs datafile '/data/oradata/orcl/rmantbs01.dbf' size 50M  
                     autoextend on next 5M maxsize 200M  
                     extent management local  
                     segment space management auto;

     2).SQL&gt; create user rman identified by oracle  
                      default tablespace rmantbs  
                      temporary tablespace temp  
                      quota unlimited on rmantbs;

     3).SQL&gt; grant create session, recovery\_catalog\_owner to rman;

     4).$ rman catalog rman/oracle  
           RMAN&gt; create catalog tablespace rmantbs;  
           RMAN&gt; connect target /  
           RMAN&gt; register database;

二. 配置RMAN  
RMAN&gt; show all;  
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 BACKUP TYPE TO BACKUPSET 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 'BZIP2'; # default  
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default  
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u01/app/oracle/product/11.1.0/db\_1/dbs/snapcf\_orcl.f'; # default

1\. 配置保留策略retention policy  
    RMAN有两种保留策略:DEDUNDANCY(默认值为1)和 RECOVERY WINDOW  
    1.1) 配置保留2份冗余备份  
RMAN&gt; configure retention policy to redundancy 2;  
new RMAN configuration parameters:  
CONFIGURE RETENTION POLICY TO REDUNDANCY 2;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

   1.2) 配置保留策略为7天  
     RMAN&gt; configure retention policy to recovery window of 7 days;  
old RMAN configuration parameters:  
CONFIGURE RETENTION POLICY TO REDUNDANCY 2;  
new RMAN configuration parameters:  
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

2\. 配置优化的备份  
   RMAN&gt; configure backup optimization on;  
new RMAN configuration parameters:  
CONFIGURE BACKUP OPTIMIZATION ON;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

3\. 配置默认的设备类型  
    默认的设备类型为DISK硬盘,配置默认的设备类型为TAPE磁带  
    RMAN&gt; configure default device type to sbt;  
new RMAN configuration parameters:  
CONFIGURE DEFAULT DEVICE TYPE TO 'SBT\_TAPE';  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

4\. 配置自动控制文件和SPFILE备份  
   RMAN&gt; configure controlfile autobackup on;  
new RMAN configuration parameters:  
CONFIGURE CONTROLFILE AUTOBACKUP ON;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

5\. 配置自动控制文件和SPFILE备份的格式  
    默认是备份到DB\_RECOVERY\_FILE\_DEST/$ORACLE\_SID/autobackup/$date目录,格式为'%F'  
    注: 格式必须包含'%F'  
RMAN&gt; configure controlfile autobackup format for device type disk to '/u01/app/oracle/admin/orcl/controlfile/ctl\_%F';  
new RMAN configuration parameters:  
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/app/oracle/admin/orcl/controlfile/ctl\_%F';  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

6\. 配置默认产生备份集还是拷贝COPY  
   6.1 配置备份类型为COPY  
RMAN&gt; configure device type disk backup type to copy;  
old RMAN configuration parameters:  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO BACKUPSET PARALLELISM 1;  
new RMAN configuration parameters:  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COPY PARALLELISM 1;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

6.2 配置并行度PARALLELISM  
RMAN&gt; configure device type disk backup type to backupset parallelism 2;  
old RMAN configuration parameters:  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COPY PARALLELISM 1;  
new RMAN configuration parameters:  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO BACKUPSET PARALLELISM 2;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

RMAN&gt; configure device type sbt\_tape backup type to backupset parallelism 2;  
new RMAN configuration parameters:  
CONFIGURE DEVICE TYPE 'SBT\_TAPE' BACKUP TYPE TO BACKUPSET PARALLELISM 2;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete  
注: 如果配置磁带设备的PARALLELISM,必须配置磁带的个数等于PARALLELISM的个数,否则报错.

6.3 配置默认压缩备份(注如果用压缩备份必须配置WALLET并OPEN)  
RMAN&gt; configure device type disk backup type to compressed backupset;  
old RMAN configuration parameters:  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO BACKUPSET PARALLELISM 2;  
new RMAN configuration parameters:  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COMPRESSED BACKUPSET PARALLELISM 2;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

7\. 配置渠道CHANNEL  
RMAN&gt; configure channel device type disk format '/bakup/rman/orcl/%d\_%s\_%p\_%t';  
new RMAN configuration parameters:  
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '/bakup/rman/orcl/%d\_%s\_%p\_%t';  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

8\. 配置加密和加密算法  
    RMAN 加密可以采用TRANSPARANT Data encryption(TDE)加密和密码加密两种方法. TDE的算法有:AES128(默认值), AES192, AES256等. 备份和恢复时都需要WALLET打开状态.  
RMAN&gt; configure encryption for database on;  
new RMAN configuration parameters:  
CONFIGURE ENCRYPTION FOR DATABASE ON;  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

RMAN&gt; configure encryption algorithm 'AES256';  
new RMAN configuration parameters:  
CONFIGURE ENCRYPTION ALGORITHM 'AES256';  
new RMAN configuration parameters are successfully stored  
starting full resync of recovery catalog  
full resync complete

参考实例: [http://users.telenet.be/oraguy.be/encryption1.htm](http://users.telenet.be/oraguy.be/encryption1.htm)

查看配置结果:  
RMAN&gt; show all;  
RMAN configuration parameters for database with db\_unique\_name ORCL are:  
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;  
CONFIGURE BACKUP OPTIMIZATION ON;  
CONFIGURE DEFAULT DEVICE TYPE TO 'SBT\_TAPE';  
CONFIGURE CONTROLFILE AUTOBACKUP ON;  
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/u01/app/oracle/admin/orcl/controlfile/ctl\_%F';  
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE SBT\_TAPE TO '%F'; # default  
CONFIGURE DEVICE TYPE DISK BACKUP TYPE TO COMPRESSED BACKUPSET PARALLELISM 2;  
CONFIGURE DEVICE TYPE 'SBT\_TAPE' BACKUP TYPE TO BACKUPSET PARALLELISM 2;  
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default  
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE SBT\_TAPE TO 1; # default  
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default  
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE SBT\_TAPE TO 1; # default  
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT   '/bakup/rman/orcl/%d\_%s\_%p\_%t';  
CONFIGURE MAXSETSIZE TO UNLIMITED; # default  
CONFIGURE ENCRYPTION FOR DATABASE ON;  
CONFIGURE ENCRYPTION ALGORITHM 'AES256';  
CONFIGURE COMPRESSION ALGORITHM 'BZIP2'; # default  
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default  
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u01/app/oracle/product/11.1.0/db\_1/dbs/snapcf\_orcl.f'; # default

三. 创建存储的备份脚本  
1\. 基本0的增量备份  
create script. b\_whole\_inc0{  
backup  
incremental level 0 database  
format '/bakup/rman/backup/orcl/db0\_%d\_%s\_%p\_%t'  
tag = 'WHOLE\_INC0';  
}

2\. 基本1的增量备份  
create script. b\_whole\_inc1{  
backup  
incremental level 1 cumulative database  
format '/bakup/rman/backup/orcl/db1\_%d\_%s\_%p\_%t'  
tag='WHOLE\_INC1';  
}

3\. 基本2的增量备份  
create script. b\_whole\_inc2{  
backup  
incremental level 2 cumulative database  
format '/bakup/rman/backup/orcl/db2\_%d\_%s\_%p\_%t'  
tag='WHOLE\_INC2';  
}

4\. 归档当前的重做日志组  
create script. archive\_log\_current{  
sql "ALTER SYSTEM ARCHIVE LOG CURRENT";  
}

5\. 备份前两天的归档日志  
create script. b\_archive\_2days{  
execute script. archive\_log\_current;  
backup archivelog from time 'SYSDATE-2'  
format '/bakup/rman/backup/orcl/ar\_%d\_%s\_%p\_%t';  
}

6\. 备份大前天的归档日志并删除  
create script. archive\_delete3{  
execute script. archive\_log\_current;  
backup archivelog from time 'SYSDATE-3' until time 'SYSDATE-2'  
format '/bakup/rman/backup/orcl/ar\_%d\_%s\_%p\_%t'  
delete input;  
}

7\. 备份类型为COPY  
run{  
    allocate channel d1 type disk;  
    copy  
    datafile 1 to '/bakup/rman/backup/orcl/system01.dbf.bak',  
    datafile 2 to '/bakup/rman/backup/orcl/undotbs01.dbf.bak',  
    datafile 3 to '/bakup/rman/backup/orcl/sysaux01.dbf.bak',  
    datafile 4 to '/bakup/rman/backup/orcl/users01.dbf.bak',  
    datafile 5 to '/bakup/rman/backup/orcl/example01.dbf.bak',  
    datafile 6 to '/bakup/rman/backup/orcl/rmantbs01.dbf.bak',  
    current controlfile to '/bakup/rman/backup/orcl/control\_bakup.ctl';  
}

or

run{  
      backup as copy database;  
}

8\. 备份增量基本为0的数据库备份和所有的归档日志  
run{  
    allocate channel c1 type disk;  
    backup incremental level 0 database  
    format '/bakup/rman/backup/orcl/db\_%d\_%s\_%p\_%t'  
    tag = 'WHOLE\_INC0'  
    archivelog all  
    format '/bakup/rman/backup/orcl/ar\_%d\_%s\_%p\_%t'  
    tag 'BACKUP\_ARCHIVELOG\_ALL';  
}

本文来自CSDN博客，转载请标明出处：[http://blog.csdn.net/sopost/archive/2009/10/27/4734395.aspx](http://blog.csdn.net/sopost/archive/2009/10/27/4734395.aspx)