es定期删除数据

1、定期删除索引

使用sentinl报警后,会产生大量如下索引,虽然不占空间,但时间久了也不好,故写个脚本定期删除

脚本如下:

1 #!/bin/bash
2 #只保留5天内的日志索引
3 LAST_DATA=`date -d "-5 days" "+%Y.%m.%d"`
4 #删除上个月份所有的索引
5 curl -XDELETE 'http://10.139.xx.xx:9200/*-'${LAST_DATA}''

再在设置一个定时策略即可

1 0 1 * * * /data1/elk/scripts/clear-index.sh

2、定期删除索引

Curator 是elasticsearch 官方的一个索引管理工具,可以删除、创建、关闭、段合并等等功能

安装

参考官网:https://www.elastic.co/guide/en/elasticsearch/client/curator/current/installation.html

pip install elasticsearch-curator

安装完如果curator 和curator_cli说明安装成功

curator核心在于俩个配置文件,配置文件名称随意无要求:

配置文件config.yml:配置要连接的ES地址、日志配置、日志级别等;

执行文件action.yml: 配置要执行的操作(可批量)、配置索引的格式(前缀匹配、正则匹配方式等)

config.yml样例

具体参数解析见官网:https://www.elastic.co/guide/en/elasticsearch/client/curator/4.2/configfile.html

client:
hosts:
- 127.0.0.1
port: 9200
url_prefix:
use_ssl: False
certificate:
client_cert:
client_key:
ssl_no_validate: False
http_auth:
timeout: 30
master_only: False logging:
loglevel: INFO
logfile: /var/log/elasticsearch-curator.log
logformat: default
blacklist: []

action.yml样例(删除3天前的数据):

参数具体意思参见官网:https://www.elastic.co/guide/en/elasticsearch/client/curator/4.2/actionfile.html

actions:
1:
action: delete_indices
description: >-
Delete metric indices older than 3 days (based on index name), for
zou_data-2018-05-01
prefixed indices. Ignore the error if the filter does not result in an
actionable list of indices (ignore_empty_list) and exit cleanly.
options :
ignore_empty_list: True
disable_action: True
filters:
- filtertype: pattern
kind: regex
value: '^(zou_data-).*$'
- filtertype: age
source: name
direction: older
timestring: '%Y-%m-%d'
unit: days
unit_count: 3

运行curator

单次运行

curator --config config.yml action.yml 

定时任务运行

0 0 */1 * * curator --config /opt/elasticsearch-curator/config.yml /opt/elasticsearch-curator/action.yml

3、定期删除索引内的数据

#!/bin/bash

indexs=` curl -X GET 'http://10.10.10.10:9200/_cat/indices?v' | awk '{print $3}' | grep -vE '(kibana|index|watcher|monitoring)'`

for index in $indexs
do
curl -X POST "10.139.34.129:9200/$index/_delete_by_query?pretty" -H 'Content-Type:application/json' -d '
{
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "now-7d",
"lte": "now",
"format": "epoch_millis"
}
}
}
],
"must_not": []
}
}
}'
echo "已清除$index 索引内七天前数据~"
done
0 1 * * * /data1/elk/scripts/clear-data.sh

ES的删除操作,不会立即生效,跟更新操作类似。只是会被标记为已删除状态,ES后期会自动删除。

es启动脚本

#!/bin/bash
#set -x
cd `dirname $0`
data_dir=/data1/elk/elasticsearch if [ ! -d $data_dir/data ]; then
mkdir $data_dir/data && chown -R dev.dev $data_dir
fi bin_dir=$data_dir/bin PID=`ps -ef | grep elasticsearch | grep -v grep | grep root | grep -v bash |awk '{print $2}'` if [ -n "$PID" ]
then kill -9 $PID
echo "before: $PID"
cd $bin_dir && nohup su - dev -c "$bin_dir/elasticsearch" >> /dev/null 2>&1 &
sleep 3
P=`ps -ef | grep elasticsearch | grep -v grep | grep root | grep -v bash |awk '{print $2}'`
echo "now : $P"
else
echo "starting"
cd $bin_dir && nohup su - dev -c "$bin_dir/elasticsearch" >> /dev/null 2>&1 &
P=`ps -ef | grep elasticsearch | grep -v grep | grep root | grep -v bash |awk '{print $2}'`
echo "now : $P"
fi

kinaba启动脚本

#!/bin/bash
#set -x
cd `dirname $0`
data_dir=/data1/elk/kibana if [ ! -d $data_dir/data ]; then
mkdir $data_dir/data && chown -R dev.dev $data_dir
fi bin_dir=$data_dir/bin PID=`netstat -nlpt | grep 5601 | awk '{print $7}' | cut -d / -f1` if [ -n "$PID" ]
then kill -9 $PID
echo "before: $PID"
cd $bin_dir && nohup su - dev -c "$bin_dir/kibana" >> $data_dir/logs/kibana.log 2>&1 &
sleep 3
P=`netstat -nlpt | grep 5601 | awk '{print $7}' | cut -d / -f1`
echo "now : $P"
else
echo "starting"
cd $bin_dir && nohup su - dev -c "$bin_dir/kibana" >> $data_dir/logs/kibana.log 2>&1 &
P=`netstat -nlpt | grep 5601 | awk '{print $7}' | cut -d / -f1`
echo "now : $P"
fi
 
 

es定期删除数据的更多相关文章

  1. Azure Automation (2) 定期删除存储账号中的文件

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 本文是对笔者之前的文档Azure Backup (1) 将SQL ...

  2. Cassandra1.2文档学习(11)—— 删除数据

    参考文档:http://www.datastax.com/documentation/cassandra/1.2/webhelp/index.html#cassandra/dml/dml_about_ ...

  3. PHP临时文件session的分级存储与定期删除

    在Windows上PHP默认的Session服务端文件存放在C:\WINDOWS\Temp下,如果说并发访问很大或者 session建立太多,目录下就会存在大量类似sess_xxxxxx的sessio ...

  4. mysql优化, 删除数据后物理空间未释放(转载)

    mysql优化, 删除数据后物理空间未释放(转载) OPTIMIZE TABLE 当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删除操作后在数据文件中留下碎片所致.OPTI ...

  5. 关于mysql 删除数据后物理空间未释放(转载)

    转自 关于mysql 删除数据后物理空间未释放(转载) - NETDATA - 博客园http://www.cnblogs.com/shawnloong/archive/2013/02/07/2908 ...

  6. ElasticSearch 学习记录之 分布式文档存储往ES中存数据和取数据的原理

    分布式文档存储 ES分布式特性 屏蔽了分布式系统的复杂性 集群内的原理 垂直扩容和水平扩容 真正的扩容能力是来自于水平扩容–为集群添加更多的节点,并且将负载压力和稳定性分散到这些节点中 ES集群特点 ...

  7. 小程序中通过判断id来删除数据,当数据长度为0时,显示隐藏部分(交流QQ群:604788754)

    欢迎加入小程序交流群:本群定期更新在工作种遇到的小知识(交流QQ群:604788754) WXML: <!--遍历循环的数据部分--> <block wx:for="{{d ...

  8. MySQL删除数据后磁盘空间的释放情况【转】

    OPTIMIZE TABLE 当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删除操作后在数据文件中留下碎片所致.OPTIMIZE TABLE 是指对表进行优化.如果已经删除 ...

  9. 【转】ElasticSearch之定时删除数据

    有的时候我们在使用ES时,由于资源有限或业务需求,我们只想保存最近一段时间的数据,所以有如下脚本可以定时删除数据 delete_es_by_day.sh #!/bin/sh # example: in ...

随机推荐

  1. Java集合类源码解析:Vector

    [学习笔记]转载 Java集合类源码解析:Vector   引言 之前的文章我们学习了一个集合类 ArrayList,今天讲它的一个兄弟 Vector.为什么说是它兄弟呢?因为从容器的构造来说,Vec ...

  2. 第四章:shiro的INI配置

    4.1 根对象SecurityManager 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...

  3. 零基础学Python--------第9章 异常处理及程序调试

    第9章 异常处理及程序调试 9.1 异常概述 在程序运行过程中,经常会遇到各种各样的错误,这些错误统称为“异常”.这些异常有的是由于开发者将关键字敲错导致的,这类错误多数产生的是SyntaxError ...

  4. H5与C3权威指南笔记--box-shadow

    box-shadow 用于给盒子添加阴影效果.IE9+ 举个栗子:box-shadow: inset 5px 5px 5px red; inset可选,该值会让阴影出现在盒子内部. 第一个5px是阴影 ...

  5. js 3D旋转效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Linux安装配置Mariadb

    一.安装数据库Mariadb 参考:http://blog.51cto.com/12173069/2047746 从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自 ...

  7. Jmeter 接口测试实战-有趣的cookie

    Jmeter 接口测试实战-有趣的cookie 场景: 接口测试时常都需要登录,请求方式(post), 登录常用的方法有通过获取token, 获取session, 获取cookie, 等等. 这几种都 ...

  8. LeetCode的刷题利器(伪装到老板都无法diss你没有工作)

    在工程效率大行其道的今天,如果不会写点代码以后也不容易在测试圈混下去.今天给大家推荐一个LeetCode的刷题利器,可以伪装到连你老板在这里走过去都无法确认你是在干活呢,还是在干活呢. LeetCod ...

  9. 【SpringBoot笔记】SpringBoot整合Druid数据连接池

    废话少说,按SpringBoot的老套路来. [step1]:添加依赖 <!-- 数据库连接池 --> <dependency> <groupId>com.alib ...

  10. js判断浏览器是否支持webGL

    起因是我之前开发的网页,用到了three.js制作了一个3d的旋转球体效果. 在各种浏览器上运行都没问题,在IE上也做了兼容代码. 但是今天接电话,老板说你这网页在xp上不显示啊.IE上好使.goog ...