Configuring Thingsboard IoT Gateway for persistent storage

By default, Thingsboard IoT Gateway uses memory storage only, which means messages that have not yet been delivered will be lost of Thingsboard IoT Gateway crashes or is restarted.

The configuration instructions show how to change the configuration to use file storage instead, but the instructions are a bit unclear.

First, we need to create a folder to store the data files. Otherwise, /var/log/thingsboard-gateway/storage.log will show

ERROR - [file_event_storage.py] - file_event_storage - 58 - Failed to create data folder! Error: [Errno 13] Permission denied: './data/'"

To create a folder, perform the following:

sudo mkdir -p /var/lib/thingsboard-gateway/data
sudo chown -R thingsboard_gateway /var/lib/thingsboard-gateway

The folder needs to be recreated whenever the server is rebooted. Run sudo crontab -e and add the following:

@reboot  mkdir -p /var/lib/thingsboard-gateway/data; chown -R thingsboard_gateway /var/lib/thingsboard-gateway

Then modify /etc/thingsboard-gateway/config/tb-gateway.yaml:

storage:
#  type: memory
#  read_records_count: 100
#  max_records_count: 100000
  type: file
  data_folder_path: /var/lib/thingsboard-gateway/data/
  max_file_count: 10
  max_read_records_count: 11
  max_records_per_file: 10000

Run sudo systemctl restart thingsboard-gateway. /var/log/thingsboard-gateway/storage.log should now show the following:

INFO - [event_storage_reader.py] - event_storage_reader - 154 - FileStorage_reader -- Initializing from state file: [/var/run/thingsboard-gateway/data/data_1614251044491.txt:0]"

Leave a Reply

Your email address will not be published. Required fields are marked *