欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RMAN异机恢复步骤

程序员文章站 2024-03-13 20:58:46
...

RMAN是DBA日常工作非常常用的工具,RMAN的应用场景大概有数据备份恢复、数据迁移等。我们不常用本机RMAN恢复,因为在实际生产中RMAN备份通常是用于异机恢复,比如数据迁移,DG搭建,恢复备份库等操作。

最近做RMAN异机恢复比较多,所以就总结下步骤,先说下重要的几点:

1. 如果恢复的那台数据库是单机库的话,那么在创建这台单机库的时候可以不用dbca创建实例。如果是RAC库的话就需要dbca创建实例了。

2. 两台数据库的路径最好一致,也就是说备份库的数据文件、参数文件、控制文件和归档日志等路径要和主库一致,包括实例名也要一致。这样做的好处是在恢复的时候,不需要修改路径。实例名最好也一致。

3. 理解数据库开启顺序有助于理解RMAN异机恢复的步骤,具体步骤下面会讲。

RMAN异机恢复先从备份开始:

异机恢复所需要的文件包括:参数文件,控制文件和数据文件。

数据库全备份,在备份全库的同时,通过format参数指定配置路径和备份格式。

backup database format '/soft/database_fullbak_%d_%T_%s.bak

这里需要注意的是如果备份了SYSTEM表空间,就会自动备份参数文件和控制文件。在执行完上面的命令后,通过list backup命令可以看到备份集信息,发现在backup database的同时也备份了参数文件和控制文件。

RMAN> list backup;


List of Backup Sets
===================


BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
201     Full    459.59M    DISK        00:01:10     28-MAY-19      
        BP Key: 201   Status: AVAILABLE  Compressed: NO  Tag: TAG20190528T222243
        Piece Name: /soft/database_fullbak_OGGDB_20190528_205.bak
  List of Datafiles in backup set 201
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  2       Full 1520550    28-MAY-19 /oracle/app/oradata/oggdb/sysaux01.dbf
  3       Full 1520550    28-MAY-19 /oracle/app/oradata/oggdb/undotbs01.dbf
  5       Full 1520550    28-MAY-19 /oracle/app/oradata/oggdb/odc01.dbf
  7       Full 1520550    28-MAY-19 /oracle/app/oradata/test02.dbf

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
202     Full    9.80M      DISK        00:00:03     28-MAY-19      
        BP Key: 202   Status: AVAILABLE  Compressed: NO  Tag: TAG20190528T222243
        Piece Name: /soft/database_fullbak_OGGDB_20190528_206.bak
  Control File Included: Ckp SCN: 1520578      Ckp time: 28-MAY-19

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
203     Full    80.00K     DISK        00:00:00     28-MAY-19      
        BP Key: 203   Status: AVAILABLE  Compressed: NO  Tag: TAG20190528T222243
        Piece Name: /soft/database_fullbak_OGGDB_20190528_207.bak
  SPFILE Included: Modification time: 28-MAY-19
  SPFILE db_unique_name: OGGDB

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
204     Full    701.17M    DISK        00:01:35     28-MAY-19      
        BP Key: 204   Status: AVAILABLE  Compressed: NO  Tag: TAG20190528T222243
        Piece Name: /soft/database_fullbak_OGGDB_20190528_204.bak
  List of Datafiles in backup set 204
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  1       Full 1520551    28-MAY-19 /oracle/app/oradata/oggdb/system01.dbf
  4       Full 1520551    28-MAY-19 /oracle/app/oradata/oggdb/users01.dbf
  6       Full 1520551    28-MAY-19 /oracle/app/oradata/oggdb/test01.dbf

RMAN异机恢复

备份完之后就可以开始恢复了,这里建议备份库的数据文件,日志文件,参数文件和实例等和主库一致,不一致也可以,但是一致的话会方便很多。

上面说到了理解数据库开启顺序有助于理解RMAN异机恢复的步骤,其实是这样的:

数据库启动三个步骤:

1. nomount:Oracle读参数文件(里面有控制文件目录),打开实例,启动Oracle后台进程,给Oracle分配SGA。此时数据库状态为未装载。

2. mount:Oracle打开并读取控制文件(里面有数据文件和日志文件的目录),获取数据文件和重做日志文件的名称和位置。此时数据库完成装载。

3. open:Oracle打开数据文件和重做日志文件,才能对外(所有有效用户)提供数据库服务。

从上面可以看出来数据库在启动时打开文件的顺序为:参数文件>控制文件>数据文件,也就是说我们在做RMAN恢复的时候也要按照这个顺序来恢复。

第一次做异机恢复,建议将备库的数据文件(包括system、temp、undo等)、日志文件、归档文件、参数文件和控制文件都删除掉,这样做的好处是使得备库更干净。其实不删也可以,我习惯在第一次异机恢复删除......

下面开始做异机恢复:

1. 恢复参数文件,由于已经删除掉了参数文件,那么在rman中执行startup nomout会启动dummy实例,然后通过restore spfile from '备份片'; 来恢复参数文件。

[[email protected] ~]$ rman target /

Recovery Manager: Release 11.2.0.4.0 - Production on Tue May 28 22:51:48 2019

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

connected to target database (not started)

RMAN> startup nomount

startup failed: ORA-01078: failure in processing system parameters
LRM-00109: could not open parameter file '/oracle/app/product/11.2.0/db_1/dbs/initoggdb.ora'

starting Oracle instance without parameter file for retrieval of spfile
Oracle instance started

Total System Global Area    1068937216 bytes

Fixed Size                     2260088 bytes
Variable Size                281019272 bytes
Database Buffers             780140544 bytes
Redo Buffers                   5517312 bytes

RMAN> restore spfile from '/soft/database_fullbak_OGGDB_20190528_207.bak';

Starting restore at 28-MAY-19
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=19 device type=DISK

channel ORA_DISK_1: restoring spfile from AUTOBACKUP /soft/database_fullbak_OGGDB_20190528_207.bak
channel ORA_DISK_1: SPFILE restore from AUTOBACKUP complete
Finished restore at 28-MAY-19

2. 恢复完参数文件后,建议在sqlplus中重新启动到nomount阶段,然后通过restore controlfile from '备份片'; 来恢复控制文件。

sql> shutdown immediate
sql> startup nomount

RMAN> restore controlfile from '/soft/database_fullbak_OGGDB_20190528_206.bak';

Starting restore at 28-MAY-19
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=18 device type=DISK

channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/oracle/app/oradata/oggdb/control01.ctl
output file name=/oracle/app/oradata/oggdb/control02.ctl
Finished restore at 28-MAY-19

3. 恢复完控制文件后,开启数据库到mount阶段,执行catalog start with '备份路径'; 或者catalog backuppiece '备份片'; 将备份片信息添加到控制文件中。其中catalog start with会添加路径下所有合理的备份片,而catalog backuppiece只会添加单个备份片。

RMAN> alter database mount;
RMAN> catalog start with '/soft/';

4.接下来恢复数据库,restore database。

RMAN> restore database;

Starting restore at 28-MAY-19
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=18 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=19 device type=DISK

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00002 to /oracle/app/oradata/oggdb/sysaux01.dbf
channel ORA_DISK_1: restoring datafile 00003 to /oracle/app/oradata/oggdb/undotbs01.dbf
channel ORA_DISK_1: restoring datafile 00005 to /oracle/app/oradata/oggdb/odc01.dbf
channel ORA_DISK_1: restoring datafile 00007 to /oracle/app/oradata/test02.dbf
channel ORA_DISK_1: reading from backup piece /soft/database_fullbak_OGGDB_20190528_205.bak
channel ORA_DISK_2: starting datafile backup set restore
channel ORA_DISK_2: specifying datafile(s) to restore from backup set
channel ORA_DISK_2: restoring datafile 00001 to /oracle/app/oradata/oggdb/system01.dbf
channel ORA_DISK_2: restoring datafile 00004 to /oracle/app/oradata/oggdb/users01.dbf
channel ORA_DISK_2: restoring datafile 00006 to /oracle/app/oradata/oggdb/test01.dbf
channel ORA_DISK_2: reading from backup piece /soft/database_fullbak_OGGDB_20190528_204.bak
channel ORA_DISK_1: piece handle=/soft/database_fullbak_OGGDB_20190528_205.bak tag=TAG20190528T222243
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
channel ORA_DISK_2: piece handle=/soft/database_fullbak_OGGDB_20190528_204.bak tag=TAG20190528T222243
channel ORA_DISK_2: restored backup piece 1
channel ORA_DISK_2: restore complete, elapsed time: 00:00:35
Finished restore at 28-MAY-19

5. 终于到了最后一步,还原数据库,recover database。

RMAN> recover database;

以上就是RMAN异机恢复的步骤了,只要多练习,多解决错误,还是可以很快掌握的。

相关标签: Oracle 备份恢复