Elasticsearch索引刪除

#!/bin/bash
#經過任務計劃自動刪除es中30天之前的索引,以釋放空間
ES_HOST='127.0.0.1';
ES_PORT=9200;
HTTP_PREFIX=http://${ES_HOST}:${ES_PORT}/

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_PREFIX}/_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
相關文章
相關標籤/搜索