Showing backup state in Home Assistant

My partner wanted to be able to see more easily that her photos are being backed up successfully.

by Robert May on Afternoon Robot

Illustrating the success of backups is pretty tricky, especially when what you're backing up is a phone. We now use SyncThing to get our data off our Android phones, but this is a "sync" solution and not a "backup" solution.

To that end, I've started working on mirroring all synchronised files on the storage server from the SyncThing folders to a new, flat-structure one. This accounts for the fairly bonkers file hierarchy on Android where any application can put its files anywhere, and makes it easier to know where everything is.

But it'd be nice to know that the backups are happening, and so I've started building out a Home Assistant dashboard to show my partner the status of her backups. The first step is illustrating that photos are being synced correctly, and what better way than to show them? Enter a rather dull bash script, and the Home Assistant Push camera integration.

First up, a bash script that listens for file changes, specifically close_write and moved_to in the SyncThing folders:

#!/bin/bash

LOGFILE=/var/log/camerasync.log

inotifywait -m -r -e close_write,moved_to --format "%w%f" /data/sync/phone/camera | while read FILE; do
        echo "${FILE}: changed" >> $LOGFILE

        if [[ $FILE == *.tmp ]]; then
                echo "${FILE}: Temp file, ignoring" >> $LOGFILE
                continue
        fi

        cp -v $FILE /data/sync/phone/media >> $LOGFILE
        curl -X POST http://localhost:8123/api/webhook/camerasync-finished >> $LOGFILE

        if [[ $FILE == *.jpg ]]; then
                echo "${FILE}: is an image, posting to home assistant" >> $LOGFILE
                curl -X POST -F "image=@${FILE}" http://localhost:8123/api/webhook/phone_camera_feed >> $LOGFILE
        fi

        echo "${FILE}: finished" >> $LOGFILE
done

I'm not amazing at bash, but this is fairly simple. It copies everything except .tmp files, which are created by SyncThing before being moved into the final position. We're mostly only interested in the moved_to event but I have both in there just in case something gets created in its final position. I don't really care about overwriting existing files, as these directories are covered by ZFS snapshots as well as a remote backup with its own snapshots. Should any files get missed, somehow, a regular rsync job will match the folders back up, when I get around to writing it.

The camera is configured in the Home Assistant configuration.yaml file:

camera:
  - platform: push
    name: Phone Camera
    webhook_id: phone_camera_feed

And after setting it up in a dashboard, ends up looking like this:

Side note: these 8bitdo controllers are pretty nice

Rather neatly the image refreshes automatically without reloading the page. Next steps will likely be showing a tail of the log file for more visibility.