Raspberry Pi - Domoticz, Z-Wave Backup Script

All kinds of 'OS' scripts
Post Reply
Posts: 494
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta

Raspberry Pi - Domoticz, Z-Wave Backup Script

Post by ben53252642 » Tuesday 05 December 2017 3:17

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 (19.07 KiB) Viewed 1696 times

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

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


Code: Select all


# 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

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

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

# 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 '')
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 ''

# 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"
echo "Z-Wave backup integrity check: Backup does not contain the same Home Id compared to what is on the card, manual check required..."
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 {} \;
I run the script via another script in the same directory called start.sh:

Code: Select all

if screen -list | grep -q "backup"; then
screen -x backup
screen -S backup -d -m /scripts/backup/backup.sh
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.

Posts: 1
Joined: Tuesday 09 January 2018 23:06
Target OS: Raspberry Pi
Domoticz version:

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

Post by edwin2008 » Tuesday 09 January 2018 23:13

anyone tried this beside ben?

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest