elasticsearch索引数据备份与恢复

elasticsearch备份与恢复

1.配置备份文件目录配置

在 config/elasticsearch.yml 文件中加入如下配置:

shell 复制代码
# 配置单个备份文件目录
path.repo: ["/backups/my_backup"]

# 配置多个备份文件目录
path.repo: 
  - "/backups/my_backup"
  - "/backups/fx_backup"

配置完成后需重启es服务。

2.创建备份仓库并查看

shell 复制代码
curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/仓库名' -u 用户名:密码 -d '{"type": "fs","settings": {"location":"备份仓库路径","compress": true}}'

# 在/backups/my_backup下新建名为my_backup的仓库
curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/my_backup' -u elastic:123456 -d '{"type": "fs","settings": {"location":"/backups/my_backup","compress": true}}'

# 在/backups/fx_backup下新建名为fx_backup的仓库
curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/fx_backup' -u elastic:123456 -d '{"type": "fs","settings": {"location":"/backups/fx_backup","compress": true}}'

# 查看仓库信息
curl -XGET 'http://127.0.0.1:9200/_snapshot?pretty' -u elastic:123456

# 返回的仓库信息
{
  "my_backup" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",
      "location" : "/backups/my_backup"
    }
  },
  "fx_backup" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",
      "location" : "/backups/fx_backup"
    }
  }
}

# 删除备份仓库
# curl -X DELETE "localhost:9200/_snapshot/仓库名" -u elastic:123456
# curl -X DELETE "localhost:9200/_snapshot/my_backup" -u elastic:123456

3.备份索引数据

shell 复制代码
# 备份单个索引数据
# curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/仓库名/备份名' -d '{"indices": "索引名"}' -u 用户名:密码

# 备份多个索引数据
# curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/仓库名/备份名' -d '{"indices": "索引名1,索引名2,索引名3"}' -u 用户名:密码
curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/my_backup/my_test_2025' -d '{"indices": "my_test_1,my_test_2"}' -u elastic:123456

curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/fx_backup/fx_test_2025?wait_for_completion=true' -d '{"indices": "fx_test_1,fx_test_2"}' -u elastic:123456
# wait_for_completion=true 是指该api在备份执行完毕后再返回结果,否则默认是异步执行的。线上执行时不用设置该参数,让其在后台异步执行即可。

# 备份全部
# curl -XPUT -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/仓库名/备份名' -u 用户名:密码

# 查看备份的索引
# curl -XGET 'http://127.0.0.1:9200/_snapshot?pretty' -u 用户名:密码

4.恢复备份索引数据

shell 复制代码
# 恢复索引前要确保目标es集群中没有存在相同的索引名,或者恢复时重命名索引名。
# 删除索引语法(支持通配符):
# curl -XDELETE "http://127.0.0.1:9200/索引名" -u 用户名:密码

# 恢复单个索引数据
# curl -XPOST -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/仓库名/备份名/_restore' -d '{"indices": "索引名"}' -u 用户名:密码
curl -XPOST -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/my_backup/my_test_2025/_restore' -d '{"indices": "my_test_1"}' -u elastic:123456

# 恢复多个索引数据
curl -XPOST -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/fx_backup/fx_test_2025/_restore' -d '{"indices": "fx_test_1,fx_test_2"}' -u elastic:123456

# 恢复索引并重命名索引名
# curl -XPOST -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/my_backup/备份名/_restore' -d '{"indices": "原索引名","rename_pattern": "原索引名","rename_replacement": "新索引名"}' -u 用户名:密码


# 查看恢复状态:
# curl -XGET "http://127.0.0.1:9200/_snapshot/仓库名/备份名/_status" -u 用户名:密码
curl -XGET "http://127.0.0.1:9200/_snapshot/fx_backup/fx_test_2025/_status" -u elastic:123456

#即只恢复指定索引名数据,下面以索引名为fx_*开头的索引数据为例,注意不要恢复".security-6"的索引,这个索引记录了用户认证信息,不能被删除
# curl -XPOST -H 'Content-Type: application/json' 'http://127.0.0.1:9200/_snapshot/my_backup/snapshot_all/_restore' -d '{"indices": "fx_*"}'  -u 用户名:密码


# 查看索引名:

# curl http://127.0.0.1:9200/_cat/indices?v -u 用户名:密码

curl http://127.0.0.1:9200/_cat/indices?v -u elastic:123456
  • 参数说明:
    • indices:只恢复匹配的索引,忽略快照中存在的其他索引,可支持通配符。
    • rename_pattern:查找所提供的模式能匹配上的正在恢复的索引。用来正则匹配要恢复的索引,并且重命名。
    • rename_replacement:将匹配的索引重命名成替代的模式。用来正则匹配要恢复的索引,并且重命名。

注:由于es创建的用户信息会存放在.security-6索引中(不能删除),所以在恢复时要排除该索引名的恢复。

相关推荐
腻害兔40 分钟前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
GIR1231 小时前
官方出品 | 多通道土壤呼吸测量系统市场现状与十五五规划深度报告:行业分析+趋势预测全收录
大数据·人工智能·机器学习
南讯股份Nascent2 小时前
洽洽全域会员项目启动会圆满召开
大数据·人工智能
大郭鹏宇3 小时前
基于 LangGraph 构建智能分诊系统(一):项目概述与环境搭建
大数据·人工智能·microsoft·langchain
阿里云大数据AI技术6 小时前
阿里云 ES AI 引擎版:面向 Agent 场景,为亿级租户、千亿规模向量设计的搜索引擎
人工智能·elasticsearch·agent
天天爱吃肉82186 小时前
【电源拆解:跟着问答逐一认识开关适配器PCB元器件】
大数据·人工智能·python·功能测试·嵌入式硬件·汽车
阿里云大数据AI技术6 小时前
FalconSeek 技术解析:阿里云 Elasticsearch 云原生内核如何让查询性能飙升600%
人工智能·elasticsearch
Elastic 中国社区官方博客7 小时前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
大数据·人工智能·elasticsearch·搜索引擎·容器·kubernetes·grafana
abcy0712137 小时前
storm核心实时机制
大数据·storm
灵机一物7 小时前
合伙制律所分红和提成律师个税怎么合规优化?
大数据·人工智能