前两天备库的归档日志尚未应用,结果被脚本给压缩了,导致备库向主库再次请求归档日志时主库的归档日志也被压缩而找不到相应文件,结果报错.因以前的压缩及删除归档的脚本存在过多弊端,故重新写了一个通用版的压缩并删除归档程序.
程序要求: 主库或备库部署相同的脚本,并在磁盘上尽可能多的保留归档日志,在空间紧张时删除最早的日志,达到空间循环使用的目的.
脚本如下:
####################压缩脚本####################
#!/bin/sh
#fuction gzip archivelog
#created by qds 20100804
. /oracle9/.profile 2>/dev/null;
ORACLE_SID='para';
ARCH_HOME='/oracle9/app/oracle/product/9.2.0/dbs/arch';
ARCH_POSTFIX='arc';
cd /oracle9/maindb/dbshell;
function get_role {
sqlplus -S "/as sysdba" > ./log/get_role.txt < set head off
set echo off
select database_role from v\$database;
exit;
EOF
db_role=`sed '/^$/d' ./log/get_role.txt`
if [ "$db_role" == "PRIMARY" ]; then
return 1;
elif [ "$db_role" == "PHYSICAL STANDBY" ]; then
return 2;
else
return 3;
fi;
}
if [ `ps -ef |grep gzip_archive.sh|wc -l` -ge 3 ]; then
echo "the process has been running";
return;
fi;
get_role;
myflag=$?;
if [ $myflag -eq 1 ]; then
echo "this database is primary database";
#find $ARCH_HOME/ -name "*.$ARCH_POSTFIX" -type f -cmin +60 -exec ls -l {} \;
#find $ARCH_HOME/ -name "*.$ARCH_POSTFIX" -type f -cmin +60 -exec gzip {} \;
ls -lt $ARCH_HOME/*.$ARCH_POSTFIX 2>/dev/null|awk '{if(NR>3) print $NF}' >./log/gzip_archive.txt
for i in `cat ./log/gzip_archive.txt`
do
echo `ls -l $i`;
gzip $i;
done;
return;
elif [ $myflag -eq 2 ]; then
echo "this database is standby database";
sqlplus -S "/as sysdba" > ./log/db_archived_log.txt < set head off
set echo off
set feedback off
select name from v\$archived_log where applied='YES' and deleted='NO' and first_time>trunc(sysdate-90);
EOF
ls ${ARCH_HOME}/*.$ARCH_POSTFIX 1> ./log/sys_archived_log.txt 2>/dev/null
for i in `cat ./log/db_archived_log.txt`
do
for j in `cat ./log/sys_archived_log.txt`
do
if [ $i == $j ]; then
echo `ls -l $i` ;
gzip $i;
continue;
fi;
done;
done;
return;
else
echo "WARNING:this database is not the monitor type";
return;
fi;
####################删除脚本####################
#!/bin/sh
#fuction gzip and delete the archivelogs
#created by qds 20100803
ARCH_HOME='/oracle9/app/oracle/product/9.2.0/dbs/arch';
OS_TYPE=`uname`;
SPACE_LIMIT=60;
ARCH_POSTFIX='arc.gz';
DEL_ARC_DAYS=60;
DEL_ARC_INTERVAL=2;
SPACE_USED=0;
cd /oracle9/maindb/dbshell;
ARCH_ROOT=`echo "$ARCH_HOME"|awk -F / '{ print "/"$2}'`;
function get_space_used {
if [ $OS_TYPE='AIX' ]; then
SPACE_USED=`df -g |grep $ARCH_ROOT|awk '{print $4}'|sed 's/%//g'`
elif [ $OS_TYPE='Linux' ];then
SPACE_USED=`df -h |grep $ARCH_ROOT|awk '{print $5}'|sed 's/%//g'`
fi;
echo $SPACE_USED
}
if [ `ps -ef |grep del_archive.sh|wc -l` -ge 3 ]; then
echo "the process has been running";
return;
fi;
while [ "`get_space_used`" -ge $SPACE_LIMIT ]
do
#ls -lrt $ARCH_HOME/*.$ARCH_POSTFIX|awk '{if(NR<10) print $NF}'|xargs rm -f;
find $ARCH_HOME -name "*$ARCH_POSTFIX" -type f -mtime +$DEL_ARC_DAYS -exec ls -l {} \;
find $ARCH_HOME -name "*$ARCH_POSTFIX" -type f -mtime +$DEL_ARC_DAYS -exec rm {} \;
DEL_ARC_DAYS=`expr $DEL_ARC_DAYS - $DEL_ARC_INTERVAL`;
if [ $DEL_ARC_DAYS -le 1 ]; then
break;
fi;
done;
注:由于Aix5.2中不支持find的cmin查找,故注释掉,使用了ls的方式.
写该脚本时曾参考meco的http://www.easyora.net/blog/dg_scripts_list.html#comment-307脚本
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11134237/viewspace-670802/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/11134237/viewspace-670802/