thingsboard icon indicating copy to clipboard operation
thingsboard copied to clipboard

[Question] How to stop thingsboard on docker but not turning off the machine

Open Aimen-Hammou opened this issue 2 years ago • 4 comments

Component

  • Installation
  • Generic

Description

I have an instance of docker with tb-postgres on it. I've made a backup of the postgresql database and a copy of conf folder files. I've created another docker machine on a PC, to which i'm connected in RDP, using tb-cassandra image. My plan was to drop the database, create a new one and restore the backup.

I've been trying to follow: https://github.com/thingsboard/thingsboard/issues/6598 but when it comes stop thingsboard service service thingsboard stop docker tells me there is no "thingsboard" service.

I need to drop the DB thingsboard but I can't since TB is running. Any way to achieve this?

I know I can do it by moving volumes from one docker machine to another, but I would rather do it by following the guide I posted above.

This is my 3rd time using docker.

Edit: Stopping the java process doesn't work.

Environment

  • OS: Docker + Windows
  • ThingsBoard: 3.6
  • Browser: Mozilla

Aimen-Hammou avatar Jan 30 '24 11:01 Aimen-Hammou

Hi!

I've been trying to follow: #6598 but when it comes stop thingsboard service service thingsboard stop docker tells me there is no "thingsboard" service.

This will not work, because instructions there are specified for another ThingsBoard deployment. You are using Docker, so you need to do everything in another way.

This image runs ThingsBoard and databases in a single container. This limits the possibility to do any operations with the database, as it would affect ThingsBoard and possibly stop the container due to error.

I can suggest the workaround:

  1. Start tb-cassandra normally. First initial start would create all required schemas for Postgres and Cassandra.
docker compose up -d
  1. Find ID if your Docker container. Execute the following command and look for thingsboard/tb-cassandra entry. E.g.:
docker ps
CONTAINER ID   IMAGE                                  COMMAND                  CREATED         STATUS         PORTS                                                                                                                                                                                            NAMES
636c6f12c5ad   thingsboard/tb-cassandra               "start-tb.sh"            7 minutes ago   Up 7 minutes   0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:7070->7070/tcp, :::7070->7070/tcp, 0.0.0.0:5683-5688->5683-5688/udp, :::5683-5688->5683-5688/udp, 0.0.0.0:8080->9090/tcp, :::8080->9090/tcp   tb-post-test-mytb-1
#      ^
#      |
#      |
#we need this ID
  1. Execute the following to access Postgres console. Use your ID instead of 636c6f12c5ad as per previous step:
docker exec -it 636c6f12c5ad psql
  1. Create another database, based on current ThingsBoard DB:
CREATE DATABASE thingsboard_default_data TEMPLATE thingsboard;

Likely, you will get an error like so:

ERROR:  source database "thingsboard" is being accessed by other users
DETAIL:  There are 16 other sessions using the database.

In this case, run the following command:

SELECT *, pg_terminate_backend(pid) 
FROM pg_stat_activity 
WHERE usename='thingsboard';

And repeat CREATE DATABASE ... command. Then, exit the console with \q 5. Edit your docker-compose.yml file, by adding the following line into the env section:

<...>
    environment:
<...>
      SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/thingsboard_default_data
<...>

For example:

version: '3.0'
services:
  mytb:
    restart: always
    image: "thingsboard/tb-cassandra"
    ports:
      - "8080:9090"
      - "1883:1883"
      - "7070:7070"
      - "5683-5688:5683-5688/udp"
    environment:
      TB_QUEUE_TYPE: in-memory
      SPRING_DATASOURCE_URL: jdbc:postgresql://localhost:5432/thingsboard_default_data
    volumes:
      - ~/.mytb-data:/data
      - ~/.mytb-logs:/var/log/thingsboard

  1. Restart container:
docker compose stop
docker compose up -d
  1. Now, you can do the work with thingsboard database, as it won't be accessed by ThingsBoard. You can DROP it, re-CREATE and then copy your backup. Change dump_name.sql with your backup path/name:
docker ps # container ID was changed
docker exec -it b9fda1e249b2 psql
DROP DATABASE thingsboard;
CREATE DATABASE thingsboard;
\q
docker exec -i b9fda1e249b2 psql -d thingsboard -t < dump_name.sql
  1. Remove the line you have added in step №5. Now, we can stop container and run it from the initial DB which has all our data:
docker compose stop
docker compose up -d

trikimiki avatar Jan 31 '24 11:01 trikimiki

This works even if I migrate from tb-postgres to tb-cassandra?

Did all the steps but I run into errors: log_thingsboard.txt

edit: also when uploading the sql backup, I get a lot of "XXXX already exists"

Aimen-Hammou avatar Jan 31 '24 11:01 Aimen-Hammou

Hi!

This works even if I migrate from tb-postgres to tb-cassandra?

Yes. But note that you will switch telemetry storage to a different DB, thus old telemetry data would not be accessible anymore. If you need your telemetry migrated - you can use this: https://github.com/thingsboard/database-migrator

edit: also when uploading the sql backup, I get a lot of "XXXX already exists"

org.springframework.dao.IncorrectResultSizeDataAccessException: query did not return a unique result: 2

Your DB contains duplicate values. In step 7, you need to DROP database and re-CREATE it to ensure there is no data before uploading the backup.

trikimiki avatar Feb 02 '24 07:02 trikimiki

@trikimiki

Thanks.

I did execute the DROP command and the CREATE one but still gave me the error. Maybe CASCADE has to be specified?

Aimen-Hammou avatar Feb 02 '24 08:02 Aimen-Hammou

Hi @Aimen-Hammou ! Have you already solved your problem?

AndriichnekoDm avatar Feb 28 '24 15:02 AndriichnekoDm

Component

  • Installation
  • Generic

Description

I have an instance of docker with tb-postgres on it. I've made a backup of the postgresql database and a copy of conf folder files. I've created another docker machine on a PC, to which i'm connected in RDP, using tb-cassandra image. My plan was to drop the database, create a new one and restore the backup.

I've been trying to follow: #6598 but when it comes stop thingsboard service service thingsboard stop docker tells me there is no "thingsboard" service.

I need to drop the DB thingsboard but I can't since TB is running. Any way to achieve this?

I know I can do it by moving volumes from one docker machine to another, but I would rather do it by following the guide I posted above.

This is my 3rd time using docker.

Edit: Stopping the java process doesn't work.

Environment

  • OS: Docker + Windows
  • ThingsBoard: 3.6
  • Browser: Mozilla

if you are using the thingsboard/tb- postgres postgres database

  1. make sure your .mytb-data and .mytb-logs and mytb-logs directories are made in root user
  2. identify the user and password of your database.(in default username: thingsboard and password is ''postgres'
  3. just create the backup using following command

" sudo docker exec -t e1d60ff312db pg_dump -U thingsboard -F t -v -f /tmp/thingsboard_backup.tar thingsboard ''

now connect your postgres data base with PGADMIN TOOL.

  1. create new data base with same database name, user and password of your old database and restore the backup .
  2. now disconnect your old database .
  3. than reset the container . it will work

dip-devlopement avatar Aug 14 '24 16:08 dip-devlopement