The answer to your questions:
-
If the user flushes twice in a row, the log from the "day before" is gone: This is no different from the current situation, the current pihole flush command removes all the logging. I might even suggest to remove the 'flush' button from the web interface...
-
This implies the log must exist: If the log does not exist, it cannot be rotated and no new log will be created. Adding missingok doesn't solve this, it just suppresses the error message, witch I would not advise, an error message indicates something is wrong.
To overcome the problem of a non existing log, add this to /opt/pihole/piholeLogFlush.sh. Tested this, works fine.
/usr/sbin/logrotate --force /etc/.pihole/advanced/logrotate
if [ ! -f /var/log/pihole.log ]; then
echo " " > /var/log/pihole.log
service dnsmasq restart
fi
Warning: I choose copytruncate because logging didn't resume using other options (always had to restart dnsmasq). My log is about 600Kb in size, I've got no idea what will happen if the log is several Mb in size. This should be tested!
-
Time might actually be an issue: I've modified /opt/pihole/piholeLogFlush.sh, witch is already triggered by cron at midnight (pihole flush). If the pi isn't running 24/7, there is no difference from the current situation e.g. no rotation.
To my knowledge, there is no logrotate option to catch up missed events. You can find all logrotate options here.
-
I would advise against the @reboot cron option, als the log will be rotated at every reboot, witch I (and others) often do when I'm testing things. You could consider implementing @reboot (you need to add the root user - tested this) with a script that detects a non rotated log and rotate it only if this is detected. Example:
line=$(head -n 1 /var/log/pihole.log)
date=$(date +"%b %d")
if [[ ${line} != *$date* ]];then
/usr/sbin/logrotate --force /etc/.pihole/advanced/logrotate
fi
-
I think the notifempty (saw this on github) directive is ignored by the command line force option.
-
The nomail instruction doesn't do anything, if nothing is configured, the mail you can recieve (see chapter 4.8 of this manual) comes from the cron daemon.
-
rotate 5 may be over the top. Even most system logs (see /etc/rotate.d/rsyslog) are rotated 4 times only.
-
You should probably check the precense of the logrotate package in install.sh
to summarize all this (tested)
/opt/pihole/piholeLogFlush.sh should look like this
#!/usr/bin/env bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Flushes /var/log/pihole.log
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.line=$(head -n 1 /var/log/pihole.log)
# detect the reboot parameter from cron
if [[ $@ == **reboot** ]];then
date=$(date +"%b %d")
if [[ ${line} != *$date* ]];then
/usr/sbin/logrotate --force /etc/.pihole/advanced/logrotate
fi
else
echo "::: Rotating /var/log/pihole.log ..."
/usr/sbin/logrotate --force /etc/.pihole/advanced/logrotate
echo "::: ... done!"
fi
#catch a non existing log
if [ ! -f /var/log/pihole.log ]; then
echo " " > /var/log/pihole.log
service dnsmasq restart
fi
/etc/cron.d/pihole should look like this:
# Pi-hole: A black hole for Internet advertisements
# (c) 2015, 2016 by Jacob Salmela
# Network-wide ad blocking via your Raspberry Pi
# http://pi-hole.net
# Updates ad sources every week
#
# Pi-hole is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This file is under source-control of the Pi-hole installation and update
# scripts, any changes made to this file will be overwritten when the softare
# is updated or re-installed. Please make any changes to the appropriate crontab
# or other cron file snippets.
# Pi-hole: Update the ad sources once a week on Sunday at 01:59
# Download any updates from the adlists
59 1 * * 7 root PATH="$PATH:/usr/local/bin/" pihole updateGravity
# Pi-hole: Update Pi-hole! Uncomment to enable auto update
#30 2 * * 7 root PATH="$PATH:/usr/local/bin/" pihole updatePihole
# Pi-hole: Flush the log daily at 00:00 so it doesn't get out of control
# Stats will be viewable in the Web interface thanks to the cron job above
00 00 * * * root PATH="$PATH:/usr/local/bin/" pihole flush >/dev/null
@reboot root /opt/pihole/piholeLogFlush.sh reboot
notice the reboot parameter on the @reboot line, it makes piholeLogFlush check the date of the pihole log (first line)
notice the redirection on the midnight (00 00) line, a mail will only be sent if somethings wrong.
/etc/.pihole/advanced/logrotate should look like this:
/var/log/pihole.log {
daily
copytruncate
rotate 4
compress
delaycompress
nomail
}
to make life easy for you, I've made an online archive with the required files.