2015-12-06 13:55:50 +00:00
|
|
|
#!/usr/bin/env bash
|
2016-01-30 20:12:40 +00:00
|
|
|
# Pi-hole: A black hole for Internet advertisements
|
2017-02-22 17:55:20 +00:00
|
|
|
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
|
|
|
|
# Network-wide ad blocking via your own hardware.
|
|
|
|
#
|
2017-04-17 19:25:15 +00:00
|
|
|
# Flushes Pi-hole's log file
|
2015-12-06 13:55:50 +00:00
|
|
|
#
|
2017-02-22 17:55:20 +00:00
|
|
|
# This file is copyright under the latest version of the EUPL.
|
|
|
|
# Please see LICENSE file for your rights under this license.
|
|
|
|
|
2017-06-21 11:49:05 +00:00
|
|
|
colfile="/opt/pihole/COL_TABLE"
|
|
|
|
source ${colfile}
|
|
|
|
|
2024-06-19 20:28:14 +00:00
|
|
|
readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"
|
|
|
|
utilsfile="${PI_HOLE_SCRIPT_DIR}/utils.sh"
|
|
|
|
source "${utilsfile}"
|
|
|
|
|
2021-07-09 05:06:10 +00:00
|
|
|
# In case we're running at the same time as a system logrotate, use a
|
|
|
|
# separate logrotate state file to prevent stepping on each other's
|
|
|
|
# toes.
|
|
|
|
STATEFILE="/var/lib/logrotate/pihole"
|
|
|
|
|
2018-02-17 11:58:57 +00:00
|
|
|
# Determine database location
|
2024-06-19 20:28:14 +00:00
|
|
|
DBFILE=$(getFTLConfigValue "files.database")
|
2018-02-17 12:01:00 +00:00
|
|
|
if [ -z "$DBFILE" ]; then
|
2018-07-20 20:07:54 +00:00
|
|
|
DBFILE="/etc/pihole/pihole-FTL.db"
|
2018-02-17 11:58:57 +00:00
|
|
|
fi
|
|
|
|
|
2024-06-19 20:28:14 +00:00
|
|
|
# Determine log file location
|
|
|
|
LOGFILE=$(getFTLConfigValue "files.log.dnsmasq")
|
|
|
|
if [ -z "$LOGFILE" ]; then
|
|
|
|
LOGFILE="/var/log/pihole.log"
|
|
|
|
fi
|
|
|
|
|
2023-06-04 19:21:48 +00:00
|
|
|
if [[ "$*" != *"quiet"* ]]; then
|
2024-06-19 20:28:14 +00:00
|
|
|
echo -ne " ${INFO} Flushing "${LOGFILE}" ..."
|
2017-05-17 10:54:43 +00:00
|
|
|
fi
|
2023-06-04 19:21:48 +00:00
|
|
|
if [[ "$*" == *"once"* ]]; then
|
2018-07-20 20:07:54 +00:00
|
|
|
# Nightly logrotation
|
|
|
|
if command -v /usr/sbin/logrotate >/dev/null; then
|
|
|
|
# Logrotate once
|
2021-07-09 05:06:10 +00:00
|
|
|
/usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate
|
2018-07-20 20:07:54 +00:00
|
|
|
else
|
|
|
|
# Copy pihole.log over to pihole.log.1
|
|
|
|
# and empty out pihole.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)
|
2024-06-19 20:28:14 +00:00
|
|
|
cp -p "${LOGFILE}" "${LOGFILE}.1"
|
|
|
|
echo " " > "${LOGFILE}"
|
|
|
|
chmod 640 "${LOGFILE}"
|
2018-07-20 20:07:54 +00:00
|
|
|
fi
|
2017-01-12 19:39:25 +00:00
|
|
|
else
|
2018-07-20 20:07:54 +00:00
|
|
|
# Manual flushing
|
|
|
|
if command -v /usr/sbin/logrotate >/dev/null; then
|
|
|
|
# Logrotate twice to move all data out of sight of FTL
|
2021-07-09 05:06:10 +00:00
|
|
|
/usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate; sleep 3
|
|
|
|
/usr/sbin/logrotate --force --state "${STATEFILE}" /etc/pihole/logrotate
|
2018-07-20 20:07:54 +00:00
|
|
|
else
|
|
|
|
# Flush both pihole.log and pihole.log.1 (if existing)
|
2024-06-19 20:28:14 +00:00
|
|
|
echo " " > "${LOGFILE}"
|
|
|
|
if [ -f "${LOGFILE}.1" ]; then
|
|
|
|
echo " " > "${LOGFILE}.1"
|
|
|
|
chmod 640 "${LOGFILE}.1"
|
2018-07-20 20:07:54 +00:00
|
|
|
fi
|
2017-05-17 10:44:35 +00:00
|
|
|
fi
|
2024-06-19 20:28:14 +00:00
|
|
|
|
|
|
|
# Stop FTL to make sure it doesn't write to the database while we're deleting data
|
|
|
|
service pihole-FTL stop
|
|
|
|
|
2018-07-20 20:07:54 +00:00
|
|
|
# Delete most recent 24 hours from FTL's database, leave even older data intact (don't wipe out all history)
|
2023-12-09 22:06:50 +00:00
|
|
|
deleted=$(pihole-FTL sqlite3 -ni "${DBFILE}" "DELETE FROM query_storage WHERE timestamp >= strftime('%s','now')-86400; select changes() from query_storage limit 1")
|
2018-01-21 12:48:13 +00:00
|
|
|
|
2024-06-19 20:28:14 +00:00
|
|
|
# Restart FTL
|
|
|
|
service pihole-FTL restart
|
2017-05-17 10:54:43 +00:00
|
|
|
fi
|
|
|
|
|
2023-06-04 19:21:48 +00:00
|
|
|
if [[ "$*" != *"quiet"* ]]; then
|
2020-09-05 13:15:03 +00:00
|
|
|
echo -e "${OVER} ${TICK} Flushed /var/log/pihole/pihole.log"
|
2018-07-20 20:07:54 +00:00
|
|
|
echo -e " ${TICK} Deleted ${deleted} queries from database"
|
2017-01-12 19:39:25 +00:00
|
|
|
fi
|