当前位置:   article > 正文

es数据备份和迁移Elasticsearch_es备份

es备份

Elasticsearch数据备份与恢复

前提

  1. # 注意:
  2. 1.在进行本地备份时使用--type需要备份索引和数据(mapping,data
  3. 2.在将数据备份到另外一台ES节点时需要比本地备份多备份一种数据类型(analyzer,mapping,data,template)

一.本地备份与恢复

  1. # 前提:
  2. · 必须要有Node环境和npm软件:nodejs,npm
  3. 1.:下载包
  4. wget https://nodejs.org/dist/v14.17.1/node-v14.17.1-linux-x64.tar.xz
  5. 2.:安装包(如果本身有node包但是不是新版本建议卸载重装)
  6. xz -d node-v14.17.1-linux-x64.tar.xz
  7. tar -xvf node-v14.17.1-linux-x64.tar
  8. echo "export NODE_HOME=/home/workspaces/node-v14.17.1-linux-x64
  9. export PATH=$PATH:$NODE_HOME/bin
  10. export NODE_PATH=$NODE_HOME/lib/node_modules" >>/etc/profile
  11. source /etc/profile
  12. 3.:验证安装是否成功
  13. node -v
  14. 4.:设置淘宝镜像
  15. npm config set registry http://registry.npm.taobao.org
  16. 5.:安装npm(只需要在一个节点安装即可,如果前端还有nginx做反向代理可以每个节点都装)
  17. [root@elkstack01 ~]# yum install -y npm
  18. 6.:进入下载head插件代码目录
  19. [root@elkstack01 src]# cd /usr/local/
  20. 7.:从GitHub上克隆代码到本地(或者直接从找es-head的包)
  21. [root@elkstack01 local]# git clone git://github.com/mobz/elasticsearch-head.git
  22. 8.:克隆完成后,进入elasticsearch插件目录
  23. [root@elkstack01 local]# cd elasticsearch-head/
  24. 9.:清除缓存
  25. [root@elkstack01 elasticsearch-head]# npm cache clean -f
  26. #使用npm安装n模块(不同的项目js脚本所需的node版本可能不同,所以就需要node版本管理工具)
  27. 10.安装elasticdump
  28. [root@db04 ~]# npm install elasticdump -g
  29. 11.本地目录备份
  30. ## 第一次进行的是索引的备份
  31. ## 第二次进行的是数据的备份
  32. [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student/user --output=/tmp/student_mapping.json --type=mapping
  33. Wed, 12 Aug 2020 07:41:59 GMT | starting dump
  34. Wed, 12 Aug 2020 07:41:59 GMT | got 1 objects from source elasticsearch (offset: 0)
  35. Wed, 12 Aug 2020 07:41:59 GMT | sent 1 objects to destination file, wrote 1
  36. Wed, 12 Aug 2020 07:41:59 GMT | got 0 objects from source elasticsearch (offset: 1)
  37. Wed, 12 Aug 2020 07:41:59 GMT | Total Writes: 1
  38. Wed, 12 Aug 2020 07:41:59 GMT | dump complete
  39. [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student/user --output=/tmp/student_data.json --type=data
  40. Wed, 12 Aug 2020 07:42:21 GMT | starting dump
  41. Wed, 12 Aug 2020 07:42:21 GMT | got 8 objects from source elasticsearch (offset: 0)
  42. Wed, 12 Aug 2020 07:42:21 GMT | sent 8 objects to destination file, wrote 8
  43. Wed, 12 Aug 2020 07:42:21 GMT | got 0 objects from source elasticsearch (offset: 8)
  44. Wed, 12 Aug 2020 07:42:21 GMT | Total Writes: 8
  45. Wed, 12 Aug 2020 07:42:21 GMT | dump complete
  46. ##备份之后用Elasticsearch Head插件模拟数据丢失,误删除student索引和数据并用elasticdump恢复
  47. [root@db04 bin]# ./elasticdump --output=http://10.0.0.54:9200/student --input=/tmp/student_mapping.json --type=mapping
  48. Wed, 12 Aug 2020 07:46:21 GMT | starting dump
  49. Wed, 12 Aug 2020 07:46:21 GMT | got 1 objects from source file (offset: 0)
  50. Wed, 12 Aug 2020 07:46:21 GMT | sent 1 objects to destination elasticsearch, wrote 1
  51. Wed, 12 Aug 2020 07:46:21 GMT | got 0 objects from source file (offset: 1)
  52. Wed, 12 Aug 2020 07:46:21 GMT | Total Writes: 1
  53. Wed, 12 Aug 2020 07:46:21 GMT | dump complete
  54. [root@db04 bin]# ./elasticdump --output=http://10.0.0.54:9200/student --input=/tmp/student_data.json --type=data
  55. Wed, 12 Aug 2020 07:46:40 GMT | starting dump
  56. Wed, 12 Aug 2020 07:46:40 GMT | got 8 objects from source file (offset: 0)
  57. Wed, 12 Aug 2020 07:46:40 GMT | sent 8 objects to destination elasticsearch, wrote 8
  58. Wed, 12 Aug 2020 07:46:40 GMT | got 0 objects from source file (offset: 8)
  59. Wed, 12 Aug 2020 07:46:40 GMT | Total Writes: 8
  60. Wed, 12 Aug 2020 07:46:40 GMT | dump complete
#  根据索引名和类型进行备份

#备份成功。数据恢复成功

二.备份到另一台ES节点(如果是同一集群的节点索引不能重名)

  1. # 备份到另一台ES节点(同一集群索引不能重名)
  2. [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student --output=http://10.0.0.55:9200/students --type=analyzer
  3. Wed, 12 Aug 2020 08:14:00 GMT | starting dump
  4. Wed, 12 Aug 2020 08:14:00 GMT | got 1 objects from source elasticsearch (offset: 0)
  5. Wed, 12 Aug 2020 08:14:01 GMT | sent 1 objects to destination elasticsearch, wrote 1
  6. Wed, 12 Aug 2020 08:14:01 GMT | got 0 objects from source elasticsearch (offset: 1)
  7. Wed, 12 Aug 2020 08:14:01 GMT | Total Writes: 1
  8. Wed, 12 Aug 2020 08:14:01 GMT | dump complete
  9. [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student --output=http://10.0.0.55:9200/students --type=mapping
  10. Wed, 12 Aug 2020 08:14:10 GMT | starting dump
  11. Wed, 12 Aug 2020 08:14:10 GMT | got 1 objects from source elasticsearch (offset: 0)
  12. Wed, 12 Aug 2020 08:14:10 GMT | sent 1 objects to destination elasticsearch, wrote 1
  13. Wed, 12 Aug 2020 08:14:10 GMT | got 0 objects from source elasticsearch (offset: 1)
  14. Wed, 12 Aug 2020 08:14:10 GMT | Total Writes: 1
  15. Wed, 12 Aug 2020 08:14:10 GMT | dump complete
  16. [root@db04 bin]# ./elasticdump --input=http://10.0.0.54:9200/student --output=http://10.0.0.55:9200/students --type=data
  17. Wed, 12 Aug 2020 08:14:15 GMT | starting dump
  18. Wed, 12 Aug 2020 08:14:15 GMT | got 8 objects from source elasticsearch (offset: 0)
  19. Wed, 12 Aug 2020 08:14:15 GMT | sent 8 objects to destination elasticsearch, wrote 8
  20. Wed, 12 Aug 2020 08:14:15 GMT | got 0 objects from source elasticsearch (offset: 8)
  21. Wed, 12 Aug 2020 08:14:15 GMT | Total Writes: 8
  22. Wed, 12 Aug 2020 08:14:15 GMT | dump complete
  23. [root@db04 bin]#elasticdump --input=http://10.0.0.54:9200/student --output=http://100.10.0.55:9200/students --type=template

三.本地备份与恢复脚本

  1. # 1.本地备份脚本
  2. [root@db04 ~]# vim output_Es.sh
  3. #!/bin/bash
  4. read -p '要备份的机器是:'${1}
  5. #要导出的索引名
  6. index_name='
  7. student
  8. '
  9. for index in `echo $index_name`
  10. do
  11. echo "start input index ${index}"
  12. elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
  13. elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_analyzer.json --type=analyzer &> /dev/null
  14. elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_data.json --type=data &> /dev/null
  15. elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_alias.json --type=alias &> /dev/null
  16. elasticdump --input=http://${1}:9200/${index} --output=/data/${index}_template.json --type=template &> /dev/null
  17. done
  18. # 2.恢复脚本
  19. [root@db04 ~]# vim input_Es.sh
  20. #!/bin/bash
  21. read -p '要导入的机器是:'${1}
  22. #要导入的索引名
  23. index_name='
  24. student
  25. '
  26. for index in `echo $index_name`
  27. do
  28. echo "start input index ${index}"
  29. elasticdump --input=/data/${index}_alias.json --output=http://${1}:9200/${index} --type=alias &> /dev/null
  30. elasticdump --input=/data/${index}_analyzer.json --output=http://${1}:9200/${index} --type=analyzer &> /dev/null
  31. elasticdump --input=/data/${index}_data.json --output=http://${1}:9200/${index} --type=data &> /dev/null
  32. elasticdump --input=/data/${index}_template.json --output=http://${1}:9200/${index} --type=template &> /dev/null
  33. done

测试脚本

  1. # 0.配置环境变量
  2. [root@db04 ~]# vim /etc/profile.d/elasticdump.sh
  3. export PATH=/root/node-v10.13.0-linux-x64/lib/node_modules/elasticdump/bin:$PATH
  4. [root@db04 ~]# source /etc/profile
  5. # 1.创建备份目录
  6. [root@db04 ~]# mkdir /data
  7. # 2.执行备份脚本
  8. [root@db04 ~]# sh output_Es.sh
  9. 要备份的机器是:10.0.0.54
  10. start input index student
  11. # 3.查看备份目录
  12. [root@db04 ~]# ll /data/
  13. total 16
  14. -rw-r--r-- 1 root root 5 Aug 12 16:59 student_alias.json
  15. -rw-r--r-- 1 root root 101 Aug 12 16:59 student_analyzer.json
  16. -rw-r--r-- 1 root root 1284 Aug 12 16:59 student_data.json
  17. -rw-r--r-- 1 root root 5 Aug 12 16:59 student_template.json

# 模拟误删除操作

  1. # 执行恢复脚本
  2. [root@db04 ~]# sh input_Es.sh
  3. 要导入的机器是:10.0.0.54
  4. start input index student
  5. ## 数据恢复

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/961227?site
推荐阅读
相关标签
  

闽ICP备14008679号