#!/bin/bash { #//////////////////////////////////// # DietPi RAMLOG Script # #//////////////////////////////////// # Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com # #//////////////////////////////////// # # Info: # - Saves /var/log/* atrributes and ownerships during shutdown to "$FILEPATH_DIETPI_RAMLOG_SAVE" # - Restores /var/log/* atrributes and ownerships during boot from "$FILEPATH_DIETPI_RAMLOG_SAVE" # - Uses cp with preserve ownership and attributes only. This generates blank file if required (ramlog) without override existing file data (non-ramlog). # # Usage: # - Called from /DietPi/dietpi/conf/dietpi-service # - /DietPi/dietpi/dietpi-ramlog 0 = Startup Phase (restores /var/log) # - /DietPi/dietpi/dietpi-ramlog 1 = Shutdown Phase (saves /var/log) #//////////////////////////////////// #Force en_GB Locale for whole script. Prevents incorrect parsing with non-english locales. LANG=en_GB.UTF-8 #Ensure we are in users home dir: https://github.com/Fourdee/DietPi/issues/905#issuecomment-298223705 cd "$HOME" INPUT=0 if [[ $1 =~ ^-?[0-9]+$ ]]; then INPUT=$1 fi #Import DietPi-Globals --------------------------------------------------------------- #. /DietPi/dietpi/func/dietpi-globals # Not compatible until dietpi-boot.service #Import DietPi-Globals --------------------------------------------------------------- FILEPATH_DIETPI_RAMLOG_SAVE='/var/lib/dietpi/dietpi-ramlog/storage' #///////////////////////////////////////////////////////////////////////////////////// #Funcs #///////////////////////////////////////////////////////////////////////////////////// Apply_Unique_Logfile_Permissions(){ # - Pihole chown www-data:www-data /var/log/pihole.log # - Mongodb chown -R mongodb:mongodb /var/log/mongodb # - EmonCMS chmod 666 /var/log/emoncms.log # - Tor notices chown -R debian-tor:nogroup /var/log/tor/notices.log } #///////////////////////////////////////////////////////////////////////////////////// #Main Loop #///////////////////////////////////////////////////////////////////////////////////// #Startup Phase (restore /var/log directory structure and files) if (( $INPUT == 0 )); then echo -e "DietPi-Ramlog: Starting" #Create core directories, regardless of Logging mode. # - This prevents core system programs failing to write to their logfile (eg: logsave process will not terminate if the /fsck directory does not exist). mkdir -p /var/log/apt &> /dev/null mkdir -p /var/log/apache2 &> /dev/null mkdir -p /var/log/fsck &> /dev/null mkdir -p /var/log/news &> /dev/null mkdir -p /var/log/samba &> /dev/null mkdir -p /var/log/ntpstats &> /dev/null #Apply previous attributes and ownerships. Generates empty file if it doesnt exist. if [ -d "$FILEPATH_DIETPI_RAMLOG_SAVE" ]; then cp -R -p --attributes-only "$FILEPATH_DIETPI_RAMLOG_SAVE"/* /var/log/ fi #Set bit permission chmod -R 775 /var/log #Apply any unique permission for software based log files. Apply_Unique_Logfile_Permissions &> /dev/null echo -e "DietPi-Ramlog: Completed" #Shutdown Phase (saves /var/log directory structure and filenames) elif (( $INPUT == 1 )); then echo -e "DietPi-Ramlog: Stopping" #Clear previous storage data rm -R "$FILEPATH_DIETPI_RAMLOG_SAVE" #Generate Storage Directory mkdir -p "$FILEPATH_DIETPI_RAMLOG_SAVE" #Copy logfile attributes and ownership to storage (not file contents) cp -R -p --attributes-only /var/log/* "$FILEPATH_DIETPI_RAMLOG_SAVE"/ echo -e "DietPi-Ramlog: Completed" fi #----------------------------------------------------------------------------------- exit #----------------------------------------------------------------------------------- }