{"id":4289,"date":"2025-06-17T12:35:43","date_gmt":"2025-06-17T12:35:43","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=4289"},"modified":"2025-06-17T12:35:45","modified_gmt":"2025-06-17T12:35:45","slug":"automatically-archive-and-compress-oracle-alert-logs","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/shell-scripts\/automatically-archive-and-compress-oracle-alert-logs\/","title":{"rendered":"Automatically Archive and Compress Oracle Alert Logs"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Oracle alert logs grow continuously and can consume significant disk space if not managed regularly. This automated script helps keep the logs tidy by archiving and compressing the current alert log file. Once archived, Oracle will automatically generate a fresh log.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is especially useful in production environments for both <strong>RAC<\/strong> and <strong>single-instance<\/strong> setups.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Shell Script: <code>rotatealertlog.sh<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>################################################################################\n# Title   : Oracle Alert Log Rotation Script\n# Script  : rotatealertlog.sh\n# Purpose : Automatically rotates and compresses Oracle alert logs.\n#\n# Features:\n#   - Detects alert log location dynamically from v$parameter\n#   - Supports argument-based ORACLE_SID input\n#   - Backs up and compresses current alert log\n#   - Ensures Oracle recreates a new log file\n#   - Cron-friendly and RAC-compatible (run per instance)\n#\n# Author  : W3Buddy\n# Version : 1.1\n################################################################################\n\n#!\/bin\/bash\n\n# === Set Oracle Environment ===\nORACLE_SID=$1; export ORACLE_SID\nORACLE_HOME=\/oracle\/app\/oracle\/product\/12.1.0.2\/dbhome_1\nORACLE_BASE=\/oracle\/app\/oracle; export ORACLE_BASE\nLD_LIBRARY_PATH=$ORACLE_HOME\/lib:\/usr\/lib; export LD_LIBRARY_PATH\nPATH=$ORACLE_HOME\/bin:$PATH; export PATH\nTO_DATE=\"20$(date +%y%m%d)\"; export TO_DATE\n\n# === Get Alert Log Directory ===\nVAL_DUMP=$(${ORACLE_HOME}\/bin\/sqlplus -S \/nolog &lt;&lt;EOF\nconn \/ as sysdba\nset pages 0 feedback off\nSELECT value FROM v\\\\$parameter WHERE name='core_dump_dest';\nexit;\nEOF\n)\n\nLOCATION=$(echo ${VAL_DUMP} | awk '{print $1}')\nALERTDB=${LOCATION}\/alert_${ORACLE_SID}.log\nELOG=$(echo ${ALERTDB} | sed s\/cdump\/trace\/)\n\n# === Archive and Compress Alert Log ===\nif &#91; -e \"$ELOG\" ]; then\n    mv ${ELOG} ${ELOG}_${TO_DATE}\n    gzip ${ELOG}_${TO_DATE}\n    > ${ELOG}\n    echo \"&#91;$(date)] Alert log archived and compressed successfully: ${ELOG}_${TO_DATE}.gz\"\nelse\n    echo \"&#91;$(date)] Alert log not found: $ELOG\"\nfi\n\nexit 0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f Setup Instructions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># ----------------------------------------------\n# 1. Create the script file\n# ----------------------------------------------\ncd \/u01\/app\/oracle\/dbscripts\nvi rotatealertlog.sh\n\n# (Paste the full script above into the file and save)\n\n# ----------------------------------------------\n# 2. Make the script executable\n# ----------------------------------------------\nchmod +x rotatealertlog.sh\n\n# ----------------------------------------------\n# 3. Test the script manually\n# ----------------------------------------------\n.\/rotatealertlog.sh PRODDB\n\n# Confirm alert log was archived and compressed:\nls -lh \/u01\/app\/oracle\/diag\/rdbms\/proddb\/PRODDB\/trace\/alert_PRODDB.log*\n\n# ----------------------------------------------\n# 4. Schedule with cron (weekly on Friday 10:00 PM)\n# ----------------------------------------------\ncrontab -e\n\n# Add this line:\n00 22 * * 5 \/u01\/app\/oracle\/dbscripts\/rotatealertlog.sh PRODDB >> \/tmp\/rotate_alertlog.log 2>&amp;1\n\n# ----------------------------------------------\n# 5. Confirm the cron entry\n# ----------------------------------------------\ncrontab -l<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Oracle alert logs grow continuously and can consume significant disk space if not managed regularly. This automated script helps keep the logs tidy by archiving and compressing the current alert log file. Once archived, Oracle will automatically generate a fresh log. This is especially useful in production environments for both RAC and single-instance setups. Shell [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,990],"class_list":["post-4289","cposts","type-cposts","status-publish","hentry","category-notes","category-shell-scripts"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/4289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=4289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=4289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}