How to backup from mounted file system

  • Platform information:
    • Hardware: Raspberry pi 4, 4GB RAM
    • OS: openhabian
    • openHAB version: 4.0.0 M2
  • Issue of the topic:

Hi. I’m in a bit of a pickle since last time I tried to upgrade openHab. The OS doesn’t boot anymore, not even to the point where it has a network connection. (The recovery method of placing the backup SD card in did also not work, unfortunately)

My plan is to simply start from scratch with a fresh openhabian install.

Since I cannot log in and create a backup, is there any way I can create the backup from a mounted file system?

And can the same be done with influxDB? (Of course not a question for OH developers, but maybe someone here has tried it)

Thanks in advance!

As long as the SD card is still readable it’s not too hard to get the data.

Either use an USB SD-Card Reader with the Raspberry Pi or use a virtual machine. Both ways are more or less the same:

  • boot to GNU/Linux shell
  • create a directory or use an existing empty one (e.g. /mnt/ should be empty)
  • mount the file system (sudo mount /dev/<name-of-sd-card's-2nd-partition> /mnt)
  • navigate to the data in question
  • copy data to wherever you want it.

To use openhab-cli backup, you will have to use chroot to (temporally) make the old file system the root file system. Be aware that this can be a bit tricky, as a one-time-job it’s easier to manually copy the files.

EDIT: everything between < and > won’t be displayed if not marked as code.
Reminder to myself: Always take a look back (i.e. is the Posting displayed the way it should be…)

Great to hear that it’s simple! My only problem is that I don’t know exactly which files that need to be copied.

In the documentation (openHAB on Linux | openHAB) it says a script is used. I guess if I get a hold of that script I could either run it from my laptop och at least look at it to find the list of files.

Where exactly would that script be located on an openhabian install?

If you wanted to see exactly what the backup and restore scripts do, these can be found in $OPENHAB_RUNTIME/bin i.e:

/usr/share/openhab/runtime/bin/backup
/usr/share/openhab/runtime/bin/restore

The backup script essentially:

  • Copies the $OPENHAB_USERDATA (/var/lib/openhab) folder.
  • Copies the $OPENHAB_CONF (/etc/openhab) folder.

The restore script essentially replaces the above folders with whatever was backed up.

There are a few folders and files inside $OPENHAB_USERDATA that shouldn’t be included in a backup:

  • ./tmp and ./cache.
  • Some files inside ./etc which are referenced inside $OPENHAB_RUNTIME/bin/userdata_sysfiles.lst
1 Like

I finally got some time to fix this, and I’d like to share the details on how I solved it. Turns out that I could run the backup script from my laptop, just making sure to set environment variables correctly! Also, moving the influxdb persistence data was as simple as replacing the database folder.

My laptop is running Manjaro linux, but almost any linux distribution should work.

  1. Create image from SD-card using dd
  2. Mount rootfs partition of image using:
sudo losetup --partscan --find --show openhabian.img
mount /dev/loop0p2 /mnt/img
  1. Create a shell script with the following content
export OPENHAB_CONF="/mnt/img/etc/openhab"
export OPENHAB_USERDATA="/mnt/img/var/lib/openhab"
export OPENHAB_BACKUPS="<arbitrary path where backup zip file should end up>"
export OPENHAB_RUNTIME="/mnt/img/usr/share/openhab/runtime"

/mnt/img/usr/share/openhab/runtime/bin/backup
  1. Run shell script with sudo
  2. Put a fresh install of openhabian on the SD-card
  3. Put the SD-card in the rpi, start it up and let it install.
  4. Use scp to copy the zip file to the rpi
  5. Restore with the openhab-cli command
  6. Install influxdb and grafana using the openhabian-config tool
  7. Shutdown the rpi and mount the SD-card with card reader to your computer
  8. Rename the folder /var/lib/influxdb on the SD-card to influxdb_bkp as a safety measure
  9. Navigate to the SD-card /var/lib/ folder and copy over the mounted image folder in its place (-p is needed to keep permissions)
sudo cp -pr /mnt/img/var/lib/influxdb .
  1. Done. When you start the rpi next time, all your settings, addons and influxdb persistence data should be in place.
1 Like

Nice howto!