脚本专栏 
首页 > 脚本专栏 > 浏览文章

shell脚本批量删除es索引的方法

(编辑:jimmy 日期: 2024/9/17 浏览:3 次 )

发现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/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

因为我们的索引是按天创建的,索引名前缀是yyyy-MM-dd, 保留一段时间后需要批量删除。shell的第一个参数为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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

上一篇:使用shell脚本每天对MySQL多个数据库自动备份的讲解
下一篇:shell脚本操作mysql数据库删除重复的数据
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap