Trying to create a rule that auto backs up the system

Hi All,

Im trying to use this rule to backup the system every week to USB media.

rule "Backup ready for USB, in the proceeding rule"
when
        Time cron "0 0 1 ? * * *"
then
        logInfo("Server Backup", "Server is being backed up")
        executeCommandLine("sudo openhab-cli backup /home/kris/Backup.zip")
end

But, it keeps asking for a password when I execute the rule.

using sudo visudo I have:

openhab ALL=(ALL) NOPASSWD: ALL

But it still wants a password :confused:

Can anyone suggest what I’m doing wrong ? Thanks

Have you considered running this from a Crontab instead?

1 Like

Hi Stuart, I havent. I’m not familiar with that method, can you elaborate? I was under the impression this was correct.

I’m certainly no expert, so take this with a pinch of salt.

Given the sudo rights issue with openHAB2 (for obvious reasons)

I put the restart / reboot / backup schedules in the Linux Crontab script, rather than anything to do with OH2 rules.

If you enter this line in the Linux command line it will open the Crontab configuration file.

sudo crontab -e

(The first time, it will ask you to select an editor, I prefer nano, but use whatever you’re comfortable with)

This thread might help you, particularly the posts towards the start -

This entry in your crontab might well do the trick

0 0 1 * * openhab-cli backup /home/kris/Backup.zip >> /var/log/openhab2/backup.log

I’ll second the suggestion to use crontab. This is the sort of thing cron was created to do. And like Stuart says, you don’t need to give openhab permissions to do stuff it doesn’t need to do.

If you set up mail on your machine (I use msmpt) cron will send an email with the output from the command after it runs so you can keep track that it did in fact run.

1 Like

Thanks gents.

So i added this, but 5mins later, no backup log or sign of the file

35 6 * * TUE sudo openhab-cli backup /home/kris/Backup.zip >> /var/logs/openhab2/backup.log

35 6 * * TUE sudo cp -f /home/krisBackup.zip /media/usb-drive  >> /var/logs/openhab2/backup.log

try theese lines:

35 6 * * tue root /usr/bin/openhab-cli backup /home/kris/Backup.zip >> /var/log/openhab2/backup.log 2>&1

40 6 * * tue root /bin/cp -f /home/kris/Backup.zip /media/usb-drive/  >> /var/log/openhab2/backup.log 2>&1

This should execute every tuesday at 6:35 as user “root” and will redirect every output (standard and error) to the log files.

edit: I would not put the two lines on the same minute…you are not sure if the backup has finished…give it some time. you also were missing a “/” in the second line to copy away the file

edit2: The log paths were wrong must be /var/log not /var/logs

1 Like

Thanks Alessio. ive updated it for a few mins time, ill report back shortly.

I was editing while you were writing…put the two lines on different minutes, to give the openhab-cli the time to create the file

1 Like

I corrected also the path of the logs… see my post above

Yes, found that issue :slight_smile:

I see the file created now… but

kris@ihp:/var/log/openhab2$ cat backup.log
/bin/sh: 1: root: not found
/bin/sh: 1: root: not found

Removing root from the string, seems to fix the first command. Just waiting now for the second.

EDIT: nup, second command doesnt execute.

Mmmmm ok…that’s because you used crontab -e to edit di cron without being root…so it’s your user’s cron, you cannot use the root user.

You can go two ways:

  1. use sudo crontab -e to edit the root cron (this way you need to drop the root user from the line)

  2. Manually edit the system crontab with sudo nano /etc/crontab (use your own text editor…I usually prefer vi ) this way you can leave those lines as they are.

1 Like

Working now :slight_smile: thanks Alessio

1 Like

Good to know!
Glad it helped! :+1:

2 Likes

Good catch, I didn’t think of that to start with.

Solution below using

sudo crontab -e
30 1 * * sun /usr/bin/openhab-cli backup /home/kris/Backup.zip >> /var/log/openhab2/backup.log 2>&1
30 2 * * sun /bin/cp -f /home/kris/Backup.zip /media/usb-drive/  >> /var/log/openhab2/backup.log 2>&1

Thanks gents for your help!

2 Likes