Skip to main content

Unraid - Tip, Tricks & Notes

Unraid is a fantastic piece of software that allows you to create a fully functional NAS, virtualization host & docker host all, on almost any piece of hardware you have lying around, able to be expanded with little-to-no-reconfiguration either.

Now to be clear, its not as flexible or as powerful as something like TrueNAS, however its a different usage case.

Unraid is a great platform to help 'get the backend out the way' of anything you want to work on - which is why i use it, i don't want to fight with the OS, i just want it to work.

That being said, ive a small collection of scripts I've built up over the years, some scheduled, some manually ran, to help keep things ticking along.
This page will list these, and their usage.

Pre-Req's

  1. Unraid
  2. Unraid User Scripts Plugin

Script 1 - Flush Logs to external device

Purpose: A flaw of Unraid is that if the system crashes or reboots, you lose the logs, which are store in RAM (/tmp).
This script, set to run every few mins, will copy the log file to an external device of your choosing.

Note: You will need to modify the path accordingly.

Run Style: Scheduled

#!/bin/bash  
cp /var/log/syslog /mnt/disks/unraid_logs/syslog

Script 2 - BTRFS - View Device Stats

Purpose: Shows you the BTRFS device stats for your cache drive, which is useful in diagnosing problems with the file system.
Run Style: Manual

#!/bin/bash
# Cache A
btrfs device stats /mnt/cache

Script 3 - BTRFS - Clear Device Stats

Purpose: Clears the stats, as they are cumulative. Useful to run after a change to monitor if the change has made a difference to the file system consistency.
Run Style: Manual

#!/bin/bash
btrfs device stats -z /mnt/cache

Script 4 - BTRFS - Notify on corrupt file

Purpose: This script was pulled from the Unraid forums, and leverages the notification system in Unraid to monitor the log and push the notification to your notification mechanism.
Run Style: Scheduled

#!/bin/bash
# #####################################
# Name: Syslog notify
# Description: Creates notifications if specific words occur in the logs
# Author: Marc Gutt
# Version: 1.1
#
# Changelog:
# 1.1
# - added -F option to grep to interpret input string literal
# - multiple lines create only one notification
# 1.0
# - Initial release
# #####################################

# #####################################
# Settings
# #####################################

# get most recent syslog file
syslog_file=$(ls -t /var/log/syslog* | head -n1)

# which words should cause a notification
words="corrupt|error|fail|tainted"

# #####################################
# Script
# #####################################

# parse logs
EOL=$'\n'
lines=""
while read -r line; do
# error found
if [[ ! -f /tmp/syslog-notify.log ]] || ! grep -Fq "$line" /tmp/syslog-notify.log; then
lines="$lines$EOL$line"
# remember error line to avoid duplicate notifications
echo "$line" >> /tmp/syslog-notify.log
fi
done < <(grep -iP "($words)" "$syslog_file")

# create notification
if [[ $lines ]]; then
/usr/local/emhttp/webGui/scripts/notify -i "alert" -s "syslog $(echo "$lines" | grep -ioP "($words)" | tr '[:upper:]' '[:lower:]' | sort -u | xargs)" -d "${lines:1}"
fi

Script 5 - Display BTRFS Corrupt Files

Purpose: Displays the files/paths that are corrupt based on BTRFS stats/status.
Run Style: Manual

#!/bin/bash
dmesg | grep -iC 5 "BTRFS warning"

Script 6 - Reset PiHole Database

Purpose: Relevant only if you use PiHole - this script stop PiHole, backs up the database, deletes the original and restarts PiHole. Useful when PiHole is becoming sluggish or the database has gotten too large.
Run Style: Manual

#!/bin/bash
docker stop Pi-hole
mv /mnt/user/appdata/pihole/pihole/pihole-FTL.db /mnt/user/Backups/Pihole/pihole-FTL.db
docker start Pi-Hole

Script 7 - Tidy UptimeKuma Backups

Purpose: Relevant only if you use UptimeKuma - tidies up excess database backups.
Run Style: Manual

#!/bin/bash
rm /mnt/user/appdata/uptimekuma/kuma.db-shm.bak*
rm /mnt/user/appdata/uptimekuma/kuma.db-wal.bak*
rm /mnt/user/appdata/uptimekuma/kuma.db.bak*

Script 8 - Fix corrupt UptimeKuma Database

Purpose: Relevant only if you use UptimeKuma - fixes a corrupt database.
Run Style: Manual

#!/bin/bash
mv /mnt/user/appdata/uptimekuma/kuma.db /mnt/user/appdata/uptimekuma/kumaOLD.db
sqlite3 /mnt/user/appdata/uptimekuma/kumaOLD.db ".dump" | sqlite3 /mnt/user/appdata/uptimekuma/kuma.db