elasticsearch索引自動清理

查看全部的索引文件:
curl -XGET http://localhost:9200/_cat/indices?vnode

刪除索引文件以釋放空間:
curl -XDELETE http://localhost:9200/filebeat-2016.12.28shell

單節點的elk可在索引目錄刪除索引文件:集羣環境刪除某節點的索引文件,會致使集羣服務不可用.集羣環境須要使用API的方式進行刪除.數組

索引文件保留在服務器中,大大減少服務器的性能,佔用硬盤空間,
所以使用腳本自動刪除elk中兩個月之前的索引以釋放空間:bash

--#!/bin/bash服務器

find '/data/elasticsearch/data/elks/nodes/0/indices/' -name 'filebeat-*' -ctime +60 > index.txtcurl

cd ~
cat index.txt | while read line
do
curl -XDELETE "http://localhost:9200/"$(basename $line)""
doneelasticsearch

添加計劃任務:
$crontab -e
0 0 * * * cd /root && ./elk_index_remove.sh >>/dev/null性能

ps:shell中單引號&雙引號:url

單引號裏的任何字符都會原樣輸出,單引號字符串中的變量是無效的.
單引號字符串中不能出現單引號(對單引號使用轉義符後也不能夠).code

雙引號裏能夠有變量.
雙引號裏能夠出現轉移字符.

使用數組循環刪除.
index=(curl -XGET 'http://localhost:9200/_cat/indices/*?v'|awk '{print $3}'|sed '1d')
for i in ${index[*]}:do
curl -XDELETE "http://localhost:9200/$i" done

相關文章
相關標籤/搜索