【问题标题】:Dump all documents of Elasticsearch转储 Elasticsearch 的所有文档
【发布时间】:2013-10-15 02:28:05
【问题描述】:

有没有办法创建一个转储文件,其中包含索引的所有数据及其设置和映射?

与 mongoDB 处理 mongodump 的方式类似
或在 Solr its data folder is copied 中复制到备份位置。

干杯!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    这是我们一直在为此目的而开发的新工具https://github.com/taskrabbit/elasticsearch-dump。您可以将索引导出到/导出 JSON 文件,或者从一个集群导出到另一个集群。

    【讨论】:

    • 我试过这个。而索引数据为112Mb。导出的数据是 json 格式 337MB 左右的 3 倍,耗时 40 分钟。我不知道索引是否太大,看看它所花费的时间是否真的很实用。与 ES 开箱即用提供的快照功能相比,它的基准是什么。
    • @Evan elasticdump 是否在内部使用 ElasticSearch 的扫描和滚动功能?
    • 你可以尝试设置--limit=10000
    【解决方案2】:

    【讨论】:

    • 虽然非常方便,但快照并不能真正以可用的格式(json、csv 等)导出您的数据
    • @Andreas Neumann 快照功能可以备份索引中的文档子集。假设一个索引有 1000 个文档,而我只想备份 500 个文档。我想这样做是因为我想导入一组文档并在其他一些索引中恢复以进行一些负载测试。我的任务不需要所有文档,索引很大,我不想拍摄所有数据的快照,因为这需要很多时间
    • 真的不明白为什么这是公认的答案,因为它与原始问题无关(它要求一种将 ES 文档转储为 json 的方法,而不是一种返回的方法他们使用快照)。
    【解决方案3】:

    我们可以使用 elasticdump 进行备份并恢复它,我们可以将数据从一个服务器/集群移动到另一个服务器/集群。

    1.使用elasticdump 将一个索引数据从一台服务器/集群移动到另一台服务器/集群的命令。

    # Copy an index from production to staging with analyzer and mapping:
    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
    

    2。使用multielasticdump 将所有索引数据从一台服务器/集群移动到另一台服务器/集群的命令。

    备份

    multielasticdump \
      --direction=dump \
      --match='^.*$' \
      --limit=10000 \
      --input=http://production.es.com:9200 \
      --output=/tmp 
    

    恢复

    multielasticdump \
      --direction=load \
      --match='^.*$' \
      --limit=10000 \
      --input=/tmp \
      --output=http://staging.es.com:9200 
    

    注意:

    • 如果 --direction 是默认的 dump, --input 必须是 ElasticSearch 服务器的基本位置的 URL(即http://localhost:9200), --output 必须是目录。每个匹配的索引都会创建一个数据、映射和分析器文件。

    • 要加载您从 multi-elasticsearch 转储的文件,--direction 应设置为 load,--input 必须是 multielasticsearch 转储的目录,--output 必须是 Elasticsearch 服务器 URL。

    • 第二条命令将备份settingsmappingstemplatedata 本身作为 JSON 文件。

    • --limit 不能超过10000,否则会报异常。

    • 了解更多详情here

    【讨论】:

    • 您可能需要将--ignoreChildError=true 添加到multielasticdump 命令。此外,可能需要将--limit 设置为低于 10000(例如,设置为 1000)以防止某些其他类型的模糊错误。后者也适用于elasticdump 命令。
    • 对于multielasticdump --direction=load,我必须添加--ignoreType='template'
    • 请注意,您需要在 URL 后面加上斜杠,例如multielasticdump ... --input=http://production.es.com:9200/ ... 而不仅仅是 multielasticdump ... --input=http://production.es.com:9200 ...。否则它会因一些非常非描述性的错误而失败。
    【解决方案4】:

    对于您的情况,Elasticdump 是完美的答案。
    首先,您需要下载映射,然后是索引

    # Install the elasticdump 
    npm install elasticdump -g
    
    # Dump the mapping 
    elasticdump --input=http://<your_es_server_ip>:9200/index --output=es_mapping.json --type=mapping
    
    # Dump the data
    elasticdump --input=http://<your_es_server_ip>:9200/index --output=es_index.json --type=data    
    
    

    如果你想在任何服务器上转储数据,我建议你通过 docker 安装 esdump。您可以从这个网站获取更多信息Blog Link

    【讨论】:

      【解决方案5】:

      ElasticSearch 本身提供了一种创建数据备份和恢复的方法。执行此操作的简单命令是:

      CURL -XPUT 'localhost:9200/_snapshot/<backup_folder name>/<backupname>' -d '{
          "indices": "<index_name>",
          "ignore_unavailable": true,
          "include_global_state": false
      }'
      

      现在,如何创建,这个文件夹,如何在ElasticSearch配置中包含这个文件夹路径,以便它可以用于ElasticSearch,恢复方法,很好解释here。看它的实用演示冲浪here

      【讨论】:

        【解决方案6】:

        数据本身是一个或多个 lucene 索引,因为您可以有多个分片。您还需要备份的是集群状态,其中包含有关集群、可用索引、它们的映射、它们组成的分片等的各种信息。

        不过,它都在data 目录中,您可以复制它。它的结构非常直观。在复制之前最好禁用自动刷新(为了备份索引的一致视图并避免在复制文件时对其进行写入),发出手动刷新,同时禁用分配。记得从所有节点复制目录。

        此外,elasticsearch 的下一个主要版本将提供一个新的快照/恢复 api,允许您执行增量快照并通过 api 恢复它们。这是相关的 github 问题:https://github.com/elasticsearch/elasticsearch/issues/3826

        【讨论】:

        • 事实上我已经尝试过这个解决方案,但它对我不起作用。我按照您所说的将数据文件夹复制到新的远程安装中,我尝试启动 ES 但由于异常而死了。不幸的是,我没有日志来粘贴它。您的解决方案成功了吗?
        • 是的,你应该这样做。您是否尝试将索引恢复到相同的弹性搜索版本?类似的机器?这是一个大索引吗?单节点?
        • 这是一个单节点、5 个分片、一个小索引(约 2k 个文档)但不同的机器。版本相同(0.9)
        【解决方案7】:

        您还可以通过 http 请求以 JSON 格式转储 elasticsearch 数据: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-scroll.html
        CURL -XPOST 'https://ES/INDEX/_search?scroll=10m'
        CURL -XPOST 'https://ES/_search/scroll' -d '{"scroll": "10m", "scroll_id": "ID"}'

        【讨论】:

          【解决方案8】:

          要将所有文档从 ElasticSearch 导出为 JSON,您可以使用 esbackupexporter 工具。它适用于索引快照。它将带有快照(S3、Azure blob 或文件目录)的容器作为输入,并每天为每个索引输出一个或多个压缩 JSON 文件。导出历史快照时非常方便。要导出您的热门索引数据,您可能需要先制作快照(请参阅上面的答案)。

          【讨论】:

            【解决方案9】:

            如果您想在数据离开 Elasticsearch 的过程中对其进行处理,您可能需要使用 Logstash。它有一个方便的Elasticsearch Input Plugin

            然后您可以导出到任何内容,从 CSV 文件到 reindexing the data on another Elasticsearch cluster。虽然对于后者,您也有Elasticsearch's own Reindex

            【讨论】:

              【解决方案10】:

              在撰写此答案时(2021 年),备份 ElasticSearch 集群的官方方法是对其进行快照。参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/snapshot-restore.html

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-06-28
                • 2020-07-17
                • 1970-01-01
                相关资源
                最近更新 更多