#!/bin/bash
# =============================================================================
# CONFIGURATION - ADJUST YOUR SETTINGS HERE
# =============================================================================
# Number of days to keep log files (default: 3 days)
DAYS_TO_KEEP=3
# Email reports on/off (true = on, false = off)
SEND_EMAIL=true
# =============================================================================
# Log cleanup script for Traccar
# Removes log files older than X days
# Automatically reads email settings from traccar.xml
# Sends email report after completion (if enabled)
# =============================================================================
# Traccar configuration file
TRACCAR_CONFIG="/opt/traccar/conf/traccar.xml"
# Log directory
LOG_DIR="/opt/traccar/logs"
# Log function
log_message() {
local message="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message"
# Add to email body
EMAIL_BODY+="$message"$'\n'
}
# Function to read email settings from traccar.xml
get_email_config() {
# First check if email is enabled
if [[ "$SEND_EMAIL" != "true" ]]; then
log_message "Email reports are disabled in configuration"
EMAIL_ENABLED=false
return
fi
if [[ ! -f "$TRACCAR_CONFIG" ]]; then
log_message "WARNING: Traccar configuration file not found: $TRACCAR_CONFIG"
log_message " Email report will not be sent"
EMAIL_ENABLED=false
return
fi
# Read email settings from traccar.xml
SMTP_HOST=$(grep -oP '<entry key='\''mail\.smtp\.host'\''>\K[^<]+' "$TRACCAR_CONFIG")
SMTP_PORT=$(grep -oP '<entry key='\''mail\.smtp\.port'\''>\K[^<]+' "$TRACCAR_CONFIG")
SMTP_USER=$(grep -oP '<entry key='\''mail\.smtp\.username'\''>\K[^<]+' "$TRACCAR_CONFIG")
SMTP_PASS=$(grep -oP '<entry key='\''mail\.smtp\.password'\''>\K[^<]+' "$TRACCAR_CONFIG")
SMTP_FROM=$(grep -oP '<entry key='\''mail\.smtp\.from'\''>\K[^<]+' "$TRACCAR_CONFIG")
# Check if all email settings are found
if [[ -z "$SMTP_HOST" || -z "$SMTP_PORT" || -z "$SMTP_USER" || -z "$SMTP_PASS" || -z "$SMTP_FROM" ]]; then
log_message "WARNING: Not all email settings found in traccar.xml"
log_message " Email report will not be sent"
EMAIL_ENABLED=false
return
fi
EMAIL_ENABLED=true
log_message "Email configuration read:"
log_message " SMTP Host: $SMTP_HOST"
log_message " SMTP Port: $SMTP_PORT"
log_message " From: $SMTP_FROM"
}
# Function to send email
send_email_report() {
if [[ "$EMAIL_ENABLED" != "true" ]]; then
log_message "Email report not sent - email is disabled or configuration incomplete"
return
fi
local subject="Traccar Log Cleanup Report - $(date '+%Y-%m-%d') - $DAYS_TO_KEEP days kept"
local temp_file=$(mktemp)
# Create email body
cat > "$temp_file" << EOF
Traccar Log Cleanup Report
Date: $(date '+%Y-%m-%d %H:%M:%S')
Server: $(hostname)
Configuration: Log files are kept for $DAYS_TO_KEEP days
$EMAIL_BODY
---
This report was automatically generated by the log cleanup script.
Configuration: $DAYS_TO_KEEP days retention period
EOF
log_message "Sending email report..."
# Method 1: Try sendmail
if command -v sendmail >/dev/null 2>&1; then
log_message "Using sendmail for email sending..."
cat > "$temp_file" << EOF
From: $SMTP_FROM
To: $SMTP_FROM
Subject: $subject
Content-Type: text/plain; charset=UTF-8
Traccar Log Cleanup Report
Date: $(date '+%Y-%m-%d %H:%M:%S')
Server: $(hostname)
Configuration: Log files are kept for $DAYS_TO_KEEP days
$EMAIL_BODY
---
This report was automatically generated by the log cleanup script.
Configuration: $DAYS_TO_KEEP days retention period
EOF
if sendmail -t < "$temp_file"; then
log_message "Email report successfully sent via sendmail to $SMTP_FROM"
rm -f "$temp_file"
return
fi
fi
# Method 2: Try mail command
if command -v mail >/dev/null 2>&1; then
log_message "Using mail command for email sending..."
if echo "$EMAIL_BODY" | mail -s "$subject" "$SMTP_FROM"; then
log_message "Email report successfully sent via mail command to $SMTP_FROM"
rm -f "$temp_file"
return
fi
fi
# Method 3: Try curl
if command -v curl >/dev/null 2>&1; then
log_message "Using curl for email sending..."
local email_content="From: $SMTP_FROM\r\nTo: $SMTP_FROM\r\nSubject: $subject\r\n\r\n$EMAIL_BODY"
if curl --mail-from "$SMTP_FROM" \
--mail-rcpt "$SMTP_FROM" \
--upload-file <(echo -e "$email_content") \
--ssl-reqd \
--user "$SMTP_USER:$SMTP_PASS" \
"smtps://$SMTP_HOST:$SMTP_PORT" >/dev/null 2>&1; then
log_message "Email report successfully sent via curl to $SMTP_FROM"
rm -f "$temp_file"
return
fi
fi
log_message "ERROR: Could not send email report"
rm -f "$temp_file"
}
# Function to perform log cleanup
perform_log_cleanup() {
local total_deleted=0
local deleted_files=""
# Check if directory exists
if [[ ! -d "$LOG_DIR" ]]; then
log_message "ERROR: Log directory $LOG_DIR does not exist!"
exit 1
fi
log_message "Starting log cleanup for directory: $LOG_DIR"
log_message "Configuration: Log files are kept for $DAYS_TO_KEEP days"
# Count files for cleanup
local files_to_delete=$(find "$LOG_DIR" -name "traccar.log.*" -mtime +$DAYS_TO_KEEP -type f | wc -l)
local gz_files_to_delete=$(find "$LOG_DIR" -name "*.log.gz" -mtime +$DAYS_TO_KEEP -type f | wc -l)
log_message "Found for cleanup:"
log_message " traccar.log.* files: $files_to_delete"
log_message " .log.gz files: $gz_files_to_delete"
# Remove traccar.log.* files older than X days
if [[ $files_to_delete -gt 0 ]]; then
log_message "Removing traccar.log.* files older than $DAYS_TO_KEEP days..."
find "$LOG_DIR" -name "traccar.log.*" -mtime +$DAYS_TO_KEEP -type f -delete
total_deleted=$((total_deleted + files_to_delete))
log_message " $files_to_delete traccar.log.* files removed"
fi
# Remove .log.gz files older than X days
if [[ $gz_files_to_delete -gt 0 ]]; then
log_message "Removing .log.gz files older than $DAYS_TO_KEEP days..."
find "$LOG_DIR" -name "*.log.gz" -mtime +$DAYS_TO_KEEP -type f -delete
total_deleted=$((total_deleted + gz_files_to_delete))
log_message " $gz_files_to_delete .log.gz files removed"
fi
# Check disk space after cleanup
local disk_usage=$(df -h "$LOG_DIR" | tail -1 | awk '{print $5}')
local available_space=$(df -h "$LOG_DIR" | tail -1 | awk '{print $4}')
log_message "Disk space after cleanup:"
log_message " Used: $disk_usage"
log_message " Available: $available_space"
return $total_deleted
}
# Main script
main() {
# Initialize email body
EMAIL_BODY=""
log_message "=== TRACCAR LOG CLEANUP START ==="
log_message "Starting log cleanup - Keeping log files from last $DAYS_TO_KEEP days"
log_message "Email reports: $([ "$SEND_EMAIL" = "true" ] && echo "ON" || echo "OFF")"
# Read email configuration
get_email_config
# Start time for performance measurement
START_TIME=$(date +%s)
# Perform log cleanup
perform_log_cleanup
total_deleted=$?
# Calculate total time
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# Summary
log_message "=== LOG CLEANUP SUMMARY ==="
log_message "Configuration: Log files are kept for $DAYS_TO_KEEP days"
log_message "Total files removed: $total_deleted"
log_message "Total execution time: ${DURATION} seconds"
log_message "Log cleanup completed for $LOG_DIR"
log_message "=== TRACCAR LOG CLEANUP END ==="
# Send email report (if enabled)
if [[ "$SEND_EMAIL" = "true" ]]; then
send_email_report
else
log_message "Email report skipped - email is disabled"
fi
}
# Start the script
main
This looks to me like a massive over engineering. Does this script really just deletes old files and sends an email about it?
This script automatically cleans up old log files from your Traccar installation to prevent disk space issues. It removes log files older than a specified number of days and can optionally send email reports about the cleanup process.
AI-GENERATED SCRIPT DISCLAIMER:
PERSONAL TESTING:
traccar.log.*
and *.log.gz
files older than specified daystraccar.xml
Your traccar.xml
file must contain the following email configuration entries:
<!-- Email SMTP Configuration -->
<entry key='mail.smtp.host'>your-smtp-server.com</entry>
<entry key='mail.smtp.port'>587</entry>
<entry key='mail.smtp.username'>your-email@domain.com</entry>
<entry key='mail.smtp.password'>your-email-password</entry>
<entry key='mail.smtp.from'>your-email@domain.com</entry>
<!-- Log File Configuration (Required) -->
<entry key='logger.file'>/opt/traccar/logs/traccar.log</entry>
Important: The logger.file
entry is required for the script to work properly, as it defines the log file location that the script will clean up.
Are you serious? You just going to dump auto-generated stuff as response?
OK, my recommendation for everyone is to avoid this script. There's a official one liner script that can clean your logs. It's not clear to me why anyone would need an email for cleared logs. This script is extremely complicated for no reason. And seems like even the author doesn't fully understand it because the description provided above is simply incorrect.
I have a script for deleting old log file entries. I know there is also a simple script on the Traccar site, but this script is a bit more elaborate; it also sends an email and retrieves the details for sending the email from traccar.xml. I have this script running on Ubuntu, and it is located in etc/cron.daily and is named traccar-clean-logs. I also have a script to delete the database after a number of days that you can specify, but this one has not been well tested and is also a bit more sensitive since it is connected to your database. The script is written using Cursor AI, use at your own risk.