It’s all well and good having a backup of OpenHAB, but unless it’s sitting on alternate media (as opposed to ON the same media as Openhab is running) then it’s not really that safe, if your disk dies (and SD cards on RPi’s especially tend to have a limited number of writes) then you can kiss your backup images goodbye. This example simply gets that backup off your Rpi and onto alternate media by way of a very simple script.
In my case, I run a QNAP NAS at home with a heap of disk storage primarily for my PC data backups. I also use it as a backup repository for Openhab (using the FTP server on it) Here’s a super quick and easy backup script I wrote to utilise the built-in openhab-cli backup utility then ftp the image off to the NAS. I run OH on a Raspberry Pi, so instructions are specifically for this. This assumes you have setup your NAS (or whatever you use) as an FTP server and have tested successful login and password for it and sending an image to it. For this example, create a folder called “ohbackup” off the root of the FTP server.
On the RPi, first install zip and curl if not already installed (“sudo apt-get update” first if necessary)
sudo apt-get install zip sudo apt-get install curl
Create a script to do a full backup and FTP it off to the NAS:
cd /home/pi sudo nano oh-bkup.sh
Paste the following contents into the new file, obviously changing “192.168.1.100” to be the IP of your FTP server, and ftpuser and mypassword as the login details that have write access. As I keep my scripts under /home/pi folder as well, I also add these to the ZIP file once the OH backups is complete (if you don’t want this, omit the lines zipfile=… to sudo zip…)
#!/bin/bash sudo rm /var/lib/openhab2/backups/*.zip sudo openhab-cli backup --full zipfile=( /var/lib/openhab2/backups/*.zip ) echo Adding scripts to $zipfile sudo zip -q $zipfile /home/pi/*.sh echo Uploading ZIP file to FTP Server curl -T /var/lib/openhab2/backups/*.zip ftp://192.168.1.100//ohbackup/ --user ftpuser:mypassword
CTRL X and save. Change ownership and permissions to execute.
sudo chown openhab:openhab oh-bkup.sh sudo chmod 777 oh-bkup.sh
Test the script by running it manually from the command line (ignore the error the first time you run, as it attempts to remove the backup image kept locally on the RPi before creating a new one then FTP’ing it off to the NAS)
Check the file exists under /var/lib/openhab2/backups and also has been FTP’d to your server successfully. Optionally, use cron to schedule it to run whenever you want:
sudo crontab -e
Add the following to the end of the cron file to, for example, schedule this to run every week at 3am.
0 3 * * 1 /home/pi-oh-bkup.sh
CTRL X and save. Can do a sudo crontab -l to check the entry exists.
For an image of the whole O/S (which I generally do before/after large upgrades) I just do what many do and shutdown OH, insert the SD card into my USB card reader and run win32diskimager and take a backup image. If I need to restore, I simply restore the image taken from win32diskimager, then boot up the RPi and use WinSCP to transfer the full backup image to the /tmp directory on the RPi then run
openhab-cli restore /tmp/*backup-image* to restore. Tested and works well (although I got to a stage where I got sick of SD cards failing so am now running an RPi with an SSD disk instead, still do the backups of course though!)