How to migrate from installed to dockerized Openhab? Not possible?

Hi there,

I’m trying to move my openhab installation from an installed version (raspbian) to a dockerized version.
I thought this is possible, however, backup/restore does not work as it restores a lot of files with paths /var/log/openhab2 instead of /openhab/userdata (docker)

Can anyone tell me how to do this? I assume just replacing the paths is not enough…

Hi Christoph,

this is definitely possible. I moved some weeks ago from my Pi3 (OH2.3 openHABian installation) to a container on my NAS.

But for me it also not worked to do this with the backup&restore scripts and some manual effort was needed. I’m not sure I remember all steps but I roughly performed the following steps:

  • Make backup on Pi3 with the backup script
  • Moved the created zip to my desktop and unzipped
  • Setup a new container on Synology
  • Started this container once without any restored files
  • Moved the files step by step from the backup to the container volumes
    • for userdata/config I only kept habpanel.config
    • for userdata/etc I compared all files from the container with my backup (via VS Code compare function) and decided for each and every file which version to use (for most of them I think I kept the version from the container)
    • I ignored all other folders in userdata
    • Important: If you use UI based config you also must keep the userdata/jsondb files with things, items, etc. (I only use text-based config and thus ignored these)
    • Moved conf and checked for path’s or IP’s to change (e.g. when using EXEC Binding)

Hint: As a first shot I tried to start with moving all conf and userdata files from the backup to the container but this led to a not working container. Not sure what exactly was the issue but I assume some files in userdata prevented the container from starting, that’s why I did it step by step

Hope this helps.

Greetings
Sebastian

2 Likes

Permissions probably.

The conf data is pretty happy to be there before hand, but I agree, it needs to start once. The. Copy over the persistence and jsondb folders.

Theoretically, there’s not a lot that is changed in the other folders any more

Thanks for this - I found it really helpful as I switched to docker today.

… I thought I’d just share my notes on what worked for me after your “Started this container once without any restored files” step:

# Copy across our configuration...
cp -r /etc/openhab2/* /var/local/containers-data/openhab/conf

# Copy over the addons folder...
cp -r /usr/share/openhab2/addons/* /var/local/containers-data/openhab/addons

# Copy over the userdata bits... (this is where it gets tricky...)
rm -rf /var/local/containers-data/openhab/userdata/cache
rm -rf /var/local/containers-data/openhab/userdata/tmp

cp -rf /var/lib/openhab2/jsondb/*        /var/local/containers-data/openhab/userdata/jsondb
cp -rf /var/lib/openhab2/openhabcloud/*  /var/local/containers-data/openhab/userdata/openhabcloud
cp -rf /var/lib/openhab2/persistence     /var/local/containers-data/openhab/userdata/persistence

cp -rf /var/lib/openhab2/config/org_apache_felix_cm_impl_DynamicBindings.config /var/local/containers-data/openhab/userdata/config
cp -rf /var/lib/openhab2/config/org/openhab /var/local/containers-data/openhab/userdata/config/org/openhab

# Reset permissions... just to be safe...
sudo chown -R openhab:openhab /var/local/containers-data/openhab

… then docker start openhab. … you will need to log in and re-enter your Amazon account details if you’re using the Amazon Echo control binding.

… in case others are interested too :slight_smile:

4 Likes

How do you backup your data after migration? Is there a tested routine?

This worked for me! Great instructions, everything seems to be functional.

Just ran into the same problem today. This is what worked for me:

bind-mount a folder into container at /restore.
Then place a backup file that you want to restore into the folder and name it restore.zip

Then replace the command (not the entrypoint) in the container with:

"bash -c 'if [ -e /restore/restore.zip ]; then echo y |/openhab/runtime/bin/restore /restore/restore.zip ; fi; exec tini -s ./start.sh server'"

start container
once it is up remove file restore.zip

Hi Christian,

I’m very interested in your solution because it looks much easier than comparing, replacing, copying files. But I’m not that familar with docker to directly understand what you meen with your explanations.
May you explain in more detail what is necessary to do.
What I need to know exactly is to what destination I have to mount the /restore folder from the host (edit: I found in your command. Destination must be /openhab/runtime/bin/restore/restore/ ) and how and where I can replace the command in the container.

Currently I have running a OH3 directly on RPi and will move/migrate it into a Docker container. What I did until now is to prepare a backup by the openhapb-cli backup command. Also I set up a containter by a docker-compose.yml. Here is a snipet of the content:

services:
  openhab:
    image: "openhab/openhab:latest"
    restart: always
    volumes:
      - "/home/pi/openhab/restore:/openhab/runtime/bin/restore/restore"
      - "/etc/localtime:/etc/localtime:ro"
      - "/etc/timezone:/etc/timezone:ro"
      - "/home/pi/openhab/openhab_addons:/openhab/addons"
      - "/home/pi/openhab/openhab_conf:/openhab/conf"
      - "/home/pi/openhab/openhab_userdata:/openhab/userdata"

Thanks and best regards

Dominik

Thanks @hannibal29 - worked like a charm! I moved my OH3 install from Docker on my main computer to Docker on a Raspberry Pi without issue. The Pi took a long while to load on first boot (expected), but all transferred across fine, including API keys for Nest, openHAB.org, etc.

1 Like

Hello,

I’m trying to do this but I can’t find where the data is that it uses in my openhab container to replace the files … The path / var / local / containers-data / openhab / does not exist for me.

Thanks.

Hi,

Those paths are created during first run of your container with openhab. However, if you are migrating from system native openhab (installed on a native os) you should create those paths by yourself seeded with files from your current openhab installation. Then you run the container with binded volumes pointing to this paths.

Regards,
Piotr

1 Like

Hello, thank you very much for your reply.

If I have not misunderstood:

  1. Create the named folders (since they have not been created for me because I come from native openhab and I go to docker)
  2. Give openhab necessary permissions on those folders
  3. Copy (whatever the necessary files in the created folders)
  4. In my case, edit the kubernetes deployment where the old volumes listed appear
  5. Launch the container / kubernetes

I do not know if I have been entirely accurate.

Thank you again!

a little late, but maybe somebody else is looking for a copy/paste solution here for docker-compose:

here is my docker-compose-yml file

version: '2.2'

services:
  openhab:
    image: "openhab/openhab:3.2.0"
    restart: always
    network_mode: host
    command: "bash -c 'if [ -e /openhab/restore/restore.zip ]; then echo y |/openhab/runtime/bin/restore /openhab/restore/restore.zip ; fi; exec tini -s ./start.sh server'"
    volumes:
      - "/etc/localtime:/etc/localtime:ro"
      - "/etc/timezone:/etc/timezone:ro"
      - "openhab_addons:/openhab/addons"
      - "openhab_conf:/openhab/conf"
      - "openhab_userdata:/openhab/userdata"
      - "./restore:/openhab/restore"
      - "./backup:/openhab/backup"
    environment:
      OPENHAB_HTTP_PORT: "8080"
      OPENHAB_HTTPS_PORT: "8443"
      EXTRA_JAVA_OPTS: "-Duser.timezone=Europe/Berlin"
volumes:
  openhab_addons:
    driver: local
  openhab_conf:
    driver: local
  openhab_userdata:
    driver: local

note the bindings:

- "./restore:/openhab/restore"
- "./backup:/openhab/backup"

and the command

command: "bash -c 'if [ -e /openhab/restore/restore.zip ]; then echo y |/openhab/runtime/bin/restore /openhab/restore/restore.zip ; fi; exec tini -s ./start.sh server'"

you can get the default start command from e.g. the openhab docker file

you should have a folder on the host system called restore from where you start the docker container.

For me it’s this folder structure:

pi@raspberrypi:/opt/openhab $ ls
addons backup  conf  docker-compose.yml  restore  userdata

every time you start the docker container it will restore the files from restore/restore.zip. unless you remove the restore/restore.zip file from the host system.

you can run the command to store the backup in the additional mount available ./backup/backup-latest.zip:

$ sudo docker-compose exec openhab /openhab/runtime/bin/backup /openhab/backup/backup-latest.zip

5 Likes