elasticsearch dashboard+保留10天内索引+导入导出备份

avatar 2022年12月12日18:16:42 评论 267 次浏览

elasticsearch集群中,elasticsearch的基本操作都是在elasticsearch的dashboard中操作,比如索引分片,索引创建,elasticsearch的备份和恢复等等,这里就简单说一下elasticsearch的索引备份和恢复。

es dashboard

有两款

先修改es的配置文件: elasticsearch.yml追加
http.cors.enabled: true
http.cors.allow-origin: "*"
docker run -d -v /etc/localtime:/etc/localtime --restart=always -p 9100:9100 mobz/elasticsearch-head:5
docker run  -d \
    --net=host \
    --restart=always \
    -v /etc/localtime:/etc/localtime:ro \
    -v /root/elasticsearch-HQ/royrusso-elasticsearch-HQ-eb117d4:/usr/share/nginx/html \
    --name nginx \

es数据保留10天以内

#!/bin/bash

###################################
#删除早于十天的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.x.x:9200/*$format_date
    fi
}

curl -XGET http://192.168.x.x: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

es数据备份: elasticsearch-dump

https://github.com/taskrabbit/elasticsearch-dump

  • 安装
npm install elasticdump
./bin/elasticdump
  • 使用

支持导出文件
支持导出文件压缩
支持导出后直接导入到另一个stage escluster
elasticdump --help
# Backup index data to a file:
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=/data/my_index_mapping.json \
  --type=mapping
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=/data/my_index.json \
  --type=data

# Backup and index to a gzip using stdout:
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=$ \
  | gzip > /data/my_index.json.gz

# Backup the results of a query to a file
elasticdump \
  --input=http://production.es.com:9200/my_index \
  --output=query.json \
  --searchBody '{"query":{"term":{"username": "admin"}}}'

1g的日志不到四五分钟就导完毕: 从一个集群到另一个集群

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: