Super simple OH backup script to FTP server

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

Paste the following contents into the new file, obviously changing “” 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…)

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 --user ftpuser:mypassword

CTRL X and save. Change ownership and permissions to execute.

sudo chown openhab:openhab
sudo chmod 777

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/

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!)


Thanks for sharing this. Just have a quick concern as it appears you are storing the password for your FTP server in the file and then giving everyone read permission allowing people to read the password?

sudo chmod 777

consider using this instead.

sudo chmod 771

Yes of course thanks, typed it all out a little too quick :+1:

I’m just familiarizing myself with the problem of SD cards failing over time due to wear, and I’ve been debating how best to prepare for the issue. I’m likely going to go the SSD route.

I don’t have a NAS, so would it make sense for me to just plug in a USB key and save the daily backups onto that as a spare media? Or is that also at risk while it’s attached to the system?


See this post.

Thanks, that’s a very helpful post!

1 Like

Clear tutorial, thanks for sharing. One question, did you consider automatically mounting a network share (from your NAS) on your RPI and backup to that mounted share automatically? It removes the need for using FTP.
Just wondering.

Hi, yeah I considered that but I already had an FTP server running and configured as a backup repository for clients network device configurations, so just persisted with that. I also have my NAS sitting in a different zone (and therefore subnet) on my firewall than OH, and don’t allow SMB through the firewall. I guess the protocol is largely irrelevant, the important part is getting those backups OFF you OH system and onto alternate media!

Instead of FTP’ing you could obviously just map a CIFS share, would be as simple as mounting a share such as:

sudo mkdir /mnt/nas
sudo mount -t cifs -o username=*USER*,password=*PASS* //*NAS_IP_ADDRESS*/Backup /mnt/nas

Then replace curl command with:

sudo cp /var/lib/openhab2/backups/*.zip /mnt/nas

That makes complete sense, thanks for sharing. I always learn something !

Thank you!