ELK,萌萌哒

发布个es迁移工具:elasticsearch-migration

Elasticsearch | 作者 medcl | 发布于2016年05月23日 | | 阅读数:31786

https://github.com/medcl/elasticsearch-migration
支持多个版本间的数据迁移,使用scroll+bulk
1.版本支持1.x,2.x.5.0 (0.x未测试)
2.支持http basic auth 认证的es集群
3.支持导入覆盖索引名称(目前只支持单个索引导入的情况下可指定)
4.支持index setting和mapping的同步(相关es大版本,2.x和5.0之间不支持)
5.支持dump到本地文件
6.支持从dump文件加载导入到指定索引
 
欢迎测试!
 
#copy index index_name from 192.168.1.x to 192.168.1.y:9200
./bin/esm -s http://192.168.1.x:9200 -d http://192.168.1.y:9200 -x index_name -w=5 -b=10 -c 10000

#copy index src_index from 192.168.1.x to 192.168.1.y:9200 and save with dest_index
./bin/esm -s http://localhost:9200 -d http://localhost:9200 -x src_index -y dest_index -w=5 -b=100

#support Basic-Auth
./bin/esm -s http://localhost:9200/ -x "src_index" -y "dest_index" -d http://localhost:9201 -n admin:111111

#copy settings and override shard size
./bin/esm -s http://localhost:9200/ -x "src_index" -y "dest_index" -d http://localhost:9201 -m admin:111111 -c 10000 --shards=50 --copy_settings

#copy settings and mapping, recreate target index, add query to source fetch, refresh after migration
./bin/esm -s http://localhost:9200/ -x "src_index" -q=query:phone -y "dest_index" -d http://localhost:9201 -c 10000 --shards=5 --copy_settings --copy_mapping --force --refresh

#dump elasticsearch documents into local file
./bin/esm -s http://localhost:9200 -x "src_index" -m admin:111111 -c 5000 -b -q=query:mixer --refresh -o=dump.bin

#loading data from dump files, bulk insert to another es instance
./bin/esm -d http://localhost:9200 -y "dest_index" -n admin:111111 -c 5000 -b 5 --refresh -i=dump.bin

 

[尊重社区原创,转载请保留或注明出处]
本文地址:http://elasticsearch.cn/article/78


135 个评论

我用了一下,没有反应诶.
我是ubuntu14.04,直接下的和build的我都试了一下,都没有什么反应
medcl

medcl 回复 stab

build出来的运行不了么?
stab

stab 回复 medcl

对,build出来的也没有响应
medcl

medcl 回复 stab

奇怪,看来我的找个Ubuntu试试了
stab

stab 回复 medcl

麻烦了,可用的话,请通知我
medcl

medcl 回复 stab

试试这个:https://github.com/medcl/elasticsearch-migration/releases/tag/v0.3.0
你自己用go写的?
基于某个开源项目,改了改
stab

stab 回复 medcl

行了
测试,从centos上elasticsearch1.4.1服务器迁移到本地windows下elaticsearch2.3.1。迁移完成。Bulk 388841 / 388841 [==========================================================================================================================] 100.00% 2m49s。但实际写入的数据只有161418条."count": 161418。
medcl

medcl 回复 aiolos

迁移的一个索引么,控制台有报错么?
请问@medcl
-q, --query= query against source elasticsearch instance, filter data before migrate, ie: name:medcl
支持DSL吗?
不支持
更新V0.3.1,支持 sliced scroll, 下载:https://github.com/medcl/elasticsearch-migration/releases
两个问题汇报一下
1 最新代码, ubuntu 14.04 下  make all-platform 生成结果,启动自动退出
2 解析 epoch_millis 字段时候,会中 json.Unmarshal 的坑,吧 int64 变成科学计数法字符串,改成 Decoder.UseNumber 好一些
@ipush 多谢,第一个问题暂时不好解决,好像 Travis 上面也有这个问,第二个问题具体是那个地方,会影响数据迁移么,unmarshal 应该只是打印返回结果有用
[root@test-master-01 ~/jianxinyu/elasticsearch-migration]# make build
rm -rif bin
mkdir bin
get Dependencies
go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/jianxinyu/elasticsearch-migration:"
GORACE=""
GOROOT="/usr/lib64/golang"
GOTOOLDIR="/usr/lib64/golang/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
go get gopkg.in/cheggaaa/pb.v1


停在这一步没反应了... ...
请问知道是为什么吗
[m@test-master-01 /root/jianxinyu]$ bin/linux64/esm
[m@test-master-01 /root/jianxinyu]$

执行命令bin/linux64/esm,没有反应,麻烦看下
自己编译的还是下载的版本
用这个 https://github.com/medcl/elasticsearch-migration/releases/tag/v0.3.2 试试
[root@test-master-01 ~/jianxinyu]# curl -L -O https://github.com/medcl/elasticsearch-migration/releases/download/v0.3.2/linux64.tar.gz
[root@test-master-01 ~/jianxinyu]# tar -zxvf linux64.tar.gz
[root@test-master-01 ~/jianxinyu]# bin/linux64/esm
[root@test-master-01 ~/jianxinyu]# bin/linux64/esm -s
[root@test-master-01 ~/jianxinyu]# bin/linux64/esm -xxx
[root@test-master-01 ~/jianxinyu]#

都没有什么反应(我的系统是CentOS 6.2)
请教下,这个是为什么呢
这个操作是,自己编译的
这个操作是,自己编译的
windows下执行32位的
C:\Users\jianxinyu\Desktop\elasticsearch-migration\bin\windows32>esm.exe
和64位的
C:\Users\jianxinyu\Desktop\elasticsearch-migration\bin\windows64>esm.exe

敲回车后,都没有什么响应,请问有可以用的朋友吗

可否麻烦先帮忙看下linux下为何不能用吧(我的linux环境是Centos 6.2,下载的版本是linux64.tar.gz),万分感谢
@medcl 可否方便留下你的联系方式,交流方便些
qq 78843382,你下载的那个版本啊? 我发你我本地打包的吧。
@medcl 您好,我这边执行bin/linux64/esm,无响应的问题,给你的qq邮箱发了封邮件。麻烦有空时看下,谢谢了
(我的联系方式,手机:18588497759,QQ:1152596901)
嗯,回复了, Travis 打的包有问题
medcl

medcl 回复 medcl

貌似 Ubuntu 打的就有问题,我的mac下打的都正常,有其他平台的可以帮忙试试
谢谢了。
我这边在测试大数据量操作时,数据导出失败,我再测试整理下,有问题的话,再请教交流。
可以先导出到本地文件,目标 es 是不是撑不住这么多数据,还是配置不合理?
谢谢@medcl ,
我自己用Java写了个工具,先把ES中的数据导出到本地文件,再把文件导入到另外的ES集群。
这个工具也支持导入到本地文件再导出的啦
@medcl 识别不到.kibana索引 要如何写才能识别到
./esm -s http://192.168.1.3:9200 -d http://192.168.1.4:9200 -x .kibana -w=5 -b=10 -c 10000 我是这样写的,其他索引都是这样写然后成功迁移了 但是这个不行 报[main.go:375,main] index not exists,.kibana
medcl

medcl 回复 mivlnd

参数启用就行了:
-a, --all copy indexes starting with . and _ (false)
请问一下,这个在centos上可以用吗?
medcl

medcl 回复 jdjwxj

可以啊
你好,我分别用了0.3.3和0.3.2编译都正常了,但是执行的时候,没有任何反映
./bin/esm -s http://10.10.104.145:9200 -d http://10.10.106.91:9200 -x hn-alarm-20170118 -w=5 -b=10 -c=10000
这是我的命令
medcl

medcl 回复 jdjwxj

在那个平台编译的啊?貌似Ubuntu平台编译出来的包有问题,我在mac下没问题。release下面有可以直接下载运行的,你试过么?
medcl

medcl 回复 medcl

https://github.com/medcl/elasticsearch-migration/releases/tag/v0.3.3
请问支持es2.x到5.x的数据迁移么
支持
为什么我的output一直是0%啊?
是要先在目标服务器上创建索引吗?还是说会自动创建?
medcl

medcl 回复 DimonHo

支持自动创建,同步配置,参数: --copy_settings --copy_mapping
你好 @medcl 大神
现在支持2.x 到 5.x 的index setting和mapping的同步吗?
你好 @medcl 大神
现在用的是2.3.4 有300G数据,怎么能快速升级到5.5.x版本?请赐教
medcl

medcl 回复 trump

es 直接指出 reindex,也可以试试,300G 应该很快。
medcl

medcl 回复 medcl

指出->支持
DimonHo

DimonHo 回复 medcl

加上参数报错 unknown flag `copy_mapping'
没有使用 --copy_settings --copy_mapping 直接使用了-a参数,成功了,但是发现导入过去之后缺失了2条数据,为什么?
medcl

medcl 回复 DimonHo

refresh之后呢?也缺么?
DimonHo

DimonHo 回复 medcl

确实是缺两条数据。另一个有1300多W条数据的,迁移过来后只有60多W条。 我是从1.2.1迁移到5.5.1
medcl

medcl 回复 DimonHo

有报错信息么?
DimonHo

DimonHo 回复 medcl

没报错信息,最后进度条停止在99%。
medcl

medcl 回复 DimonHo

看来有没有测到的 bug
esm.exe /s http://10.163.175.98:9200 /d http://10.163.175.97:9200 /x '/ngr_lft-test/test50nightly' /w 5 /b 10 /c 1000
[10-09 21:30:33] [ERR] [main.go:100,main] No handler found for uri [/'/ngr_lft-test/test50nightly'/_search?scroll=1m&search_type=scan&size=1000] and method [POST]

windows 的 迁移 老报这个错 是什么原因
medcl

medcl 回复 Evan C

参数看起来不对啊,为什么要加 / 和 单引号
Evan C

Evan C 回复 medcl

:\Users\ECUNEVA\bin\windows32>esm.exe /s http://10.163.175.98:9200 /d http://10.163.175.97:9200 /x ngr_lft-60degree/60dtest /w 5 /b 10 /c 1000
Scroll 0 / 205 [---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------] 0.00%
Scroll 205 / 205 [==================================================================================================================================================================================================================================================================================] 100.00% 1s
Scroll 205 / 205 [==================================================================================================================================================================================================================================================================================] 100.00% 1s
Output 0 / 205 [--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------] 0.00%
参数修改了, 执行后一直卡在这不动了,linux 和windows都试了,都同样这个问题,麻烦帮看看,谢谢
medcl

medcl 回复 Evan C

从什么版本到什么版本?
Evan C

Evan C 回复 medcl

从2.3.3 到5.5.2 ,这样可以吗?
为啥从一个索引导入到另一个索引,doc中少了两个key:value键值对啊?
版本为:esm-0.3.4.zip,直接下载使用的
我也发现缺少记录,直接下载的v0.3.4 linux64.tar.gz,在centos6.6执行

src_index有208条记录
导出文件src_index.bin 也208条记录
但重新导入会该集群只有156条记录

./esm -s http://10.24.8.XX:9200 -x "src_index" --refresh -o=src_index.bin
./esm -d http://10.24.8.XX:9200 -y "dest_index" --refresh -i=src_index.bin
我也发现缺少记录,直接下载的v0.3.4 linux64.tar.gz,在centos6.6执行

src_index有208条记录
导出文件src_index.bin 也208条记录
但重新导入会该集群只有156条记录

./esm -s http://10.24.8.XX:9200 -x "src_index" --refresh -o=src_index.bin
./esm -d http://10.24.8.XX:9200 -y "dest_index" --refresh -i=src_index.bin
应该是有 mapping 不兼容的异常,异常没有吐出来,这块是个问题,需要修复一下
6.x只支持一个 type,正在看怎么处理一下,打算导出到 index-type 的索引里面,欢迎提交 pr 一起完善
cfc4n

cfc4n 回复 medcl

mark
mark
6.x的支持么
编译后的程序没反应哦,分别在centos 6 和 mac环境编译都不行。 我用过release版本,但是需要加点代码,所以需要自己加些逻辑。我碰到的问题是github下下来的代码编译后都没反应。。。
可以怎么解决呢?

以下方式都不行:
make build 和
go build -gcflags "-N -l" -o ./esm ./*.go
medcl

medcl 回复 ua_long

你的golang开发环境弄好没有,编译其他程序ok么?
可以的,是公司的开发平台。编译其他程序可以用,比如自己业务写的es导入工具
medcl

medcl 回复 ua_long

你可以发一下 go env 的返回内容么?
go version go1.9 linux/amd64

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="xxx"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build872736006=/tmp/go-build"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
medcl

medcl 回复 ua_long

看起来没有什么问题啊,除了我是用 clang 编译,你是 gcc。这个是我的:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/medcl/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/r4/fp0jznxj48z8_g_dcdv1f5lh0000gn/T/go-build959804095=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
query似乎有问题,已在github上提交issue。
urlencode一下查询条件试试
还是不行。
[04-02 15:04:23] [ERR] [main.go:346,main] server error: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"unknown setting [index.provided_name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings"}],"type":"illegal_argument_exception","reason":"unknown setting [index.provided_name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings"},"status":400}
[04-02 15:04:23] [ERR] [v0.go:240,UpdateIndexMapping] http://x.x.x.x:9200/production_shop_nginx_err-2018.04.01/production_shop_nginx_err/_mapping
[04-02 15:04:23] [ERR] [v0.go:241,UpdateIndexMapping]
[04-02 15:04:23] [ERR] [v0.go:242,UpdateIndexMapping] server error: {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"production_shop_nginx_err-2018.04.01","index_uuid":"_na_","index":"production_shop_nginx_err-2018.04.01"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"production_shop_nginx_err-2018.04.01","index_uuid":"_na_","index":"production_shop_nginx_err-2018.04.01"},"status":404}
panic: server error: {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"production_shop_nginx_err-2018.04.01","index_uuid":"_na_","index":"production_shop_nginx_err-2018.04.01"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"production_shop_nginx_err-2018.04.01","index_uuid":"_na_","index":"production_shop_nginx_err-2018.04.01"},"status":404}

goroutine 1 [running]:
main.(*ESAPIV0).UpdateIndexMapping(0xc423130810, 0xc423117770, 0x24, 0xc423130f60, 0xc421a81d40, 0xc423211eb8)
/Users/medcl/BTSync/github/elasticsearch-migration/v0.go:243 +0x9ad
main.(*ESAPIV5).UpdateIndexMapping(0xc423130810, 0xc423117770, 0x24, 0xc423130f60, 0xc421a81dc8, 0xc4231421a0)
/Users/medcl/BTSync/github/elasticsearch-migration/v5.go:59 +0x4b
main.main()
/Users/medcl/BTSync/github/elasticsearch-migration/main.go:364 +0x2a1e

请问这个报错是啥意思,我用的这个命令
./esm -s http://172.x.x.x:9200 -d http://x.x.x.x:9200 -x "production_shop_nginx_err-2018.04.01" -y "production_shop_nginx_err-2018.04.01" -w=5 -b=10 -c 10000 --copy_settings --copy_mappings --force --refresh

但是我不加--copy_mappings就可以迁移成功,但是到99%就finish了
@medcl 大神 make build 的时候一直有报错,
# cd .; git clone https://github.com/mattn/go-runewidth /usr/local/src/esm-master/src/github.com/mattn/go-runewidth
Initialized empty Git repository in /usr/local/src/esm-master/src/github.com/mattn/go-runewidth/.git/
error: while accessing https://github.com/mattn/go-runewidth/info/refs

fatal: HTTP request failed
package github.com/mattn/go-runewidth: exit status 128
package golang.org/x/sys/unix: unrecognized import path "golang.org/x/sys/unix" (https fetch: Get https://golang.org/x/sys/unix?go-get=1: dial tcp 216.239.37.1:443: getsockopt: connection timed out)
make: *** [config] Error 1

这个是git链接有问题吗?请解????
这个翻墙的问题不在讨论范围内啊 @9527
elasticsearch-5.4.1 支持么
我看只写了5.0
另外 要把一个实例的所有数据全部迁移到另外一个实例怎么操作呢?
刚用了没几天es 就赶上一个迁移的事情,对es还没啥了解
求大神帮助
自己在cent os7下编译,request.Header("Content-Type", "application/json")出现编译错误.改完编译通过后,只能从es导出索引值,未能导出到本地文件,为什么呢?
但是用你编译好的可执行文件导出时,可以导到本地.
自己编译后的执行完只是打印出了索引值,并无实际文件写入到本地.
现在能支持2.0到6.0的数据迁移吗?
6.0的已经支持,https://github.com/medcl/esm/releases/tag/v0.4.1
请问这个工具是否支持全量导出和导入,就是不指定索引的那种,将A集群的数据导入B集群,请问参数应该怎么写,还有就是导的时候结构会随之导出导入么?还是要在新的环境里面先建立结构呢?
medcl

medcl 回复 ryf3064

两边版本一致的话,支持在线的导入 setting 和 mapping,暂时不支持配置信息导出到文件,支持全量数据。可以先设置好 mapping 再导入数据。
func Request(method string, r string, auth *Auth, body *bytes.Buffer, proxy string) (string, error) 方法,每次new的http.Client,并且没有设置KeepAlive,超时这些,会导致ES集群服务的tcp连接用完的情况,麻烦大神能否改一下。
你用的那个版本啊?好像0.4 fix了啊
我试一试,还有个问题,就是这个工具很吃内存,普通几千万上亿条数据,esm能吃几个G,如果是导几亿十几亿,内存有时能暴涨20多个G
看来有 bug 啊,待我来撸一把,需要你来帮我测试一下。
刚刚试了一下,最新版本的tcp链接用完的问题没有了,但是吃内存问题一样,我这边目前导入7亿条,吃了21G的内存,服务器快整蹦了
最后自己蹦了,报错如下:

net/http.(*persistConn).writeLoop(0xc58b5da000)
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1822 +0x14b
created by net/http.(*Transport).dialConn
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1238 +0x97f

goroutine 579 [IO wait]:
internal/poll.runtime_pollWait(0x7f041a7a56e0, 0x72, 0xc424a859a8)
/usr/local/Cellar/go/1.10.2/libexec/src/runtime/netpoll.go:173 +0x57
internal/poll.(*pollDesc).wait(0xc5770a8418, 0x72, 0xffffffffffffff00, 0x813aa0, 0x98c260)
/usr/local/Cellar/go/1.10.2/libexec/src/internal/poll/fd_poll_runtime.go:85 +0x9b
internal/poll.(*pollDesc).waitRead(0xc5770a8418, 0xc5e8a62000, 0x1000, 0x1000)
/usr/local/Cellar/go/1.10.2/libexec/src/internal/poll/fd_poll_runtime.go:90 +0x3d
internal/poll.(*FD).Read(0xc5770a8400, 0xc5e8a62000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
/usr/local/Cellar/go/1.10.2/libexec/src/internal/poll/fd_unix.go:157 +0x17d
net.(*netFD).Read(0xc5770a8400, 0xc5e8a62000, 0x1000, 0x1000, 0x4515f0, 0xc422a38780, 0x4)
/usr/local/Cellar/go/1.10.2/libexec/src/net/fd_unix.go:202 +0x4f
net.(*conn).Read(0xc4228b8008, 0xc5e8a62000, 0x1000, 0x1000, 0x0, 0x0, 0x0)
/usr/local/Cellar/go/1.10.2/libexec/src/net/net.go:176 +0x6a
net/http.(*persistConn).Read(0xc58b5da000, 0xc5e8a62000, 0x1000, 0x1000, 0xc424a85b98, 0x403355, 0xc55c8922a0)
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1453 +0x136
bufio.(*Reader).fill(0xc61d238180)
/usr/local/Cellar/go/1.10.2/libexec/src/bufio/bufio.go:100 +0x11e
bufio.(*Reader).Peek(0xc61d238180, 0x1, 0x0, 0x0, 0x1, 0xc55c8930e0, 0x0)
/usr/local/Cellar/go/1.10.2/libexec/src/bufio/bufio.go:132 +0x3a
net/http.(*persistConn).readLoop(0xc58b5da000)
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1601 +0x185
created by net/http.(*Transport).dialConn
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1237 +0x95a

goroutine 72 [select]:
net/http.(*persistConn).writeLoop(0xc5f0db97a0)
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1822 +0x14b
created by net/http.(*Transport).dialConn
/usr/local/Cellar/go/1.10.2/libexec/src/net/http/transport.go:1238 +0x97f
请问mac是用linux64.tar.gz 这个产出么?
mac是darwin64.tar.gz
-y参数到底怎么用的?必须在目的集群建了索引才行啊,没建索引的话,虽然也在迁移,但是目的集群根本没有该索引的任何信息,是不是程序的bug?
./esm -s http://xx:9200 -d http://tt:9200 -x idx-usercmtrelated-v2 -w=10 -b=10 -c 20000 --shards=5 --copy_settings --copy_mapping --force --refresh
这条语句执行报错“unknown flag `copy_mapping'”,我这是copy的你的命令行,有什么问题吗?

要回复文章请先登录注册