Zabbix Monitoring Data Cleanup Bash Script
After prolonged Zabbix monitoring generates massive history tables, this guide provides a Bash script that stops Zabbix services, disables foreign key checks, truncates and optimizes various history tables in MySQL, backs up the database, and restarts the services.
When Zabbix monitoring runs for an extended period, its history tables can consume dozens of gigabytes; the following Bash script safely clears all monitoring data while preserving a backup.
#!/bin/bash
User="zabbix"
Passwd="zabbix69"
systemctl stop zabbix-server
systemctl stop zabbix-agent
mysql -u${User} -p${Passwd} -e "
SET foreign_key_checks=0;
use zabbix;
truncate table history;
optimize table history;
truncate table history_uint;
optimize table history_uint;
truncate table history_str;
optimize table history_str;
truncate table history_text;
optimize table history_text;
truncate table history_log;
optimize table history_log;
truncate table trends;
optimize table trends;
truncate table trends_uint;
optimize table trends_uint;
truncate table events;
optimize table events;
truncate table event_recovery;
optimize table event_recovery;
SET foreign_key_checks=1;
"
mysqldump -u${User} -p${Passwd} --quick --single-transaction zabbix|gzip > /data/mysqlbackup/`date +%F_%H%M%S`_zabbix.sql.gz
systemctl start zabbix-server
systemctl start zabbix-agentThe script stops Zabbix services, clears and optimizes all history tables, creates a compressed backup of the Zabbix database, and then restarts the services; use it with caution and ensure a backup before execution.
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.