十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
使用shell腳本怎么批量刪除es索引?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
發(fā)現(xiàn)elasticsearch集群的狀態(tài)是red,unassign的分片數(shù)很多,看了下都是些舊的日期的索引(應(yīng)該是定時(shí)任務(wù)刪除失敗導(dǎo)致的)。
curl -XGET ip:port/_cat/shards | grep UNASSIGNED
數(shù)量有幾百個(gè),寫個(gè)腳本處理下,先恢復(fù)成green。red狀態(tài)好像會(huì)影響索引創(chuàng)建和數(shù)據(jù)遷移
先把需要?jiǎng)h除的索引導(dǎo)出到文件
curl -XGET ip:port/_cat/shards | grep UNASSIGNED >> needDelIndex.txt
確認(rèn)下要?jiǎng)h除的索引列表。沒(méi)問(wèn)題就執(zhí)行下面刪除shell(es的ip和端口需要修改下)
#!/bin/bash echo "$1" esUrl=${esip}:${esport} indexfile=needDelIndex.txt #cp -f /dev/null ${indexfile} #curl -XGET ip:port/_cat/shards | grep UNASSIGNED >> needDelIndex.txt if [ ! -f ./${indexfile} ]; then echo $indexfile not exists exit 0 fi logfile=esindex_del.`date +"%m-%d"`.log cp -f /dev/null ${logfile} lastIndexName="test" for item in `cat ${indexfile} | awk '{print $1}'` do if [ "$item" = "error" ] then continue fi if [ "$item" != "$lastIndexName" ] then curl -XDELETE ${esUrl}/${item} >> ${logfile} echo ---------${item} `date` >> ${logfile} sleep 5 fi lastIndexName=${item} done
因?yàn)槲覀兊乃饕前刺靹?chuàng)建的,索引名前綴是yyyy-MM-dd, 保留一段時(shí)間后需要批量刪除。shell的第一個(gè)參數(shù)為yyyy-MM-dd,將刪除該天及以前的舊索引
#!/bin/bash esUrl=${esip}:${esport} echo "$1" if [ $# -ge 1 ] then deleteDate=$1 else echo "please inpust detete esindex's date(yyyy-MM-dd)" exit 0 fi indexfile=esindex.info cp -f /dev/null ${indexfile} curl '${esUrl}/_cat/indices' >> ${indexfile} logfile=esindex_del.`date +"%m-%d"`.out cp -f /dev/null ${logfile} for item in `cat ${indexfile} | awk '{print $3}'` do if [ "$item" = "error" ] then continue fi parameter=${esUrl}/${item} indexdate=${item:0:10} if [ "$indexdate" = "$deleteDate" ] then curl -XDELETE ${parameter} >> ${logfile} echo ---------${item} >> ${logfile} sleep 5 elif [[ "$indexdate" < "$deleteDate" ]] then curl -XDELETE ${parameter} >> ${logfile} echo ---------${item} >> ${logfile} sleep 5 fi done
看完上述內(nèi)容,你們掌握使用shell腳本怎么批量刪除es索引的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!