Log Rotation and Cleanup Scripts

Log Rotation and Cleanup Scripts

🛠️ Why Log Rotation and Cleanup Matters

In real-world observability:

✅ Logs grow very fast:

  • Application logs
  • Monitoring logs
  • System audit logs

✅ If you don't rotate or clean up:

  • The disk gets full
  • Servers crash
  • Incidents become harder to debug

Managing logs = Managing system health and reliability.


🛠️ What is Log Rotation?

Log rotation means:

  • Archiving old logs
  • Compressing them to save space
  • Starting new fresh logs
  • Deleting very old logs after a few days

✅ Keeps your system healthy and searchable without eating up all disk space.


📚 Example 1: Manual Log Rotation Script (Simple)

#!/bin/bash

log_file="/var/log/myapp.log"
timestamp=$(date +%Y%m%d_%H%M%S)
archive_dir="/var/log/archive"

mkdir -p $archive_dir

cp $log_file $archive_dir/myapp.log.$timestamp
> $log_file  # Empty the original log file

echo "Log rotated and archived as myapp.log.$timestamp"

        

✅ What happens:

  • Copy current log with timestamp
  • Empty original log file
  • Store archive safely


📚 Example 2: Auto Compress Old Logs

#!/bin/bash

find /var/log/archive -name "*.log*" -mtime +7 -exec gzip {} \\;

        

✅ Meaning:

  • Find log files older than 7 days
  • Compress them into .gz

✅ Compression saves disk space heavily in observability setups!


📚 Example 3: Cleanup Very Old Logs

#!/bin/bash

find /var/log/archive -name "*.gz" -mtime +30 -exec rm {} \\;

        

✅ Meaning:

  • Find compressed logs older than 30 days
  • Delete them safely

✅ Always keep a balance between retention and disk usage.


🛠️ How to Automate Log Rotation with Cron

✅ Add this to crontab:

0 2 * * * /home/ubuntu/scripts/rotate_logs.sh > /tmp/rotate_logs.log 2>&1

        

✅ Rotate logs every night at 2 AM

✅ Log rotation itself logs to /tmp/rotate_logs.log

✅ Self-healing observability — no human intervention needed.


🛠️ Bonus: Linux logrotate Tool

✅ Linux has a built-in tool called logrotate!

✅ For complex setups, you can configure /etc/logrotate.d/yourapp:

/var/log/myapp.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
    create 0640 ubuntu ubuntu
}

        


  • Rotate daily
  • Keep 7 copies
  • Compress old logs
  • Skip missing or empty logs
  • Create new log files properly

✅ logrotate runs automatically via cron!

✅ But small shell scripts are super useful for custom automation.


🧠 Simple Analogy

Log rotation is like taking out the trash 🗑️:

  • You don't keep every pizza box forever 🍕
  • You archive important stuff
  • You compress when needed
  • You clean out very old junk

Good observability = Good log hygiene.



Article content

Preventive log management saves production systems!


🗺️ Where Are We in the Journey?

Linux Fundamentals → Kubernetes Observability → Shell Basics → Background Jobs → Scheduling with Cron → (Now) Smart Log Rotation & Cleanup

        

You are now automating true system hygiene for observability!

To view or add a comment, sign in

More articles by Anamika Sanjay

Explore topics