rman初步简单使用

13年前
1、确保数据库在archive(归档)模式
SQL> archive log list;
Database log mode              No Archive Mode
Automatic archival             Enabled
Archive destination            /u01/oradata/general/archive
Oldest online log sequence     13
Current log sequence           15
SQL>
修改成归档模式:
SQL> startup mount;

SQL> alter database archivelog;

Database altered.

SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/oradata/general/archive
Oldest online log sequence     13
Next log sequence to archive   15
Current log sequence           15
SQL>
2、确保环境变量(即稍后运行的rman命令是Oracle的命令)

[oracle@linuxForOra general10g]$ echo $PATH
/u01/oracle/oracle/product/10.2.0/db_1/bin:/usr/java/jdk1.6.0_18/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/NX/bin:/u01/oracle//bin:/usr/NX/bin:/usr/NX/bin:/usr/NX/bin
[oracle@linuxForOra general10g]$

即oracle的目录位置在环境变量的前面就可以了,否则可能就是执行的操作系统的命令,如果需要修改
,将Oracle的bin目录放到环境变量前面即可
3、进入rman环境进行备份
[oracle@linuxForOra ~]$ rman nocatalog

Recovery Manager: Release 9.2.0.4.0 - Production

Copyright (c) 1995, 2002, Oracle Corporation.  All rights reserved.

RMAN> connect target /

connected to target database: GENERAL (DBID=1855318131)
using target database controlfile instead of recovery catalog

RMAN> list backupset;

RMAN> backup database;

Starting backup at 02-SEP-11
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=13 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current SPFILE in backupset
including current controlfile in backupset
input datafile fno=00001 name=/u01/oradata/general/system01.dbf
input datafile fno=00002 name=/u01/oradata/general/undotbs01.dbf
input datafile fno=00011 name=/u01/oradata/general/COMMONSPACE.dbf
input datafile fno=00005 name=/u01/oradata/general/example01.dbf
input datafile fno=00010 name=/u01/oradata/general/xdb01.dbf
input datafile fno=00006 name=/u01/oradata/general/indx01.dbf
input datafile fno=00009 name=/u01/oradata/general/users01.dbf
input datafile fno=00003 name=/u01/oradata/general/cwmlite01.dbf
input datafile fno=00004 name=/u01/oradata/general/drsys01.dbf
input datafile fno=00007 name=/u01/oradata/general/odm01.dbf
input datafile fno=00008 name=/u01/oradata/general/tools01.dbf
channel ORA_DISK_1: starting piece 1 at 02-SEP-11
channel ORA_DISK_1: finished piece 1 at 02-SEP-11
piece handle=/u01/oracle/dbs/01mlhfp6_1_1 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
Finished backup at 02-SEP-11(说明备份成功)

RMAN> list backupset;

List of Backup Sets
===================
BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
1       Full    778M       DISK        00:01:21     02-SEP-11
        BP Key: 1   Status: AVAILABLE   Tag: TAG20110902T104702
        Piece Name: /u01/oracle/dbs/01mlhfp6_1_1
  SPFILE Included: Modification time: 24-SEP-10
  List of Datafiles in backup set 1
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  1       Full 811809     02-SEP-11 /u01/oradata/general/system01.dbf
  2       Full 811809     02-SEP-11 /u01/oradata/general/undotbs01.dbf
  3       Full 811809     02-SEP-11 /u01/oradata/general/cwmlite01.dbf
  4       Full 811809     02-SEP-11 /u01/oradata/general/drsys01.dbf
  5       Full 811809     02-SEP-11 /u01/oradata/general/example01.dbf
  6       Full 811809     02-SEP-11 /u01/oradata/general/indx01.dbf
  7       Full 811809     02-SEP-11 /u01/oradata/general/odm01.dbf
  8       Full 811809     02-SEP-11 /u01/oradata/general/tools01.dbf
  9       Full 811809     02-SEP-11 /u01/oradata/general/users01.dbf
  10      Full 811809     02-SEP-11 /u01/oradata/general/xdb01.dbf
  11      Full 811809     02-SEP-11 /u01/oradata/general/COMMONSPACE.dbf

RMAN>
4、为了模拟rman的恢复操作,首先删除刚才的演示备份操作
(1)、查看已有的备份集的信息
RMAN> crosscheck backup;

allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=17 devtype=DISK
crosschecked backup piece: found to be 'AVAILABLE'
backup piece handle=/u01/oracle/dbs/01mlhfp6_1_1 recid=1 stamp=760790823
Crosschecked 1 objects

(2)、删除所有的备份集
RMAN> delete backup;

using channel ORA_DISK_1

List of Backup Pieces
BP Key  BS Key  Pc# Cp# Status      Device Type Piece Name
------- ------- --- --- ----------- ----------- ----------
1       1       1   1   AVAILABLE   DISK        /u01/oracle/dbs/01mlhfp6_1_1

Do you really want to delete the above objects (enter YES or NO)? yes
deleted backup piece
backup piece handle=/u01/oracle/dbs/01mlhfp6_1_1 recid=1 stamp=760790823
Deleted 1 objects

(3)、查看已有的镜像备份
RMAN> crosscheck copy;

released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=17 devtype=DISK
validation succeeded for archived log
archive log filename=/u01/oradata/general/archive/1_10.dbf recid=1 stamp=732901799
validation succeeded for archived log
archive log filename=/u01/oradata/general/archive/1_11.dbf recid=2 stamp=741876225
validation succeeded for archived log
archive log filename=/u01/oradata/general/archive/1_12.dbf recid=3 stamp=743094932
validation succeeded for archived log
archive log filename=/u01/oradata/general/archive/1_13.dbf recid=4 stamp=760111347
validation succeeded for archived log
archive log filename=/u01/oradata/general/archive/1_14.dbf recid=5 stamp=760789488
Crosschecked 5 objects

(4)、删除所有的镜像备份
RMAN> delete copy;

released channel: ORA_DISK_1
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=17 devtype=DISK

List of Archived Log Copies
Key     Thrd Seq     S Low Time  Name
------- ---- ------- - --------- ----
1       1    10      A 24-SEP-10 /u01/oradata/general/archive/1_10.dbf
2       1    11      A 20-OCT-10 /u01/oradata/general/archive/1_11.dbf
3       1    12      A 31-JAN-11 /u01/oradata/general/archive/1_12.dbf
4       1    13      A 14-FEB-11 /u01/oradata/general/archive/1_13.dbf
5       1    14      A 25-AUG-11 /u01/oradata/general/archive/1_14.dbf

Do you really want to delete the above objects (enter YES or NO)? yes
deleted archive log
archive log filename=/u01/oradata/general/archive/1_10.dbf recid=1 stamp=732901799
deleted archive log
archive log filename=/u01/oradata/general/archive/1_11.dbf recid=2 stamp=741876225
deleted archive log
archive log filename=/u01/oradata/general/archive/1_12.dbf recid=3 stamp=743094932
deleted archive log
archive log filename=/u01/oradata/general/archive/1_13.dbf recid=4 stamp=760111347
deleted archive log
archive log filename=/u01/oradata/general/archive/1_14.dbf recid=5 stamp=760789488
Deleted 5 objects

(5)、通过下面的命令查出已经不存在rman备份了
RMAN> list backupset;
RMAN> list copy;
specification does not match any archive log in the recovery catalog

RMAN>

5、模拟删除数据后的rman恢复
(1)、在自己创建的表空间COMMONSPACE上创建表rmantest,并插入3条数据
SQL> select segment_name,tablespace_name from user_segments where segment_name='RMANTEST';

SEGMENT_NAME                   TABLESPACE_NAME
------------------------------ ------------------------------
RMANTEST                       COMMONSPACE
SQL> select * from rmantest;

         A
----------
        11
        12
        13

SQL>
(2)、使用rman进行备份
[oracle@linuxForOra ~]$ rman nocatalog

Recovery Manager: Release 9.2.0.4.0 - Production

Copyright (c) 1995, 2002, Oracle Corporation.  All rights reserved.

RMAN> connect target /

connected to target database: GENERAL (DBID=1855318131)
using target database controlfile instead of recovery catalog

RMAN> list backupset;


RMAN> list copy;

specification does not match any archive log in the recovery catalog

RMAN> backup database;

Starting backup at 02-SEP-11
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=18 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current SPFILE in backupset
including current controlfile in backupset
input datafile fno=00001 name=/u01/oradata/general/system01.dbf
input datafile fno=00002 name=/u01/oradata/general/undotbs01.dbf
input datafile fno=00011 name=/u01/oradata/general/COMMONSPACE.dbf
input datafile fno=00005 name=/u01/oradata/general/example01.dbf
input datafile fno=00010 name=/u01/oradata/general/xdb01.dbf
input datafile fno=00006 name=/u01/oradata/general/indx01.dbf
input datafile fno=00009 name=/u01/oradata/general/users01.dbf
input datafile fno=00003 name=/u01/oradata/general/cwmlite01.dbf
input datafile fno=00004 name=/u01/oradata/general/drsys01.dbf
input datafile fno=00007 name=/u01/oradata/general/odm01.dbf
input datafile fno=00008 name=/u01/oradata/general/tools01.dbf
channel ORA_DISK_1: starting piece 1 at 02-SEP-11
channel ORA_DISK_1: finished piece 1 at 02-SEP-11
piece handle=/u01/oracle/dbs/02mlhike_1_1 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:26
Finished backup at 02-SEP-11

RMAN> quit
(3)、关闭数据库(shutdown immediate),然后删除文件
[oracle@linuxForOra general]$ rm COMMONSPACE.dbf
(4)、启动到mount状态,开始使用rman进行恢复
SQL> startup mount;
ORACLE instance started.

Total System Global Area  236000356 bytes
Fixed Size                   451684 bytes
Variable Size             201326592 bytes
Database Buffers           33554432 bytes
Redo Buffers                 667648 bytes
Database mounted.
SQL>
[oracle@linuxForOra general]$ rman nocatalog

Recovery Manager: Release 9.2.0.4.0 - Production

Copyright (c) 1995, 2002, Oracle Corporation.  All rights reserved.

RMAN> connect target /

connected to target database: GENERAL (DBID=1855318131)
using target database controlfile instead of recovery catalog

RMAN> restore database;

Starting restore at 02-SEP-11

allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=13 devtype=DISK
skipping datafile 1; already restored to file /u01/oradata/general/system01.dbf
skipping datafile 2; already restored to file /u01/oradata/general/undotbs01.dbf
skipping datafile 3; already restored to file /u01/oradata/general/cwmlite01.dbf
skipping datafile 4; already restored to file /u01/oradata/general/drsys01.dbf
skipping datafile 5; already restored to file /u01/oradata/general/example01.dbf
skipping datafile 6; already restored to file /u01/oradata/general/indx01.dbf
skipping datafile 7; already restored to file /u01/oradata/general/odm01.dbf
skipping datafile 8; already restored to file /u01/oradata/general/tools01.dbf
skipping datafile 9; already restored to file /u01/oradata/general/users01.dbf
skipping datafile 10; already restored to file /u01/oradata/general/xdb01.dbf
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00011 to /u01/oradata/general/COMMONSPACE.dbf
channel ORA_DISK_1: restored backup piece 1
piece handle=/u01/oracle/dbs/02mlhike_1_1 tag=TAG20110902T113542 params=NULL
channel ORA_DISK_1: restore complete
Finished restore at 02-SEP-11
recover database之后才能打开数据库
RMAN> recover database;

Starting recover at 02-SEP-11
using channel ORA_DISK_1

starting media recovery
media recovery complete

Finished recover at 02-SEP-11

RMAN> exit
Recovery Manager complete.

SQL> select open_mode from v$database;

OPEN_MODE
----------
MOUNTED

SQL> alter database open;

Database altered.

SQL> select * from rmantest;

         A
----------
        11
        12
        13
到此为止,恢复完毕!
6、rman原理简单分析
rman 备份是针对块一级的,支持增量备份。
Rman备份并不需要将数据库或者表空间置于backup状态,但是它会把scn记录在catalog中对应你的backupset 
准备在恢复的时候来使用。
对users表空间做一个完全备份 
$ rman target sys/oracle nocatalog 
RMAN>; run { 
2>; allocate channel d1 type disk; 
3>; backup 
4>; format='/u03/oraclebk/%d_%N_%s.bk' tablespace users; 
5>; release channel d1; 
6>; } 
看一下备份集里都有什么,注意看Ckp SCN  546792, 
RMAN>; list backup of tablespace users; 

List of Backup Sets 
=================== 
BS Key  Type LV Size       Device Type Elapsed Time Completion Time 
------- ---- -- ---------- ----------- ------------ --------------- 
3       Full    1M         DISK        00:00:02     31-MAR-05 
        BP Key: 3   Status: AVAILABLE  Compressed: NO  Tag: TAG20050331T153729 
        Piece Name: /u03/oraclebk/SALES_USERS_4.bk 
  List of Datafiles in backup set 3 
  File LV Type Ckp SCN    Ckp Time  Name 
  ---- -- ---- ---------- --------- ---- 
  4       Full 546792     31-MAR-05 /u02/oradata/sales/users01.dbf 

恢复的时候应用546792开始到现在的归档日志和重做日志. 
rman的增量备份的基本原理 
其实原理很简单,主要就是弄明白怎么样在做增量备份时确定某个数据块需要备份,哪个不需要 
rman在做1级备份的时候怎么来确定0级备份之后都有哪些数据块做了修改呢?看下面一段 
Each data block in a datafile contains a system change number (SCN), which is the 
SCN at which the most recent change was made to the block. During an incremental 
backup, RMAN reads the SCN of each data block in the input file and compares it to 
the checkpoint SCN of the parent incremental backup. If the SCN in the input data 
block is greater than or equal to the checkpoint SCN of the parent, then RMAN copies 
the block. 
原来block里边也有一个change scn 
也就是说在做level 1级备份的时候,需要扫描所有的数据块并且用块中记录修改的SCN跟level 0备份时的SCN做比较(备份记录中的Ckp SCN),来确定这个块是否需要备份。 
所以扫描整个数据文件是不可避免的 ! 

这是传统的rman做增量备份 

在10g中rman做增量备份不再需要扫描整个数据文件了 
10g引入的新特性 block change tracking: 
Block change tracking进程记录自从上一次备份以来数据块的变化,并把这些信息记录在跟踪文件中。RMAN使用这个文件判断增量备份中需要备份的变更数据。这极大的促进了备份性能,RMAN可以不再扫描整个文件以查找变更数据。 
RMAN's change tracking feature for incremental backups improves incremental 
backup performance by recording changed blocks in each datafile in a change tracking 
file. If change tracking is enabled, RMAN uses the change tracking file to identify 
changed blocks for incremental backup, thus avoiding the need to scan every block in 
the datafile. 
估计是使用的位图文件做的记录! 
有兴趣的可以看看dump的数据块 

通过下面的查询找一个表对应的数据块 
SQL>; select file_id,block_id,blocks 
  2  from dba_extents 
  3  where segment_name='EMPLOYEES'; 

   FILE_ID   BLOCK_ID     BLOCKS 
---------- ---------- ---------- 
         5         81          8 

dump一个块到udump的trc文件 
SQL>; alter system dump datafile 5 block 81; 

System altered. 


在udump目录找到对应的trc文件,找到dump那段 
Start dump data blocks tsn: 6 file#: 5 minblk 81 maxblk 81 
buffer tsn: 6 rdba: 0x01400051 (5/81) 
scn: 0x0000.00086c4d seq: 0x01 flg: 0x04 tail: 0x4b502001 
后面省略了 


scn: 0x0000.00086c4d是16进制你可以换算过来552013 

你可以尝试做一下修改,不过一定要保证对应的块被修改了,并且被写了,才能反映出来。