elasticsearch 自动删除索引脚本

脚本
原文 https://blog.csdn.net/felix_y...shell

背景
须要按期清理的索引的后缀日期格式为YYYY.MM.DD,如:project-index-2017.10.01bash

思路
经过_cat/indices接口能够获取当前ES所有索引信息,取第三列为索引名。过滤出索引名中带有的日期字符串,而后进行日期比较,早于10天前的日期即可经过日期模糊匹配索引来删除。curl

#!/bin/bash
# https://blog.csdn.net/felix_yujing/article/details/78207667
###################################
#删除早于十天的ES集群的索引
###################################
function delete_indices() {
    comp_date=`date -d "10 day ago" +"%Y-%m-%d"`
    date1="$1 00:00:00"
    date2="$comp_date 00:00:00"

    t1=`date -d "$date1" +%s`
    t2=`date -d "$date2" +%s`

    if [ $t1 -le $t2 ]; then
        echo "$1时间早于$comp_date,进行索引删除"
        #转换一下格式,将相似2017-10-01格式转化为2017.10.01
        format_date=`echo $1| sed 's/-/\./g'`
        curl -XDELETE http://192.168.10.54:9200/logs-*$format_date
    fi
}

curl -XGET http://192.168.10.54: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

添加定时任务 天天 1点10分函数

crontab -e
10 1 * * * sh /tmp/es-index-clear.sh > /dev/null 2>&1
相关文章
相关标签/搜索