mirror of
https://github.com/pi-hole/pi-hole
synced 2025-07-27 09:28:16 +00:00
functionise log rotation and flushing code to avoid dupliation
Signed-off-by: Adam Warner <me@adamwarner.co.uk>
This commit is contained in:
parent
f403468450
commit
6e06a93c31
@ -40,6 +40,42 @@ if [ -z "$WEBFILE" ]; then
|
|||||||
WEBFILE="/var/log/pihole/webserver.log"
|
WEBFILE="/var/log/pihole/webserver.log"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Helper function to handle log rotation for a single file
|
||||||
|
rotate_log() {
|
||||||
|
# This function copies x.log over to x.log.1
|
||||||
|
# and then empties x.log
|
||||||
|
# Note that moving the file is not an option, as
|
||||||
|
# dnsmasq would happily continue writing into the
|
||||||
|
# moved file (it will have the same file handler)
|
||||||
|
local logfile="$1"
|
||||||
|
if [[ "$*" != *"quiet"* ]]; then
|
||||||
|
echo -ne " ${INFO} Rotating ${logfile} ..."
|
||||||
|
fi
|
||||||
|
cp -p "${logfile}" "${logfile}.1"
|
||||||
|
echo " " > "${logfile}"
|
||||||
|
chmod 640 "${logfile}"
|
||||||
|
if [[ "$*" != *"quiet"* ]]; then
|
||||||
|
echo -e "${OVER} ${TICK} Rotated ${logfile} ..."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Helper function to handle log flushing for a single file
|
||||||
|
flush_log() {
|
||||||
|
local logfile="$1"
|
||||||
|
if [[ "$*" != *"quiet"* ]]; then
|
||||||
|
echo -ne " ${INFO} Flushing ${logfile} ..."
|
||||||
|
fi
|
||||||
|
echo " " > "${logfile}"
|
||||||
|
chmod 640 "${logfile}"
|
||||||
|
if [ -f "${logfile}.1" ]; then
|
||||||
|
echo " " > "${logfile}.1"
|
||||||
|
chmod 640 "${logfile}.1"
|
||||||
|
fi
|
||||||
|
if [[ "$*" != *"quiet"* ]]; then
|
||||||
|
echo -e "${OVER} ${TICK} Flushed ${logfile} ..."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [[ "$*" == *"once"* ]]; then
|
if [[ "$*" == *"once"* ]]; then
|
||||||
# Nightly logrotation
|
# Nightly logrotation
|
||||||
if command -v /usr/sbin/logrotate >/dev/null; then
|
if command -v /usr/sbin/logrotate >/dev/null; then
|
||||||
@ -50,87 +86,16 @@ if [[ "$*" == *"once"* ]]; then
|
|||||||
fi
|
fi
|
||||||
/usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate
|
/usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate
|
||||||
else
|
else
|
||||||
# Copy pihole.log over to pihole.log.1
|
# Handle rotation for each log file
|
||||||
# and empty out pihole.log
|
rotate_log "${LOGFILE}"
|
||||||
# Note that moving the file is not an option, as
|
rotate_log "${FTLFILE}"
|
||||||
# dnsmasq would happily continue writing into the
|
rotate_log "${WEBFILE}"
|
||||||
# moved file (it will have the same file handler)
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -ne " ${INFO} Rotating ${LOGFILE} ..."
|
|
||||||
fi
|
|
||||||
cp -p "${LOGFILE}" "${LOGFILE}.1"
|
|
||||||
echo " " > "${LOGFILE}"
|
|
||||||
chmod 640 "${LOGFILE}"
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} Rotated ${LOGFILE} ..."
|
|
||||||
fi
|
|
||||||
# Copy FTL.log over to FTL.log.1
|
|
||||||
# and empty out FTL.log
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -ne " ${INFO} Rotating ${FTLFILE} ..."
|
|
||||||
fi
|
|
||||||
cp -p "${FTLFILE}" "${FTLFILE}.1"
|
|
||||||
echo " " > "${FTLFILE}"
|
|
||||||
chmod 640 "${FTLFILE}"
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} Rotated ${FTLFILE} ..."
|
|
||||||
fi
|
|
||||||
# Copy webserver.log over to webserver.log.1
|
|
||||||
# and empty out webserver.log
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -ne " ${INFO} Rotating ${WEBFILE} ..."
|
|
||||||
fi
|
|
||||||
cp -p "${WEBFILE}" "${WEBFILE}.1"
|
|
||||||
echo " " > "${WEBFILE}"
|
|
||||||
chmod 640 "${WEBFILE}"
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} Rotated ${WEBFILE} ..."
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
# Manual flushing
|
# Manual flushing
|
||||||
|
flush_log "${LOGFILE}"
|
||||||
# Flush both pihole.log and pihole.log.1 (if existing)
|
flush_log "${FTLFILE}"
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
flush_log "${WEBFILE}"
|
||||||
echo -ne " ${INFO} Flushing ${LOGFILE} ..."
|
|
||||||
fi
|
|
||||||
echo " " > "${LOGFILE}"
|
|
||||||
chmod 640 "${LOGFILE}"
|
|
||||||
if [ -f "${LOGFILE}.1" ]; then
|
|
||||||
echo " " > "${LOGFILE}.1"
|
|
||||||
chmod 640 "${LOGFILE}.1"
|
|
||||||
fi
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} Flushed ${LOGFILE} ..."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Flush both FTL.log and FTL.log.1 (if existing)
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -ne " ${INFO} Flushing ${FTLFILE} ..."
|
|
||||||
fi
|
|
||||||
echo " " > "${FTLFILE}"
|
|
||||||
chmod 640 "${FTLFILE}"
|
|
||||||
if [ -f "${FTLFILE}.1" ]; then
|
|
||||||
echo " " > "${FTLFILE}.1"
|
|
||||||
chmod 640 "${FTLFILE}.1"
|
|
||||||
fi
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} Flushed ${FTLFILE} ..."
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Flush both webserver.log and webserver.log.1 (if existing)
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -ne " ${INFO} Flushing ${WEBFILE} ..."
|
|
||||||
fi
|
|
||||||
echo " " > "${WEBFILE}"
|
|
||||||
chmod 640 "${WEBFILE}"
|
|
||||||
if [ -f "${WEBFILE}.1" ]; then
|
|
||||||
echo " " > "${WEBFILE}.1"
|
|
||||||
chmod 640 "${WEBFILE}.1"
|
|
||||||
fi
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
|
||||||
echo -e "${OVER} ${TICK} Flushed ${WEBFILE} ..."
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$*" != *"quiet"* ]]; then
|
if [[ "$*" != *"quiet"* ]]; then
|
||||||
echo -ne " ${INFO} Flushing database, DNS resolution temporarily unavailable ..."
|
echo -ne " ${INFO} Flushing database, DNS resolution temporarily unavailable ..."
|
||||||
|
Loading…
Reference in New Issue
Block a user