Advanced Bash logging to handle errors.

So we've all done a quick log from a bash script, which looks like this

LOGFILE="logs/$(date +%F)_backup.log"
# Append to the file
restic backup /etc/ >> $LOGFILE
find /root/scripts/ -type f -name "restic*.log" -mtime +14 -delete -print >> $LOGFILE

That's great for small scripts. But sometimes you find that its not logging errors, which are also helpful, and you need to add those:

LOGFILE="logs/$(date +%F)_backup.log"
# Append to the file
restic backup /etc/ 2>&1 >> $LOGFILE
find /root/scripts/ -type f -name "restic*.log" -mtime +14 -delete -print 2>&1 >> $LOGFILE

And different commands behave differently, so it all starts to become complicated. But there's an easier way. Two actually. The first is to use curly brackets to contain all the commands you want to log, and then pipe it through tee at the end. Commands after that block won't be logged.

Read more

Bash script for managing htaccess files

Had to create this for a less technical user to manage htaccess, so thought I'd share. Your mileage may vary. Change the location of the default htpass file in the config. You can supply another location in mid script if you have more than one. #!/bin/bash# ConfigurationHTACCESS_FILE_DEFAULT="htpass_test"# Function to display a list of usersdisplay_users() { … Read more

Changing default port for grafana agent

This was much more difficult than it should have been. Grafana agent runs on port 9090 and 9091, so when I installed it on a server where nginx was using those ports, I got an error, not unreasonably. error creating the agent server entrypoint" err="creating HTTP listener: listen tcp 127.0.0.1:9090: bind: address already in use … Read more