全部產品
Search
文件中心

Elasticsearch:通過elasticsearch-dump工具遷移資料

更新時間:Aug 21, 2026

通過elasticsearch-dump工具,您可以將Elasticsearch執行個體索引的settings、mapping、data等資料移轉至本地或者其他阿里雲執行個體上。本文介紹如何安裝與使用elasticsearch-dump工具。

背景資訊

elasticsearch-dump是一個開源的Elasticsearch資料移轉工具,詳細資料請參見elasticsearch-dump官方文檔。

前提條件

  • 已建立Elasticsearch執行個體,具體操作請參見建立Elasticsearch執行個體。

  • 已開啟目標端Elasticsearch執行個體的自動建立索引功能,或提前建立對應索引。開啟自動建立索引功能的具體操作,請參見配置YML參數。

  • 已建立ECS伺服器,該ECS用來安裝elasticsearch-dump工具。具體操作,請參見自訂購買執行個體。

適用情境

elasticsearch-dump工具適用於資料量較小,索引數量不多的遷移情境。

安裝elasticsearch-dump工具

  1. 串連ECS伺服器。

  2. 安裝node.js。

    1. 下載安裝包。

      wget https://nodejs.org/dist/v16.18.0/node-v16.18.0-linux-x64.tar.xz
    2. 解壓。

      tar -xf node-v16.18.0-linux-x64.tar.xz
    3. 設定環境變數。

      • 臨時生效

        export PATH=$PATH:/root/node-v16.18.0-linux-x64/bin/
      • 永久生效

        vim ~/.bash_profile
        export PATH=$PATH:/root/node-v16.18.0-linux-x64/bin/
        source ~/.bash_profile
  3. 安裝elasticsearch-dump。

    npm install elasticdump -g

使用樣本

說明

如果密碼中存在特殊符號(如 #、$等),發送請求時網路會被阿里雲Web Application Firewall(WAF)攔截,相應的本地執行命令也會報錯。詳細報錯和處理方法,請參見常見問題。

遷移資料到阿里雲執行個體

  • 遷移指定索引的settings

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=http://"<OtherName>:<OtherPassword>"@<OtherEsHost>/<OtherEsIndex> --type=settings
  • 遷移指定索引的mapping

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=http://"<OtherName>:<OtherPassword>"@<OtherEsHost>/<OtherEsIndex> --type=mapping
  • 遷移指定索引的data

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=http://"<OtherName>:<OtherPassword>"@<OtherEsHost>/<OtherEsIndex> --type=data

遷移資料到本地

  • 遷移指定索引的settings

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> --type=settings
  • 遷移指定索引的mapping

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> --type=mapping
  • 遷移指定索引的data

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> --type=data
  • 通過query遷移資料

    elasticdump --input=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --output=<YourLocalFile> ----searchBody="<YourQuery>"

恢複本機資料到阿里雲執行個體

恢複本地data資料到阿里雲執行個體:

elasticdump --input=<YourLocalFile> --output=http://"<UserName>:<YourPassword>"@<YourEsHost>/<YourEsIndex> --type=data

如果您有其他的資料移轉需求,請參見elasticsearch-dump官方文檔。

使用上述樣本時,需要將以下欄位替換為對應的值。

參數

說明

<YourEsHost>/<OtherEsHost>

源或目標Elasticsearch執行個體的私網或公網訪問地址,可在執行個體的基本資料頁面擷取,擷取方法請參見查看執行個體基本資料。

說明

如果安裝elasticsearch-dump工具的伺服器與源和目標Elasticsearch均在同一VPC下,可使用私網訪問地址;如果不在同一VPC下,需要使用公網訪問地址,且需要配置公網訪問白名單,詳細資料請參見配置執行個體公網或私網訪問白名單。

<UserName>/<OtherName>

源或目標Elasticsearch執行個體的訪問使用者名稱,預設為elastic。

<YourPassword>/<OtherPassword>

源或目標Elasticsearch執行個體的訪問密碼。elastic使用者的密碼在建立執行個體時設定,如果忘記可進行重設,重設密碼的注意事項和操作步驟請參見重設執行個體訪問密碼。

<YourEsIndex>/<OtherEsIndex>

源或目標索引名稱。

<YourLocalFile>

本地檔案路徑。例如:/data/test.json。

重要

將資料移轉至本地時,同一目錄中不能存在目標檔案,elasticsearch-dump工具會自動產生對應檔案。

<YourQuery>

查詢語句。樣本:{\"query\":{\"term\":{\"_id\":\"100\"}}}。

常見問題

Q:遇到報錯Error: getaddrinfo ENOTFOUND elastic,怎麼處理?

[root@i-xxx ~]# elasticdump --input=http://"xxx.public.elasticsearch.aliyuncs.com:9200/customers --output=/root/customers.json --type=settings
Tue, 04 Jul 2023 08:42:53 GMT | starting dump
(node:25448) NOTE: We are formalizing our plans to enter AWS SDK for JavaScript (v2) into maintenance mode in 2023.

Please migrate your code to use AWS SDK for JavaScript (v3).
For more information, check the migration guide at https://a.co/7PzMCcy
(Use `node --trace-warnings ...` to show where the warning was created)
Tue, 04 Jul 2023 08:43:13 GMT | Error Emitted => getaddrinfo ENOTFOUND elastic
Tue, 04 Jul 2023 08:43:13 GMT | Error Emitted => getaddrinfo ENOTFOUND elastic
Tue, 04 Jul 2023 08:43:13 GMT | Total Writes: 0
Tue, 04 Jul 2023 08:43:13 GMT | dump ended with error (get phase) => Error: getaddrinfo ENOTFOUND elastic

A:產生這個報錯的原因是,HTTP認證密碼包含特殊字元導致URL解析異常,解決辦法是添加HTTP認證檔案。

  1. 認證檔案auth.ini中寫入Elasticsearch執行個體的訪問使用者名稱和訪問密碼。樣本如下:

    user=elastic
    password="Es#123456"
    說明

    password需要添加半形雙引號("")。

  2. 執行如下命令,通過參數--httpAuthFile指定HTTP認證檔案。

    elasticdump --input=http://es-*****.public.elasticsearch.aliyuncs.com:9200/customers --output=/root/customers.json --httpAuthFile=/root/auth.ini --type=settings

Q:遇到報錯URIError: URI malformed,怎麼處理?

A:elasticsearch-dump外掛程式無法解析特殊字元的使用者名稱和密碼,如果您的使用者名稱或密碼中包含特殊符號,可能遇到以上報錯。處理方法:

  • 方法一:修改使用者名稱或密碼,將使用者名稱或密碼中的特殊符號去掉。

  • 方法二:登入Kibana建立一個新的使用者,為新使用者配置要求的權限後,用新使用者進行遷移。