Keep your Domoticz config safe by using a Domoticz backup script!

Tutorial Domoticz Backup safely

Backup before it’s too late!

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 backup script to secure your config, adjust it a little bit to your own needs, and be safe!

 

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 script itself

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash

### backup.sh
### @author : Remb0
### @since : 29-11-2016
### @updated: 28-12-2016
### Script to backup up Domoticz database, lua, bash, python script, just in case something goes wrong.

# Additional you can install a dropbox uploader so this script can upload your backups to Dropbox as a offside backup
# It is not mandatory, its just na extra service
# Script is looking for it and skips the uploading if you didn't want to use Dropbox
# Installing Dropbox_Uploader is very easy.
# Download dropbox_uploader.sh, install it and your done
# More information here: https://github.com/andreafabrizi/Dropbox-Uploader

<code></code>### This will backup your database, scripts and LUA to your NAS drive
### USER CONFIGURABLE PARAMETERS
DESTDIR="/home/pi/domoticz/backup2Nas" # used for: NAS
IP_NAS="192.168.0.10"
MOUNTPATH="//$IP_NAS/Storage/domoticz"
USERNAME="USERNAME"
PASSWORD="PASSWORD"
PI="pi1_"

DOMO_IP="127.0.0.1" # Domoticz IP
DOMO_PORT="8080"
HOME_DIR="/home/pi"

### Which unwanted files to remove from the backup
files_exclude=".get/*"

### Retrieve current and updated Domoticz version number
DOMO_JSON_CURRENT=`curl -s -X GET "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command&param=getversion"`
DOMO_CURRENT_VERSION=$(echo $DOMO_JSON_CURRENT |grep -Po '(?<="version" : "3.)[^"]*')

### Retrieve current Timestamp
TIMESTAMP=`/bin/date +%Y%m%d-%Huur-%M`
BACKUPFILE="domoticz.db"
BACKUPFILEGZ="$BACKUPFILE".gz

### Domoticz Various Backup Folders

#DROPBOX_UPLOADER="/home/pi/domoticz/scripts/bash/dropbox_uploader.sh"

###############################################################################################################################################################
### END OF USER CONFIGURABLE PARAMETERS
###############################################################################################################################################################

###############################################################################################################################################################

### Do not edit anything below this line unless your knowing what to do!

###############################################################################################################################################################

echo "
____ _ _ ____ _
| _ \ ___ _ __ ___ ___ | |_(_) ___ ____ | __ ) __ _ ___| | ___ _ _ __
| | | |/ _ \| _ _ \ / _ \| __| |/ __|_ / | _ \ / _ |/ __| |/ / | | | _ \
| |_| | (_) | | | | | | (_) | |_| | (__ / / | |_) | (_| | (__| <| |_| | |_) |
|____/ \___/|_| |_| |_|\___/ \__|_|\___/___| |____/ \____|\___|_|\_\\____| ___/
|_|
"

sleep 2

echo "::: Checking dependencies"
# apt-get install -y cifs-utils
MOUNTCOMMAND="sudo mount -t cifs -o username=$USERNAME,password=$PASSWORD $MOUNTPATH $DESTDIR"
$MOUNTCOMMAND

### Check if location is mounted
if [ -d "$DESTDIR" ] ; then
echo "$DESTDIR directory exists!"
echo " "
echo "::: Make backup folder structure"
echo "---------------------------------------------------"
cd /tmp
mkdir /tmp/backup
mkdir /tmp/backup/izsynth
mkdir /tmp/backup/habridge
mkdir /tmp/backup/habridge/data
mkdir /tmp/backup/phrases
mkdir /tmp/backup/scripts
#mkdir /tmp/backup/plugins
mkdir /tmp/backup/Logs
mkdir /tmp/backup/Config
mkdir /tmp/backup/www/
mkdir /tmp/backup/www/styles/
mkdir /tmp/backup/www/images/
mkdir /tmp/backup/www/images/floorplans
mkdir /tmp/backup/www/templates
mkdir /tmp/backup/plugins
mkdir /tmp/backup/www/dashboard/
mkdir /tmp/backup/www/dashboard/custom

echo " "
echo "::: Backing up Domoticz"
echo "---------------------------------------------------"
/usr/bin/curl -s http://127.0.0.1:8080/backupdatabase.php > /tmp/backup/$BACKUPFILE

echo " "
echo "::: Backing up files"
echo "---------------------------------------------------"

rsync -av /home/pi/domoticz/scripts/* /tmp/backup/scripts --exclude='.git' > /dev/null 2>&1

rsync -av /home/pi/domoticz/plugins/* /tmp/backup/plugins --exclude='.git' > /dev/null 2>&1

cp -r /home/pi/domoticz/Logs/* /tmp/backup/Logs
cp -r /home/pi/domoticz/www/styles/* /tmp/backup/www/styles
cp -r /home/pi/domoticz/www/images/floorplans/* /tmp/backup/www/images/floorplans
cp -r /home/pi/domoticz/www/templates/* /tmp/backup/www/templates

cp -r /home/pi/domoticz/private_cert.pem /tmp/backup
cp -r /home/pi/domoticz/server_cert.pem /tmp/backup
cp -r /home/pi/domoticz/domoticz.sh /tmp/backup

sudo cp -r /etc/monit/monitrc /tmp/backup
sudo cp -r /home/pi/domoticz/Config/zwcfg* /tmp/backup/Config/

echo "--- Backing up .bash_profile"
cp -R /etc/profile.d/motd.sh /tmp/backup/.bash_profile

cp -r /home/pi/domoticz/www/dashboard/custom/* /tmp/backup/www/dashboard/custom

cp -r /home/pi/habridge/data/* /tmp/backup/habridge/data
cp -r /home/pi/.config/izsynth/* /tmp/backup/izsynth

crontab -u pi -l > /tmp/backup/$(date +%Y%m%d).crontab

sleep 1
echo " "
echo "::: Zipping backup"
echo "---------------------------------------------------"
echo "--- Zipping Domoticz backup files"
echo "--- Please standby..."

cd /tmp/backup/
#echo compress with tar the database, scripts and files.
#sudo tar -zcvf $PI$TIMESTAMP.tar.gz /tmp/backup/*
sudo tar pcfz $PI$TIMESTAMP.tar.gz *

echo "--- Done zipping!"
sleep 1

echo "::: Transfer backup"
echo "---------------------------------------------------"
echo "--- transferring backups to NAS drive "
echo "--- Please standby..."
if ping -c 1 $IP_NAS >/dev/null ;then
cp $PI$TIMESTAMP.tar.gz $DESTDIR
echo "A copy of database, scripts and LUA are now on your NAS"
else
echo "NAS is offline"
exit 1
fi

### Uploading backup to Dropbox
# echo " "
# echo "::: Dropbox"
# echo "---------------------------------------------------"
# echo "--- Uploading backup to Dropbox"
# echo "--- Please standby..."
# $DROPBOX_UPLOADER upload $MOUNT/$DOMO_BACKUP_HOME/$DOMO_BACKUP_ZIP.tar.gz /
# echo "--- Done!"
# else
# echo " "
# echo "::: Dropbox"
# echo "---------------------------------------------------"
# echo "--- Skipping uploading to Dropbox as it seems it ain't installed"
# echo "--- Skipped!"
# sleep 2
# fi

### Removing backups older then 31 days
echo " "
echo "::: Removing backups older then 30 days from NAS"
echo "---------------------------------------------------"
echo "--- Cleaning old backups packages"
echo "--- Please standby..."
sudo find /$DESTDIR/$PI* -name '*.gz' -mtime +30 -delete
echo deleting old files used in backups
sudo /bin/rm -rf /tmp/backup
sudo /bin/rm -rf /tmp/$PI$TIMESTAMP.tar.gz
sleep 2

### unmount
sudo umount $DESTDIR

else

echo "---------------------------------------------------"
echo "--- Backup location isn't mounted"
echo "--- Please mount your backup location"
echo " "
exit 1
fi

exit
%d bloggers like this: