RMAN实例入门,备份与恢复

来源:互联网 发布:淘宝店铺搬迁 编辑:程序博客网 时间:2024/05/16 06:20
本文用step by step的方式介绍一下RMAN的入门使用,通常保存备份目录的目录数据库和目标数据库应该在不同的机器上,这里两个数据库在同一台机器上.
在listener.ora中加入
    (SID_DESC =
      (GLOBAL_DBNAME = rmtgt)
      (ORACLE_HOME = D:\oracle\product\10.2.0\db_1)
      (SID_NAME = but)
    )
tns配置如下:(让target 为静态注册,这样startup nomount的时候才能从rman连上)
target =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = NBK-DAL-625040.ap.bt.com)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = rmtgt)
    )
  )
catalog =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = NBK-DAL-625040.ap.bt.com)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

1,在保存备份目录的目录数据库建立一个rman用户并创建相应的权限
sqlplus sys/sys@catalog as sysdba
create tablespace rmants datafile 'D:\oracle\product\10.2.0\oradata\orcl\rmants.dbf' size 20M;
create user rman identified by rman default tablespace rmants temporary tablespace temp quota unlimited on rmants;
grant recovery_catalog_owner to rman;
grant connect, resource to rman;
2,在目录数据库中创建恢复目录,建立一些表和视图.
C:\>rman catalog rman/rman@catalog
Recovery Manager: Release 10.2.0.1.0 - Production on Thu Jan 12 15:02:22 2012
Copyright (c) 1982,,2005,Oracle.  All rights reserved.
connected to recovery catalog database
RMAN> create catalog tablespace rmants;
recovery catalog created
3,注册目标数据库到恢复目录,注意这里的连接串是targe
C:\>rman target sys/sys@target
Recovery Manager: Release 10.2.0.1.0 - Production on Thu Jan 12 15:08:42 2012
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
connected to target database: BUT (DBID=1215064705)
4,连接到目录数据库,注册目标数据库,注册完后在目录数据库的rman下有一个表DB里会查询到目标数据库的信息.
RMAN> connect catalog rman/rman@catalog;
connected to recovery catalog database
RMAN> register database;
database registered in recovery catalog

starting full resync of recovery catalog

full resync complete

到此为止前面的准备工作都做好了,下面就可以使用RMAN 来进行备份和恢复了。

** 如果想注销数据库,步骤如下:

C:\>sqlplus rman/rman@catalog

SQL> select db_key,db_id from db;
    DB_KEY      DB_ID
---------- ----------
       178 3712141887

SQL> execute dbms_rcvcat.unregisterdatabase(178,3712141887);
PL/SQL procedure successfully completed.

下面来进行备份:
1,创建一个目录 D:\oracle\rmanrep
2,C:\>rman target sys/sys@target catalog rman/rman@catalog
3,RMAN> run{
2>  allocate channel c1 type disk;
3>  backup database format 'D:\oracle\rmanrep\butdb.dmp';
4> }

目标数据库备份为D:\oracle\rmanrep\butdb.dmp
RMAN> list backup; 可以查看备份的信息.
**如果想删掉备份,(194 为备份的BS key)
RMAN> allocate channel for delete type disk;
RMAN> change backupset 194 delete;

下面来做恢复
1,将目标数据库shutdown,将USERS01.DBF文件重命名为USERS01.DBF.bak模拟数据文件丢失.
2,将目标数据库 startup mount
3,在rman下恢复
a,C:\>rman target sys/sys@target catalog rman/rman@catalog
Recovery Manager: Release 10.2.0.1.0 - Production on Thu Jan 12 17:59:50 2012
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
connected to target database: BUT (DBID=3712141887, not open)

注意这里可以看到数据库不是open状态,网络字符串'target'必须是通过静态注册的才能连接上.
这时候open数据库
b,RMAN> alter database open;
using target database control file instead of recovery catalog
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of alter db command at 01/12/2012 18:01:42
ORA-01157: cannot identify/lock datafile 4 - see DBWR trace file
ORA-01110: data file 4: 'D:\ORACLE\PRODUCT\10.2.0\ORADATA\BUT\USERS01.DBF'
可以看到datafile 4找不到
c,将数据文件从备份集合里拷贝到数据库的数据文件目录里.
RMAN> restore datafile 4;
Starting restore at 12-JAN-12
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00004 to D:\ORACLE\PRODUCT\10.2.0\ORADATA\BUT\USERS01.DBF
channel ORA_DISK_1: reading from backup piece D:\ORACLE\RMANREP\BUTDB.DMP
channel ORA_DISK_1: restored backup piece 1
piece handle=D:\ORACLE\RMANREP\BUTDB.DMP tag=TAG20120112T172743
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
Finished restore at 12-JAN-12
d,恢复数据文件
RMAN> recover datafile 4;
Starting recover at 12-JAN-12
using channel ORA_DISK_1
starting media recovery
media recovery complete, elapsed time: 00:00:04
Finished recover at 12-JAN-12
e,恢复成功,打开数据库,
RMAN> alter database open;

database opened


RMAN支持的其他备份:

backup current controlfile format 'D:\oracle\rmanrep\but_controlfile.bkp';
backup tablespace 'USERS' format 'D:\oracle\rmanrep\but_tbs_users.bkp';
backup datafile 4 format 'D:\oracle\rmanrep\but_dtfile_users.bkp';
backup spfile format 'D:\oracle\rmanrep\but_spfile.bkp';
backup archivelog all format 'D:\oracle\rmanrep\but_archivedlog.bkp';


原创粉丝点击