ES数据备份与恢复
1.安装npm环境
# 安装npm(只需要在一个节点安装即可,如果前端还有nginx做反向代理可以每个节点都装)
[root@elkstack01 ~]# yum install -y npm
# 进入下载head插件代码目录
[root@elkstack01 src]# cd /usr/local/
# 从GitHub上克隆代码到本地
[root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
# 克隆完成后,进入elasticsearch插件目录
[root@elkstack01 local]# cd elasticsearch-head/
# 清除缓存
[root@elkstack01 elasticsearch-head]# npm cache clean -f
# 使用npm安装n模块(不同的项目js脚本所需的node版本可能不同,所以就需要node版本管理工具)
2.安装备份工具
[root@db01 ~]# npm install elasticdump -g
3.备份命令
1)备份参数
--input: 数据来源
--output: 接收数据的目标
--type: 导出的数据类型(settings, analyzer, data, mapping, alias, template)
2)备份数据到另一个ES集群
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://100.10.0.51:9200/my_index \
--type=analyzer
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://100.10.0.51:9200/my_index \
--type=mapping
elasticdump --input=http://10.0.0.51:9200/my_index --output=http://100.10.0.51:9200/my_index --type=data
elasticdump \
--input=http://10.0.0.51:9200/my_index \
--output=http://100.10.0.51:9200/my_index \
--type=template
3)备份数据到本地的json文件
elasticdump \
--input=http://10.0.0.51:9200/student \
--output=/tmp/student_mapping.json \
--type=mapping
elasticdump \
--input=http://10.0.0.51:9200/student \
--output=/tmp/student_data.json \
--type=data
4)导出文件打包
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=$ \
| gzip > /data/my_index.json.gz
5)备份指定条件的数据
elasticdump \
--input=http://production.es.com:9200/my_index \
--output=query.json \
--searchBody="{\"query\":{\"term\":{\"username\": \"admin\"}}}"
# 帮助文档:https://github.com/elasticsearch-dump/elasticsearch-dump
4.导入命令
elasticdump \
--input=./student_template.json \
--output=http://10.0.0.51:9200 \
--type=template
elasticdump \
--input=./student_mapping.json \
--output=http://10.0.0.51:9200 \
--type=mapping
elasticdump \
--input=./student_data.json \
--output=http://10.0.0.51:9200 \
--type=data
elasticdump \
--input=./student_analyzer.json \
--output=http://10.0.0.51:9200 \
--type=analyzer
# 恢复数据的时候,如果数据已存在,会覆盖原数据
5.备份脚本
#!/bin/bash
echo '要备份的机器是:'${1}
index_name='
test
student
linux7
'
for index in `echo $index_name`
do
echo "start input index ${index}"
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_analyzer.json --type=analyzer &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_data.json --type=data &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_template.json --type=template &> /dev/null
done
6.导入脚本
#!/bin/bash
echo '要导入的机器是:'${1}
index_name='
test
student
linux7
'
for index in `echo $index_name`
do
echo "start input index ${index}"
elasticdump --input=/data/${index}_alias.json --output=http://${1}:9200/${index} --type=alias &> /dev/null
elasticdump --input=/data/${index}_analyzer.json --output=http://${1}:9200/${index} --type=analyzer &> /dev/null
elasticdump --input=/data/${index}_data.json --output=http://${1}:9200/${index} --type=data &> /dev/null
elasticdump --input=/data/${index}_template.json --output=http://${1}:9200/${index} --type=template &> /dev/null
done
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络