elasticsearch部分命令

一.ES相关操作

1.ES启动与关闭相关命令

su elastic  # elastic用户为安装ES时创建的用户,es不能用root账号启动
cd /usr/local/elasticsearch-6.4.3/  # cd 进入es安装目录
./bin/elasticsearch -d -p pid # -p 选项可以将es进程PID记录到pid文件中,该文件可以指定  -d 后台运行
####
kill -SIGTERM PID  # PID 为ES进程的PID号

2.查看ES相关信息

]$ curl -XGET http://localhost:9200/
{
  "name" : "SonoxO1",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "Oktq2zxmSRWcU8Fvhat2TA",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

二.索引的相关操作

1.查看索引

]# curl localhost:9200/_cat/indices?v
health status index                             uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   logstash-nq-coupon-2018.11.14     V2ilbJErQZGW2m9rBLXeEw   5   1     182396            0     24.9mb         24.9mb
green  open   .kibana                           llTJB8gQTS-r6oGjF8fTdg   1   0          3            0     17.6kb         17.6kb

2.删除索引

]# curl -XDELETE http://localhost:9200/logstash-2018.11.14
{"acknowledged":true}

3.设置索引副本数量

curl -H 'Content-Type: application/json' -XPUT http://localhost:9200/prod*/_settings -d '{"index":{"number_of_replicas":0}}'
#  prod* 所有以prod开头的index

4.设置index模板

curl -X PUT 127.0.0.1:9200/_template/my_template -H 'Content-Type: application/json' \ 
-d'{"index_patterns" : ["dev*","prod*"],"order" : 0,"settings" : {"number_of_replicas" : 0}}'

# my_template  模板名称
# index_patterns 模板匹配模式,支持通配符。名称与其匹配的index将使用该模板
# order 一个index与多个模板匹配成功,则通过order大小决定使用哪个模板,默认使用数字小的
# settings 设置了默认的副本数,如果ES只有一个节点,那么副本设置为0,否则设置为1

三.备份与恢复

1.创建备份仓库

}# mkdir  /data/elasticsearch/bakup  # 创建备份目录
}# chown elastic.elastic  /data/elasticsearch/bakup  # 更改目录权限
# 在elasticsearch.yml配置文件中新增一行,并重新启动ES
    path.repo: /data/elasticsearch/bakup
#创建仓库   es_bakup 可自定义
}# curl -H 'Content-Type: application/json' -XPUT http://localhost:9200/_snapshot/es_bakup \
    -d '{"type":"fs","settings":{"location":"/data/elasticsearch/bakup","compress": "true"}}'
{"acknowledged":true}

注:在6.X的版本中需要添加 -H 'Content-Type: application/json' 设置header,原因可参考:https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests

2.查看备份仓库

]# curl -XGET http://127.0.0.1:9200/_snapshot?pretty
{
  "es_bakup" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",
      "location" : "/data/elasticsearch/bakup"
    }
  }
}

3.备份索引

# 备份单个索引  log_bak_2018.11.14 快照名称,可自定义
]# curl -H'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/log_bak_2018.11.14 \ 
    -d '{"indices":"logstash-2018.11.14"}' 
# 备份多个索引
]# curl -H 'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/log_bak_2018.11.14 \
    -d '{"indices": "idx_1,idx_2,idx_3"}'
# 备份所有索引
]# curl -H 'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/bakup_all_2018.11.16

4.查看备份的索引

]# curl -XGET http://127.0.0.1:9200/_snapshot/es_bakup/log_bak_2018.11.14?pretty
{
  "snapshots" : [
    {
      "snapshot" : "log_bak_2018.11.14",
      "uuid" : "wrDpuL8aQHe7kNn5l11WqA",
      "version_id" : 6040399,
      "version" : "6.4.3",
      "indices" : [
        "logstash-2018.11.14"
      ],
      "include_global_state" : true,
      "state" : "SUCCESS",
      "start_time" : "2018-11-16T07:22:49.001Z",
      "start_time_in_millis" : 1542352969001,
      "end_time" : "2018-11-16T07:22:49.914Z",
      "end_time_in_millis" : 1542352969914,
      "duration_in_millis" : 913,
      "failures" : [ ],
      "shards" : {
        "total" : 5,
        "failed" : 0,
        "successful" : 5
      }
    }
  ]
}

5.恢复索引

# 恢复单个索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/_snapshot/es_bakup/xp-coupon-2018.11.14/_restore  \
   -d '{"indices":"logstash-2018.11.14"}'
# 恢复所有索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/_snapshot/es_bakup/bakup_all_2018.11.16/_restore  
# 恢复所有索引备份中的部分索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/_snapshot/es_bakup/bakup_all_2018.11.16/_restore \
   -d '{"indices":"idx_1,idx_2"}'

6.其他

(1)备份目录中的文件可以打包压缩,恢复时解压至备份目录即可

(2)恢复索引时,如果该索引存在则需要先关闭该索引,恢复完成之后重新开启该索引

# 关闭某个索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/logstash-2018.11.16/_close
# 打开某个索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/logstash-2018.11.16/_open

四.示例脚本
示例脚本用于每天备份前一天的index,其他备份策略则需要修改脚本

#!/bin/bash

SNAP_DIR=/data/elasticsearch/bakup  # 快照目录
BAKUP_DIR=/tmp  # 备份文件存放目录
DATE=`date -d -1days +%Y.%m.%d`  # 备份文件时间 index后缀  eg: logstash-lab-xp-coupon-2018.11.15
BAKUP_FILENAME=${BAKUP_DIR}/es-bakup-${DATE}.tar.gz  # 备份文件名

# 进入快照目录,删除之前的备份的快照
cd $SNAP_DIR
rm -rf ./*  # 存在一定风险性,可以考虑其他方式,比如mv

# 创建快照  其中index名称使用 *通配符加上DATE变量的形式指定
curl -H'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/es_bakup_${DATE}?wait_for_completion=true -d '{"indices":"*'${DATE}'"}'

sleep 5

# 打包快照目录至备份目录
tar czf ${BAKUP_FILENAME} ./*

转载于:https://blog.51cto.com/11650412/2318042

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值