溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何實(shí)現(xiàn)自動(dòng)刪除歸檔日志的腳本

發(fā)布時(shí)間:2021-12-20 09:35:16 來(lái)源:億速云 閱讀:210 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹如何實(shí)現(xiàn)自動(dòng)刪除歸檔日志的腳本,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

自動(dòng)刪除歸檔日志的腳本(尤其是dataguard環(huán)境)

已有 236 次閱讀2011-12-16 21:02 |個(gè)人分類(lèi):oracle data guard

自動(dòng)刪除歸檔日志的腳本(尤其是dataguard環(huán)境)

在歸檔模式下,要時(shí)刻注意磁盤(pán)空間不要被歸檔撐爆,尤其在dataguard環(huán)境中,更是需要定期清理已經(jīng)apply的日志,以免把硬盤(pán)撐爆。

在自動(dòng)刪除日志需要考慮幾點(diǎn):
1. 日志必須是已經(jīng)被apply的
2. 日志備份已經(jīng)被備份過(guò)的
3. 為了保證一定的管理余地,不要apply后馬上刪除,而應(yīng)該根據(jù)實(shí)際情況設(shè)定一個(gè)刪除策略。
4. 腳本要能夠兼容primary和standby兩種狀態(tài),且自動(dòng)判斷狀態(tài)并執(zhí)行不同的邏輯,否則在切換后,如果忘記修改腳本,那就可能杯具了。

以下是我用于刪除歸檔日志的一個(gè)腳本,運(yùn)行這個(gè)腳本需要輸入一個(gè)參數(shù)來(lái)控制日志的保留時(shí)間。
這個(gè)腳本可用于primary端也可用于standby端,
1. 對(duì)于standby端,只要在保存周期內(nèi)且被apply的歸檔都會(huì)被刪除
2. 對(duì)于primary端,除了滿(mǎn)足保存周期以及被apply條件外,還要保證歸檔已經(jīng)被備份過(guò)才會(huì)被刪除

對(duì)于dataguard環(huán)境,雖然備份可以選擇在primary和standby端執(zhí)行,但如果壓力不是非常大的話(huà),為了管理方便,更建議在primary端執(zhí)行。

詳細(xì)腳本如下:

[oracle@dwapp1 DBA]$ cat delete_arch.sh
#!/bin/bash

##################################################################################################################
#
# This script is to delete the arch logs for the standby database after it has applied the logs to the instance.
#
##################################################################################################################

source /home/oracle/.bash_profile

#####################
usage()
{ #usage
echo " USAGE: `basename $0` $retention"
exit 2
}


ArgNum=1

if [ ! $# -eq $ArgNum ];then
echo " "
echo " Incorrect parameter"
usage
fi


retention=$1

script=`basename $0`

dir=/tmp
tmpf=$dir/.$script.tmp

# get archived log list for standby database
function GetLogListForStandby
{
sqlplus -S /nolog <<EOF > $tmpf
connect / as sysdba
set head off
set feedback off
set pages 0
select name from(
select name,sequence#,row_number() over(partition by a.sequence# order by name) rn,
count(decode(applied,'YES',1,null)) over (partition by a.sequence#) cn from v$archived_log a
where completion_time <sysdate-$retention
and a.resetlogs_id in (
select i.resetlogs_id from v$database_incarnation i where status = 'CURRENT')
)
where rn=1 and cn=1
order by sequence#;
exit
EOF
return
}

function GetDBRole
{
sqlplus -S /nolog <<EOF
connect / as sysdba
set head off
set feedback off
set pages 0
select controlfile_type from v$database;
exit
EOF
return
}

# get archived log list for primary database
function GetLogListForPrimary
{
sqlplus -S /nolog <<EOF > $tmpf
connect / as sysdba
set head off
set feedback off
set pages 0
select name from(
select name,sequence#,row_number() over(partition by a.sequence# order by name) rn,
sum(backup_count) over(partition by a.sequence# ) bk_cnt,
count(decode(applied,'YES',1,null)) over (partition by a.sequence#) cn
from v$archived_log a where completion_time <sysdate-$retention
and a.resetlogs_id in (
select i.resetlogs_id from v$database_incarnation i where status = 'CURRENT')
)
where rn=1 and cn=1 and bk_cnt>0
order by sequence#;
exit
EOF
return
}

function GetDBRole
{
sqlplus -S /nolog <<EOF
connect / as sysdba
set head off
set feedback off
set pages 0
select controlfile_type from v$database;
exit
EOF
return
}



# check database role
DBROLE=`GetDBRole`

NUM=0

if [ $DBROLE = "CURRENT" ];then
echo "It's a primary database ......"
# get archived log list for primary
GetLogListForPrimary

elif [ $DBROLE = "STANDBY" ];then
echo "It's a standby database ......"
# get archived log list for standby
GetLogListForStandby
fi

echo "deleting archived log files ......"

if [ -n $tmpf ]; then
for ARCH in `cat $tmpf`;do
if [ -f $ARCH ];then
NUM=`expr $NUM + 1`
rm -f $ARCH
fi
done
fi

rm -f $tmpf

echo "finished deleting $NUM files"


使用測(cè)試:需要輸入一個(gè)參數(shù),用于設(shè)定保存周期。以下例子是刪除3天前的歸檔

[oracle@dwapp1 DBA]$ ./delete_arch.sh 3
It's a primary database ......
deleting archived log files ......
finished deleting 12 files

設(shè)定定時(shí)任務(wù)自動(dòng)執(zhí)行

1 */4 * * * /home/oracle/DBA/delete_arch.sh 2


當(dāng)然,對(duì)于非dataguard環(huán)境或者dataguard環(huán)境的primary端,更建議使用RMAN來(lái)管理歸檔了。

以上是“如何實(shí)現(xiàn)自動(dòng)刪除歸檔日志的腳本”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI