#!/bin/bash
#sadoc.blog.51cto.com
#备份zabbix数据库,不备份监控历史数据
HOST=127.0.0.1
SQL_PORT=3306
SQL_USER=zabbix
SQL_PASS=123456
DATABASE_NAME=zabbix
DUMP_PATH=/backup/mysql_backup
DATE=$(date '+%Y-%m-%d:%H:%M:%S')
DATED=$(date '+%Y-%m-%d')
[ -d ${DUMP_PATH} ] || mkdir -p ${DUMP_PATH}
cd ${DUMP_PATH}
[ -d logs ] || mkdir -p logs
[ -d ${DATED} ] || mkdir -p ${DATED}
cd ${DATED}
ALL_TABLES=$(mysql -u${SQL_USER} -p${SQL_PASS} -P${SQL_PORT} -h${HOST} ${DATABASE_NAME} -e "show tables" | \
egrep -v "(Tables_in_zabbix|trends*|acknowledges|alerts|auditlog|events|service_alarms)")
for TABLE_NAME in ${ALL_TABLES}
do
mysqldump -u${SQL_USER} -p${SQL_PASS} -P${SQL_PORT} -h${HOST} ${DATABASE_NAME} ${TABLE_NAME} > ${TABLE_NAME}.sql
sleep 10
done
[ $? = 0 ] && echo "${DATE}: Backup zabbix_mysql succeed" >> ${DUMP_PATH}/logs/zabbix_dump.log
[ $? != 0 ] && echo "${DATE}: Backup zabbix_mysql not succeed" >> ${DUMP_PATH}/logs/zabbix_dump_error.log
cd ${DUMP_PATH}/
rm -rf $(date +%Y%m%d --date='7 days ago')
exit 0
下面导入数据库:
删除数据库
drop database zabbix;
创建数据库
create database zabbix character set utf8 collate utf8_bin;
use zabbix;
source /backup/mysql_backup/xxx.sql;