ORACLE RMAN 增量备份完整恢复测试

 

ORACLE RMAN 增量备份完整恢复测试

 

创建测试表空间:

SQL> create tablespace tablespace1  

  2  datafile '/db/app/oracle/oradata/orcl/tablespace1.dbf'

  3  size 10m;

Tablespace created.

SQL>

 

创建测试用户并指定为默认表空间:

SQL> create user user1 identified by oracle default tablespace tablespace1;

User created.

SQL>

 

设置该测试用户权限:

SQL> grant create session,create table,unlimited tablespace to user1;

Grant succeeded.

SQL>

 

用新创建的用户登录:

SQL> conn user1/

Enter password:

Connected.

SQL>

 

建表并写入数据:

SQL> create table table1 (id int,name varchar2(30));

Table created.

 

SQL> insert into table1 values(0001,'Tian');

1 row created.

 

SQL> insert into table1 values(0002,'zhong');

1 row created.

 

SQL> select * from table1;

        ID NAME

---------- ------------------------------

         1 Tian

         2 zhong

SQL> commit;

Commit complete.

 

配置RMAN环境变量

切换sys用户作0级备份:

 

利用configure命令配置RMAN环境:

RMAN>CONFIGURE CONTROLFILE AUTOBACKUP ON;#开启归档日志自动备份

RMAN>CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/db/bak/ctf/%d_ctf_bak_%F';#指定归档日志备份位置

RMAN>CONFIGURE DEVICE TYPE DISK PARALLELISM 2 BACKUP TYPE TO COMPRESSED BACKUPSET;#采用并行度为2且压缩备份集的方式备份到磁盘。

RMAN>CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT   '/db/bak/disk1/%d_datafile_bak_%U';

RMAN>CONFIGURE CHANNEL 2 DEVICE TYPE DISK FORMAT   '/db/bak/disk2/%d_datafile_bak_%U';

 

RMAN> 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 ON;

CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '/db/bak/ctf/%d_ctf_bak_%F';

CONFIGURE DEVICE TYPE DISK PARALLELISM 2 BACKUP TYPE TO COMPRESSED BACKUPSET;

CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default

CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default

CONFIGURE CHANNEL DEVICE TYPE DISK MAXPIECESIZE 2 G;

CONFIGURE CHANNEL 1 DEVICE TYPE DISK FORMAT   '/db/bak/disk1/%d_datafile_bak_%U';

CONFIGURE CHANNEL 2 DEVICE TYPE DISK FORMAT   '/db/bak/disk2/%d_datafile_bak_%U';

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 '/db/app/oracle/product/11.2.0/dbhome_1/dbs/snapcf_orcl.f'; # default

 

备份全库(包括归档日志及启动参数文件等):

RMAN>

RMAN> backup incremental level 0 database plus archivelog;

 

Starting backup at 06-MAY-11

current log archived

using channel ORA_DISK_1

….

….(省略过程)

提示备份成功。可以再通过RMAN>list backup;查看已有备份确认(注意LV 字段全为0,为0级增量备份)。

继续往测试表中插入新数据:

SQL> insert into table1 values(3,'wang');

1 row created.

SQL> insert into table1 values(4,'newtest');

1 row created.

 

SQL> select * from table1;

        ID NAME

---------- ------------------------------

         1 Tian

         2 zhong

         3 wang

         4 newtest

SQL> commit;

Commit complete.插入成功,并提交。

 

1级增量备份全库:

RMAN> backup incremental level 1 database plus archivelog;

省略备份过程细节。

 

提示备份成功。可以再通过RMAN>list backup;查看已有备份确认(此时LV字段包含0和1,表示目前备份中已包含有0级和1级增量备份)。

 

接下来开始恢复:


 

===============================

恢复

===============================

模拟全库数据文件,spfile文件及控制文件的丢失(物理删除):

模拟方式,操作系统层面rm命令强行删除,包括oradata目录下的数据文件和$ORACLE_HOME/dbs目录下的spfile等启动参数文件。

 

//注意:在操作系统层面rm删除oracle相关文件时,可以选择oracle open状态删除,也可以选择先将oracle正常shutdown后再删除。但后者在recover过程需要设置前滚until点,可以通过设置until SCN=故障发生时前一个正常恢复点。(SCN点在recover过程中会有提示)

 

此处为正常shutdown immediate后的恢复。

SQL> startup

ORA-01078: failure in processing system parameters

LRM-00109: could not open parameter file '/db/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora'

SQL>此处可见已经找不到启动参数文件

 

首先恢复spfile文件:

RMAN> connect target /

connected to target database (not started)

 

RMAN> set dbid 1274918132#dbid号可通过查看备份文件得到或者select dbid from v$database

executing command: SET DBID

 

RMAN> startup nomount;#RMAN可从默认的Oracle配置参数中启动到nomount状态

startup failed: ORA-01078: failure in processing system parameters

LRM-00109: could not open parameter file '/db/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora'

 

starting Oracle instance without parameter file for retrieval of spfile

Oracle instance started

Total System Global Area     159019008 bytes

Fixed Size                     1335192 bytes

Variable Size                 75497576 bytes

Database Buffers              79691776 bytes

Redo Buffers                   2494464 bytes

 

RMAN> restore spfile from '/db/bak/ctf/ORCL_ctf_bak_c-1274918132-20110506-02';#控制文件备份地址

Starting restore at 06-MAY-11

using channel ORA_DISK_1

 

channel ORA_DISK_1: restoring spfile from AUTOBACKUP /db/bak/ctf/ORCL_ctf_bak_c-1274918132-20110506-02

channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete

Finished restore at 06-MAY-11

恢复参数文件成功!!!!

关闭数据库实例,重新从刚才已恢复的参数文件启动数据库实例。

RMAN> shutdown immediate;

Oracle instance shut down

 

RMAN> startup nomount;

 

connected to target database (not started)

Oracle instance started

 

Total System Global Area     422670336 bytes

Fixed Size                     1336960 bytes

Variable Size                322963840 bytes

Database Buffers              92274688 bytes

Redo Buffers                   6094848 bytes

以上参数可见原先SGA的配置和默认RMAN启动调用的参数是不同滴。。。。

 

 

接下来恢复控制文件

RMAN> restore controlfile from '/db/bak/ctf/ORCL_ctf_bak_c-1274918132-20110506-02';#备份控制文件路径

 

Starting restore at 06-MAY-11

using channel ORA_DISK_1

 

channel ORA_DISK_1: restoring control file

channel ORA_DISK_1: restore complete, elapsed time: 00:00:01

output file name=/db/app/oracle/oradata/orcl/control01.ctl

output file name=/db/app/oracle/flash_recovery_area/orcl/control02.ctl

Finished restore at 06-MAY-11

控制文件恢复成功~!!!!

 

此时就可以启动数据库到mount状态

RMAN> alter database mount;

database mounted

 

继续恢复数据~

RMAN> restore database;

RMAN> recover database;

RMAN>alter database open resetlogs;

以上三步无报错,提示成功~


 

restore database 与recover database 区别:

restore 只是用备份来还原,recover是用archivelog或者online log 举例说明:假设我时间点A,做了个备份,时间点B数据库挂了restore database ;// 这个操作利用时间点A做的备份来还原,返回到时间点A recover database ;//这个操作利用archivelog and online log做recover,从时间点A,推进到时间点B restore 是轉儲也是還原被損壞文件(RMAN經常用)recover 是恢復通過redo log & archive log恢復 restore 还原recover 恢复

 

 

 

 

最后检查测试的数据一致性:

 

SQL> select * from user1.table1;

 

        ID NAME

---------- ------------------------------

         1 Tian

         2 zhong

         3 wang

         4 newtest

 

SQL>

 

数据恢复成功!!!!

 

 

Bruce

2011.05.06

 

测试环境:

Linux 5.4-32位,Oracle 11.2

 

 

 

 

 

 

 

 

 

 

 

 

公告:CSDN下载频道即将全面改版

RMAN备份

分类:Oracle DBA Fundamentals II2011-06-29 09:590人阅读评论(0)收藏举报

sql*plus与操作系统命令行切换
linux:用!符号
window:sql>到c:>用host命令,c:>到sql>用exit。

RMAN备份模式:全备、增量备份、冷备、热备。

RMAN备份的文件类型:表空间、数据文件、控制文件、参数文件、归档日志文件、备份集。

RMAN备份的目的地:磁盘、磁带、闪存恢复区。

RMAN通过内部算法去检测热备中的改变块,这个由RMAN自动完成。

备份的约束条件
1.数据库必须是mount或者open;
2.不对redo log file备份

备份类型:image copy、backup set。

backup set的特点:
一个backup set包含多个文件,每个文件称为backup piece;
backup set可以备份到磁盘或磁带上;(image copy不能备份到磁带)
从backup set提取文件需要还原操作;
数据文件backup set支持增量备份和全备;(归档文件的backup set不支持)
backup set不包含从未用过的块。

backup piece:
backup set里的一个文件,大小受限与操作系统;
backup piece能包括多个数据文件(datafile)的数据块。

列出备份信息,显示备份集信息。
RMAN>list backup

备份数据库
RMAN>backup database format 'D:/backup/md_%U';//%U=%u_%p_%c.
查看RMAN通道的对应的会话sid
select sid,username from v$session order by 2;

backup命令参数:
filesperset:每个备份集的输入文件数
maxpiecesize:一个备份片的大小
RMAN>run {
allocate channel d1 device type disk maxpiecesize=100M;
backup database filesperset 3;
}
或者
backup format 'd:/backup/md_%U' database filesperset 3;


删除备份集
RMAN>delete backupset 43,44 ;//43、44为BS号。

备份数据文件
RMAN>report schema;
RMAN>backup datafile 4,5 format 'd:/backup/md_%U';//4、5为文件编号
RMAN>backup datafile 'd:/oracle/oradata/orcl/system01.dbf' format 'd:/backup/md_%U';

备份表空间
select tablespace_name,contents from dba_tablespace;
RMAN>backup tablespace xxwz_data,xxwz_index format 'd:/backup/md_%U';

备份控制文件和spfile
1.开启RMAN自动备份控制文件和spfile(不启动system表空间也会自动备份表空间)
RMAN>show all;
RMAN>configure controlfile autobackup on;
2.单独备份控制文件和spfile
RMAN>backup current controlfile;
3.附加备份控制文件和spfile
RMAN>backup datafile 4 include current controlfile;
4.SQL备份控制文件和spfile
RMAN>sql "alter database backup controlfile to ''d:/tmp/xwx.ctl''";

备份spfile文件
RMAN>backup spfile format 'd:/backup/sd_%U';
backup copies 2 device type disk SPFILE;

备份归档文件(自动执行alter system archive log current,选择没损坏的进行备份)
backup archivelog all format 'd:/backup/ar_%U' delete all input;//备份完后删除原来的归档日志

backup archivelog sequence between 44 and 100 thread 1 format 'd:/backup/ar_%U' delete all input;//备份log sequence number44到100的archivelog

backup archivelog from time "sysdate-15" until time "sysdate-7" format 'd:/backup/ar_%U' delete all input;//备份前15天到前7天的archivelog

数据文件和归档日志文件一起备份
RMAN>backup database plus archivelog format 'd:/backup/db_%U';

并发备份
1.
RMAN>run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
backup incremental level=0
format 'd:/backup/p_%U'
(datafile 1 channel c1 tag='sys')
(datafile 3 channel c2 tag='aux')
(datafile 2,4,5 channel c3 tag='other')
;
sql 'alter system archive log current';
}
2.
configure device type disk parallelism 3 backup type to backupset;//设置并发为3


复制备份集
backup copies 2 datafile 5 format 'd:/a1/d_%U','d:/a2/d_%U';//复制2个副本

备份备份集
backup backupset all format='e:/rman/backupset_%U.dbf'
backup backupset completed before 'sysdate-2';

image备份
RMAN>copy datafile 5 to 'd:/oracle/xwx.dbf' tag='example';
或者
RMAN>backup as copy datafile 5 format 'd:/oracle/xwx.dbf';
查看copy信息
RMAN>list copy

hold backup备份整个数据库;
full backup:相对于增量备份,备份整个datafile blocks;
incremental backup:增量备份datafile;

操作超过6秒的操作
select sid,sofar,totalwork from v$session_longops;

增量备份等级
0:相当于full backup,备份整个datafile blocks,可以作为增量备份的起点;
1-4:该等级有2个备份模式
差异性增量备份:备份包括从level n or lower的改变块,n可指定;lower为大数字
累积性增量备份:备份包括从level n-1 or lower的改变块,n可指定
例如差异性增量备份是备份每天改变的块,而累积性增量备份从起点开始多天改变的块。

RMAN>run{
allocate channel d1 type disk;
backup incremental level 1 database format 'xxx/inc_%U';
} //默认为差异性增量备份

指定为累积性增量备份
RMAN>run{
allocate channel d1 type disk;
backup incremental level 1 cumulative database format 'xxx/inc_%U';

根据SCN进行块级别的备份,大大缩短备份时间;

RMAN备份读取整个数据库文件进行块级别检查时要花费大量时间,可以通过设置快改变跟踪文件,避免进行备份时全部对比块。
alter database enable block change tracking using file 'D:/trace/xx.trc';
alter database disable block change tracking;//同时会删除xx.trc文件。

备份标签
backup incremental level 2 datafile 4 tag='xwxrman';
查看指定标签备份
list backup tag='xwxrman';

RMAN相关的动态视图(corruption=损坏)
v$archive_log
v$backup_corruption
v$copy_corruption
v$database_block_corruption
v$backup_datafile
v$backup_redolog
v$backup_set
v$backup_piece

监控RMAN备份
查看server process和session的关联
RMAN> run {
allocate channel d1 type disk;
set command id to 'xwxtry'; //写一个字符串信息
backup datafile 1 format 'd:/backup/df_%U';
}
然后通过下面的SQL语句就可看到d1进程对应的字符串
select sid,spid,client_info
from v$process p,v$session s
where p.addr=s.paddr
and client_info like '%id=xwxtry%';

查看备份进度(查询v$session_longops)
select sid,serial#,context,sofar,totalwork round(sofar/totalwork*100,2) "% Complete",
from v$session_longops
where opname like 'RMAN:%'
and opname NOT LIKE 'RMAN: aggregate%';

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值