Backup Configuration Button in Sitemap - Run script

Im attempting to make a manual Button within the sitemap, that when I press it, it will run the backup command line for OpenHAB2 and save the file with a format similar to on a network share.

For reference:

  • I am using Armbain (Ubuntu for ARM processors basically).
  • My $OPENHAB_RUNTIME doesnt work, so I am specifying the exact path to the folders to run the commands (I have seen this problem detailed elsewhere).
  • Everything else within OpenHAB is generally running well

What I have done so far is:

today=`date '+%d-%m-%Y_at_%H%M'`
/usr/share/openhab2/runtime/bin/backup $filename
  • Added an ITEM
String 	BACKUP
  • Added a button in sitemap
Switch	item=BACKUP	mappings=["BACKUP"="Backup"]
  • Finally a Rule, which takes effect when the button is pressed
rule "BACKUP"
		Item BACKUP received command BACKUP
		executeCommandLine=("sudo bash /etc/openhab2/scripts/")

According to the Openhab.log, the script executes.

I have 2 problems from this:

  1. if I run the script manually from the command line, it DOES backup in the correct location, but my filename isnt OHbackup_$ it actually becomes a random name e.g. last time I ran the script, the file was called O8715Q~W however, if I rename it as a ZIP file, it is a working file. What have I done wrong in my shell script?

  2. When the command is executed from OpenHAB basic UI, the logs say it runs, but nothing happens, no files are created. Ive tried variations such as:

executeCommandLine=("sudo bash /etc/openhab2/scripts/")
executeCommandLine=("bash /etc/openhab2/scripts/")

I have done a chmod +x

If anyone has any thoughts/insight as to what Im doing wrong or a more efficient way to do this, it would be great to hear!


@ewrw using sudo as openhab requires some additional work. Better make sure openhab has acces to all required folders by setting the right permission to those files. Additional openhab has no bash as default configuration, see below link for more information.

  1. try to fix the date problem. But your command filename command works as intended on my RPI. So maybe a problem when calling the zip command.
today=$(date '+%d-%m-%Y_at_%H:%M') 

My way to get openhab to execute external commands allways follows the same procedure.

  1. Try to execute your script as user openhab in the command line.
sudo -u openhab /etc/openhab2/scripts/

When this does not work, you can’t proceed. Have a look here on how to solve it if you really need sudo.

After that also read back the return value and print it to the log.

var String cmdReturn = executeCommandLine=("sudo bash /etc/openhab2/scripts/")
logInfo("Backup", "Result:" + cmdReturn  )

Maybe you get some more info for a better understanding here .

1 Like

Good evening!

Thanks for the reply. Ive tried what you suggested and also performed some further reading.

From what I can so far gather, the included OpenHAB backup script calls on other nested scripts to setup/check environment variables. This should be fine in principle and works perfectly from the perspective of a logged in user. What appears to be the problem (my best guess after research) is that the SUDO command which is required to run the backup command, will strip out environment variables. or at least, calling a script from within a script does this. Ive been looking through the following to try and make sense of it. (the second/third post on this page describe it better than I can). As does the second reply in this page

So I believe that OpenHAB is executing the script correctly! All the logfile returns (as you showed me) come back with a NULL result and there are no errors.

Ive edited my OpenHAB environment variables file, so that it points to the correct backup folder I want to use and as such, I can now literally type backup and a file will appear in the correct network folder, with a time stamp name (it appears backup does this as standard if you DONT specify a filename to use).

But the backup file provided with OpenHAB seems to be restricted by the limitations of SUDO, even if you use -E, perhaps because a new blank environment is already created in the first place, when scripts are initiated by OpenHAB. (Im not saying this is absolutely correct, Im purely speculating).

Im going to do some further reading on the SUDO command and also try and understand what the backup script is doing that could cause this to occur, however its not a huge problem, I was mainly hoping to have a quick way to perform regular backups while Im making lots changes in the sitemaps/rules/items etc.

Ill post back if I find a solution!

Again, thanks for your help and insight! :slight_smile:

@ewrw Openhab 2.2 brings its own backups skritps, i just use them for this example.

I just folllowed my own examples, which i pointed you to, and this is how ot works for me.

The folder to the skript has to be made executable by openhab as sudo.
To do this add permission to the folder to the skript.

sudo visudo -f /etc/sudoers.d/010_pi-nopasswd
openhab ALL=(ALL) NOPASSWD: /usr/share/openhab2/runtime/bin/backup

Execute the command in the shell as user openhab. This is a must, do not skip.
I also add a different folder and set the date as name, make sure this folder exists.

sudo -u openhab sudo /usr/share/openhab2/runtime/bin/backup /etc/openhab2/test/$(date '+%d-%m-%Y_at_%H:%M')

When this worked set up openhab.
Be aware that i changed the way to set the date. I use the build in function of exec binding, as the above won’t work.

Thing exec:command:backup [
        // Command to execute
        command="sudo /usr/share/openhab2/runtime/bin/backup /etc/openhab2/test/%1$tY-%1$tm-%1$td-%1$tH-%1$tM",
        // interval time in seconds
        // should it run when input channel is changed


Switch Backup_run "Backup Now" { channel="exec:command:backup:run" }
String Backup_out { channel="exec:command:backup:output" }

This is not really neccessary as openhab log should also post the state change of Backup_out.
But for me the switch does not snap back, as it should.

 rule "Backup"
     Item Backup_run received update
    logInfo("Backup", "\n" + Backup_out.state ) 

And finally a switch on a

        Frame {
            Switch item=Backup_run

When this works then try to save the file to your directory.

sudo -u openhab sudo /usr/share/openhab2/runtime/bin/backup <YOURFOLDER>/$(date '+%d-%m-%Y_at_%H:%M')

And finally set the path to the thing.


Thank-you for the reply. I followed it to the letter and as you say, it works perfectly! Im very happy thank-you once again!

To summarize for anyone in the future who wishes to backup to a network path, I created the symbolic link/mapping on my local Ubuntu to the network share of my choice.

I have then followed the above instructions provided by Josar. The only change i made was to change the switch item to a button in my sitemap:

Switch item=Backup_run	mappings=["ON"="Backup Now"]

This now creates a backup using the inbuilt OpenHAB backup command each time you press the button, which is very useful for versioning/development of your Items/Sitemap/Things etc, or just to perform an occasional backup.

Many thanks.

1 Like

One small update… in the .rules section, there is a small edit to make (Item Backup_run):

rule "Backup"
   Item Backup_run received update

I noticed that other rules didnt run (but strangely this one did, something to do with it being the last rule!?!?). Either way that change seems to have fixed them and the backup still works.

yes some copy paste error, without trigger this rule should not run anytime.
I will correct that.

with the Backup Rule added

rule "Backup"
     Item Backup_run changed from OFF to ON

    logInfo("Backup", "\n" + Backup_out.state )

as it got stuck in a loop of backing up with just

received update

I was trying another method of checking for ON with if but could not figure out the bizarre error

I used your method. The bash script makes the backup but with filename: “OHbackup_17-11-2018_at_1902.zip” like this which is shown as ‘…zip.’ kind of file in Windows Explorer. I don’t know what’s wrong.

Hi Sirijan

Are you saying that you dont know why its a zip file? As this is the correct kind of file for the backup to make. A compressed archive file of all the configuration and can be restored to a fresh openhab server. Please see the details below in Backup and Restore

Or are you saying you want a different naming convention?