1. 设置数据库归档模式,把其中一个归档路径指定为FRA(可选设置)。
SQL> alter system set log_archive_dest_1='location=/oracle/archivelog'
2 /
System altered.
SQL> alter system set log_archive_dest_2='location=use_db_recovery_file_dest'
2 /
System altered.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 167772160 bytes
Fixed Size 1218316 bytes
Variable Size 67111156 bytes
Database Buffers 96468992 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> alter database archivelog;
Database altered.
SQL> alter database open;
Database altered.
2. 设置FRA (flash_reccovery_area)
SQL> alter system set db_recovery_file_dest_size=2G;
System altered.
SQL> alter system set db_recovery_file_dest='/oracle/backup/fra';
System altered.
这里的顺序不能颠倒。
3. 配置自动备份controlfile与spfile
这样在进行任何一个backup操作时都会备份controlfile与spfil,否则只是在备份datafile 1时,才对控制文件及spfile进行自动备份:
RMAN> connect target /
RMAN> configure controlfile autobackup on;
4. rman备份恢复实践:
1) 全库备份:
RMAN> connect target /
connected to target database: LAW (DBID=2098252449) 要记下这里的dbid号码
RMAN> backup database;
Starting backup at 21-AUG-07
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=154 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/oracle/oradata/law/system01.dbf
input datafile fno=00003 name=/oracle/oradata/law/sysaux01.dbf
input datafile fno=00002 name=/oracle/oradata/law/undotbs01.dbf
input datafile fno=00004 name=/oracle/oradata/law/users01.dbf
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T071112_3dn7wjlc_.bkp tag=TAG20070821T071112 comment=NONE 这里是数据文件备份
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:15
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_ncsnf_TAG20070821T071112_3dn7yyj4_.bkp tag=TAG20070821T071112 comment=NONE 这里是控制文件备份
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 21-AUG-07
2) 到FRA目录查看生成的备份文件:
[oracle@redhat fra]$ ls
LAW
[oracle@redhat fra]$ cd LAW
[oracle@redhat LAW]$ ls
archivelog backupset
[oracle@redhat LAW]$ cd backupset/
[oracle@redhat backupset]$ ls
2007_08_21
[oracle@redhat backupset]$ cd 2007_08_21/
[oracle@redhat 2007_08_21]$ ls
o1_mf_ncsnf_TAG20070821T071112_3dn7yyj4_.bkp
o1_mf_nnndf_TAG20070821T071112_3dn7wjlc_.bkp
3) 设置控制文件自动备份
RMAN> connect target /
connected to target database: LAW (DBID=2098252449)
RMAN> configure controlfile autobackup on;
using target database control file instead of recovery catalog
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored
4) 查看上述设置的效果
RMAN> backup tablespace users;
Starting backup at 21-AUG-07
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=141 devtype=DISK
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00004 name=/oracle/oradata/law/users01.dbf
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T071952_3dn8drmb_.bkp tag=TAG20070821T071952 comment=NONE 这里是数据文件备份
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 21-AUG-07
Starting Control File and SPFILE Autobackup at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631178395_3dn8dvxs_.bkp comment=NONE 这里是控制文件备份
Finished Control File and SPFILE Autobackup at 21-AUG-07
至此备份完成
5) 切换日志文件,强制归档:
SQL> conn / as sysdba
Connected.
SQL> alter system switch logfile;
System altered.
SQL> /
System altered.
SQL> /
System altered.
6) 用scott连接数据库,创建一个新表,看进行数据库恢复后,这个表能否恢复:
SQL> connect scott/tiger
Connected.
SQL> create table t(a int);
Table created.
SQL> insert into t values(10);
1 row created.
SQL> commit;
Commit complete.
切换日志,强制包含上述操作的日志文件归档:
SQL> conn / as sysdba
Connected.
SQL> alter system switch logfile;
System altered.
SQL> /
System altered.
SQL> /
System altered.
7) 毁掉数据库
毁掉数据文件:
正常关闭数据库,先备份所有数据库文件(以防万一),然后删除数据库的所有文件,模拟数据库彻底损坏:
[oracle@redhat oradata]$ cp -r law /oracle/bakup/bak/
[oracle@redhat oradata]$ cd law
[oracle@redhat law]$ rm -rf *
毁掉参数文件:
修改spfile的名称,模拟参数文件丢失:
[oracle@redhat archivelog]$ cd $ORACLE_HOME/dbs
[oracle@redhat dbs]$ ls
hc_law.dat lkLAWLAW snapcf_law.f lkLAW orapwlaw spfilelaw.ora
[oracle@redhat dbs]$ mv spfilelaw.ora spfile.ora.bak
8) 查看当前的归档日志的序号,
从以下结果可以看出最后一个归档日志的序号是53(这个步骤不要忽略),恢复时,归档日志只能应用到这个序号
[oracle@redhat ~]$ cd /oracle/archivelog/
[oracle@redhat archivelog]$ ls
1_39_616888033.dbf 1_43_616888033.dbf 1_47_616888033.dbf 1_51_616888033.dbf
1_40_616888033.dbf 1_44_616888033.dbf 1_48_616888033.dbf 1_52_616888033.dbf
1_41_616888033.dbf 1_45_616888033.dbf 1_49_616888033.dbf 1_53_616888033.dbf
1_42_616888033.dbf 1_46_616888033.dbf 1_50_616888033.dbf
9) 进行恢复工作
启动rman:
[oracle@redhat dbs]$ rman
Recovery Manager: Release 10.2.0.1.0 - Production on Tue Aug 21 07:31:58 2007
Copyright (c) 1982, 2005, Oracle. All rights reserved.
执行set dbid命令:
RMAN> set dbid=2098252449
executing command: SET DBID
连接数据库:
RMAN> connect target /
connected to target database (not started)
启动数据库:
这时因为spfile已经不存在,Oracle使用一个默认的spfile启动实例:
RMAN> startup nomount
startup failed: ORA-01078: failure in processing system parameters
LRM-00109: could not open parameter file '/oracle/product/10.2.0/db_1/dbs/initlaw.ora'
starting Oracle instance without parameter file for retrival of spfile 注意这句说明
Oracle instance started
Total System Global Area 159383552 bytes
Fixed Size 1218268 bytes
Variable Size 54528292 bytes
Database Buffers 100663296 bytes
Redo Buffers 2973696 bytes
恢复参数文件:
下面命令报错
RMAN> restore spfile from autobackup;
Starting restore at 21-AUG-07
using channel ORA_DISK_1
channel ORA_DISK_1: looking for autobackup on day: 20070821
channel ORA_DISK_1: looking for autobackup on day: 20070820
channel ORA_DISK_1: looking for autobackup on day: 20070819
channel ORA_DISK_1: looking for autobackup on day: 20070818
channel ORA_DISK_1: looking for autobackup on day: 20070817
channel ORA_DISK_1: looking for autobackup on day: 20070816
channel ORA_DISK_1: looking for autobackup on day: 20070815
channel ORA_DISK_1: no autobackup in 7 days found
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 08/21/2007 07:41:16
RMAN-06172: no autobackup found or specified handle is not a valid copy or piece
指定参数文件所在的备份文件避免报错
RMAN> restore spfile from '/oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631178395_3dn8dvxs_.bkp';
Starting restore at 21-AUG-07
using channel ORA_DISK_1
channel ORA_DISK_1: autobackup found: /oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631178395_3dn8dvxs_.bkp
channel ORA_DISK_1: SPFILE restore from autobackup complete
Finished restore at 21-AUG-07
切换至dbs目录,查看参数文件已经恢复到原来目录:
[oracle@redhat law]$ cd $ORACLE_HOME/dbs
[oracle@redhat dbs]$ ls
hc_law.dat lkLAWLAW snapcf_law.f spfilelaw.ora.bak
lkLAW orapwlaw spfilelaw.ora
重新启动数据库,从而读取刚刚恢复的参数文件,得到其控制文件应该恢复至的目录:
RMAN> shutdown immediate
Oracle instance shut down
RMAN> startup nomount 这时spfile已经存在,不再使用默认的参数文件
connected to target database (not started)
Oracle instance started
Total System Global Area 167772160 bytes
Fixed Size 1218316 bytes
Variable Size 67111156 bytes
Database Buffers 96468992 bytes
Redo Buffers 2973696 bytes
恢复控制文件:
RMAN> restore controlfile from autobackup;
Starting restore at 21-AUG-07
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
recovery area destination: /oracle/bakup/fra
database name (or database unique name) used for search: LAW
channel ORA_DISK_1: autobackup found in the recovery area
channel ORA_DISK_1: autobackup found: /oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631178395_3dn8dvxs_.bkp
channel ORA_DISK_1: control file restore from autobackup complete
output filename=/oracle/oradata/law/control01.ctl
output filename=/oracle/oradata/law/control02.ctl
output filename=/oracle/oradata/law/control03.ctl
Finished restore at 21-AUG-07
Mount数据库,准备恢复数据文件:
RMAN> alter database mount;
database mounted
released channel: ORA_DISK_1
恢复数据文件
控制文件中保存了数据文件的目录以及rman的备份信息,这时只要使用restore database即可。
RMAN> restore database;
Starting restore at 21-AUG-07
Starting implicit crosscheck backup at 21-AUG-07
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=156 devtype=DISK
Crosschecked 3 objects
Finished implicit crosscheck backup at 21-AUG-07
Starting implicit crosscheck copy at 21-AUG-07
using channel ORA_DISK_1
Finished implicit crosscheck copy at 21-AUG-07
searching for all files in the recovery area
cataloging files...
cataloging done
List of Cataloged Files
=======================
File Name: /oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631178395_3dn8dvxs_.bkp
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 00001 to /oracle/oradata/law/system01.dbf
restoring datafile 00002 to /oracle/oradata/law/undotbs01.dbf
restoring datafile 00003 to /oracle/oradata/law/sysaux01.dbf
channel ORA_DISK_1: reading from backup piece /oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T071112_3dn7wjlc_.bkp
channel ORA_DISK_1: restored backup piece 1
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T071112_3dn7wjlc_.bkp tag=TAG20070821T071112
channel ORA_DISK_1: restore complete, elapsed time: 00:00:46
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00004 to /oracle/oradata/law/users01.dbf
channel ORA_DISK_1: reading from backup piece /oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T071952_3dn8drmb_.bkp
channel ORA_DISK_1: restored backup piece 1
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T071952_3dn8drmb_.bkp tag=TAG20070821T071952
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 21-AUG-07
对数据库进行recover操作:
RMAN> recover database until sequence 54; 注意这里的54是最后一个归档日志序号
Starting recover at 21-AUG-07
using channel ORA_DISK_1
starting media recovery
archive log thread 1 sequence 48 is already on disk as file /oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_48_3dncbvtv_.arc
archive log thread 1 sequence 49 is already on disk as file /oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_49_3dnccs7p_.arc
archive log thread 1 sequence 50 is already on disk as file /oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_50_3dnccwo3_.arc
archive log thread 1 sequence 51 is already on disk as file /oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_51_3dnccxg2_.arc
archive log thread 1 sequence 52 is already on disk as file /oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_52_3dncd3bw_.arc
archive log thread 1 sequence 53 is already on disk as file /oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_53_3dncd3dt_.arc
archive log filename=/oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_48_3dncbvtv_.arc thread=1 sequence=48
archive log filename=/oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_49_3dnccs7p_.arc thread=1 sequence=49
archive log filename=/oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_50_3dnccwo3_.arc thread=1 sequence=50
archive log filename=/oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_51_3dnccxg2_.arc thread=1 sequence=51
archive log filename=/oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_52_3dncd3bw_.arc thread=1 sequence=52
archive log filename=/oracle/bakup/fra/LAW/archivelog/2007_08_21/o1_mf_1_53_3dncd3dt_.arc thread=1 sequence=53
media recovery complete, elapsed time: 00:00:03
Finished recover at 21-AUG-07
因为进行了不完全恢复,必须以resetlogs方式打开数据库;
RMAN> alter database open resetlogs;
database opened
在sqlplus中以scott连接数据库,查看其创建的表t是否被完整恢复:
SQL> conn scott/tiger
Connected.
SQL> select * from t;
A
----------
10
5. 差异备份
首先进行0级备份
RMAN> backup incremental level=0 database;
Starting backup at 21-AUG-07
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental level 0 datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/oracle/oradata/law/system01.dbf
input datafile fno=00003 name=/oracle/oradata/law/sysaux01.dbf
input datafile fno=00002 name=/oracle/oradata/law/undotbs01.dbf
input datafile fno=00004 name=/oracle/oradata/law/users01.dbf
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnnd0_TAG20070821T082940_3dndhnh0_.bkp tag=TAG20070821T082940 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:45
Finished backup at 21-AUG-07
Starting Control File and SPFILE Autobackup at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631182625_3dndk29l_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 21-AUG-07
1级备份
RMAN> backup incremental level=1 database;
Starting backup at 21-AUG-07
using channel ORA_DISK_1
channel ORA_DISK_1: starting incremental level 1 datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/oracle/oradata/law/system01.dbf
input datafile fno=00003 name=/oracle/oradata/law/sysaux01.dbf
input datafile fno=00002 name=/oracle/oradata/law/undotbs01.dbf
input datafile fno=00004 name=/oracle/oradata/law/users01.dbf
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnnd1_TAG20070821T083051_3dndkvmq_.bkp tag=TAG20070821T083051 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:25
Finished backup at 21-AUG-07
Starting Control File and SPFILE Autobackup at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631182676_3dndlo8t_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 21-AUG-07
6. 备份数据文件
RMAN> backup datafile 3;
Starting backup at 21-AUG-07
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00003 name=/oracle/oradata/law/sysaux01.dbf
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T083129_3dndm1wr_.bkp tag=TAG20070821T083129 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:15
Finished backup at 21-AUG-07
Starting Control File and SPFILE Autobackup at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/autobackup/2007_08_21/o1_mf_s_631182704_3dndmkln_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 21-AUG-07
7. 备份当前控制文件
RMAN> backup current controlfile;
8. 修改自动备份控制文件为off的效果
RMAN> configure controlfile autobackup off;
old RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP OFF;
new RMAN configuration parameters are successfully stored
RMAN> backup tablespace users;
Starting backup at 21-AUG-07
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00004 name=/oracle/oradata/law/users01.dbf
channel ORA_DISK_1: starting piece 1 at 21-AUG-07
channel ORA_DISK_1: finished piece 1 at 21-AUG-07
piece handle=/oracle/bakup/fra/LAW/backupset/2007_08_21/o1_mf_nnndf_TAG20070821T090700_3dngonxl_.bkp tag=TAG20070821T090700 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
Finished backup at 21-AUG-07 这里已经不包含控制文件及参数文件备份。