🌐 Overview

ThisBash script automates the backup ofZigbee2MQTT data. It begins by defining the locations for the log file, source directory, and backup directory. The script then creates the backup directory if it does not already exist. It creates a compressed tarball of the Zigbee2MQTT data, excluding the backup directory itself, and names the backup file with a timestamp.

If the backup creation is successful, the script logs this event with a timestamp in the log file. If the backup creation fails, it logs an error message. This ensures that the Zigbee2MQTT data is consistently and securely backed up.

Script Execution via CRON

This script has been designed to run automatically via aCRON job on the server, ensuring consistent and unattended backups of Zigbee2MQTT data.

🚀 Script

backup_zigbee2mqtt.sh
#!/bin/bash
 
log_file="/var/log/backup.log"
source_dir="/var/www/homebridge/zigbee2mqtt/"
backup_dir="backups"
 
mkdir -p "${source_dir}${backup_dir}"
backup_file="${source_dir}${backup_dir}/zigbee2mqtt-backup-$(date +%Y%m%d%H%M%S).tar.gz"
tar --exclude="./${backup_dir}" -czf "$backup_file" -C "$source_dir" .
 
if [ $? -eq 0 ]; then
    echo "$(date) - Backup created successfully: $backup_file" >> "$log_file"
else
    echo "$(date) - Backup failed" >> "$log_file"
fi