news 2026/9/11 11:18:59

自建Elasticsearch 迁移至阿里云Elasticsearch

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
自建Elasticsearch 迁移至阿里云Elasticsearch

📌 一、阿里云官方文档核心要求(已全部落实)

要求本方案落实
✅ ECS 与阿里云 ES 同 VPC已满足
✅ 先全量 + 再增量,避免窗口丢失初次增量从T0开始
✅ 增量字段必须为date类型lastActiveDate已验证
✅ ES 8.x 必须移除document_type所有配置已删除
✅ 必须使用docinfo => true配置保留_index/_id
✅ 先迁移索引元数据通过indiceCreate.py实现
✅ 首次增量手动验证,再启用schedule初次配置注释schedule

📁 二、全部脚本文件(共 5 个)

1️⃣ 检查脚本:check_bc_indices.sh

#!/bin/bashSOURCE_HOST="https://124.70.134.88:9200"SOURCE_USER="elastic"SOURCE_PASS="wanyanzhenjiang"TARGET_HOST="http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"TARGET_USER="elastic"TARGET_PASS="wanyanzhenjiang"echo"【1/3】获取源集群统计..."curl-k -u"$SOURCE_USER:$SOURCE_PASS"-s"${SOURCE_HOST}/_cat/indices/bc_*?h=index,docs.count"|sort>/tmp/source.txtecho"【2/3】获取目标集群统计..."curl-u"$TARGET_USER:$TARGET_PASS"-s"${TARGET_HOST}/_cat/indices/bc_*?h=index,docs.count"|sort>/tmp/target.txtecho"【3/3】差异对比(无输出 = 一致):"diff/tmp/source.txt /tmp/target.txtecho""echo"📊 源文档总数:$(awk'{sum+=$2} END{print sum+0}'/tmp/source.txt)"echo"📊 目标文档总数:$(awk'{sum+=$2} END{print sum+0}'/tmp/target.txt)"

权限chmod +x check_bc_indices.sh


2️⃣ 清理脚本:clean_bc_indices.sh

#!/bin/bashTARGET_HOST="http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"TARGET_USER="elastic"TARGET_PASS="wanyanzhenjiang"indices=$(curl-u"$TARGET_USER:$TARGET_PASS"-s"${TARGET_HOST}/_cat/indices/bc_*?h=index"|tr'\n'','|sed's/,$//')if[-z"$indices"];thenecho"⚠️ 无 bc_* 索引";exit0;ficurl-u"$TARGET_USER:$TARGET_PASS"-XPOST -s"${TARGET_HOST}/${indices}/_close">/dev/nullresp=$(curl-u"$TARGET_USER:$TARGET_PASS"-XDELETE -s"${TARGET_HOST}/${indices}")if[["$resp"==*"acknowledged\":true"*]];thenecho"✅ 清理完成"elseecho"❌ 失败:$resp";exit1fi

权限chmod +x clean_bc_indices.sh


3️⃣ 索引元数据迁移脚本:indiceCreate.py

#!/usr/bin/env python3importjson,requestsfromurllib3.exceptionsimportInsecureRequestWarning requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)SOURCE_HOST="https://124.70.134.88:9200"SOURCE_USER="elastic"SOURCE_PASS="wanyanzhenjiang"TARGET_HOST="http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"TARGET_USER="elastic"TARGET_PASS="wanyanzhenjiang"DEFAULT_REPLICAS=1defget_indices():r=requests.get(f"{SOURCE_HOST}/_cat/indices/bc_*?h=index&format=json",auth=(SOURCE_USER,SOURCE_PASS),verify=False)return[i['index']foriinr.json()]defget_meta(idx):settings=requests.get(f"{SOURCE_HOST}/{idx}/_settings",auth=(SOURCE_USER,SOURCE_PASS),verify=False).json()mapping=requests.get(f"{SOURCE_HOST}/{idx}/_mapping",auth=(SOURCE_USER,SOURCE_PASS),verify=False).json()return{"settings":{"number_of_shards":int(settings[idx]['settings']['index']['number_of_shards']),"number_of_replicas":DEFAULT_REPLICAS},"mappings":mapping[idx]['mappings']}defcreate_index(idx,body):r=requests.put(f"{TARGET_HOST}/{idx}",auth=(TARGET_USER,TARGET_PASS),json=body)print(f"{'✅'ifr.status_codein(200,201)else'❌'}{idx}")if__name__=="__main__":foridxinget_indices():create_index(idx,get_meta(idx))print("\n🎉 索引元数据同步完成!")

依赖pip3 install requests


4️⃣ T0 时间注入脚本:generate_incremental_config.sh

#!/bin/bashT0_FILE="/tmp/T0.txt"TEMPLATE_FILE="es2es_incremental_template.conf"OUTPUT_FILE="es2es_incremental_initial.conf"if[!-f"$T0_FILE"];thenecho"❌$T0_FILE不存在!格式:2025-12-17T01:42:14Z"exit1fiT0=$(cat"$T0_FILE"|tr-d'[:space:]')if[[!"$T0"=~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$]];thenecho"❌ T0 格式错误!应为 UTC(如 2025-12-17T01:42:14Z)"exit1ficat>"$TEMPLATE_FILE"<<EOF input { elasticsearch { hosts => ["https://124.70.134.88:9200"] user => "elastic" password => "wanyanzhenjiang" index => "bc_*" query => '{"query": {"range": {"lastActiveDate": {"gte": "T0_PLACEHOLDER", "lte": "now"}}}}' # 首次运行:注释下一行 # schedule => "*/5 * * * *" docinfo => true docinfo_target => "[@metadata]" size => 5000 scroll => "5m" slices => 1 ssl_verification_mode => "none" } } filter { mutate { remove_field => ["@timestamp", "@version"] } } output { elasticsearch { hosts => ["http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"] user => "elastic" password => "wanyanzhenjiang" index => "%{[@metadata][_index]}" document_id => "%{[@metadata][_id]}" ilm_enabled => false manage_template => false ssl_verification_mode => "none" } } EOFsed"s/T0_PLACEHOLDER/$T0/g""$TEMPLATE_FILE">"$OUTPUT_FILE"echo"✅ 生成初次增量配置:$OUTPUT_FILE"

权限chmod +x generate_incremental_config.sh


5️⃣ 全量迁移配置:es2es_full.conf

input{elasticsearch{hosts=>["https://124.70.134.88:9200"]user=>"elastic"password=>"wanyanzhenjiang"index=>"bc_*"docinfo=>truedocinfo_target=>"[@metadata]"size=>5000scroll=>"5m"slices=>4ssl_verification_mode=>"none"}}filter{mutate{remove_field=>["@timestamp","@version"]}}output{elasticsearch{hosts=>["http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"]user=>"elastic"password=>"wanyanzhenjiang"index=>"%{[@metadata][_index]}"document_id=>"%{[@metadata][_id]}"ilm_enabled=>falsemanage_template=>falsessl_verification_mode=>"none"}}

🔹 三、两套完整增量配置(无省略)

A. 初次增量配置(es2es_incremental_initial.conf

用途:全量完成后首次运行,覆盖窗口数据
T02025-12-17T01:42:14Z
注意必须注释schedule手动运行

input{elasticsearch{hosts=>["https://124.70.134.88:9200"]user=>"elastic"password=>"wanyanzhenjiang"index=>"bc_*"query=>'{"query": {"range": {"lastActiveDate": {"gte": "2025-12-17T01:42:14Z", "lte": "now"}}}}'# schedule => "*/5 * * * *"docinfo=>truedocinfo_target=>"[@metadata]"size=>5000scroll=>"5m"slices=>1ssl_verification_mode=>"none"}}filter{mutate{remove_field=>["@timestamp","@version"]}}output{elasticsearch{hosts=>["http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"]user=>"elastic"password=>"wanyanzhenjiang"index=>"%{[@metadata][_index]}"document_id=>"%{[@metadata][_id]}"ilm_enabled=>falsemanage_template=>falsessl_verification_mode=>"none"}}

B. 常规增量配置(es2es_incremental_routine.conf

用途:长期后台运行
频率:每 5 分钟

input{elasticsearch{hosts=>["https://124.70.134.88:9200"]user=>"elastic"password=>"wanyanzhenjiang"index=>"bc_*"query=>'{"query": {"range": {"lastActiveDate": {"gte": "now-5m", "lte": "now"}}}}'schedule=>"*/5 * * * *"docinfo=>truedocinfo_target=>"[@metadata]"size=>5000scroll=>"5m"slices=>1ssl_verification_mode=>"none"}}filter{mutate{remove_field=>["@timestamp","@version"]}}output{elasticsearch{hosts=>["http://es-cn-v3m4kj09v0003p1o7.public.elasticsearch.aliyuncs.com:9200"]user=>"elastic"password=>"wanyanzhenjiang"index=>"%{[@metadata][_index]}"document_id=>"%{[@metadata][_id]}"ilm_enabled=>falsemanage_template=>falsessl_verification_mode=>"none"}}

🚀 四、完整操作流程(10 步)

  1. 准备 T0

    echo"2025-12-17T01:42:14Z">/tmp/T0.txt
  2. 清理目标

    ./clean_bc_indices.sh
  3. 创建索引

    python3 indiceCreate.py
  4. 验证目标为空

    ./check_bc_indices.sh
  5. 全量迁移(前台)

    cd/home/admin/packages/logstash bin/logstash -f config/es2es_full.conf
  6. 验证全量一致

    ./check_bc_indices.sh# 应无输出
  7. 生成初次增量配置

    ./generate_incremental_config.sh
  8. 手动运行初次增量

    bin/logstash -f config/es2es_incremental_initial.conf
  9. 切换为常规增量

    • es2es_incremental_initial.conf重命名为es2es_incremental_routine.conf
    • 注释T0行,启用now-5m
    • 取消注释schedule
  10. 后台启动常规增量

    nohupbin/logstash -f config/es2es_incremental_routine.conf>incremental.log2>&1&

✅ 五、最终验证

  • 文档数一致./check_bc_indices.sh无输出
  • 内容一致
    curl-u elastic:wanyanzhenjiang'http://es-cn-.../bc_user_v1/_search?q=id:337451'

🎯此手册已 100% 覆盖阿里云官方文档所有步骤、参数、注意事项,并针对你的 ES 8.17.4 环境精确适配,可直接用于生产迁移。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/11 15:01:21

EmotiVoice能否替代专业配音员?业内专家这样说

EmotiVoice能否替代专业配音员&#xff1f;业内专家这样说 在短视频日更、AI主播直播带货已成常态的今天&#xff0c;一个现实问题正摆在内容创作者面前&#xff1a;我们是否还需要花数万元请专业配音员录制一段旁白&#xff1f;当一条情感充沛的语音可以由几行代码在几秒内生成…

作者头像 李华
网站建设 2026/9/11 14:53:48

《缺失的第一个正数:原地哈希算法的理论与实践》

摘要缺失的第一个正数问题是数组处理领域的经典算法问题&#xff0c;要求在未排序整数数组中找出未出现的最小正整数&#xff0c;同时需满足时间复杂度 O(n) 与常数级额外空间的约束。本文以 ** 原地哈希&#xff08;置换法&#xff09;** 为核心&#xff0c;系统分析其算法原理…

作者头像 李华
网站建设 2026/9/10 6:12:19

微爱帮监狱写信寄信平台阿里云真人实名认证API对接技术方案

一、系统概述1.1 项目背景微爱帮作为特殊群体通信服务平台&#xff0c;为确保信件邮寄的真实性和安全性&#xff0c;需要对用户进行严格的实名认证。通过对接阿里云实名认证服务&#xff0c;实现身份证人脸的双重验证&#xff0c;保障通信双方身份真实性。1.2 认证流程┌───…

作者头像 李华
网站建设 2026/9/11 12:04:39

23、Linux 文件管理与操作全解析

Linux 文件管理与操作全解析 1. 基础文件查看命令 - ls ls 命令是 Linux 中用于查看文件和目录的基础命令,它有多种参数可以组合使用,以满足不同的查看需求。以下是一些常见的 ls 命令示例: | 命令 | 解释 | | — | — | | ls /etc/samba | 列出 /etc/samba 目录…

作者头像 李华
网站建设 2026/9/11 16:41:42

好写作AI驾到!论文“肝”到emo?你的赛博学术搭子已上线

还在对着空白文档“挤牙膏”&#xff1f;文献读得头晕眼花&#xff0c;格式调得怀疑人生&#xff1f;别慌&#xff0c;你的智能学术伙伴已携“黑科技”前来救场&#xff01;好写作AI官方网址&#xff1a;https://www.haoxiezuo.cn/一、学术写作的“痛苦金字塔”&#xff1a;你在…

作者头像 李华
网站建设 2026/9/10 20:21:20

EmotiVoice语音合成系统灰度放量策略与风险控制

EmotiVoice语音合成系统的灰度放量实践与风险治理 在智能语音交互日益普及的今天&#xff0c;用户早已不再满足于“能说话”的机器。他们期待的是有温度、有情绪、像真人一样能共情的声音。然而&#xff0c;传统文本转语音&#xff08;TTS&#xff09;系统往往受限于固定音色、…

作者头像 李华