Periodical backup

I tried looking for documentation about backing up my openHAB installation, but I only came across a tutorial for openHABian, which I don’t have, and a very long (and vehement) discussion.

But what for systems that didn’t install with openHABian? Are there any documents?

Thanks in advance for anyone’s help!

You can backup openhab by

sudo openhab-cli backup
or
sudo /usr/share/openhab/runtime/bin/backup

and add this command to your cron jobs.

Note: Additional tools like mosquitto, etc are not backed up by this script.

I use this:
/usr/bin/openhab-cli backup $FILELOCATION/openhab-$(date +%A).zip

It gives a rolling 1 week of backups.
-rw-r–r-- 1 root root 47864049 Mar 1 23:15 openhab-Friday.zip
-rw-r–r-- 1 root root 48628234 Mar 4 23:15 openhab-Monday.zip
-rw-r–r-- 1 root root 48089129 Mar 2 23:15 openhab-Saturday.zip
-rw-r–r-- 1 root root 613640541 Mar 3 23:16 openhab-Sunday.zip
-rw-r–r-- 1 root root 47810615 Feb 29 23:15 openhab-Thursday.zip
-rw-r–r-- 1 root root 48585794 Mar 5 23:15 openhab-Tuesday.zip
-rw-r–r-- 1 root root 48587173 Mar 6 05:15 openhab-Wednesday.zip

I backup those zip files to another location not on the openhab machine.

2 Likes

Okay, I tried that, and it seems to work!

I now also tried to make a cron job. Let’s see tomorrow whether it did what it was supposed to do :wink:

Did it work?

It didn’t the night from the day before yesterday to yesterday. I suppose that’s because I had put the job in the crontab of my user, instead of that of root.

I’ll check later today whether the backup succeeded last night (after moving the cronjob to my root’s crontab, obviously :slight_smile:).

It should work as the root user.

How did you get this overview? My Linux knowledge is still minimal, and I always like improving knowldegde. :slight_smile:

That is just a listing of the directory where the files are.
You type ls -l (the l means long listing)

I have it set as alias in the .bashrc file in your home directory so I just type ll

It looks like this:
alias ll=‘ls -la’

Edit the file and save it and log out and then log in again and then ll will work. This saves a lot of typing.

The best terminal keyboard shortcut to use is ctrl-r This is the history of your commands.
Just press ctrl-r keys and then type in some command you previously used and it will show up in the command line. You can press enter to execute it again or use the arrow keys to change it.
My ctrl-r keys are worn out as I use it so much.

It didn’t work. The parentheses caused problems:

grep CRON /var/log/syslog resulted in:

Mar  7 17:37:01 MinipcLG2 CRON[688542]: (root) CMD (sudo /usr/bin/openhab-cli backup /var/lib/openhab/backups/openhab-$(date +)

So (after some googleing) I put your command in a shell script, and let cron run that shell script. Just tested it now, and that works!

However, I wanted to go a step further, and have the script put the backup .zip files in a directory I’ve mounted onto my NAS. But that fails for some reason…

This works:

sudo /usr/bin/openhab-cli backup /home/erik/Openhab-backups/openhab-$(date +%A).zip

But if I then later mount this directory (or how is it phrased) using this:

sudo mount -t nfs 192.168.1.10:/openHAB-backup /home/erik/Openhab-backups/

… and retry this:

sudo /usr/bin/openhab-cli backup /home/erik/Openhab-backups/openhab-$(date +%A).zip

… this is the result:

Using '/etc/openhab' as conf folder...
Using '/var/lib/openhab' as userdata folder...
Using '/usr/share/openhab/runtime' as runtime folder...
Unable to locate specified directory '/home/erik/Openhab-backups'

So I assume there’s some permission problem or something when trying to backup to a mounted directory?

Are you able to copy manually a file to /home/erik/Openhab-backups/ ?
Did you add openhab server to the NFS permission settings of the mounted server?

Yes

Yes

Just tested both again (I copied a file manually, both via the GUI as via command line. I then checked whether these files are visible through the SMB connection to my Windows laptop, which was the case).

could you try this:

sudo OPENHAB_BACKUPS=/home/erik/Openhab-backups $OPENHAB_RUNTIME/bin/backup
Using '/etc/openhab' as conf folder...
Using '/var/lib/openhab' as userdata folder...
Using '/usr/share/openhab/runtime' as runtime folder...
Using '/home/erik/Openhab-backups' as backup folder...
Writing to '/home/erik/Openhab-backups/openhab-backup-24_03_08-21_23_52.zip'...
Making Temporary Directory if it is not already there
Using /tmp/openhab/backup as TempDir

#########################################
          openHAB backup script
#########################################

Copying configuration to temporary folder...
find: Failed to restore initial working directory: /home/erik/Openhab-backups: Toegang geweigerd

Toegang geweigerd = access denied (although English Linux may use different words)

Like you already assumed it is probably a permission problem. Could you try to mount the directory to /backup instead of your home folder?

I remeber similar issues and ended up in permanently mounting the folder in /etc/fstab

What is the output of ls -l /home/erik/

totaal 36
drwxr-xr-x 2 erik erik  4096 nov 17 10:17  Afbeeldingen
drwxr-xr-x 2 erik erik  4096 nov 17 10:17  Bureaublad
drwxr-xr-x 2 erik erik  4096 mrt  5 22:50  Documenten
drwxr-xr-x 2 erik erik  4096 mrt  7 18:00  Downloads
drwxr-xr-x 2 erik erik  4096 nov 17 10:17  Muziek
drwxr-xr-x 2 erik erik  4096 nov 17 10:17  Openbaar
drwxrws--- 2 root users 4096 mrt  8 20:26  Openhab-backups
drwxr-xr-x 2 erik erik  4096 nov 17 10:17  Sjablonen
drwxr-xr-x 2 erik erik  4096 feb 16 23:20 "Video's"
erik@MinipcLG2:~$ cd /
erik@MinipcLG2:/$ ls
bin  boot  cdrom  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media  mnt  opt  proc  root  run  sbin  srv  swapfile  sys  timeshift  tmp  usr  var
erik@MinipcLG2:/$ sudo mkdir backup
erik@MinipcLG2:/$ ls
backup  bin  boot  cdrom  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media  mnt  opt  proc  root  run  sbin  srv  swapfile  sys  timeshift  tmp  usr  var
erik@MinipcLG2:/$ sudo mount -t nfs 192.168.1.10:/NASErik /backup/
erik@MinipcLG2:/$ sudo /usr/bin/openhab-cli backup /backup/openhab-$(date +%A).zip

Using '/etc/openhab' as conf folder...
Using '/var/lib/openhab' as userdata folder...
Using '/usr/share/openhab/runtime' as runtime folder...
Writing to '/backup/openhab-vrijdag.zip'...
Making Temporary Directory if it is not already there
Using /tmp/openhab/backup as TempDir

#########################################
          openHAB backup script
#########################################

Copying configuration to temporary folder...
Removing unnecessary files...
Zipping folder...
zip I/O error: Read-only file system
zip error: Could not create output file (/backup/openhab-vrijdag.zip)
zip failed to store a backup.

Folder is owned by root. Please try

chmod 775 /backup
or
chmod 775 /home/erik/Openhab-backups

I first unmounted /backup and mounted /home/erik/Openhab-backups again, and then executed chmod 775 /home/erik/Openhab-backups. But…:

erik@MinipcLG2:~$ sudo chmod 775 /home/erik/Openhab-backups
chmod: veranderen van toegangsrechten van '/home/erik/Openhab-backups': Actie is niet toegestaan

‘Action is not permitted’…

(But regardless, if the folder is owned by root, and root has rwx permissions, should not any sudo command work?
Also, should w permission not be enough, since the command itself is not executed from the mounted folder? The command only writes to the mounted folder, no?)

I mounted /backup again and tried chmod 775 /backup. Same result…

Even stranger:

erik@MinipcLG2:~$ sudo mount -t nfs 192.168.1.10:/openHAB-backup /backup/
erik@MinipcLG2:~$ sudo chmod 775 /backup
chmod: veranderen van toegangsrechten van '/backup': Actie is niet toegestaan
erik@MinipcLG2:/$ sudo /usr/bin/openhab-cli backup /backup/openhab-$(date +%A).zip

Using '/etc/openhab' as conf folder...
Using '/var/lib/openhab' as userdata folder...
Using '/usr/share/openhab/runtime' as runtime folder...
Unable to locate specified directory '/backup'

Whereas that did do something yesterday. What’s different?
(Edit: I see what’s different. Yesterday I mounted the wrong NAS folder (NASErik instead of
openHAB-backup). But why that would result in a different outcome is beyond me: their settings on my NAS server were identical!)