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 OHbackup_30-01-2018_1154.zip 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'`
filename="/media/openhabbackup/OHbackup_$today.zip"
/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"
	when 
		Item BACKUP received command BACKUP
	then
		executeCommandLine=("sudo bash /etc/openhab2/scripts/backup.sh")
	end

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_$today.zip 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/backup.sh")
executeCommandLine=("sudo@@bash@@/etc/openhab2/scripts/backup.sh")
executeCommandLine=("bash /etc/openhab2/scripts/backup.sh")
executeCommandLine=("bash@@/etc/openhab2/scripts/backup.sh")
executeCommandLine=("/etc/openhab2/scripts/backup.sh")

I have done a chmod +x backup.sh

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!

Thanks

@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') 
filename="/media/openhabbackup/OHbackup_${today}.zip" 

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/backup.sh

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/backup.sh")
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. https://askubuntu.com/questions/20953/sudo-source-command-not-found (the second/third post on this page describe it better than I can). As does the second reply in this page https://stackoverflow.com/questions/8352851/how-to-call-shell-script-from-another-shell-script

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.
.things

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
        interval=0,
        // should it run when input channel is changed
        autorun=false]

.items

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.
.rules

 rule "Backup"
  when
     Item Backup_run received update
 then
    Backup_run.sendCommand(OFF)
    logInfo("Backup", "\n" + Backup_out.state ) 
 end

And finally a switch on a
.sitemap

        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.

2 Likes

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.

https://wiki.ubuntu.com/MountWindowsSharesPermanently#Mount_password_protected_network_folders

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"
when
   Item Backup_run received update
then

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"
 when
     Item Backup_run changed from OFF to ON
 then
Backup_run.sendCommand(OFF)

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

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?