操作步骤:
1、将脚本放到任意位置下,不容易被误删即可
2、定时任务,一周执行一次脚本
脚本如下:#!bin/bash
backuppath=/data/mysql/backup
date=$(date +%Y%m%d%H%M)
SQLuser=root
SQLpwd=password
mysqldump=/usr/local/mysql/bin/mysqldump
mysql=/usr/local/mysql/bin/mysql
HIS_Date=`date -d $(date -d "-30 day" +%Y%m%d) +%s` #取30天之前的时间戳
Sql_Date=`date -d $(date -d "-7 day" +%Y%m%d) +%s` #取7天之前的时间戳
#进入mysql数据库,清除zabbix历史数据
$mysql -u$SQLuser -p$SQLpwd -e"
use zabbix;
delete from history WHERE 'clock' < $Sql_Date;
delete from history_uint WHERE 'clock' < $Sql_Date;
delete from history_str WHERE 'clock' < $Sql_Date;
delete from history_text WHERE 'clock' < $Sql_Date;
delete from history_log WHERE 'clock' < $Sql_Date;
exit
"
#建立备份目录
if [ ! -e $backuppath ];then
mkdir -p $backuppath
fi
#开始备份zabbix库$mysqldump -u$SQLuser -p$SQLpwd zabbix >$backuppath/zabbix$date.sql
#删除1个月前的备份数据find /data/mysql/backup -mtime +30 -type f -name \zabbix*.sql -exec rm -f {} \;
在数据库历史数据过大,可以考虑truncate掉历史数据。
$mysql -u$SQLuser -p$SQLpwd -e"
use zabbix;
truncate table history;
truncate table history_log;
truncate table history_uint;
truncate table history_str;
truncate table history_text;
exit
"
(出处: 乐维)