#!/bin/bash
User="zabbix"
Passwd="zabbix"
date >>/data/zabbix_server/script/clearzabbix_log.txt
Date=`date -d $(date -d "-7 day" +%Y%m%d) +%s` #clear before 7 days
$(which mysql) -u${User} -p${Passwd} -e "
use zabbix;
DELETE FROM history WHERE clock < $Date;
optimize table history;
DELETE FROM history_str WHERE clock < $Date;
optimize table history_str;
DELETE FROM history_uint WHERE clock < $Date;
optimize table history_uint;
DELETE FROM history_text WHERE clock < $Date;
optimize table history_text;
DELETE FROM trends WHERE clock < $Date;
optimize table trends;
DELETE FROM trends_uint WHERE clock < $Date;
optimize table trends_uint;
DELETE FROM events WHERE clock < $Date;
optimize table events;
" >>/data/zabbix_server/script/clearzabbix_log.txt;