Run rsync with a rule

i have a rule that takes backup with openhab-cli tool and trying to rsync the backups folder to my nas.When i run

rsync -avzx /var/lib/openhab/backups /usr/share/openhab/addons admin@192.168.1.11:/volume1/NetBackup/openhabbk

from console ,it work just fine,so i am trying to do it with a rule

rule "auto_backup"
when
    Time cron "0 0 0 1 * ? *"
	or
	Item Backup changed from OFF to ON
then
    executeCommandLine("sudo","openhab-cli","backup")
	Thread::sleep(10000)
	executeCommandLine("rsync","-avzx","/var/lib/openhab/backups","admin@192.168.1.11:/volume1/NetBackup/openhabbk")
	
end

It creates the new backup but doesnt rsync the folders to my nas…
Any ideas?

Why not use the system cron service?

Review the docs for executeCommandLine again. Add a duration and catch and log the output of the command by adding a Duration as the first argument. (Note, then you can get rid of that sleep since the backup won’t return until the command is done).

Finally, openHAB is running as a different user from your command line. Does the openhab user have all the files and permissions needed to execute that rsync command? I bet the output from the command itself will tell you that.

cause sometimes i want to trigger it manually before i make a big change for example…

will do that…

openhabian@openhabian:~ $ sudo -u openhab sudo -l
[sudo] password for openhabian:
Matching Defaults entries for openhab on openhabian:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin, env_keep+=NO_AT_BRIDGE, env_keep+="http_proxy HTTP_PROXY",
    env_keep+="https_proxy HTTPS_PROXY", env_keep+="ftp_proxy FTP_PROXY", env_keep+=RSYNC_PROXY, env_keep+="no_proxy NO_PROXY"

User openhab may run the following commands on openhabian:
    (ALL) NOPASSWD: ALL

i run many scripts with executeCommandLine for example

executeCommandLine("/etc/openhab/scripts/life360_presence.sh")

and works just fine.Do i need to set another permission for openhab user to run rsync?Forgive me i am a linux noob!
edit: i did catch and log the output of the command

2023-08-15 00:44:48.836 [INFO ] [hab.core.model.script.ScriptResponse] - Host key verification failed.
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(228) [sender=3.2.3]

See How to fix rsync host key verification failed . There are two possible root causes for the error message:

  • public host key cannot be found
  • problem to access /dev/tty

Try if

rsync -e "ssh -o StrictHostKeyChecking=no" -avzx /var/lib/openhab/backups admin@192.168.1.11:/volume1/NetBackup/openhabbk

works.

+1

This should just be done using Linux Cron, outside of openhab

I recommend mapping a folder of your nas to your openhab device (e.g. /backup). With this command

sudo OPENHAB_BACKUPS=/backup $OPENHAB_RUNTIME/bin/backup
//thanks to Wolfgang_S :-)

the backup is directly created on your nas.
As mentioned above, add this to your cron so that your backup is created outside of OH and if you want to start a backup manually, add the backup command into a sh script and start the script from a rule. I gave up on trying to start complex commands with executeCommandLine

1 Like

But you are not running the rsync with sudo and that’s the command that is failing.

i solved it guys,many thnx for the replies,The problem was that at first i generate the ssh key from user openhabian ,that s why the line

rsync -avzx /var/lib/openhab/backups /usr/share/openhab/addons admin@192.168.1.11:/volume1/NetBackup/openhabbk

worked fine from console.So i create and send another ssh key using sudo -u openhab because openhab cannot “exist” as a user,and the rule works fine now.It doent need sudo to work that way.
Thnx all !