标签:
由于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用户下设置。
标签:
原文地址:http://blog.csdn.net/jaune161/article/details/50453868