[size=small]数据库归档设置 


1.归档参数设置: 

alter system set db_recovery_file_dest='/u01/oracle/' scope=spfile; ---归档日志的存放位置 

alter system set log_archive_dest_1='location=USE_DB_RECOVERY_FILE_DEST' scope=spfile; --指定第一个位置指向 

alter system set archive_lag_target=900 scope=spfile; ----指定15min切换一次归档日志 

--RTO(小时)*3600,最大3600,单位:秒 

一旦设置了archive_lag_target初始化参数,数据库将会周期性的检查实例的当前重做日志。如果遇到下列情况,实例将会切换日志: 

n 当前日志n秒前创建,当前日志的归档时间估计为m秒,n+m的值超过archive_lag_target参数设置。 

n 当前日志包含重做记录。 

alter system set db_recovery_file_dest_size=20G scope=spfile; ---指定归档目录大小 


2.开启归档: 

shutdown immediate; 

startup mount; 

alter database archivelog; alter database noarchivelog;//关闭归档 

alter database open; 


SQL> archive log list; 

Database log mode Archive Mode --归档模式 

Automatic archival Enabled 

Archive destination USE_DB_RECOVERY_FILE_DEST 

Oldest online log sequence 94 

Next log sequence to archive 96 

Current log sequence 96 


3.显示归档日志可达到的大小: 

show parameter recovery 

NAME TYPE VALUE 

------------------------------------ ----------- ------------------------------ 

db_recovery_file_dest string +FRA_DG 

db_recovery_file_dest_size big integer 150G 


4.查询目前归档日志的大小: 

select trunc(first_time),dest_id,sum(g.blocks*g.block_size)/1024/1024/1024 

from gv$archived_log g 

where g.first_time > trunc(sysdate - 7) and g.INST_ID='1' 

group by trunc(first_time),dest_id 

order by 2,1; 


5.每小时产生的日志的大小: 

select to_char(first_time,'yyyy-mm-dd hh24'),dest_id,sum(g.blocks*g.block_size)/1024/1024/1024 

from gv$archived_log g 

where g.first_time > trunc(sysdate - 4) and g.INST_ID='1' ---rac某一个节点的 

group by to_char(first_time,'yyyy-mm-dd hh24'),dest_id 

order by 2,1; 


1)发现已经接近150g,调大size。 

alter system set db_recovery_file_dest_size=300g,scope=both ; 

虽然默认是both,但是还是带上比较放心,以后调整时候一定要仔细查看邮件里面的gi fra monitor 

查看v$asm_diskgroup 有时候是不准的。 


2)只保留3天的归档日志信息。 

delete archivelog all completed before 'sysdate-3'; 

delete archivelog until time 'sysdate-3'; 

delete archivelog all completed until time 'sysdate -720/1440'; 删除12小时以前的。 1/1440 就是1分钟。 

系统会精确到秒级别的,因此,sysdate-2 是当前时间48小时以前的全都删掉。 


3)检测归档日志 

RMAN> crosscheck archivelog all; --命令的作用是将磁盘或者磁带上不存在的日志标记为expired 


4)删除过期的归档日志 

select * from v$flash_recovery_area_usage; 可看到已用多少,可重用的有多少 

RMAN> delete expired archivelog all; 

rman target / 

show all; 

CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON STANDBY; 执行此策略试试。在查看可重用正常否。 

在备库设置这个,表示已经在备库应用的日志就可以删除了。 


在主库设置 

CONFIGURE ARCHIVELOG DELETION POLICY TO SHIPPED TO ALL STANDBY 

表示已经传送到备库的日志就可以删除了。 


备库起来之后要recover managed standby database disconnect; 



6.查看每天的日志归档量:节点1 

SQL> select trunc(first_time),dest_id,sum(g.blocks*g.block_size)/1024/1024/1024 

from gv$archived_log g 

where g.first_time > trunc(sysdate - 7) and THREAD#='1' and CREATOR='ARCH' 

group by trunc(first_time),dest_id 

order by 2,1; 


7.按小时 

select to_char(first_time,'yyyy-mm-dd hh24'),dest_id,sum(g.blocks*g.block_size)/1024/1024/1024 

from gv$archived_log g 

where g.first_time > trunc(sysdate - 7) and THREAD#='1' 

group by to_char(first_time,'yyyy-mm-dd hh24'),dest_id 

order by 2,1; 



8.查询在线日志没有归档的 

SQL> select GROUP#,MEMBERS,ARCHIVED,STATUS from v$log where STATUS='INACTIVE'; 

 GROUP# MEMBERS ARC STATUS 

---------- ---------- --- ---------------- 

 1 1 YES INACTIVE 

 2 1 YES INACTIVE 

 3 1 YES INACTIVE 

 4 1 YES INACTIVE 

 5 1 YES INACTIVE 

 6 1 YES INACTIVE 

 7 1 YES INACTIVE 

 8 1 YES INACTIVE 

 9 1 YES INACTIVE 

 10 1 YES INACTIVE 

 11 1 NO INACTIVE 


 GROUP# MEMBERS ARC STATUS 

---------- ---------- --- ---------------- 

 12 1 NO INACTIVE 

 13 1 NO INACTIVE 

 14 1 NO INACTIVE 

 15 1 NO INACTIVE 

 16 1 NO INACTIVE 


果然有6个日志没有归档。 

做个checkpoint 

alter system checkpoint; 如果是rac的,那就要加上local 

alter system checkpoint local; 


删日志前,可以通过以下命令来查看日志归档到什么时间: 

select min(first_time) from v$archived_log x where x.dest_id=1 and x.BACKUP_COUNT=0; 

dest_id是归档到本地,backup_count=0是未归档。 


9.rman删除归档日志 

rman删除归档日志 删除N天以前的归档日志,有两个命令: 

delete archivelog until time 'sysdate-N' 

delete archivelog all completed before 'sysdate-N' 

list archivelog until time 'sysdate-1'; 此时是按照归档日志开始时间即fisrt_time作为截止时间 

list archivelog all completed before 'sysdate-1' 是按照归档日志完成时间即completion_time作为截止时间 

试想如果删除归档日志,还是采用delete archivelog all completed before 'sysdate-N' 


1)用RMAN连接目标DB: 

RMAN target sys/*****@orcl 


2)在RMAN命令窗口中,输入如下命令: 

crosscheck archivelog all; 

delete expired archivelog all; 

或者删除指定时间之前的archivelog: 

DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-7';(指定删除7天前的归档日志) 


3)其它相关命令: 

查看归档日志列表: 

list archivelog all; 

查看失效的归档日志列表: 

list expired archivelog all; 

查看过期备份 

RMAN> report obsolete; 

删除过期备份 

RMAN> delete obsolete; 


4)定期清除archivelog: 

可以将如下代码写成.bat文件,在控制面版的任务计划下添加新的定时任务: 

RMAN target sys/*****@orcl 

crosscheck archivelog all; 

delete expired archivelog all; 


5) 

DELETE ARCHIVELOG ALL COMPLETED BEFORE 'SYSDATE-7'; //删除七天前的归档 

DELETE ARCHIVELOG FROM TIME 'SYSDATE-7'; //删除七天到现在的归档 


6)制定cron定期删除 

删除归档日志的监控脚本 

[oracle@oratest ~]$ cat /dba/user_oracle/delete_tinadb.sh 

#!/bin/bash 

export ORACLE_SID=tinadb 

export ORACLE_HOME=/u01/oracle/ 

rman target / <<EOF 

run{ 

delete noprompt archivelog until time 'sysdate - 2'; 

} 

EOF 


[oracle@cnsz02pl0002 ~]$ crontab -l 

*/6 * * * * /dba/user_oracle/delete_tinadb.sh 


[/size]