DDorch / ScadaSupAgro

Client/Server applications for SCADA system installed at SupAgro Montpellier
GNU General Public License v3.0
0 stars 1 forks source link

Clean up Thingsboard database #24

Closed DDorch closed 5 years ago

DDorch commented 5 years ago

The size of thingsboard database is increasing continuously. Old data should be wipe out from time to time.

DDorch commented 5 years ago

Data are available through a postgreSQL database. Thanks to https://stackoverflow.com/questions/54643417/how-to-remove-old-events-from-thingsboard/57757872#57757872, I was able to create a bash script executed each day (stored in the /etc/cron.daily folder of the thingsboard server):

#!/bin/bash

# Delete events older than 30 days
sudo -u thingsboard psql -d thingsboard -c "DELETE FROM public.event WHERE uuid_timestamp(event_uid::u uid) < now() - '30 days'::interval;" >> /var/log/thingsboard_maintenance.log 2 <&1

# Delete telemetry of Halle Hydraulique older than 90 days
sudo -u thingsboard psql -d thingsboard -c "DELETE FROM public.ts_kv WHERE entity_id = '1e8f3e918d56d509dbfcbc1e37c11e3' AND ts < (extract(epoch from now()) - 86400*90) * 1000;" >> /var/log/thingsboard_maintenance.log 2 <&1