利用脚本自动清理Oracle归档日志

背景

由于Oracle数据库开启了归档模式,经常因为归档日志文件占用空间过大而导致数据库无法正常连接,发现问题后需要手动清理归档日志,而发现问题时可能已经过去很长时间了,在生产环境中对用户的使用有非常严重的影响。

项目中涉及到多方数据对接,对数据库的频繁插入和更新会生成大量的归档日志,归档日志空间是500G,大概一周左右的时间归档日志空间就100%。

为解决这个问题这里使用脚本+定时任务自动清理归档日志,只保留5天归档。

归档日志清理脚本

#!/bin/bash
#Author wangchengwei
#FileName clear_archivelog.sh
#Date 2015-12-31
#DESC Delete all archivelog.

if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

#set env
echo "Oracle home:"$ORACLE_HOME
echo "Oracle SID:"$ORACLE_SID

$ORACLE_HOME/bin/rman target sys/oracle@rac log=/oracle/logs/rman.log <<EOF
crosscheck archivelog all;
delete noprompt expired archivelog all;
delete noprompt archivelog all completed before 'sysdate - 5';
exit;
EOF

另一种写法

$ORACLE_HOME/bin/rman log=/oracle/logs/rman.log <<EOF       
connect target sys/oracle@rac
run{
crosscheck archivelog all;    
delete noprompt expired archivelog all;    
delete noprompt archivelog all completed before 'sysdate - 5';    
}  

网上很多脚本都使用$ORACLE_HOME/bin/rman target /,而在实际使用中发现直接使用/会出现下面的问题。所以这里建议把用户名、密码和SID都加上。

Recovery Manager: Release 11.2.0.4.0 - Production on Thu Dec 31 10:38:08 2015

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database (not started)

RMAN> 
using target database control file instead of recovery catalog
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of crosscheck command at 12/31/2015 10:38:08
RMAN-12010: automatic channel allocation initialization failed
RMAN-06403: could not obtain a fully authorized session
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Linux-x86_64 Error: 2: No such file or directory

设置定时任务

oracle@racnode1:/$ crontab -e   #打开配置
#加入以下内容,定于每天11点执行
0 11 * * * /oracle/scripts/clear_archivelog.sh > /oracle/scripts/runlog.log

#
ps -ef | grep crond #判断定时服务是否启动
service crond start|stop|restart #启动、停止或重启服务

定时任务需要在oracle用户下设置。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大扑棱蛾子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值