一、基本概念
Oracle可以将联机日志文件保存到多个不同的位置,将联机日志转换为归档日志的过程称之为归档。相应的日志被称为归档日志。
只有在归档的模式下才能触发归档操作。
其实理解归档日志很简单,redo log file会把所有的操作全部记下来,但是日志组会覆盖(循环),那么我们把redo log file全部保存下来,换句话说就是归档。就有了归档日志。当然这里就要启用归档模式了。
对于日志组而言,当然要归档完毕,才能被覆盖,10g之后,开启自动归档功能,后台进程arcn会把在一定条件下日志组切换后会对日志组进行归档。
归档日志文件的主要作用:
1、恢复数据库
2、更新standby数据库
3、使用LogMiner提取历史日志的相关信息
二、日志的模式
1、非归档模式
不适于与生产数据库
1)创建数据库时,缺省的日志管理模式为非归档模式
当然这里日志切换后,dbwr把修改过的数据块记录写入数据文件,状态变为inactive。这时候可以被覆盖。
2)备份与恢复
这种状态下只有关闭数据库备份才是有效备份(打开数据库的备份不建议使用),单个表空间备份,需要离线表空间或者数据文件(offline或者only read)
应当备份所有的数据文件、控制文件(单个)、参数文件、密码文件、联机日志文件
这种模式下,恢复只能恢复到最近的备份。
不支持联机备份表空间,一个表空间损坏将导致整个数据库不可用,需要删除掉损坏的表空间或从备份恢复
2、归档模式
1)这种模式下能够对日志文件进行归档,生产库强烈建议使用。
需要手动开启(Oracle 9i需要设置参数log_archive_start=true才能够进行自动归档)
在日志切换时,dbwr把修改过的数据块记录写入数据文件,并且把日志copy(归档)完毕,日志组才可以被覆盖。
归档日志的信息记录在控制文件中
必须要有足够的磁盘空间
2)备份与恢复
支持热备份,且当某个非系统表空间损坏,数据库仍然处于可用状态,且支持在线恢复
使用归档日志能够实现联机或脱机时点恢复(即可以恢复到指定的时间点、指定的归档日志或指定的SCN)
三、两种模式的切换设置及手动归档
1、非归档模式到归档模式
a.一致性关闭数据库(shutdown [immediate | transactional |normal])
b.启动到mount阶段(startup mount)
c.切换到归档模式(alter database archivelog [manual])
d.切换到open阶段(alter database open)
e.对数据做一个完整备份(full backup)
下面是非归档到归档的操作
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Current log sequence 7
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 1218992 bytes
Variable Size 109053520 bytes
Database Buffers 171966464 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Current log sequence 7
SQL> alter database archivelog;
Database altered.
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Next log sequence to archive 7
Current log sequence 7
SQL> alter database open;
Database altered.
SQL> select log_mode from v$database;
LOG_MODE
------------------------------------
ARCHIVELOG
SQL>
2.归档模式切换到非归档模式
a.一致性关闭数据库(shutdown [immediate | transactional |normal])
b.启动到mount阶段(startup mount)
c.切换到归档模式(alter database noarchivelog )
d.切换到open阶段(alter database open)
和上面过程一样,把启用归档改为关闭归档
SQL> select log_mode from v$database;
LOG_MODE
------------------------------------
ARCHIVELOG
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 285212672 bytes
Fixed Size 1218992 bytes
Variable Size 109053520 bytes
Database Buffers 171966464 bytes
Redo Buffers 2973696 bytes
Database mounted.
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Next log sequence to archive 7
Current log sequence 7
SQL> alter database noarchivelog;
Database altered.
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Current log sequence 7
SQL> alter database open;
Database altered.
SQL> select log_mode from v$database;
LOG_MODE
------------------------------------
NOARCHIVELOG
SQL>
SQL> archive log list;
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 22
Current log sequence 24
SQL> select log_mode from v$database;
LOG_MODE
------------
NOARCHIVELOG
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1570009088 bytes
Fixed Size 2207128 bytes
Variable Size 1090519656 bytes
Database Buffers 469762048 bytes
Redo Buffers 7520256 bytes
Database mounted.
SQL> alter database archivelog manual;
Database altered.
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 22
Next log sequence to archive 24
Current log sequence 24
SQL> alter database open;
Database altered.
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC
---------- ---------- ---------- ---------- ---------- ---------- ---
STATUS FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME
---------------- ------------- --------- ------------ ---------
1 1 22 52428800 512 1 YES
INACTIVE 1392578 15-JUL-12 1415822 15-JUL-12
2 1 23 52428800 512 1 YES
INACTIVE 1415822 15-JUL-12 1436920 16-JUL-12
3 1 24 52428800 512 1 NO
CURRENT 1436920 16-JUL-12 2.8147E+14
SQL> alter system archive log current;
System altered.
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC
---------- ---------- ---------- ---------- ---------- ---------- ---
STATUS FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME
---------------- ------------- --------- ------------ ---------
1 1 25 52428800 512 1 NO
CURRENT 1444599 16-JUL-12 2.8147E+14
2 1 23 52428800 512 1 YES
INACTIVE 1415822 15-JUL-12 1436920 16-JUL-12
3 1 24 52428800 512 1 YES
ACTIVE 1436920 16-JUL-12 1444599 16-JUL-12
SQL> alter system archive log all;
alter system archive log all
*
ERROR at line 1:
ORA-00271: there are no logs that need archiving
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1570009088 bytes
Fixed Size 2207128 bytes
Variable Size 1090519656 bytes
Database Buffers 469762048 bytes
Redo Buffers 7520256 bytes
Database mounted.
SQL> alter database archivelog;
Database altered.
SQL> alter database open;
Database altered.
SQL> alter system archive log current;
System altered.
SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES BLOCKSIZE MEMBERS ARC
---------- ---------- ---------- ---------- ---------- ---------- ---
STATUS FIRST_CHANGE# FIRST_TIM NEXT_CHANGE# NEXT_TIME
---------------- ------------- --------- ------------ ---------
1 1 25 52428800 512 1 YES
ACTIVE 1444599 16-JUL-12 1445324 16-JUL-12
2 1 26 52428800 512 1 NO
CURRENT 1445324 16-JUL-12 2.8147E+14
3 1 24 52428800 512 1 YES
INACTIVE 1436920 16-JUL-12 1444599 16-JUL-12
4.归档进程的调整
默认两个arcn进程
通过LOG_ARCHIVE_MAX_PROCESSES参数来设置,一般情况下不需要修改该参数,Oracle会根据归档情况自动启动额外的归档进程,也可以动态的增加或减少归档进程数,比如有些业务在月底需要进行大量归档,平时则归档较少,对此可以动态修改。
SQL> ALTER SYSTEM SET LOG_ARCHIVE_MAX_PROCESSES=3;
System altered.
oracle:/oracle# ps -ef | grep arc
oracle 4522192 1 0 21:11:15 - 0:00 ora_arc0_wolf
oracle 5374030 1 0 21:11:16 - 0:00 ora_arc1_wolf
root 6619372 7012574 0 21:25:52 pts/0 0:00 grep arc
oracle 9896028 1 0 21:11:17 - 0:00 ora_arc2_wolf
5、归档的位置设置
查看归档位置
show arch
从上面的官方文档可以看出,可以有两种归档方式
SQL> alter system set log_archive_dest_1='location=/oracle ';
System altered.
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /oracle
Oldest online log sequence 26
Next log sequence to archive 28
Current log sequence 28
SQL>
如果有远程的归档备份,按照上面官方文档去操作。
alter system set log_archive_dest_2 = 'SERVICE=standby1';
修改归档目的地好像要在mount状态下
小于等于2个归档
这种方式一般不用,不多说。
6、归档日志命名格式
设置LOG_ARCHIVE_FORMAT参数,Unix下的缺省设置为LOG_ARCHIVE_FORMAT=%t_%s_%r.dbf
%s: 日志序列号:
%S: 日志序列号(带有前导)
%t: 重做线程编号.
%T: 重做线程编号(带有前导)
%a: 活动ID号
%d: 数据库ID号
%r RESETLOGS的ID值.
alter system set log_archive_format = 'arch_%t_%s_%r.arc';
SQL> show parameter arch
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
archive_lag_target integer 0
log_archive_config string
log_archive_dest string
log_archive_dest_1 string location=/oracle
log_archive_dest_10 string
log_archive_dest_11 string
log_archive_dest_12 string
log_archive_dest_13 string
log_archive_dest_14 string
log_archive_dest_15 string
log_archive_dest_16 string
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_17 string
log_archive_dest_18 string
log_archive_dest_19 string
log_archive_dest_2 string
log_archive_dest_20 string
log_archive_dest_21 string
log_archive_dest_22 string
log_archive_dest_23 string
log_archive_dest_24 string
log_archive_dest_25 string
log_archive_dest_26 string
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_27 string
log_archive_dest_28 string
log_archive_dest_29 string
log_archive_dest_3 string
log_archive_dest_30 string
log_archive_dest_31 string
log_archive_dest_4 string
log_archive_dest_5 string
log_archive_dest_6 string
log_archive_dest_7 string
log_archive_dest_8 string
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_9 string
log_archive_dest_state_1 string enable
log_archive_dest_state_10 string enable
log_archive_dest_state_11 string enable
log_archive_dest_state_12 string enable
log_archive_dest_state_13 string enable
log_archive_dest_state_14 string enable
log_archive_dest_state_15 string enable
log_archive_dest_state_16 string enable
log_archive_dest_state_17 string enable
log_archive_dest_state_18 string enable
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_state_19 string enable
log_archive_dest_state_2 string enable
log_archive_dest_state_20 string enable
log_archive_dest_state_21 string enable
log_archive_dest_state_22 string enable
log_archive_dest_state_23 string enable
log_archive_dest_state_24 string enable
log_archive_dest_state_25 string enable
log_archive_dest_state_26 string enable
log_archive_dest_state_27 string enable
log_archive_dest_state_28 string enable
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_state_29 string enable
log_archive_dest_state_3 string enable
log_archive_dest_state_30 string enable
log_archive_dest_state_31 string enable
log_archive_dest_state_4 string enable
log_archive_dest_state_5 string enable
log_archive_dest_state_6 string enable
log_archive_dest_state_7 string enable
log_archive_dest_state_8 string enable
log_archive_dest_state_9 string enable
log_archive_duplex_dest string
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_format string arh_%t_%s_%r.log
log_archive_local_first boolean TRUE
log_archive_max_processes integer 4
log_archive_min_succeed_dest integer 1
log_archive_start boolean FALSE
log_archive_trace integer 0
standby_archive_dest string ?/dbs/arch
SQL>
四、归档日志相关视图
v$archived_log -->从控制文件中获得归档的相关信息
v$archive_dest -->归档路径及状态
v$log_history -->控制文件中日志的历史信息
v$database -->查看数据库是否处于归档状态
v$archive_processes -->归档相关的后台进程信息