Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  397 随笔 :: 33 文章 :: 29 评论 :: 0 Trackbacks
Oracle备份/恢复案例02——文件恢复
 
一、非归档模式下的备份与恢复

备份方案:采用OS冷备份

1、连接数据库并创建测试表
 
SQL*Plus: Release 9.2.0.1.0 - Production on Tue Jan 13 10:03:27 2009
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
 
SQL> connect sys/sys as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete

2、备份数据库

SQL> @D:\test\coldbak.sql

3、再插入记录

SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
A
------------------------
1
2

4、关闭数据库

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

5毁坏一个或多个数据文件,如删除user01.dbf

C:>del D:\ORACLE\ORADATA\DODO\USERS01.DBF

6、重新启动数据库,Oracle报错

SQL> startup
ORACLE instance started.
Total System Global Area  135338868 bytes
Fixed Size                   453492 bytes
Variable Size             109051904 bytes
Database Buffers           25165824 bytes
Redo Buffers                 667648 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'

在报警文件中,会有更详细的信息

Errors in file d:\oracle\admin\dodo\bdump\dodo_dbw0_3648.trc:
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'
ORA-27041: unable to open file
OSD-04002: 无法打开文件
O/S-Error: (OS 2) 系统找不到指定的文件。

7、拷贝备份复原到原来位置(restore过程)

C:>xcopy D:\DATABASE\BACK\USERS01.DBF D:\ORACLE\ORADATA\DODO/H/R/S

8、打开数据库,检查数据

SQL> alter database open;
Database altered.
SQL> select * from test;
A
------------------------
1

这里可以发现,数据库恢复成功,但在备份之后与崩溃之前的数据丢失了。
 

说明:

1、非归档模式下的恢复方案可选性很小,一般情况下只能有一种恢复方式,就是数据库的冷备份的完全恢复。仅仅需要拷贝原来的备份就可以(restore),不需要recover
2、这种情况下的恢复,可以完全恢复到备份的点上,但是可能是丢失数据的,在备份之后与崩溃之前的数据将全部丢失。
3、不管毁坏了多少数据文件或是联机日志或是控制文件,都可以通过这个办法恢复,因为这个恢复过程是Restore所有的冷备份文件,而这个备份点上的所有文件是一致的,与最新的数据库没有关系,就好比把数据库又放到了一个以前的“点”上。
4、对于非归档模式下,最好的办法就是采用OS的冷备份,建议不要用RMAN来作冷备份,效果不好,因为RMAN不备份联机日志,restore不能根本解决问题。
5、如果没有备份联机日志,如RMAN的备份,就需要利用不完全恢复(until cancel)的方法来重新创建联机日志文件。
 
 
 
二、归档模式下丢失或损坏一个数据文件

 
OS备份方案
 
在归档方式下损坏或丢失一个数据文件,如果存在相应的备份与该备份以来的归档日志,恢复还是比较简单的,可以作到尽量少的Down机时间,并能作到数据库的完全恢复。
 
1、连接数据库,创建测试表并插入记录

SQL*Plus: Release 9.2.0.1.0 - Production on Tue Jan 13 10:03:27 2009
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
 
SQL> connect sys/sys as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete

2、备份数据库

SQL> @D:\test\hotbak.sql

3、继续在测试表中插入记录

SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
A
---------------------------------------
1
2
SQL> alter system switch logfile;
System altered.
SQL> alter system switch logfile;
System altered.

4、关闭数据库,模拟丢失数据文件

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down

C:>del D:\ORACLE\ORADATA\DODO\USERS01.DBF

5、启动数据库错误,脱机该数据文件

SQL> startup
ORACLE instance started.
Total System Global Area  135338868 bytes
Fixed Size                   453492 bytes
Variable Size             109051904 bytes
Database Buffers           25165824 bytes
Redo Buffers                 667648 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'

还可以查看报警文件(见上一个恢复案例)或动态视图v$recover_file

idle> select * from v$recover_file;
 
     FILE# ONLINE  ONLINE_ ERROR                   CHANGE# TIME
---------- ------- ------- -------------------- ---------- ----------
         6 ONLINE  ONLINE  FILE NOT FOUND                0

脱机数据文件:

SQL> alter database datafile 6 offline drop;
Database altered.

6、打开数据库,拷贝备份回来(restore),恢复(recover)该数据文件,并联机
 
打开数据库:
SQL> alter database open;
Database altered.

拷贝备份从备份处
C:>xcopy D:\DATABASE\BACK\USERS01.DBF D:\ORACLE\ORADATA\DODO/H/R/S

恢复该数据文件
SQL> recover datafile 6;
Media recovery complete.

恢复成功,联机该数据文件:
SQL> alter database datafile 6 online;
Database altered.

7、检查数据库的数据(完全恢复)

SQL> select * from test;
A
--------------------------
1
2
 
说明:
 
1、采用热备份,需要运行在归档模式下,可以实现数据库的完全恢复,也就是说,从备份后到数据库崩溃时的数据都不会丢失。
2、可以采用全备份数据库的方式备份,对于特殊情况,也可以只备份特定的数据文件,如只备份用户表空间(一般情况下对于某些写特别频繁的数据文件,可以单独加大备份频率)
3、如果在恢复过程中,发现损坏的是多个数据文件,即可以采用一个一个数据文件的恢复方法(第5步中需要对数据文件一一脱机,第6步中需要对数据文件分别恢复),也可以采用整个数据库的恢复方法。
4、如果是系统表空间的损坏,不能采用此方法
 

RMAN备份方案
 
RMAN也可以进行联机备份,而且备份与恢复方法将比OS备份更简单可靠。
 
1、连接数据库,创建测试表并插入记录

SQL*Plus: Release 9.2.0.1.0 - Production on Tue Jan 13 10:03:27 2009
Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.
 
SQL> connect sys/sys as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete

2、备份数据库表空间users

C:>rman
Recovery Manager: Release 9.2.0.1.0 - Production
Copyright (c) 1995, 2002, Oracle Corporation.  All rights reserved.
RMAN> connect rcvcat rman/rman
connected to recovery catalog database
RMAN> connect target sys/sys
connected to target database: DODO (DBID=472976704)
RMAN> run{
2> allocate channel c1 type disk;
3> backup tag 'tsuser' format 'D:\DATABASE\RMAN\tsuser_%u_%s_%p';
4> tablespace users;
5> release channel c1;
6> }

allocated channel: c1
channel c1: sid=18 devtype=DISK
 
Starting backup at 2009-01-13
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backupset
input datafile fno=00006 name=D:\ORACLE\ORADATA\DODO\USERS01.DBF
channel c1: starting piece 1 at 2009-01-13
channel c1: finished piece 1 at 2009-01-13
piece handle=D:\DATABASE\RMAN\TSUSER_1NK4MUKK_55_1 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:35
Finished backup at 2009-01-13
 
Starting Control File and SPFILE Autobackup at 2009-01-13
piece handle=D:\ORACLE\ORADATA\DODO\RMANBACK\CTL_C-472976704-20090113-01 comment=NONE
Finished Control File and SPFILE Autobackup at 2009-01-13
 
released channel: c1

3、继续在测试表中插入记录

SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
A
---------------------------------
1
2

SQL> alter system switch logfile;
System altered.
SQL> r
1* alter system switch logfile;
System altered.

4、关闭数据库,模拟丢失数据文件

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down

C:>del D:\ORACLE\ORADATA\DODO\USERS01.DBF

5、启动数据库,检查错误

SQL> startup
ORACLE instance started.
 
Total System Global Area  135338868 bytes
Fixed Size                   453492 bytes
Variable Size             109051904 bytes
Database Buffers           25165824 bytes
Redo Buffers                 667648 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'

6、先打开数据库

SQL> alter database datafile 6 offline drop;
Database altered.

SQL> alter database open;
Database altered.

7、恢复该表空间

恢复脚本可以是恢复单个数据文件

run{
allocate channel c1 type disk;
restore datafile 3;
recover datafile 3;
sql 'alter database datafile 3 online';
release channel c1;
}

也可以是,恢复表空间

run{
allocate channel c1 type disk;
restore tablespace users;
recover tablespace users;
sql 'alter database datafile 3 online';
release channel c1;
}

过程如下:

C:>rman
Recovery Manager: Release 9.2.0.1.0 - Production
Copyright (c) 1995, 2002, Oracle Corporation.  All rights reserved.
RMAN> connect rcvcat rman/rman
connected to recovery catalog database
RMAN> connect target sys/sys
connected to target database: DODO (DBID=472976704)

RMAN> run{
2> allocate channel c1 type disk;
3> restore datafile 6;
4> recover datafile 6;
5> sql 'alter database datafile 6 online';
6> release channel c1;
7> }

allocated channel: c1
channel c1: sid=18 devtype=DISK
 
Starting restore at 2009-01-13
 
channel c1: starting datafile backupset restore
channel c1: specifying datafile(s) to restore from backup set
restoring datafile 00006 to D:\ORACLE\ORADATA\DODO\USERS01.DBF
channel c1: restored backup piece 1
piece handle=D:\DATABASE\RMAN\TSUSER_1NK4MUKK_55_1 tag=TSUSER params=NULL
channel c1: restore complete
Finished restore at 2009-01-13
 
Starting recover at 2009-01-13
 
starting media recovery
media recovery complete
 
Finished recover at 2009-01-13
 
sql statement: alter database datafile 6 online
 
released channel: c1

8、检查数据是否完整

SQL> select * from test;
A
--------------------------------
1
2
 

说明:
 
1、RMAN也可以实现单个表空间或数据文件的恢复,恢复过程可以在mount下或open方式下,如果在open方式下恢复,可以减少down机时间
2、如果损坏的是一个数据文件,建议offline并在open方式下恢复
3、这里可以看到,RMAN进行数据文件与表空间恢复的时候,代码都比较简单,而且能保证备份与恢复的可靠性,所以建议采用RMAN的备份与恢复
 
 
posted on 2008-12-15 21:58 decode360 阅读(204) 评论(0)  编辑  收藏 所属分类: 09.Recover

只有注册用户登录后才能发表评论。


网站导航: