如何安全备份 Elasticsearch 数据?两种方案任你选

背景

有时候需要对 Elasticsearch 集群进行备份,或者恢复到其他集群。

方案

以下提供两种方案:使用 Elasticsearch 自带的 SNAPSHOT 快照机制,或者利用 elasticsearch-dump 工具完成。

使用快照与恢复功能

将索引数据备份到其他文件存储,恢复时先恢复快照,再恢复索引。

在 elasticsearch.yaml 配置快照存储路径。

yaml 复制代码
path.repo: ["/path/to/snapshot"]

注册快照存储库。

json 复制代码
PUT /_snapshot/my_backup
{
  "type": "fs",
  "settings": {
    "location": "/path/to/snapshot"
  }
}

创建快照。

json 复制代码
# 全量备份
PUT /_snapshot/my_backup/snapshot_cluster?wait_for_completion=true

# 按需备份
PUT /_snapshot/my_backup/snapshot_demo_index?wait_for_completion=true
{
  "indices": "demo_*",
  "ignore_unavailable": true
  "include_global_state": false.
  "metadata": {
    "author": "mengxiangge",
    "description": "backup before reindex"
  }
}

恢复快照。

json 复制代码
# 全量恢复
POST /_snapshot/my_backup/snapshot_cluster/_restore

# 按需恢复
POST /_snapshot/my_backup/snapshot_demo_index/_restore

使用 elasticsearch-dump 工具

elasticsearch-dump 是一个开源的命令行工具,用于将 Elasticsearch 索引数据导出为 JSON 文件,或将 JSON 文件导入 Elasticsearch 中。项目地址可以查阅链接。

假设源数据节点为 10.2.0.1:9200,迁移到 10.2.1.1:9200。

bash 复制代码
# 迁移 Analyzer、Settings、Mapping
elasticdump --input=http://10.2.0.1:9200/my_index --output=http://10.2.1.1:9200/my_index --type=analyzer
elasticdump --input=http://10.2.0.1:9200/my_index --output=http://10.2.1.1:9200/my_index --type=settings
elasticdump --input=http://10.2.0.1:9200/my_index --output=http://10.2.1.1:9200/my_index --type=mapping

# 迁移数据
elasticdump --input=http://10.2.0.1:9200/my_index --output=http://10.2.1.1:9200/my_index --type=data
相关推荐
yukai080083 小时前
【203篇系列】056 我的Agent系统
大数据·elasticsearch·搜索引擎
IT大白鼠3 小时前
搜索系列 · 第 08 篇——面试收官:高频题与全景总结
elasticsearch·面试·nosql
光影少年12 小时前
langchain与langgraph区别以及学习路线
elasticsearch·langchain·llm
Elasticsearch13 小时前
错误最多的服务运行正常:使用 ES|QL 从日志进行根因分析
elasticsearch
Elasticsearch13 小时前
将你自己的密钥用于现有 Elastic Cloud 部署
elasticsearch
IT大白鼠1 天前
搜索系列 · 第 06 篇——避坑汇总:经典生产问题
elasticsearch·nosql
IT大白鼠1 天前
搜索系列 · 第 07 篇——调优实战:写入、查询与监控
elasticsearch·nosql
Elasticsearch1 天前
检查 100 个候选项,而不是 1000 万个文档:更快速的 Elasticsearch kNN 过滤
elasticsearch
Elasticsearch1 天前
当 AI agent 群集出现时,银行能跟上吗?
elasticsearch
Sayai2 天前
Elasticsearch 日志检索 DSL 实战:时间范围查询、字段去重、分钟级统计与最新日志获取
大数据·运维·elasticsearch·搜索引擎·日志分析