分享

elasticsearch-dump 迁移es数据 (elasticdump)

 馒头的人生 2018-10-09
复制代码
# 获取集群的节点列表:
curl 'localhost:9200/_cat/nodes?v'

# 列出所有索引:
curl 'localhost:9200/_cat/indices?v'

创建一个名为“customer”的索引,然后再查看所有的索引:
curl -X PUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'

删除索引 DELETE
curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'

通过以上命令语句的学习,我们发现索引的增删改查有一个类似的格式,总结如下:

  curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

  <REST Verb>:REST风格的语法谓词

  <Node>:节点ip

  <port>:节点端口号,默认9200

  <Index>:索引名

  <Type>:索引类型

  <ID>:操作对象的ID号 

复制代码

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

或者 : https://www./package/elasticdump

 

复制代码
wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.xz

tar xf node-v8.11.2-linux-x64.tar.xz 

mv node-v8.11.2-linux-x64 /usr/local

ln -s /usr/local/node-v8.11.2-linux-x64/bin/npm /usr/local/bin/npm

ln -s /usr/local/node-v8.11.2-linux-x64/bin/node /usr/local/bin/node

npm init -f

npm install elasticdump

#因为我只用一次,所以这里没有安装到全局,需要到node_modules目录下才能找到 elasticdump , 我安装的位置如下:

/usr/local/node-v8.11.2-linux-x64/node_modules/elasticdump/bin/elasticfump 
复制代码

 

 

数据迁移: 

复制代码
'#拷贝analyzer分词
elasticdump   --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=analyzer
'#拷贝映射
elasticdump   --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=mapping
'#拷贝数据
elasticdump   --input=http://production.es.com:9200/my_index \
  --output=http://staging.es.com:9200/my_index \
  --type=data
复制代码

 

复制代码
# 注意 elasticdump 提供给了--httpAuthFile 参数来做认证
--httpAuthFile      When using http auth provide credentials in ini file in form
                    `user=<username>
                    password=<password>`

# 只需要写一个ini文件 ,文件中写入用户名和密码就可以了
复制代码

 如果网络情况不好,或者没有网络还可以先备份到文件:

复制代码
# 备份索引数据到文件里:
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

# 备份到标准输出,且进行压缩(这里有一个需要注意的地方,我查询索引信息有6.4G,用下面的方式备份后得到一个789M的压缩文件,这个压缩文件解压后有19G):
elasticdump   --input=http://production.es.com:9200/my_index \
  --output=$   | gzip > /data/my_index.json.gz

# 把一个查询结果备份到文件中
elasticdump   --input=http://production.es.com:9200/my_index \
  --output=query.json   --searchBody '{"query":{"term":{"username": "admin"}}}'
复制代码

 

elasticdump还是非常方便的,主要是可以指定查询条件,把查询结果进行备份。如果按照日期进行查询,那么就可以迁移指定之间段内的数据,

恢复数据
# 将备份文件的数据导入ES
elasticdump   --input=./data.json   --output=http://es.com:9200 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多