Files
vanlink/failover/sync-usb-backup.sh
T
andreasandClaude Haiku 4.5 565e8f31ab Fix watchdog timeout and MQTT connectivity issues
Problem: R5C spontaneously rebooted due to hardware watchdog timeout
Cause 1: van-gps-owntracks in infinite restart loop (MQTT connect failed)
Cause 2: mosquitto only listening on localhost, so clients couldn't connect
Solution:
- Add restart limits to van-gps-owntracks and van-li3-battery services
  (max 3 restarts per 5 minutes prevents infinite restart loops)
- Configure mosquitto to listen on all interfaces (0.0.0.0:1883)
- Deploy mosquitto listener config via deploy.sh
- Restart mosquitto during deploy to apply config changes

This prevents the system from hitting the 11-second hardware watchdog timeout
when services crash and systemd auto-restarts them indefinitely.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-09-18 18:20:11 -04:00

69 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Refresh the USB disk (sda) as a live boot-fallback clone of the
# running root filesystem. Run manually after a push:
# sudo ./failover/sync-usb-backup.sh
#
# EEPROM BOOT_ORDER falls back to the SD card, then this USB disk, if the
# primary boot disk (NVMe) doesn't come up. /etc/fstab, /etc/machine-id,
# and /var/log are excluded from the sync so the clone keeps its own
# partition identity (PARTUUIDs differ per disk) and boot/journal history
# instead of inheriting the source disk's — see failover/README or repo
# memory "sd-fallback-fstab-clobber" for what breaks if those leak across.
set -euo pipefail
[ "$(id -u)" -eq 0 ] || { echo "Run as root (sudo)." >&2; exit 1; }
USB_ROOT_PART=/dev/sda2
MOUNT_POINT=/mnt/usb-backup-root
MOUNT_POINT=10.42.1.44:/media/andreas/writable-nvme
[[ "$MOUNT_POINT" == *":"* ]]
LOCAL_MOUNT=$?
root_src=$(findmnt -no SOURCE /)
if [ "$root_src" = "$USB_ROOT_PART" ]; then
echo "Currently booted from $USB_ROOT_PART — refusing to sync USB onto itself." >&2
exit 1
fi
if [ $LOCAL_MOUNT -eq 1 ]; then
mkdir -p "$MOUNT_POINT"
mounted_here=0
if ! mountpoint -q "$MOUNT_POINT"; then
mount "$USB_ROOT_PART" "$MOUNT_POINT"
mounted_here=1
else
mount -o remount,rw "$MOUNT_POINT"
fi
cleanup() {
if [ "$mounted_here" -eq 1 ]; then
umount "$MOUNT_POINT"
fi
}
trap cleanup EXIT
fi
# rsync exit 24 ("partial transfer due to vanished source files") is expected on a
# live root — files like container shm sockets or in-progress logs routinely disappear
# mid-sync. Tolerate that one code; anything else is a real failure and still aborts.
rc=0
rsync -aHAX --numeric-ids --delete \
--info=progress2 \
--exclude=/proc/* --exclude=/sys/* --exclude=/dev/* --exclude=/run/* \
--exclude=/tmp/* --exclude=/mnt/* --exclude=/media/* --exclude=/lost+found \
--exclude=/boot/firmware/* --exclude=/swapfile \
--exclude=/etc/fstab --exclude=/etc/machine-id --exclude=/var/log/* \
--stats / "$MOUNT_POINT/" || rc=$?
if [ "$rc" -ne 0 ] && [ "$rc" -ne 24 ]; then
echo "rsync failed (exit $rc)." >&2
exit "$rc"
fi
if [ "$rc" -eq 24 ]; then
echo "Note: some files vanished mid-sync (rsync exit 24) — normal on a live root." >&2
fi
echo "USB backup ($USB_ROOT_PART) synced from $root_src."