合 Oracle定时删除归档日志文件(Windows+Linux+DG)
Tags: OracleLinuxWindowsDBA脚本脚本分享归档日志定时删除归档日志
Linux环境
单机或rac非DG环境
对于单实例的数据库,在没有DG的环境下,可以使用如下的脚本,保留归档日志2天:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | mkdir -p /home/oracle/log cat > /home/oracle/del_arc.sh <<"EOF0" #!/bin/bash export ORACLE_SID=lhrdb export ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1 export NLS_LANG="american_america.ZHS16GBK" export NLS_DATE_FORMAT="YYYY-MM-DD HH24:Mi:SS" LOG_DIR=/home/oracle/log DATEL=`date '+%Y-%m-%d'` LOG_NAME=${LOG_DIR}/del_arc_${ORACLE_SID}_${DATEL}".log" $ORACLE_HOME/bin/rman log=$LOG_NAME target / <<EOF crosscheck archivelog all; delete force noprompt archivelog all completed before 'sysdate-2'; exit; EOF EOF0 |
修改ORACLE_SID和ORACLE_HOME即可。
DG环境
对于DG环境,需要删除已经应用到备库的归档日志,可以使用如下的脚本,在主备库都需要部署:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | mkdir -p /home/oracle/log cat > /home/oracle/del_arc_dg.sh <<"EOF0" #!/bin/bash export ORACLE_SID=lhrdb export ORACLE_HOME=/u01/app/oracle/product/12.2.0/dbhome_1 export NLS_DATE_FORMAT="YYYY-MM-DD HH24:Mi:SS" LOG_DIR=/home/oracle/log DATEL=`date '+%Y-%m-%d'` LOG_NAME=${LOG_DIR}/deladgarc_${ORACLE_SID}_${DATEL}".log" SQL_NAME=${LOG_DIR}/deladgarc_${ORACLE_SID}_${DATEL}".sql" $ORACLE_HOME/bin/sqlplus -S / as sysdba <<EOF set feedback off heading off pagesize 0 linesize 100 col exec_sql format a50 spool ${SQL_NAME} SELECT 'delete archivelog sequence ' || A.SEQUENCE# || ' thread ' || A.THREAD# || ';' EXEC_SQL FROM V\$ARCHIVED_LOG A WHERE (A.THREAD#, A.SEQUENCE#, a.RESETLOGS_CHANGE#) IN (SELECT b.THREAD#, b.SEQUENCE#, b.RESETLOGS_CHANGE# FROM V\$ARCHIVED_LOG B WHERE B.APPLIED = 'YES' AND b.COMPLETION_TIME <= SYSDATE - 8) AND a.NAME NOT IN (SELECT b.DESTINATION FROM v\$archive_dest b WHERE b.DESTINATION IS NOT NULL) AND A.COMPLETION_TIME <= SYSDATE - 8 ORDER BY A.THREAD#, A.SEQUENCE#; spool off exit EOF $ORACLE_HOME/bin/rman log=$LOG_NAME target / <<EOF crosscheck archivelog all; delete noprompt expired archivelog all; @${SQL_NAME} exit; EOF EOF0 |
配置定时任务
配置定时任务(每天下午5点执行删除任务):
1 2 3 4 5 6 7 | chmod +x del_arc.sh crontab -e 0 17 * * * /home/oracle/del_arc.sh systemctl status crond ps -ef | grep crond |
Windows环境
1、配置脚本
创建文件del_arch.sql文件,内容如下:
1 2 3 4 5 6 | connect target / run{ crosscheck archivelog all; delete noprompt expired archivelog all; delete noprompt archivelog until time 'sysdate-2/24'; } |
创建文件del_arch.bat文件,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @echo off chcp 65001 set ORACLE_SID=orcl set ORACLE_HOME=D:\app\Administrator\product\11.2.0\dbhome_1 SET PATH=%ORACLE_HOME%\bin;%PATH% set "filename=del_archivelog_task_log_%date:~3,4%%date:~8,2%%date:~11,2%%time:~0,2%%time:~3,2%%time:~6,2%.log" ( echo. echo ========================= begin delete arch ...... %date% %time% ========================= echo. rman cmdfile=D:\app\Administrator\delete_arch\del_arch.sql echo. echo ========================= 【end delete arch %date% %time%】 ========================= echo. )>>D:\delete_arch\%filename% 2>&1<nul |
双击del_arch.bat
脚本,然后将自动生成日志文件del_archivelog_task_log_20240725.log
。
2、添加系统任务计划
位置:开始 => 所有程序 => 附件 => 系统工具 => 任务计划
打开任务计划