使用shell脚本怎么批量删除es索引?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。发现elasticsearch集群的状态是red,unassign的分片数很多,看了下都是些
使用shell脚本怎么批量删除es索引?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
发现elasticsearch集群的状态是red,unassign的分片数很多,看了下都是些旧的日期的索引(应该是定时任务删除失败导致的)。
curl -XGET ip:port/_cat/shards | grep UNASSIGNED
数量有几百个,写个脚本处理下,先恢复成green。red状态好像会影响索引创建和数据迁移
先把需要删除的索引导出到文件
curl -XGET ip:port/_cat/shards | grep UNASSIGNED >> needDelIndex.txt
确认下要删除的索引列表。没问题就执行下面删除shell(es的ip和端口需要修改下)
#!/bin/bashecho "$1"esUrl=${esip}:${esport}indexfile=needDelIndex.txt#cp -f /dev/null ${indexfile}#curl -XGET ip:port/_cat/shards | grep UNASSIGNED >> needDelIndex.txtif [ ! -f ./${indexfile} ]; then echo $indexfile not exists exit 0filogfile=esindex_del.`date +"%m-%d"`.loGCp -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
因为我们的索引是按天创建的,索引名前缀是yyyy-MM-dd, 保留一段时间后需要批量删除。shell的第一个参数为yyyy-MM-dd,将删除该天及以前的旧索引
#!/bin/bashesUrl=${esip}:${esport}echo "$1"if [ $# -ge 1 ]then deleteDate=$1else echo "please inpust detete esindex's date(yyyy-MM-dd)" exit 0fiindexfile=esindex.infocp -f /dev/null ${indexfile}curl '${esUrl}/_cat/indices' >> ${indexfile}logfile=esindex_del.`date +"%m-%d"`.outcp -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 fidone
看完上述内容,你们掌握使用shell脚本怎么批量删除es索引的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!
--结束END--
本文标题: 使用shell脚本怎么批量删除es索引
本文链接: https://www.lsjlt.com/news/256199.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0