After my scare with the Z-wave controller dying due to SD card failure (See the blog post), I decided that my Zigbee network is important enough to back up, especially because a whole lot more important data is stored on disk rather than in the dongle as with Z-wave. I’m going to follow the same path I took in the Z-wave blog, but for Zigbee2MQTT. Since it’s running ‘bare’ on a raspberry pi, I can’t just backup the whole virtual machine. Here’s the solution I came up with.

First, Install Autofs according to my other blog post. Setup a folder on your NAS to keep backups for Zigbee2MQTT, and mount that directory with autofs to /mnt/backup

Next, we need to find where zigbee2mqtt is storing its local data. In my case, I installed it with npm, and the data directory is at /opt/zigbee2mqtt/data. This contains your configuration.yaml, which is important, along with other internal files used by Zigbee2MQTT (such as database.db).

I’m going to use essentially the same backup script as before, where I take the day of the week and append it to the filename so I automatically keep the last 7 days of backups. I keep the file on the NAS in the backup directory so the backup won’t fill the SD card with backups if the NAS doesn’t mount, since it can’t access the file. I named the shell script /mnt/backup/backup.sh

#!/bin/bash
####################################
#
# Backup the Zigbee2MQTT data directory
#
####################################

# Create archive filename based on the day of the week
day=$(date +%A)

# Backup the files using tar.
tar czf /mnt/backup/zigbee_data_$day.tar.gz /opt/zigbee2mqtt/data

Next, edit the root user’s crontab so it will run all the time once per day:

sudo crontab -e

And the new contents - this will run at the 0 minute of the 0 hour (midnight), all days of all weeks of all months (so every day).

0 0 * * * bash /mnt/backup/backup.sh

The End. After a week, there should be 7x .tar.gz files in the backup directory (Which is mounted from the NAS and can be managed according to your backup and long term storage strategy from there).

Beware when extracting the archive that it restores permissions, so you need to chmod the folder once you restore it to give yourself permissions. I also realized with this that some of my Pi’s never had their timezone set, so they are still on UTC time. If that’s the case, you can sudo raspi-config to change it.