#!/bin/bash do { #//////////////////////////////////// # DietPi-logclear Script # #//////////////////////////////////// # Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com # #//////////////////////////////////// # Info: # Clears logs in $FILEPATH_LOGFOLDER, with backup option. # # usage: # - dietpi-logclear 0 | Update current log files data to "$FILEPATH_BACKUPFOLDER/*. Then clear contents. # - dietpi-logclear 1 | Clear contents of all logs in $FILEPATH_LOGFOLDER. # - dietpi-logclear 2 | Delete all logs in $FILEPATH_LOGFOLDER and backups. #//////////////////////////////////// setglobal INPUT = '-1' if [[ $1 =~ ^-?[0-9]+$ ]] { setglobal INPUT = $1 } #Import DietPi-Globals --------------------------------------------------------------- source /DietPi/dietpi/func/dietpi-globals G_CHECK_ROOT_USER export G_PROGRAM_NAME='DietPi-Logclear' #Import DietPi-Globals --------------------------------------------------------------- #//////////////////////////////////////////////////////////////// # Global #//////////////////////////////////////////////////////////////// setglobal TEMP_FILE = '"/tmp/dietpi-logclear'" setglobal FILEPATH_LOGFOLDER = '"/var/log'" setglobal FILEPATH_BACKUPFOLDER = ""$HOME/logfile_storage"" setglobal FILE_NAME = ''"" #0=text log, 1=compressed setglobal FILE_TYPE = '0' setglobal FILESIZE_BYTES = '0' setglobal PROCESS_FILE = '0' #//////////////////////////////////////////////////////////////// #Excluded log files (user created) #//////////////////////////////////////////////////////////////// setglobal FP_EXCLUDED_LOGFILES = '"/DietPi/dietpi/.dietpi-logclear_exclude'" setglobal INFO_EXCLUDED_LOGFILES = '0' #//////////////////////////////////////////////////////////////// # Info Printouts #//////////////////////////////////////////////////////////////// setglobal INFO_SIZE_CLEARED = '0' setglobal INFO_FILES_PROCESSED = '0' setglobal INFO_LOGS_DELETED = '0' setglobal INFO_LOGS_CLEARED = '0' setglobal INFO_LOGS_NOTSUPPORTED = '0' setglobal INFO_BACKUPS_MADE = '0' #//////////////////////////////////////////////////////////////// # Process log files. #//////////////////////////////////////////////////////////////// proc Process_Logfiles{ #----------------------------------------------------------------------------------- #Find existing logs and generate a filepath list. find $FILEPATH_LOGFOLDER -type f > $TEMP_FILE #Read logfile filepath list into array. readarray -t ARRAY_LOG_FILEPATH < $TEMP_FILE #----------------------------------------------------------------------------------- #Process Logfiles for ((i=0; i<${#ARRAY_LOG_FILEPATH[@]}; i++)) do #File details FILE_NAME=$(echo -e ${ARRAY_LOG_FILEPATH[$i]} | sed 's/\/var\/log\///g') FILESIZE_BYTES=$(stat -c%s "${ARRAY_LOG_FILEPATH[$i]}") PROCESS_FILE=1 FILE_TYPE=0 #Special Filetypes #PiHole logs (contains dns stats) | FILE_TYPE 2 if [ "${ARRAY_LOG_FILEPATH[$i]}" = "/var/log/pihole.log" ]; then FILE_TYPE=2 #Compessed files (zip etc) | FILE_TYPE 1 elif [[ ${FILE_NAME: -4} == ".zip" ]] || [[ ${FILE_NAME: -3} == ".gz" ]]; then FILE_TYPE=1 #Normal Log Files. Do we have any data to clear? Check size of file. elif (( $FILESIZE_BYTES <= 10 )); then PROCESS_FILE=0 fi #Process File if (( $PROCESS_FILE == 1 )); then #PiHole if (( $FILE_TYPE == 2 )); then local pihole_restart_required=0 #Find all non-matching lines with todays date # NB: day is space padded without trailling 0 (eg: Sep 1, Sep 11) # NB: Force english dates to match english dates that dnsmasq always logs with: https://github.com/Fourdee/DietPi/issues/233#issuecomment-196407135 local month_day_today=$(LC_ALL=en_GB.UTF-8 date +'%b %e') # - Check for at least 1 non-matching line before applying. Remove all lines in logfile for all other days, excluding today. if (( $(grep -v -ci -m1 "$month_day_today" /var/log/pihole.log) == 1 )); then # RAMlog mode 2: if (( $INPUT == 0 )); then cat "${ARRAY_LOG_FILEPATH[$i]}" | sed '/'"$month_day_today"'/d' >> "$FILEPATH_BACKUPFOLDER"/"$FILE_NAME" ((INFO_BACKUPS_MADE++)) fi G_DIETPI-NOTIFY 2 'Clearing log entries for PiHole, that are not from today, please wait...' sed -i '/'"$month_day_today"'/!d' /var/log/pihole.log pihole_restart_required=1 fi # recheck size again FILESIZE_BYTES=$(stat -c%s "${ARRAY_LOG_FILEPATH[$i]}") #Clear all entries if over 5MB (we have to limit and prevent /var/log hitting the 20mb tmpfs limit) # - 5mb should be more than enough for 1+ days of pihole logs if (( $FILESIZE_BYTES >= 5242880 )); then G_DIETPI-NOTIFY 2 "${ARRAY_LOG_FILEPATH[$i]} has exceeded 5MB, clearing, please wait..." # RAMlog mode 2: if (( $INPUT == 0 )); then cat "${ARRAY_LOG_FILEPATH[$i]}" >> "$FILEPATH_BACKUPFOLDER"/"$FILE_NAME" ((INFO_BACKUPS_MADE++)) fi echo -e "" > "${ARRAY_LOG_FILEPATH[$i]}" ((INFO_LOGS_CLEARED++)) pihole_restart_required=1 #Prevent size effecting results else FILESIZE_BYTES=0 fi #restart PiHole if we modified the log file if (( $pihole_restart_required )); then G_DIETPI-NOTIFY 2 'PiHole logs have been changed, restarting, please wait...' systemctl stop pihole-FTL systemctl stop dnsmasq sleep 1 systemctl start dnsmasq systemctl start pihole-FTL # Reapply DietPi-Process_Tool /DietPi/dietpi/dietpi-process_tool 1 fi #Delete all compressed filetypes. elif (( $FILE_TYPE == 1 )); then rm "${ARRAY_LOG_FILEPATH[$i]}" ((INFO_LOGS_DELETED++)) ((INFO_LOGS_NOTSUPPORTED++)) #Update current log files data to $FILEPATH_BACKUPFOLDER/*. Then clear. elif (( $INPUT == 0 )); then #Generate filepaths if [ ! -f "$FILEPATH_BACKUPFOLDER/$FILE_NAME" ]; then #This is a little "hack" to automatically generate the required subdirectories. # EG: /this/is/my/logfile.txt | will create the folders /this/is/my mkdir -p "$FILEPATH_BACKUPFOLDER/$FILE_NAME" rm -R "$FILEPATH_BACKUPFOLDER/$FILE_NAME" fi #Write current logfile contents to existing. cat "${ARRAY_LOG_FILEPATH[$i]}" >> "$FILEPATH_BACKUPFOLDER"/"$FILE_NAME" ((INFO_BACKUPS_MADE++)) #Clear logfile contents echo -e "" > "${ARRAY_LOG_FILEPATH[$i]}" ((INFO_LOGS_CLEARED++)) #Clear logfile contents elif (( $INPUT == 1 )); then echo -e "" > "${ARRAY_LOG_FILEPATH[$i]}" ((INFO_LOGS_CLEARED++)) #Hard delete log files elif (( $INPUT == 2 )); then rm "${ARRAY_LOG_FILEPATH[$i]}" ((INFO_LOGS_DELETED++)) fi #Update Size cleared. INFO_SIZE_CLEARED=$(($INFO_SIZE_CLEARED + $FILESIZE_BYTES)) fi ((INFO_FILES_PROCESSED++)) done #Remove temp files rm $TEMP_FILE &> /dev/null #delete[] array unset ARRAY_LOG_FILEPATH } #//////////////////////////////////////////////////////////////// # Main Loop #//////////////////////////////////////////////////////////////// #---------------------------------------------------------------- G_DIETPI-NOTIFY 3 DietPi-Logclear "Welcome" #---------------------------------------------------------------- #How to use print. if sh-expr ' $INPUT == -1 ' { G_DIETPI-NOTIFY 2 'Available commands:' echo -e "" echo -e "\e[1m dietpi-logclear 0\e[0m" echo -e "\e[38;5;244m Backup contents of all log files from $FILEPATH_LOGFOLDER to $FILEPATH_BACKUPFOLDER/*.\n Also clears the contents of all logs files in $FILEPATH_LOGFOLDER.\e[0m" echo -e "" echo -e "\e[1m dietpi-logclear 1\e[0m" echo -e "\e[38;5;244m Clear contents of all logs files in $FILEPATH_LOGFOLDER.\e[0m" echo -e "" echo -e "\e[1m dietpi-logclear 2\e[0m" echo -e "\e[38;5;244m Physically delete all files in $FILEPATH_LOGFOLDER and backups in $FILEPATH_BACKUPFOLDER/*.\n May prevent log files from being updated, restart services or reboot. \e[0m" echo -e "" #---------------------------------------------------------------- #Process log files } elif sh-expr ' $INPUT >= 0 ' { Process_Logfiles #Reset permissions for log files/dir chmod -R 775 /var/log #Delete logfile backups if sh-expr ' $INPUT == 2 ' { rm -R $FILEPATH_BACKUPFOLDER &> /dev/null } #Print Info G_DIETPI-NOTIFY 2 echo -e " - Log file directory \e[90m|\e[0m $FILEPATH_LOGFOLDER" echo -e " - Processed files \e[90m|\e[0m $INFO_FILES_PROCESSED" #Excluded echo -e " - Excluded files \e[90m|\e[0m $INFO_EXCLUDED_LOGFILES" #Cleared echo -e " - Cleared log files \e[90m|\e[0m $INFO_LOGS_CLEARED" #Deleted echo -e " - Unsupported files \e[90m|\e[0m $INFO_LOGS_NOTSUPPORTED" echo -e " - Deleted files \e[90m|\e[0m $INFO_LOGS_DELETED" #convert size to kb #/= (i miss it) setglobal INFO_SIZE_CLEARED = $shExpr('$INFO_SIZE_CLEARED / 1024') echo -e " - Space cleared \e[90m|\e[0m $INFO_SIZE_CLEARED KB" #Backups if sh-expr ' $INPUT == 0 ' { echo -e "" echo -e " \e[38;5;244mBackup Info:\e[0m" echo -e " - Backup directory \e[90m|\e[0m $FILEPATH_BACKUPFOLDER" echo -e " - Updated log files \e[90m|\e[0m $INFO_BACKUPS_MADE" } echo -e "" } #----------------------------------------------------------------------------------- exit #----------------------------------------------------------------------------------- }