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!
Contents
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:
cd /domoticz/scripts/
mkdir bash
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:
sudo chmod +x backup.sh
- Schedule backup script
crontab -e
- paste (and make sure you are using tabs instead of spaces.) :
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: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
[ccen_bash width=”90%” line_numbers=”on”]
#!/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
### 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¶m=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
[/ccen_bash]
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.