shell之删除elasticsearch30天以前的索引

news/2024/7/7 7:58:05

在elasticsearch的运维工作中,由于es每天会产生大量的日志,如果一直保存不进行删除的话,再大的磁盘空间也会不够用,由此需要删除满足条件的index,从而释放磁盘空间;
我们公司的es要求只保留30天的日志即可,超出30天的index则自动进行删除;
es-index-delete-30days-ago.sh

#!/bin/bash
##############################################
#Author: Liuzhengwei - 1135960569@qq.com
#QQ:1135960569
#Last modified: 2018-10-15 16:29
#Filename:es-index-delete-30days-ago
#Description: 通过任务计划自动删除es中30天以前的索引,以释放空间
##############################################
source /etc/profile
#定义删除30天以前的函数
delete_indices(){
    check_day=`date -d '-30 days' '+%F'`
    index_day=$1
    #将日期转换为时间戳
    check_day_timestamp=`date -d "$check_day" +%s`
    index_day_timestamp=`date -d "$index_day" +%s`
    #当索引的时间戳值小于当前日期30天前的时间戳时,删除此索引
    if [ ${index_day_timestamp} -lt ${check_day_timestamp} ];then
        #转换日期格式
        format_date=`echo $1 | sed 's/-/\./g'`
        curl -XDELETE http://10.78.1.184:9200/*$format_date
    fi
}

curl -XGET http://10.78.1.184:9200/_cat/indices | awk -F" " '{print $3}' | awk -F"-" '{print $NF}' | egrep "[0-9]*\.[0-9]*\.[0-9]*" | sort | uniq  | sed 's/\./-/g' | while read LINE
do
    #调用索引删除函数
    delete_indices $LINE
done

任务计划,每天执行一次:

0 2 * * * /server/scripts/es-index-delete-30days-ago.sh &> /dev/null

转载于:https://blog.51cto.com/liuzhengwei521/2300163


http://www.niftyadmin.cn/n/4472224.html

相关文章

Metricbeat 参考指南(步骤3:在Elasticsearch中加载索引模板)

步骤3:在Elasticsearch中加载索引模板 在Elasticsearch中,索引模板用于定义设置和映射,以确定如何分析字段。 Metricbeat推荐的索引模板文件是由Metricbeat包安装的,如果你接受metricbeat.yml配置文件中的默认配置,那么…

android手机屏幕适配补充

如何将一个应用程序适配在不同的手机上&#xff0c;虽然这不算是一个技术问题&#xff0c;但是对于刚刚做屏幕的开发人员来说&#xff0c;还真不是一件多么简单的事情。 首先&#xff1a;你需要在AndroidManifest.xml文件的<manifest>元素如下添加子元素 <supports-sc…

url加密 比较

1.escape 简单来说&#xff0c;escape是对字符串(string)进行编码(而另外两种是对URL)&#xff0c;作用是让它们在所有电脑上可读。 编码之后的效果是%XX或者%uXXXX这种形式。 其中 ASCII字母、数字、*/ &#xff0c;这几个字符不会被编码&#xff0c;其余的都会。 最关键的是&…

ERROR tool.ImportTool: Import failed: java.io.IOException: Generating splits for a textual index col

sqoop任务报错&#xff1a; 异常内容&#xff1a; ERROR tool.ImportTool: Import failed: java.io.IOException: Generating splits for a textual index column allowed only in case of “-Dorg.apache.sqoop.splitter.allow_text_splittertrue” property passed as a par…

Android GridView 横向滚动 一行显示

都知道gridview和listview是android比较重要的数据组件&#xff0c; 接到一个横向显示数据的任务&#xff0c;头大半天&#xff0c;最后只能用gridview试试了。呵呵&#xff0c;费了N多脑细胞&#xff0c;搞定。 布局文件main.xml [java] view plaincopy <?xml version&…

JAVA 获取两个日期间的所有日期

2019独角兽企业重金招聘Python工程师标准>>> public static List<String> getDates(Date startDate, Date endDate){ SimpleDateFormat sdf new SimpleDateFormat("yyyyMMdd"); List<String> dates new ArrayLis…

navicat报错SSH:Unable to load key

navicat的版本比较老了。在加载ssh的pub key时报这个错。这个private key确实可以用来登录服务器。而且navicat也能够加载以前的private key&#xff0c;那么问题只有一个&#xff1a;文件有问题 我的解决办法是安装新版的navicat&#xff0c;问题已解决&#xff01;

Error: Error while compiling statement: FAILED: SemanticException Schema of both sides of union shou

sqoop任务导数据到hive报错&#xff1a; Error: Error while compiling statement: FAILED: SemanticException Schema of both sides of union should match. (state42000,code40000) 原因是hive中做union时&#xff0c;查询到的两个表的有差异&#xff0c;列名不一致。删除…