听说过还原(restore)数据库,表空间及数据库文件,使用归档日志恢复(recover)数据库,表空间,数据库文件。咦,还有还原归档日志这一说法呢?没错,可能我们忽略了还原归档日志这一个过程,原因是还原归档日志通常情况下是Oracle在recover时自动完成的。大多数情况下我们是先还原数据库,恢复数据库,打开数据库。实际上在恢复数据库之前有一个动作,那就是还原归档日志,也就是将日志文件还原到缺省的归档位置,如果我们在备份归档日志时使用了delete [all] input子句的话。本文对此给出了单独还原归档日志以及恢复归档日志的示例以及restore archivelog的一些用法,仅仅是为了更好来的理解还原与恢复的过程,因为大多数情形下,数据文件被还原到缺省路径。如果是还原到非缺省路径,那就需要手动restore archivelog。
1、理解还原与恢复
还原(restore): 还原指将数据文件(可能受损)用之前的备份来替代或者复制到新的路径,这个是大多数情形和通常的说法。
恢复(recover): 将备份之后的归档日志apply到数据库,也就根据归档日志的事务将数据库刷新到特定或最新状态(通常在还原之后操作)。对于归档日志中那些已提交的事务进行前滚,未提交的事务进行回滚。
还原归档日志: 还原归档日志是位于还原数据库与恢复数据库之间的这么一个过程。它会将那些在备份归档日志时使用delete [all] input方式删除的归档日志还原到缺省的归档位置。在还原数据库之后,如果要做recover,也就是作介质恢复那就需要用到归档日志。那还原之后进行
recover需要的归档日志在哪里呢?归档日志在指定的归档路径那里,那到底有没有呢?如果有,还原时出现提示,归档日志已经在指定位置。
如果没有,但是备份的归档备份集那里有,也行啊。备份集里包含备份片,也就是打包了归档日志。那既然打包就要解包,解包到缺省路径或指定路径。这就是还原归档日志。
2、示例演示还原归档日志
--演示环境
--为了较好的模拟还原归档日志,我们仅仅使用了一个特定的数据文件进行copy方式备份,然后备份归档日志(备份时删除归档日志)
--接下来破坏数据文件,还原数据文件,还原归档日志文件,恢复日志文件。
[oracle@linux3 ~]$ cat /etc/issue
Enterprise Linux Enterprise Linux Server release 5.5 (Carthage)
Kernel r on an m
[oracle@linux3 ~]$ sqlplus -V
SQL*Plus: Release 11.2.0.1.0 Production
a、备份数据文件及归档日志
RMAN> list backup of archivelog all; --->列出当前数据库已经备份的归档日志
specification does not match any backup in the repository
RMAN> list backupset; --->列出当前数据库已存在的备份集
specification does not match any backup in the repository
SQL> select username,default_tablespace from dba_users where username='SCOTT'; -->查看用户scott所在的表空间及数据文件
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
SCOTT USERS
SQL> select name,file# from v$datafile where tablespace_name='USERS';
NAME FILE#
------------------------------------------------------------ ----------
/u01/database/sybo3/oradata/users01.dbf 4
SQL> conn scott/tiger;
Connected.
SQL> select name,sequence#,status,COMPLETION_TIME from v$archived_log where status='A'; -->当前系统无任何归档日志
no rows selected
SQL> host;
RMAN> copy datafile 4 to '/u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/users01.dbf'; -->使用rman copy方式备份数据文件
RMAN> list copy;
using target database control file instead of recovery catalog
specification does not match any control file copy in the repository
specification does not match any archived log in the repository
List of Datafile Copies
=======================
Key File S Completion Time Ckp SCN Ckp Time
------- ---- - ------------------- ---------- -------------------
3 4 A 2013/07/26 20:10:31 961662 2013/07/26 20:10:31
Name: /u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/users01.dbf
Tag: TAG20130726T201031
-->准备测试表用于验证还原恢复是否成功
SQL> create table t1 (seq varchar2(10),who varchar2(20));
SQL> insert into t1 select 'First','Robin' from dual;
SQL> commit;
SQL> alter system archive log current; -->产生归档日志
SQL> select name,sequence#,status,COMPLETION_TIME from v$archived_log where status='A';
NAME SEQUENCE# S COMPLETION_TIME
-------------------------------------------------------------------------------- ---------- - -----------------
/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4sy5ft_.arc 27 A 20130726 20:12:53
SQL> insert into t1 select 'Second','Robinson' from dual;
SQL> commit;
SQL> alter system archive log current; -->再次产生归档日志
SQL> select name,sequence#,status,COMPLETION_TIME from v$archived_log where status='A';
NAME SEQUENCE# S COMPLETION_TIME
-------------------------------------------------------------------------------- ---------- - -----------------
/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4sy5ft_.arc 27 A 20130726 20:12:53
/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_28_8z4t1q0s_.arc 28 A 20130726 20:14:47
-->下面备份归档日志并删除已备份的归当日志
RMAN> backup archivelog all delete input;
Starting backup at 2013/07/26 20:16:39
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=21 device type=DISK
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set --->备份集里包含的归档日志
input archived log thread=1 sequence=27 RECID=23 STAMP=821823173
input archived log thread=1 sequence=28 RECID=24 STAMP=821823287
input archived log thread=1 sequence=29 RECID=25 STAMP=821823400
channel ORA_DISK_1: starting piece 1 at 2013/07/26 20:16:40
channel ORA_DISK_1: finished piece 1 at 2013/07/26 20:16:41
piece handle=/u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/o1_mf_annnn_TAG20130726T201640_8z4t58tn_.bkp tag=TAG20130726T201640 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: deleting archived log(s) --->这里提示删除
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4sy5ft_.arc RECID=23 STAMP=821823173
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_28_8z4t1q0s_.arc RECID=24 STAMP=821823287
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_29_8z4t585k_.arc RECID=25 STAMP=821823400
Finished backup at 2013/07/26 20:16:41
b、模拟破坏数据文件
SQL> insert into t1 select 'Last','End of test' from dual;
SQL> commit;
SQL> ho cat /dev/null>/u01/database/sybo3/oradata/users01.dbf --->破坏数据文件
SQL> select * from t1; --->此时buffer cache依旧可以查询到数据
SEQ WHO
---------- --------------------
First Robin
Second Robinson
Last End of test
SQL> alter system checkpoint; --->实施检查点进程
System altered.
SQL> select * from t1; --->此时数据文件不可访问
select * from t1
*
ERROR at line 1:
ORA-00376: file 4 cannot be read at this time
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
SQL> select * from v$recover_file;
select * from v$recover_file
*
ERROR at line 1:
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
c、还原与恢复受损的数据文件
SQL> select TABLESPACE_NAME,STATUS from dba_tablespaces where tablespace_name='USERS'; --->tablespace 依旧是Online
TABLESPACE_NAME STATUS
------------------------------ ---------
USERS ONLINE
SQL> alter tablespace users offline immediate; --->offline受损的tablespace
Tablespace altered.
SQL> select tablespace_name,status from dba_tablespaces where tablespace_name='USERS'; --->此时状态为offline
TABLESPACE_NAME STATUS
------------------------------ ---------
USERS OFFLINE
RMAN> restore datafile 4; --->此时使用restore datafile 方式提示失败
Starting restore at 2013/07/26 20:30:20
using channel ORA_DISK_1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 07/26/2013 20:30:20
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
RMAN-06010: error while looking up datafile: 4
RMAN> restore tablespace users; --->此时使用restore tablespace 方式提示失败,看来,对于copy方式的备份,必须要copy回去
Starting restore at 2013/07/26 20:31:12
using channel ORA_DISK_1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 07/26/2013 20:31:12
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
RMAN-06019: could not translate tablespace name "USERS"
-->下面直接使用copy方式进行还原
SQL> ho cp /u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/users01.dbf /u01/database/sybo3/oradata/users01.dbf
-->接下来我们还原归档日志,制定了from sequence子句,实际上,如果我们没有指定restore archivelog,在recover时也会自动完成还原归档日志
RMAN> restore archivelog from sequence 27;
Starting restore at 2013/07/26 20:36:55
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log restore to default destination --->这个地方是关键提示,还原到缺省位置
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=27
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=28
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=29
channel ORA_DISK_1: reading from backup piece /u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/o1_mf_annnn_TAG20130726T201640_8z4t58tn_.bkp
channel ORA_DISK_1: piece handle=/u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/o1_mf_annnn_TAG20130726T201640_8z4t58tn_.bkp tag=TAG20130726T201640
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 2013/07/26 20:36:57
--Author : Robinson
--Blog : http://www.linuxidc.com
-->此时在缺省的路径下可以看到已经被还原的归档日志文件
SQL> ho ls -hltr /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/*
-rw-r----- 1 oracle oinstall 13K Jul 26 20:36 /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_29_8z4vc85w_.arc
-rw-r----- 1 oracle oinstall 4.5K Jul 26 20:36 /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_28_8z4vc85z_.arc
-rw-r----- 1 oracle oinstall 4.0M Jul 26 20:36 /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4vc85o_.arc
-->进行介质恢复
RMAN> recover datafile 4;
Starting recover at 2013/07/26 20:39:56
using channel ORA_DISK_1
starting media recovery ---->下面提示归档日志已经存在,是因为我们之前做了restore archivelog
archived log for thread 1 with sequence 27 is already on disk as file /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4vc85o_.arc
archived log for thread 1 with sequence 28 is already on disk as file /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_28_8z4vc85z_.arc
archived log for thread 1 with sequence 29 is already on disk as file /u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_29_8z4vc85w_.arc
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4vc85o_.arc thread=1 sequence=27
media recovery complete, elapsed time: 00:00:00
Finished recover at 2013/07/26 20:39:56
-->online tablespace
SQL> alter tablespace users online;
Tablespace altered.
-->验证结过成功
SQL> select * from t1;
SEQ WHO
---------- --------------------
First Robin
Second Robinson
Last End of test
3、restore archivelog 的其它用法
restore archivelog all; 还原全部归档日志文件
restore archivelog from logseq 27 ; 还原log sequence为27之后的所有归档日志
restore archivelog from logseq 27 until logseq 29; 还原log sequence为27到29这几个归档日志
restore archivelog from time 'sysdate-7'; 还原七天以内的归档日志
restore archivelog until logseq 29; 还原到seqence 为29的日志文件为止
set archivelog destination to '/u01/database/sybo5/arch';设定还原日志文件到新路径,如
run{
set archivelog destination to '/u01/database/sybo5/arch';
restore archivelog low logseq 27;}
关于resoter archive的更多用法: http://docs.oracle.com/cd/B19306_01/backup.102/b14194/rcmsynta008.htm
推荐阅读:
Oracle EBS 克隆之数据库克隆
基于用户管理的同机数据库克隆
基于 RMAN 的同机数据库克隆
RMAN 异机复制数据库