[Question] How to stop thingsboard on docker but not turning off the machine
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
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:
- Start
tb-cassandranormally. First initial start would create all required schemas for Postgres and Cassandra.
docker compose up -d
- Find ID if your Docker container. Execute the following command and look for
thingsboard/tb-cassandraentry. 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
- Execute the following to access Postgres console. Use your ID instead of
636c6f12c5adas per previous step:
docker exec -it 636c6f12c5ad psql
- 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
- Restart container:
docker compose stop
docker compose up -d
- Now, you can do the work with
thingsboarddatabase, as it won't be accessed by ThingsBoard. You canDROPit, re-CREATEand then copy your backup. Changedump_name.sqlwith 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
- 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
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"
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
Thanks.
I did execute the DROP command and the CREATE one but still gave me the error. Maybe CASCADE has to be specified?
Hi @Aimen-Hammou ! Have you already solved your problem?
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 stopdocker 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
- make sure your .mytb-data and .mytb-logs and mytb-logs directories are made in root user
- identify the user and password of your database.(in default username: thingsboard and password is ''postgres'
- 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.
- create new data base with same database name, user and password of your old database and restore the backup .
- now disconnect your old database .
- than reset the container . it will work