02 · Projects / Server scripts
Server scripts.
Four small Bash scripts for an Ubuntu home server. They're short enough to read in a minute, and you should read them before you run them. Each one was checked with ShellCheck and tested on Ubuntu.
Getting a script onto your server: copy the download link, then on the server run curl -fsSLO followed by the link. Open it with less to read it, then run it with bash. Nothing here pipes straight from the internet into your shell.
health-report.sh
One screen telling you how the server is doing: uptime, memory, disk space (warns at 90%), temperature, waiting updates, failed services, Docker containers that are unhealthy or stopped, and whether the internet and DNS work.
curl -fsSLO https://wgdively.com/scripts/health-report.sh
bash health-report.sh
Read the script
#!/usr/bin/env bash
# health-report.sh: a one-screen health check for an Ubuntu home server.
# From https://wgdively.com/projects/server-scripts/
# Safe to run any time. It only reads; it changes nothing.
# Usage: bash health-report.sh
set -u
bold=$(tput bold 2>/dev/null || true); dim=$(tput dim 2>/dev/null || true); reset=$(tput sgr0 2>/dev/null || true)
ok() { printf ' \e[32m✔\e[0m %s\n' "$*"; }
warn() { printf ' \e[33m!\e[0m %s\n' "$*"; }
section() { printf '\n%s%s%s\n' "$bold" "$*" "$reset"; }
section "System"
echo " $(hostname) · $(. /etc/os-release && echo "$PRETTY_NAME") · kernel $(uname -r)"
echo " Up $(uptime -p | sed 's/^up //') · load $(cut -d' ' -f1-3 /proc/loadavg) on $(nproc) CPU threads"
section "Memory"
free -h | awk 'NR==2{printf " RAM %s used of %s (%s available)\n",$3,$2,$7} NR==3 && $2!="0B"{printf " Swap %s used of %s\n",$3,$2}'
section "Disks"
df -h -x tmpfs -x devtmpfs -x squashfs -x overlay -x efivarfs --output=pcent,used,size,target | tail -n +2 | while read -r pct used size target; do
n=${pct%\%}
if [ "$n" -ge 90 ]; then warn "$target is $pct full ($used of $size)"; else ok "$target $pct ($used of $size)"; fi
done
section "Temperature"
if ls /sys/class/thermal/thermal_zone*/temp >/dev/null 2>&1; then
max=0; for f in /sys/class/thermal/thermal_zone*/temp; do t=$(( $(cat "$f") / 1000 )); [ "$t" -gt "$max" ] && max=$t; done
if [ "$max" -ge 85 ]; then warn "Hottest sensor: ${max}°C (hot)"; else ok "Hottest sensor: ${max}°C"; fi
else echo " ${dim}No temperature sensors found${reset}"; fi
section "Updates"
if [ -x /usr/lib/update-notifier/apt-check ]; then
IFS=';' read -r all sec < <(/usr/lib/update-notifier/apt-check 2>&1)
if [ "${all:-0}" -gt 0 ]; then warn "$all updates waiting ($sec security). Run safe-update.sh"; else ok "Up to date"; fi
else
n=$(apt list --upgradable 2>/dev/null | grep -c upgradable); [ "$n" -gt 0 ] && warn "$n updates waiting" || ok "Up to date"
fi
[ -f /var/run/reboot-required ] && warn "A restart is needed to finish updates" || ok "No restart needed"
section "Services"
failed=$(systemctl --failed --no-legend --plain 2>/dev/null | awk '{print $1}')
if [ -n "$failed" ]; then warn "Failed: $(echo "$failed" | tr '\n' ' ')"; else ok "No failed system services"; fi
if command -v docker >/dev/null 2>&1; then
section "Docker"
if docker info >/dev/null 2>&1; then
total=$(docker ps -q | wc -l); unhealthy=$(docker ps --filter health=unhealthy --format '{{.Names}}')
stopped=$(docker ps -a --filter status=exited --filter status=dead --format '{{.Names}}')
ok "$total containers running"
[ -n "$unhealthy" ] && warn "Unhealthy: $(echo "$unhealthy" | tr '\n' ' ')"
[ -n "$stopped" ] && warn "Stopped: $(echo "$stopped" | tr '\n' ' ')"
echo " ${dim}Disk used by Docker: $(docker system df --format '{{.Type}} {{.Size}}' | tr '\n' ' ')${reset}"
else warn "Docker is installed but you can't reach it (try with sudo, or add yourself to the docker group)"; fi
fi
section "Network"
echo " LAN address: $(hostname -I | awk '{print $1}')"
if ping -c1 -W2 1.1.1.1 >/dev/null 2>&1; then ok "Internet reachable"; else warn "No internet (ping 1.1.1.1 failed)"; fi
if getent hosts cloudflare.com >/dev/null 2>&1; then ok "DNS working"; else warn "DNS lookups failing"; fi
echosafe-update.sh
Installs all Ubuntu updates without asking questions, keeps your existing config files if a package wants to replace one, tidies up old packages, and logs everything to /var/log/safe-update.log. Give it your compose folder and it updates those containers too. It tells you if a restart is needed, but never restarts by itself.
curl -fsSLO https://wgdively.com/scripts/safe-update.sh
sudo bash safe-update.sh # packages only
sudo bash safe-update.sh ~/homelab # packages + containers
Read the script
#!/usr/bin/env bash
# safe-update.sh: update Ubuntu packages (and Docker containers, if you want) with a log.
# From https://wgdively.com/projects/server-scripts/
# Usage: sudo bash safe-update.sh (system packages only)
# sudo bash safe-update.sh ~/homelab (also pull new images for that compose folder)
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo "Run it with sudo: sudo bash $0 $*"; exit 1; }
LOG=/var/log/safe-update.log
exec > >(tee -a "$LOG") 2>&1
echo "=== $(date '+%F %T') safe-update started"
export DEBIAN_FRONTEND=noninteractive
apt-get update
# Keep existing config files if a package asks.
apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold full-upgrade
apt-get -y autoremove --purge
apt-get -y autoclean
command -v snap >/dev/null 2>&1 && snap refresh || true
if [ $# -ge 1 ]; then
dir=$1
if [ -f "$dir/docker-compose.yml" ] || [ -f "$dir/compose.yaml" ] || [ -f "$dir/compose.yml" ]; then
echo "--- Updating containers in $dir"
(cd "$dir" && docker compose pull && docker compose up -d --remove-orphans)
docker image prune -f
else
echo "!!! No compose file in $dir, so containers were not updated"
fi
fi
if [ -f /var/run/reboot-required ]; then
echo "!!! A restart is needed to finish. Run: sudo reboot"
else
echo "No restart needed."
fi
echo "=== $(date '+%F %T') done. Full log: $LOG"docker-cleanup.sh
Docker quietly fills disks with old images and build cache. This shows what it would remove and asks first. It removes stopped containers, unused networks, untagged images and build cache, and leaves your volumes (your data) and any image a container is using alone.
curl -fsSLO https://wgdively.com/scripts/docker-cleanup.sh
bash docker-cleanup.sh # asks before deleting
bash docker-cleanup.sh --yes # no question (for a schedule)
Read the script
#!/usr/bin/env bash
# docker-cleanup.sh: free disk space Docker no longer needs.
# From https://wgdively.com/projects/server-scripts/
# Removes stopped containers, unused networks, dangling images and build cache.
# It never touches volumes (your data) or images that running containers use.
# Usage: bash docker-cleanup.sh (shows what would go, then asks)
# bash docker-cleanup.sh --yes (no question, for a timer)
set -euo pipefail
command -v docker >/dev/null || { echo "Docker isn't installed."; exit 1; }
echo "Before:"; docker system df
echo
echo "Stopped containers that would be removed:"
docker ps -a --filter status=exited --filter status=created --filter status=dead --format ' {{.Names}} ({{.Image}}, {{.Status}})' || true
echo
if [ "${1:-}" != "--yes" ]; then
read -rp "Clean up now? [y/N] " a
[[ "$a" =~ ^[Yy]$ ]] || { echo "Nothing changed."; exit 0; }
fi
docker container prune -f
docker network prune -f
docker image prune -f
docker builder prune -f
echo; echo "After:"; docker system dfbackup-folder.sh
Makes a dated .tar.gz of a folder (your homelab folder, say), checks the archive opens, and deletes the oldest backups so only the newest few remain. Point it at a USB drive or a second disk. A backup on the same disk won't save you if that disk dies.
curl -fsSLO https://wgdively.com/scripts/backup-folder.sh
bash backup-folder.sh ~/homelab /mnt/usb/backups 7
Databases (like Uptime Kuma's) are safest to back up while their container is stopped: docker compose stop, back up, then docker compose start.
Read the script
#!/usr/bin/env bash
# backup-folder.sh: dated, compressed backups of a folder, keeping the newest few.
# From https://wgdively.com/projects/server-scripts/
# Good for a homelab folder (compose files, .env and data). Stops nothing, so for
# databases it's safest to run while those containers are stopped, or use their own export.
# Usage: bash backup-folder.sh SOURCE DESTINATION [HOW_MANY_TO_KEEP]
# e.g. bash backup-folder.sh ~/homelab /mnt/usb/backups 7
set -euo pipefail
SRC=${1:-}; DEST=${2:-}; KEEP=${3:-7}
[ -n "$SRC" ] && [ -n "$DEST" ] || { sed -n '2,8p' "$0"; exit 1; }
[ -d "$SRC" ] || { echo "Source folder not found: $SRC"; exit 1; }
[[ "$KEEP" =~ ^[0-9]+$ ]] && [ "$KEEP" -ge 1 ] || { echo "HOW_MANY_TO_KEEP must be a number, 1 or more"; exit 1; }
mkdir -p "$DEST"
name=$(basename "$(realpath "$SRC")")
file="$DEST/$name-$(date +%Y-%m-%d_%H%M).tar.gz"
echo "Backing up $SRC to $file"
# --warning=no-file-changed: don't fail just because a log file changed mid-backup
tar --warning=no-file-changed -czf "$file.part" -C "$(dirname "$(realpath "$SRC")")" "$name" || [ $? -eq 1 ]
mv "$file.part" "$file"
tar -tzf "$file" >/dev/null && echo "Checked: the archive opens fine ($(du -h "$file" | cut -f1))."
# Keep only the newest $KEEP backups of this folder
mapfile -t old < <(ls -1t "$DEST/$name-"*.tar.gz 2>/dev/null | tail -n +"$((KEEP + 1))")
for f in "${old[@]}"; do rm -f -- "$f" && echo "Removed old backup $(basename "$f")"; done
echo "Done. Backups kept: $(ls -1 "$DEST/$name-"*.tar.gz | wc -l)"Run them on a schedule
Run sudo crontab -e, add lines like these, and save. The cron explainer turns the times into plain English. Move the scripts somewhere permanent first, for example sudo install -m 755 *.sh /usr/local/bin/.
# Every Sunday at 4:00 — updates (packages + containers)
0 4 * * 0 /usr/local/bin/safe-update.sh /home/YOU/homelab
# Every day at 3:30 — back up the homelab folder, keep a week
30 3 * * * /usr/local/bin/backup-folder.sh /home/YOU/homelab /mnt/usb/backups 7 >> /var/log/backup.log 2>&1
# First of the month at 5:00 — Docker clean-up
0 5 1 * * /usr/local/bin/docker-cleanup.sh --yes >> /var/log/docker-cleanup.log 2>&1