一、实践
需求:将生产环境指定的es索引导入到测试环境
思路:
1、查询prod环境要导入的索引,导入到txt文件
2、删除test环境所有索引(依据实际情况)
3、编写脚本
4、查看进度
步骤:
1、查看索引
查看prod环境索引
curl -s -XGET --user "用户名:密码" -H "Content-Type: application/json" 'es地址/_cat/indices?v' |grep ka_budget_spu-
保存索引名
curl -s -XGET --user "用户名:密码" -H "Content-Type: application/json" 'es地址/_cat/indices?v' |grep ka_budget_spu- |awk '{print $3}' | sort > prd_index_name.txt
2、删除索引
删除指定索引
curl -XDELETE -u "用户名:密码" 'es地址/索引名称'
删除所有索引
curl -X DELETE 'http://localhost:9200/_all'
# 一般不建议,最好是将要删除的索引放入txt文件,通过for循环遍历删除
3、编写脚本
[root@node01 es]# cat import_pro_to_dev_index.sh
#!/bin/bash
for line in `cat prd_index_name.txt`
do
echo -------- $line --- $uat_index_name ----------------
echo "开始导入 $line -> $uat_index_name [mapping]"
elasticdump --input=https://账户:密码@es地址/$line --output=https://账户:密码@es地址/$uat_index_name --type=mapping
echo "开始导入 $line -> $uat_index_name [data]"
# --limit=20000依据实际情况调整
elasticdump --limit=20000 --input=https://账户:密码@es地址/$line --output=https://账户:密码@es地址/$uat_index_name --type=data
echo "导入结束 $line -> $uat_index_name"
done
查看进度
查看test环境目前导入索引的情况
curl -s -XGET --user "xxx:xxx" -H "Content-Type: application/json" 'https://xxx/_cat/indices?v' |grep ka_budget_spu
推荐:ES 索引查询与删除
二、索引相关操作
2.1、导出索引到文件
elasticdump --input=https://账号:密码@es地址/索引名 --output=./索引名.json
举例:
elasticdump --input=https://mpc:kZfxxxj2xo@es01-prd.xxx.net.cn:9200/mpc_behavior_log --output=./mpc_behavior_log.json
2.2、查看索引
curl -s -XGET --user "账号:密码" -H "Content-Type: application/json" 'es地址/_cat/indices?v'|grep xxx
举例:
curl -s -XGET --user "shmall:EqPexxxxxppe" -H "Content-Type: application/json" 'https://elk-uat.xxxx.net.cn:9200/_cat/indices?v'|grep recipe_dev
2.3、克隆索引
curl -XPOST --user "账号:密码" -H "Content-Type: application/json" -d '{
"source": {
"index": "索引名称"
},
"dest": {
"index": "克隆后的索引名称"
}
}' 'es地址/_reindex'
举例:
curl -XPOST --user "shmall:EqPxxxxxCppe" -H "Content-Type: application/json" -d '{
"source": {
"index": "syj_recipe_dev"
},
"dest": {
"index": "shmall_recipe_dev"
}
}' 'https://elk-uat.xxxx.net.cn:9200/_reindex'