Creating backup of Traccar Server MariaDB error

Juliet Tee 11 days ago

My daily backup output and error.

mysqldump: Error 1034: Index for table 'tc_positions' is corrupt; try to repair it when dumping table `tc_positions` at row:

Could not fix it and could not restore from the failed backup as the database is empty.

Any idea how to go about it?

Installed from scratch and lose all the complex configurations.

Problem was due to unreliable micro sd card used in Raspberry Pi for the last past two and half years since.

My mistake of using unreliable storage media for live server operation.

Anton Tananaev 11 days ago

Sounds like you need to restore the last good backup.

Juliet Tee 11 days ago

On a Raspberry Pi, Error 1877 typically signifies that the SD card is physically failing or has corrupted the filesystem. This happens because SD cards have limited "write" cycles, and frequent database updates can wear them out in as little as two years.

Step 1: Locate your Configuration File : /etc/mysql/mariadb.conf.d/50-server.cnf
Step 2: Enable Force Recovery : [mysqld] , innodb_force_recovery = 1 , systemctl restart mysql
Step 3: Extract and Rebuild : mysqldump -u [user] -p [database] tc_positions > tc_positions_recovered.sql, DROP TABLE tc_positions;
SOURCE tc_positions_recovered.sql;

This last method works to restore the database back to working order.

I consulted Google AI assistance for help.

Juliet Tee 11 days ago

Anton, Thanks to your MySQL database clearing script I manage to use it to drop all data records leaving only one day data at 1,673KB backup dump file size with all my Traccar server configuration luckly still intact not corrupted.

#!/bin/bash
result=""
while [[ "$result" != *" 0 rows affected"* ]]; do
  result=$(mysql -u root -pXXXXXXXXXXXX traccar -vve "DELETE FROM tc_positions WHERE fixTime < DATE(DATE_ADD(NOW(), INTERVAL -1 DAY)) AND id NOT IN (SELECT positionId FROM tc_devices WHERE positionid IS NOT NULL) LIMIT 10000")
  sleep 0
done
result=""
while [[ "$result" != *" 0 rows affected"* ]]; do
  result=$(mysql -u root -pXXXXXXXXXXXX traccar -vve "DELETE FROM tc_events WHERE eventTime < DATE(DATE_ADD(NOW(), INTERVAL -1 DAY)) LIMIT 10000")
  sleep 0
done
Juliet Tee 11 days ago

Thanks Anton for the help. Luckly my database fully recovered and restored. Except for the old records which is empty I decided to delete it.