logrotate

Das Verzeichnis /var/log füllt sich mit Logdateien. Wenn ein Logverzeichnis als Ramdisk angebunden ist, fehlt häufig der Platz und das Dateisystem läuft voll. Hier kann der logrotate helfen: Er komprimiert die Dateien regelmäßig und legt sie zur Seite. Ältere Logs werden automatisch gelöscht. Die zentrale Konfigurationsdatei /etc/logrotate.conf könnte wie folgt geändert werden, damit die Logfiles möglichst stark komprimiert und nicht allzu lange vorgehalten werden.

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 2 weeks worth of backlogs - default 4
rotate 2

# Archive log files by adding a YYYYMMDD instead of simply adding a number.
dateext

# create new (empty) log files after rotating old ones
create

# uncomment this if you want your log files compressed
compress

# use bzip2 whith higher compression than gzip
compresscmd /bin/bzip2
uncompresscmd /bin/bunzip2
compressoptions -9
compressext .bz2

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

Nun gilt es noch, die einzelnen Logfiles durch Dateien im Verzeichnis /etc/logrotate.d/ zu beschreiben. Im Folgenden einige Beispiele, mit denen ich meine Logverzeichnisse von Ballast befreie:

fhem:

/opt/fhem/log/*.log {
  daily
  rotate 14
  compress
  copytruncate
  create 644 fhem dialout
  missingok
}

hmland:

/var/log/hmland.log {
     rotate 7
     daily
     compress
     missingok
     postrotate
         /etc/init.d/hmland restart
     endscript
}

snapper:

/var/log/snapper.log {
    compress
    dateext
    notifempty
    missingok
    nocreate

    maxage 60
    rotate 9
    size 1M
}

umtskeeper:

/var/log/umtskeeper.log {
    compress
    dateext
    notifempty
    missingok
    nocreate

    maxage 60
    rotate 9
    size 1M
}

mpd:

/var/log/mpd/*.log {
        weekly
        missingok
        rotate 7
        compress
        notifempty
        copytruncate
        create 600
}

pihole:

/var/log/pihole.log {
        daily
        copytruncate
        rotate 5
        compress
        delaycompress
        notifempty
        nomail
}
/var/log/pihole-FTL.log {
        weekly
        copytruncate
        rotate 3
        compress
        delaycompress
        notifempty
        nomail
}

Da es immer wieder gefragt wird, hier noch ein Auszug aus der Manpage zum Thema maxsize, minsize und size:

       maxsize size
              Log files are rotated when they grow bigger than size bytes even
              before  the additionally specified time interval (daily, weekly,
              monthly, or yearly).  The related size option is similar  except
              that  it  is  mutually exclusive with the time interval options,
              and it causes log files to be rotated  without  regard  for  the
              last  rotation  time.   When  maxsize is used, both the size and
              timestamp of a log file are considered.
       minsize  size
              Log files are rotated when they grow bigger than size bytes, but
              not  before  the  additionally  specified  time interval (daily,
              weekly, monthly, or yearly).  The related size option is similar
              except  that it is mutually exclusive with the time interval op‐
              tions, and it causes log files to be rotated without regard  for
              the last rotation time.  When minsize is used, both the size and
              timestamp of a log file are considered.
       size size
              Log files are rotated only if they grow bigger than size  bytes.
              If  size  is  followed  by k, the size is assumed to be in kilo‐
              bytes.  If the M is used, the size is in megabytes, and if G  is
              used,  the  size  is  in gigabytes. So size 100, size 100k, size
              100M and size 100G are all valid.

Das Ergebnis einer Rotation können Sie sich auf der Kommandozeile ansehen:

pi@rpi ~ $ sudo logrotate -v /etc/logrotate.conf
reading config file /etc/logrotate.conf
compress_prog is now /bin/bzip2
compress_ext was changed to .bz2
uncompress_prog is now /bin/bunzip2
compress_options is now  -9
compress_ext is now .bz2
including /etc/logrotate.d
reading config file apache2
reading config file apt
reading config file aptitude
reading config file consolekit
reading config file cups-daemon
reading config file dpkg
reading config file exim4-base
reading config file exim4-paniclog
reading config file fhem
reading config file hmland
reading config file nginx
reading config file pihole
reading config file rsyslog
reading config file snapper
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 20 logs

rotating pattern: /var/log/access.log /var/log/error.log /var/log/fhem_access.log /var/log/fhem_nagios_vhost_access.log /var/log/fhem_nagios_vhost_error.log /var/log/other_vhosts_access.log  after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/access.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /var/log/error.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /var/log/fhem_access.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /var/log/fhem_nagios_vhost_access.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /var/log/fhem_nagios_vhost_error.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-04 06:32
  log does not need rotating (log is empty)
considering log /var/log/other_vhosts_access.log
  Now: 2019-04-13 10:42
  Last rotated at 2018-08-20 06:35
  log does not need rotating (log is empty)
not running prerotate script, since no logs will be rotated
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/apt/term.log  monthly (12 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/apt/term.log
  log /var/log/apt/term.log does not exist -- skipping

rotating pattern: /var/log/apt/history.log  monthly (12 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/apt/history.log
  log /var/log/apt/history.log does not exist -- skipping

rotating pattern: /var/log/aptitude  monthly (6 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/aptitude
  log /var/log/aptitude does not exist -- skipping

rotating pattern: /var/log/ConsoleKit/history  monthly (6 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ConsoleKit/history

rotating pattern: /var/log/cups/*log  after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/cups/access_log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-12 06:54
  log does not need rotating (log is empty)
considering log /var/log/cups/error_log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /var/log/cups/page_log
  Now: 2019-04-13 10:42
  Last rotated at 2019-01-06 06:00
  log does not need rotating (log is empty)
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/dpkg.log  monthly (12 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/dpkg.log
  log /var/log/dpkg.log does not exist -- skipping

rotating pattern: /var/log/alternatives.log  monthly (12 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/alternatives.log
  log /var/log/alternatives.log does not exist -- skipping

rotating pattern: /var/log/exim4/mainlog /var/log/exim4/rejectlog  after 1 days (10 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/exim4/mainlog
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /var/log/exim4/rejectlog
  log /var/log/exim4/rejectlog does not exist -- skipping

rotating pattern: /var/log/exim4/paniclog  10485760 bytes (10 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/exim4/paniclog
  log /var/log/exim4/paniclog does not exist -- skipping

rotating pattern: /opt/fhem/log/*.log  after 1 days (14 rotations)
empty log files are rotated, old logs are removed
considering log /opt/fhem/log/ddimmer3-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/dimmer2-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/Ei413-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/FBDECT_aquarium-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/FBDECT_garage-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/FBDECT_nas-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/fhem-2019-03.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/fhem-2019-04.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/FS20_060400-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/meinwetter-2019-03.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/meinwetter-2019-04.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)
considering log /opt/fhem/log/sgaragentor-2019.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)

rotating pattern: /var/log/hmland.log  after 1 days (7 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/hmland.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)

rotating pattern: /var/log/nginx/*.log  after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

rotating pattern: /var/log/pihole.log  after 1 days (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/pihole.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 00:00
  log does not need rotating (log has been rotated at 2019-4-13 0:0, that is not day ago yet)

rotating pattern: /var/log/pihole-FTL.log  weekly (3 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/pihole-FTL.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 00:00
  log does not need rotating (log has been rotated at 2019-4-13 0:0, that is not week ago yet)

rotating pattern: /var/log/syslog
 after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/syslog
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-13 06:27
  log does not need rotating (log has been rotated at 2019-4-13 6:27, that is not day ago yet)

rotating pattern: /var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
 weekly (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/mail.info
  log /var/log/mail.info does not exist -- skipping
considering log /var/log/mail.warn
  log /var/log/mail.warn does not exist -- skipping
considering log /var/log/mail.err
  log /var/log/mail.err does not exist -- skipping
considering log /var/log/mail.log
  log /var/log/mail.log does not exist -- skipping
considering log /var/log/daemon.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-07 06:47
  log does not need rotating (log has been rotated at 2019-4-7 6:47, that is not week ago yet)
considering log /var/log/kern.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-09 06:32
  log does not need rotating (log has been rotated at 2019-4-9 6:32, that is not week ago yet)
considering log /var/log/auth.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-07 06:47
  log does not need rotating (log has been rotated at 2019-4-7 6:47, that is not week ago yet)
considering log /var/log/user.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-07 06:47
  log does not need rotating (log has been rotated at 2019-4-7 6:47, that is not week ago yet)
considering log /var/log/lpr.log
  log /var/log/lpr.log does not exist -- skipping
considering log /var/log/cron.log
  log /var/log/cron.log does not exist -- skipping
considering log /var/log/debug
  Now: 2019-04-13 10:42
  Last rotated at 2019-03-15 06:39
  log does not need rotating (log is empty)
considering log /var/log/messages
  Now: 2019-04-13 10:42
  Last rotated at 2019-04-07 06:47
  log does not need rotating (log has been rotated at 2019-4-7 6:47, that is not week ago yet)
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/snapper.log  10485760 bytes (99 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/snapper.log
  Now: 2019-04-13 10:42
  Last rotated at 2019-02-22 06:00
  log does not need rotating (log size is below the 'size' threshold)

rotating pattern: /var/log/wtmp  monthly (1 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/wtmp
  log /var/log/wtmp does not exist -- skipping

rotating pattern: /var/log/btmp  monthly (1 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/btmp
  log /var/log/btmp does not exist -- skipping

last change: 2021-01-09
Previous page: Network Boot Next page: rsyslog