腳本
原文 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