In the past I had a crashed and lost some data, and I had to pair all my devices again.
That time I thought: It will happen never again! Use this Domoticz backup script to secure your config, adjust it a little bit to your own needs, and be safe! So Backup before it’s too late!

How does it work?

Every night I make a complete backup of the most important stuff on my raspberry to a temporary folder.

That temporary folder would be zipped and transferred to my NAS.

Backups on my NAS share older than 31 days would be deleted.
This way I have not only my database but also: styles, bash scripts, cronjobs and everything I need to build a new Domoticz config very fast.

 

How to install

First: make a share on a pc or NAS

Edit the script. delete unused paths and configs and add the things you want to include.

  • Place the script
    • ssh into your Raspberry Pi
    • make a script folder:
      • 1
        cd /domoticz/scripts/
      • 1
        mkdir bash
      • 1
        nano backup.sh
      • Paste the script from below this page in that file (ctrl+c > right click on PuTTy) and save the script
      • make backup.sh executable:
      • 1
        sudo chmod +x backup.sh
  • Schedule backup script
    • 1
      crontab -e
    • paste (and make sure you are using tabs instead of spaces.) :
    • 1
      1 0 * * * sudo sh /home/pi/domoticz/scripts/bash/backup.sh
    • save the cronjob. this would run the backup.sh script the first minute of every day.

Test your backup

In SSH go to the folder and execute:

1
sudo ./backup.sh

Watch output, go to your share and validate the content of the zip.
An untested backup is no backup.

 

 

Recover and restore a backup

I hope you have never had to use it, but when you needed to restore it’s very simple.
Do a clean install with everything you used before,  then place the file of the backup back to the raspberry, in the right folder path. Reboot and you are up and running in no time.

 

The actual Domoticz backup script

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<br />#!/bin/bash</p>
<p>### backup.sh<br />### @author : Remb0<br />### @since : 29-11-2016<br />### @updated: 28-12-2016<br />### Script to backup up Domoticz database, lua, bash, python script, just in case something goes wrong.</p>
<p># Additional you can install a dropbox uploader so this script can upload your backups to Dropbox as a offside backup<br /># It is not mandatory, its just na extra service<br /># Script is looking for it and skips the uploading if you didn't want to use Dropbox<br /># Installing Dropbox_Uploader is very easy.<br /># Download dropbox_uploader.sh, install it and your done<br /># More information here: https://github.com/andreafabrizi/Dropbox-Uploader</p>
<p><code></code>### This will backup your database, scripts and LUA to your NAS drive<br />### USER CONFIGURABLE PARAMETERS<br />DESTDIR="/home/pi/domoticz/backup2Nas" # used for: NAS<br />IP_NAS="192.168.0.10"<br />MOUNTPATH="//$IP_NAS/Storage/domoticz"<br />USERNAME="USERNAME"<br />PASSWORD="PASSWORD"<br />PI="pi1_"</p>
<p>DOMO_IP="127.0.0.1" # Domoticz IP<br />DOMO_PORT="8080"<br />HOME_DIR="/home/pi"</p>
<p>### Which unwanted files to remove from the backup<br />files_exclude=".get/*"</p>
<p>### Retrieve current and updated Domoticz version number<br />DOMO_JSON_CURRENT=`curl -s -X GET "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=getversion"`<br />DOMO_CURRENT_VERSION=$(echo $DOMO_JSON_CURRENT |grep -Po '(?<="version" : "3.)[^"]*')</p>
<p>### Retrieve current Timestamp<br />TIMESTAMP=`/bin/date +%Y%m%d-%Huur-%M`<br />BACKUPFILE="domoticz.db"<br />BACKUPFILEGZ="$BACKUPFILE".gz</p>
<p>### Domoticz Various Backup Folders</p>
<p>#DROPBOX_UPLOADER="/home/pi/domoticz/scripts/bash/dropbox_uploader.sh"</p>
<p>###############################################################################################################################################################<br />### END OF USER CONFIGURABLE PARAMETERS<br />###############################################################################################################################################################</p>
<p>###############################################################################################################################################################</p>
<p>### Do not edit anything below this line unless your knowing what to do!</p>
<p>###############################################################################################################################################################</p>
<p>echo "<br />____ _ _ ____ _<br />| _ \ ___ _ __ ___ ___ | |_(_) ___ ____ | __ ) __ _ ___| | ___ _ _ __<br />| | | |/ _ \| _ _ \ / _ \| __| |/ __|_ / | _ \ / _ |/ __| |/ / | | | _ \<br />| |_| | (_) | | | | | | (_) | |_| | (__ / / | |_) | (_| | (__| <| |_| | |_) |<br />|____/ \___/|_| |_| |_|\___/ \__|_|\___/___| |____/ \____|\___|_|\_\\____| ___/<br />|_|<br />"<br />sleep 2</p>
<p>echo "::: Checking dependencies"<br /># apt-get install -y cifs-utils<br />MOUNTCOMMAND="sudo mount -t cifs -o username=$USERNAME,password=$PASSWORD $MOUNTPATH $DESTDIR"<br />$MOUNTCOMMAND</p>
<p>### Check if location is mounted<br />if [ -d "$DESTDIR" ] ; then<br />echo "$DESTDIR directory exists!"<br />echo " "<br />echo "::: Make backup folder structure"<br />echo "---------------------------------------------------"<br />cd /tmp<br />mkdir /tmp/backup<br />mkdir /tmp/backup/izsynth<br />mkdir /tmp/backup/habridge<br />mkdir /tmp/backup/habridge/data<br />mkdir /tmp/backup/phrases<br />mkdir /tmp/backup/scripts<br />#mkdir /tmp/backup/plugins<br />mkdir /tmp/backup/Logs<br />mkdir /tmp/backup/Config<br />mkdir /tmp/backup/www/<br />mkdir /tmp/backup/www/styles/<br />mkdir /tmp/backup/www/images/<br />mkdir /tmp/backup/www/images/floorplans<br />mkdir /tmp/backup/www/templates<br />mkdir /tmp/backup/plugins<br />mkdir /tmp/backup/www/dashboard/<br />mkdir /tmp/backup/www/dashboard/custom</p>
<p>echo " "<br />echo "::: Backing up Domoticz"<br />echo "---------------------------------------------------"<br />/usr/bin/curl -s http://127.0.0.1:8080/backupdatabase.php > /tmp/backup/$BACKUPFILE</p>
<p>echo " "<br />echo "::: Backing up files"<br />echo "---------------------------------------------------"</p>
<p>rsync -av /home/pi/domoticz/scripts/* /tmp/backup/scripts --exclude='.git' > /dev/null 2>&1</p>
<p>rsync -av /home/pi/domoticz/plugins/* /tmp/backup/plugins --exclude='.git' > /dev/null 2>&1</p>
<p>cp -r /home/pi/domoticz/Logs/* /tmp/backup/Logs<br />cp -r /home/pi/domoticz/www/styles/* /tmp/backup/www/styles<br />cp -r /home/pi/domoticz/www/images/floorplans/* /tmp/backup/www/images/floorplans<br />cp -r /home/pi/domoticz/www/templates/* /tmp/backup/www/templates</p>
<p>cp -r /home/pi/domoticz/private_cert.pem /tmp/backup<br />cp -r /home/pi/domoticz/server_cert.pem /tmp/backup<br />cp -r /home/pi/domoticz/domoticz.sh /tmp/backup</p>
<p>sudo cp -r /etc/monit/monitrc /tmp/backup<br />sudo cp -r /home/pi/domoticz/Config/zwcfg* /tmp/backup/Config/</p>
<p>echo "--- Backing up .bash_profile"<br />cp -R /etc/profile.d/motd.sh /tmp/backup/.bash_profile</p>
<p>cp -r /home/pi/domoticz/www/dashboard/custom/* /tmp/backup/www/dashboard/custom</p>
<p>cp -r /home/pi/habridge/data/* /tmp/backup/habridge/data<br />cp -r /home/pi/.config/izsynth/* /tmp/backup/izsynth</p>
<p>crontab -u pi -l > /tmp/backup/$(date +%Y%m%d).crontab</p>
<p>sleep 1<br />echo " "<br />echo "::: Zipping backup"<br />echo "---------------------------------------------------"<br />echo "--- Zipping Domoticz backup files"<br />echo "--- Please standby..."</p>
<p>cd /tmp/backup/<br />#echo compress with tar the database, scripts and files.<br />#sudo tar -zcvf $PI$TIMESTAMP.tar.gz /tmp/backup/*<br />sudo tar pcfz $PI$TIMESTAMP.tar.gz *</p>
<p>echo "--- Done zipping!"<br />sleep 1</p>
<p>echo "::: Transfer backup"<br />echo "---------------------------------------------------"<br />echo "--- transferring backups to NAS drive "<br />echo "--- Please standby..."<br />if ping -c 1 $IP_NAS >/dev/null ;then<br />cp $PI$TIMESTAMP.tar.gz $DESTDIR<br />echo "A copy of database, scripts and LUA are now on your NAS"<br />else<br />echo "NAS is offline"<br />exit 1<br />fi</p>
<p>### Uploading backup to Dropbox<br /># echo " "<br /># echo "::: Dropbox"<br /># echo "---------------------------------------------------"<br /># echo "--- Uploading backup to Dropbox"<br /># echo "--- Please standby..."<br /># $DROPBOX_UPLOADER upload $MOUNT/$DOMO_BACKUP_HOME/$DOMO_BACKUP_ZIP.tar.gz /<br /># echo "--- Done!"<br /># else<br /># echo " "<br /># echo "::: Dropbox"<br /># echo "---------------------------------------------------"<br /># echo "--- Skipping uploading to Dropbox as it seems it ain't installed"<br /># echo "--- Skipped!"<br /># sleep 2<br /># fi</p>
<p>### Removing backups older then 31 days<br />echo " "<br />echo "::: Removing backups older then 30 days from NAS"<br />echo "---------------------------------------------------"<br />echo "--- Cleaning old backups packages"<br />echo "--- Please standby..."<br />sudo find /$DESTDIR/$PI* -name '*.gz' -mtime +30 -delete<br />echo deleting old files used in backups<br />sudo /bin/rm -rf /tmp/backup<br />sudo /bin/rm -rf /tmp/$PI$TIMESTAMP.tar.gz<br />sleep 2</p>
<p>### unmount<br />sudo umount $DESTDIR</p>
<p>else</p>
<p>echo "---------------------------------------------------"<br />echo "--- Backup location isn't mounted"<br />echo "--- Please mount your backup location"<br />echo " "<br />exit 1<br />fi</p>
<p>exit<br />

Further considerations on the backup script

As every backup, the backup script is only a good as its tested restore. Know what it does and what you need to do get things up and running. It is also good to have a spare SD card at hand, especially if you depend heavily on your Domoticz setup.

Make sure you check out our other Domoticz tutorials.

  • Design
  • Quality
  • Usability
  • Home-Automation compatibility
  • Price