Raspberry Pi - Domoticz, Z-Wave Backup Script

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Raspberry Pi - Domoticz, Z-Wave Backup Script

Post by ben53252642 »

This script is designed to create a backup to a network share that can be restored to completely new identical hardware including the Z-Wave network (must be using the Razberry GPIO Gen 5 model and have Z-Way software installed).

It does:

1) Backs up Z-Wave network via Z-Way
2) Backs up the Domoticz database
3) Creates a gz compressed DD image of the Raspberry Pi's entire Micro SD card
4) Delete backups older than 180 days (configured at the bottom of the script)

Note that there is a chance for a DD backup to contain some corrupt files if they change during the moment they are backed up. I use the BTRFS filesystem on my Pi which allows me to run an integrity check after a restore to make sure there are no issues.

This script requires moderate - advanced Linux knowledge, general support will not be provided, use at your own risk.

You will need to understand and modify sections of the script to suite your environment.

Example output:
Backupoutput1.PNG
Backupoutput1.PNG (19.07 KiB) Viewed 2532 times

Displays backup progress and speed:
Backupoutput2.PNG
Backupoutput2.PNG (1.92 KiB) Viewed 2532 times

Creates folder on the network share with contents similar to:
Backupoutput3.PNG
Backupoutput3.PNG (26.39 KiB) Viewed 2509 times

backup.sh

Code: Select all

#!/bin/bash

# Required: apt-get install jq curl pv gzip cifs-utils

# Basics
date=$(date +"%d-%m-%Y-%I_%M%p")
hostname=$(cat /etc/hostname)
# Domoticz Configuration
domoticzserver="192.168.0.5"
domoticzport="80"
domoticzuser="USERNAME"
domoticzpass="PASSWORD"

# Create network folder if nessesary
mkdir -p /network/Backup

# Mount network drive
if mount | grep /network/Backup > /dev/null; then
echo "Network drive mounted"
else
echo "Mounting network drive"
mount -t cifs //192.168.0.2/Backup /network/Backup -o username=USERNAME,password=PASSWORD
fi

# Create backup folder
mkdir -p /network/Backup/"$hostname"/"$date"

# Backup Domoticz database
echo "Backing up Domoticz database"
curl --max-time 60 -s -o "/network/Backup/${hostname}/${date}/domoticzdb-${date}.db" "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/backupdatabase.php"

# Stop / Start services nessesary for zwave backup
echo "Backing up Z-Wave network via Z-Way"
service domoticz stop
service z-way-server start
service mongoose start
sleep 5

# Check Z-Way can see the Z-Wave hardware
controller=$(curl --max-time 60 -s -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"password":"PASSWORD","login":"USERNAME","rememberme":false}' --compressed 'http://192.168.0.5:8083/ZWave.zway/Data/0')
zwavechipmodel=$(echo "$controller" | jq '.controller.data.ZWaveChip.value')
vendor=$(echo "$controller" | jq '.controller.data.vendor.value')
echo "Found Z-Wave controller: ${vendor} chip model ${zwavechipmodel}"

# Create backup of the zwave network
curl --max-time 60 -s -o "/network/Backup/${hostname}/${date}/z-way-backup-${date}.zbk" -H 'Accept: application/json, text/plain, */*' -H 'Content-Type: application/json;charset=UTF-8' --data-binary '{"password":"PASSWORD","login":"USERNAME","rememberme":false}' --compressed 'http://192.168.0.5:8083/ZWaveAPI/Backup'

# Verify integrity of zwave network backup
controllerhomeid=$(echo "$controller" | jq '.controller.data.homeId.value')
mkdir -p /tmp/zwavebackupcheck
tar -zxf "/network/Backup/${hostname}/${date}/z-way-backup-${date}.zbk" -C /tmp/zwavebackupcheck/
backupcontrollerhomeid=$(cat /tmp/zwavebackupcheck/zddx/*.xml | grep '<data name="homeId"' | awk '{ print $6 }' | sed 's/[^0-9-]*//g')
if [ "$controllerhomeid" == "$backupcontrollerhomeid" ]; then
echo "Z-Wave backup integrity check: Home Id on card and in backup file match"
else
echo "Z-Wave backup integrity check: Backup does not contain the same Home Id compared to what is on the card, manual check required..."
fi
rm -rf /tmp/zwavebackupcheck

# Stop / Start services nessesary for Domoticz
service z-way-server stop
service mongoose stop
service zbw_connect stop
service domoticz start

# Create a DD backup of the entire sd card
echo "Creating new DD backup" "$hostname"-"$date"
nice -n 19 ionice -c2 -n7 dd if=/dev/mmcblk0 | pv -c | nice -n 19 ionice -c2 -n7 gzip -1 > /network/Backup/"$hostname"/"$date"/"$hostname"-"$date".gz
echo "Deleting backups older than 180 days"
find /network/Backup/"$hostname"* -mtime +180 -exec rm {} \;
exit
I run the script via another script in the same directory called start.sh:

Code: Select all

#!/bin/bash
if screen -list | grep -q "backup"; then
screen -x backup
else
screen -S backup -d -m /scripts/backup/backup.sh
fi
Running start.sh will start a backup running in the background, running it a second time while a backup is still in progress then displays the screen session showing output similar to that in the example screenshots above.

Known issues:
* Cannot run the Echo Java bridge (if you are using it) while the Z-Way backup is in progress due to port conflict, be sure to stop Echo Bridge before running the script (or you can simply stop the service as part of the script which is what I do).
* Domoticz is stopped for about 30 seconds while the Z-Wave network backup is done via Z-Way, it is able to be running while the longer DD backup is in progress.

My use case:
I keep a spare Raspberry Pi with a second identical Z-Wave controller onboard. In the event that my primary controller fails, I can do a total restore to the backup hardware.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
edwin2008
Posts: 2
Joined: Tuesday 09 January 2018 22:06
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Raspberry Pi - Domoticz, Z-Wave Backup Script

Post by edwin2008 »

anyone tried this beside ben?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest