Clear History - traccar docker with Postgre SQL

Tracking Faso8 months ago

hello,
I use Docker for my Traccar server and connect with a Postgre database. I would like to do a planned task to clean up histotics older than 90 days as illustrated in this link https://www.traccar.org/clear-history/
I've been searching for a long time but haven't found any results. I use docker-compose.yml, if anyone has a solution they can help me. THANKS

B.Ihab7 months ago

Just make a bash script that will handle the backup, and create a cronjob to make this script run periodically.

Karim momo7 months ago

it doesn't work for me with doker compose.

Anton Tananaev7 months ago

Do you have two forum accounts?

Karim momo7 months ago

No

Karim momo7 months ago

I topic was covered in another account?

jata4 months ago

Hi folks - I have been looking into a solution for this too. I have traccar and mySQL in docker containers running on a linux server.

I found the easiest way to handle both DB cleanup and log file cleanup was to write a bash script to handle both. I have the following set to run weekly from the docker host using a scheduled cron job.

The tricky part was finding a good way to run the sql queries on docker mysql container. I found a way in the end...

#!/bin/bash

# start script message
logger "traccar cleanup script has started to clean location data and log files"

### Part 1 - Clean up database ###
# set inputs
days="60"
user="user"
pwd="pwd"
container="traccar-db"
db="traccar"
delposition="DELETE FROM tc_positions WHERE fixTime < DATE(DATE_ADD(NOW(), INTERVAL -$days DAY)) AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionid IS NOT NULL)"
delevent="DELETE FROM tc_events WHERE eventTime < DATE(DATE_ADD(NOW(), INTERVAL -$days DAY))"

# use docker exec to connect to docker container and delete data
echo "location data older then $days days will be deleted from $db"

return=0
errdocker=0

docker exec -i $container mysql -u$user -p$pwd <<< "USE $db; $delposition; $delevent;"
return=$?

echo "traccar database cleanup code returned:" $return

if [ $return -ne 0 ]; then
    logger "traccar database cleanup failed. Check container named $container is running."
    errdocker=1
else
    logger "traccar database cleanup completed successfully. Location data older then $days days has been deleted from $db"
fi

### Part 2 - Clean up log files ###
# set inputs
days="5"
logdir="/symlinks/omv-system/appdata/traccar/logs/"

# delete old log files# some info for cmd/shell
echo "cleaning traccar log files more than $days old from $logdir"

# delete old files
return=0
errlog=0
find $logdir -mtime +$days -type f -delete
return=$?

echo "traccar logfile cleanup code returned:" $return

if [ $return -ne 0 ]; then
    logger "traccar logfile cleanup failed. Check script for errors."
    errlog=1
else
    logger "traccar logfile cleanup completed successfully. Log files older than $days days have been deleted from $logdir"
fi

# set the exit code for the script
if [ $errdocker -ne 0 ] || [ $errlog -ne 0 ] ; then
    exit 1
else
    exit 0
fi