Database Maintenance¶
Maintaining the MariaDB database for optimal performance.
Regular Tasks¶
Optimize Tables¶
Monthly optimization:
Manual optimization:
sudo mysql mailborder << EOF
OPTIMIZE TABLE mb_log_audit;
OPTIMIZE TABLE mb_log_email;
OPTIMIZE TABLE mb_quarantine;
OPTIMIZE TABLE mb_users;
EOF
Analyze Tables¶
Update statistics:
sudo mysql mailborder << EOF
ANALYZE TABLE mb_users;
ANALYZE TABLE mb_policy_rules;
ANALYZE TABLE mb_quarantine;
EOF
Check Table Integrity¶
Check all tables:
Repair if needed:
Cleanup Tasks¶
Remove Old Logs¶
Automatic cleanup:
Manual cleanup:
-- Delete logs older than 90 days
DELETE FROM mb_log_audit WHERE created_at < DATE_SUB(NOW(), INTERVAL 90 DAY);
-- Delete email logs older than 30 days
DELETE FROM mb_log_email WHERE received_at < DATE_SUB(NOW(), INTERVAL 30 DAY);
-- Delete old sessions
DELETE FROM mb_auth_sessions WHERE expires_at < NOW();
Archive Old Data¶
Export before deletion:
# Export logs to file
sudo mysqldump mailborder mb_log_audit \
--where="created_at < DATE_SUB(NOW(), INTERVAL 90 DAY)" \
| gzip > /backups/logs-archive-$(date +%Y%m%d).sql.gz
# Then delete
sudo mysql mailborder -e \
"DELETE FROM mb_log_audit WHERE created_at < DATE_SUB(NOW(), INTERVAL 90 DAY)"
Performance Monitoring¶
Check Table Sizes¶
SELECT
table_name,
ROUND(((data_length + index_length) / 1024 / 1024), 2) AS size_mb,
table_rows
FROM information_schema.TABLES
WHERE table_schema = 'mailborder'
ORDER BY (data_length + index_length) DESC;
Check Slow Queries¶
Enable slow query log:
Add:
Analyze:
Monitor Connections¶
Active connections:
Connection statistics:
sudo mysql -e "SHOW STATUS LIKE 'Threads_connected'"
sudo mysql -e "SHOW STATUS LIKE 'Max_used_connections'"
Backup & Recovery¶
Backup Database¶
Full backup:
Table-specific backup:
Restore Database¶
Full restore:
Verify: