Logrotate rotiert, komprimiert und löscht automatisch Log-Dateien. Ohne Rotation wachsen Logs unkontrolliert und füllen die Festplatte.

Warum Logrotate?

Probleme ohne Rotation

- Festplatte füllt sich
- Logs werden unübersichtlich
- Analyse großer Dateien langsam
- Keine strukturierte Archivierung

Logrotate-Funktionen

- Rotation nach Größe oder Zeit
- Komprimierung alter Logs
- Automatisches Löschen
- Skripte vor/nach Rotation
- Eigentümer und Rechte setzen

Installation und Konfiguration

Installation prüfen

# Meist vorinstalliert
which logrotate
logrotate --version

Konfigurationsdateien

/etc/logrotate.conf          # Hauptkonfiguration
/etc/logrotate.d/            # Anwendungsspezifische Configs

Hauptkonfiguration

# /etc/logrotate.conf

# Wöchentliche Rotation
weekly

# 4 Wochen aufbewahren
rotate 4

# Nach Rotation neue Datei erstellen
create

# Datum im Dateinamen
dateext

# Komprimieren
compress

# Include-Verzeichnis
include /etc/logrotate.d

Konfigurationsoptionen

Rotations-Intervalle

| Option | Beschreibung | |--------|--------------| | daily | Täglich | | weekly | Wöchentlich | | monthly | Monatlich | | yearly | Jährlich |

Größenbasierte Rotation

size 100M    # Rotieren wenn > 100 MB
minsize 10M  # Mindestens 10 MB vor Rotation
maxsize 100M # Maximum 100 MB (zusammen mit daily/weekly)

Wichtige Optionen

# Anzahl aufbewahrter Logs
rotate 7

# Komprimieren
compress
compresscmd /bin/gzip
compressoptions -9

# Verzögert komprimieren (erst beim nächsten Mal)
delaycompress

# Dateiendung
dateext
dateformat -%Y%m%d

# Neue Datei erstellen
create 0640 www-data www-data

# Oder copytruncate (für Programme die Datei offen halten)
copytruncate

# Leere Logs nicht rotieren
notifempty

# Fehlende Logs ignorieren
missingok

# Sharedscripts (Skripte nur einmal bei mehreren Logs)
sharedscripts

Eigene Logrotate-Configs

Nginx-Logs

# /etc/logrotate.d/nginx

/var/log/nginx/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}

Apache-Logs

# /etc/logrotate.d/apache2

/var/log/apache2/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        if invoke-rc.d apache2 status > /dev/null 2>&1; then
            invoke-rc.d apache2 reload > /dev/null
        fi
    endscript
}

PHP-FPM-Logs

# /etc/logrotate.d/php-fpm

/var/log/php*-fpm.log {
    weekly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 0640 www-data www-data
    postrotate
        /usr/lib/php/php-fpm-reopenlogs
    endscript
}

MySQL-Logs

# /etc/logrotate.d/mysql-server

/var/log/mysql/*.log {
    daily
    rotate 7
    compress
    delaycompress
    missingok
    create 640 mysql adm
    sharedscripts
    postrotate
        if test -x /usr/bin/mysqladmin && /usr/bin/mysqladmin ping &>/dev/null; then
            /usr/bin/mysqladmin flush-logs
        fi
    endscript
}

Eigene Anwendung

# /etc/logrotate.d/myapp

/var/log/myapp/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 0644 appuser appgroup
    sharedscripts
    postrotate
        systemctl reload myapp > /dev/null 2>&1 || true
    endscript
}

Skripte

prerotate / postrotate

/var/log/myapp/*.log {
    daily
    rotate 7

    prerotate
        # Vor Rotation ausführen
        echo "Rotation startet" >> /var/log/logrotate-status.log
    endscript

    postrotate
        # Nach Rotation ausführen
        systemctl reload myapp
    endscript
}

firstaction / lastaction

/var/log/myapp/*.log {
    daily
    rotate 7
    sharedscripts

    firstaction
        # Einmal vor allen Rotationen
        echo "Start: $(date)" >> /var/log/rotation.log
    endscript

    lastaction
        # Einmal nach allen Rotationen
        echo "Ende: $(date)" >> /var/log/rotation.log
    endscript
}

Testen und Debuggen

Trockenlauf

# Simulation ohne Änderungen
logrotate -d /etc/logrotate.conf

# Einzelne Config testen
logrotate -d /etc/logrotate.d/nginx

Manuell ausführen

# Rotation erzwingen
logrotate -f /etc/logrotate.conf

# Einzelne Config
logrotate -f /etc/logrotate.d/nginx

# Verbose
logrotate -v /etc/logrotate.conf

Status-Datei

# Status prüfen
cat /var/lib/logrotate/status
# Oder
cat /var/lib/logrotate.status

# Status zurücksetzen (für Tests)
> /var/lib/logrotate/status

Häufige Probleme

Logs rotieren nicht

# 1. Syntax prüfen
logrotate -d /etc/logrotate.d/myapp

# 2. Status-Datei prüfen
cat /var/lib/logrotate/status | grep myapp

# 3. Berechtigungen prüfen
ls -la /var/log/myapp/

# 4. Rotation erzwingen
logrotate -f /etc/logrotate.d/myapp

Fehlermeldungen

# Log-Rotation-Fehler finden
grep -i logrotate /var/log/syslog

# Oder in separatem Log
cat /var/log/logrotate.log

Copytruncate vs. Create

# copytruncate: Für Apps die Datei offen halten
# - Kopiert Inhalt, leert Original
# - Kann Zeilen verlieren zwischen Kopie und Truncate
copytruncate

# create: Standard-Methode
# - Benennt Datei um, erstellt neue
# - App muss Datei neu öffnen (Signal nötig)
create 0640 www-data www-data

Cron-Job

Standard-Cron

# /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf

Systemd Timer (moderne Systeme)

# Status prüfen
systemctl status logrotate.timer
systemctl status logrotate.service

# Aktivieren
systemctl enable --now logrotate.timer

Speicherplatz sparen

Aggressive Kompression

/var/log/bigapp/*.log {
    daily
    rotate 7
    compress
    compresscmd /usr/bin/xz
    compressoptions -9
    compressext .xz
}

Alte Logs archivieren

/var/log/archive/*.log {
    monthly
    rotate 12
    compress
    olddir /var/log/archive/old
    createolddir 755 root root
}

Mail bei Löschung

/var/log/important/*.log {
    daily
    rotate 7
    mail admin@example.com
    mailfirst
}

Best Practices

Empfehlungen

1. Täglich für aktive Logs
2. 7-14 Tage für normale Logs
3. 30+ Tage für wichtige Logs
4. Kompression aktivieren
5. notifempty verwenden
6. sharedscripts bei mehreren Logs
7. postrotate für Service-Reload

Standard-Template

# /etc/logrotate.d/template

/var/log/APPNAME/*.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 USER GROUP
    sharedscripts
    postrotate
        systemctl reload APPNAME > /dev/null 2>&1 || true
    endscript
}

Zusammenfassung

| Option | Beschreibung | |--------|--------------| | daily/weekly | Rotations-Intervall | | rotate N | N Dateien behalten | | compress | Komprimieren | | delaycompress | Erst nächstes Mal | | create | Neue Datei erstellen | | copytruncate | Kopieren und leeren | | postrotate | Nach Rotation ausführen | | notifempty | Leere ignorieren | | missingok | Fehlende ignorieren |

Fazit

Logrotate ist essentiell für Server-Wartung. Ohne Rotation füllen sich Festplatten und Logs werden unübersichtlich. Konfigurieren Sie für jede Anwendung eine passende Rotation mit Kompression und angemessener Aufbewahrungsdauer. Testen Sie neue Konfigurationen immer mit -d und erzwingen Sie bei Bedarf mit -f.