• Curl 命令方式对elasticsearch备份和恢复—— 筑梦之路


    前提条件:

    修改elasticsearch的配置文件elasticsearch.yml:

    1. # 配置示例
    2. path.repo: ["/data/es/snapshot"]

    创建备份仓库:

    1. # curl命令如下
    2. curl -XPOST '192.168.0.100:9200/_snapshot/bro_backup' \
    3. -H 'Content-Type: application/json' -d \
    4. '{
    5. "type": "fs",
    6. "settings": {
    7. "location": "/data/es/snapshot",
    8. "compress": true,
    9. "chunk_size": "1g",
    10. "max_snapshot_bytes_per_sec": "50m",
    11. "max_restore_bytes_per_sec": "50m"
    12. }
    13. }'
    14. # max_snapshot_bytes_per_sec 和max_restore_bytes_per_sec 为了限制备份和恢复时的速度
    15. # 如果新建仓库的时候报错 "reason": "failed to create blob container" access_denied_exception 请检查/data/es/snapshot文件夹权限

    备份数据:

    1. curl -XPUT "192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905?wait_for_completion=true"
    2. # 如果不希望快照作为后台进程运行,可以通过添加wait_for_completion=true参数,使其在前台运行,知道备份完成。
    3. # 如果想备份部分索引,可以加上indices 参数
    4. curl -XPUT http://192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905 -d '
    5. {
    6. "indices": "index_1,index_2"
    7. }'

    中止备份:

    1. #
    2. curl -XDELETE http://192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905

    查看备份信息:

    1. #
    2. curl -XGET http://192.168.0.100:9200/_snapshot/bro_backup/ss_2022100905

    恢复数据:

    1. 恢复前准备:
    2. 将备份数据打包传到新机器上,并解压到/data/es/snapshot目录下
    3. 1. 修改配置
    4. path.repo:["/data/backup/elasticsearch"],
    5. 2. 创建备份仓库
    6. curl -XPOST 'http://192.168.0.200:9200/_snapshot/bro_backup' \
    7. -H 'Content-Type: application/json' \
    8. -d '{
    9. "type": "fs",
    10. "settings": {
    11. "location": "/data/es/snapshot",
    12. "compress": true,
    13. "chunk_size": "1g",
    14. "max_snapshot_bytes_per_sec": "50m",
    15. "max_restore_bytes_per_sec": "50m"
    16. }
    17. }'
    18. 3. 恢复数据
    19. curl -XPOST '192.168.0.200:9200/_snapshot/bro_backup/ss_20220905/_restore' \
    20. -H 'Content-Type: application/json' \
    21. -d'{ "ignore_unavailable": true, "include_global_state": false }'
    22. 如果只想恢复某些分片的数据,还可以在json参数里加{"indices": "game_info"},以指定只恢复game_info分片数据。
    23. 4. 查看恢复的分片数据
    24. curl '192.168.0.200:9200/_cat/indices?v'
    1. # elasticsearch快照方式备份和恢复
    2. 1. 配置elasticsearch.yml快照存储位置
    3. ```yaml
    4. path.repo: ["/data/backups/my_backup"]
    5. ```
    6. 配置添加后需要重启elasticsearch
    7. 2. 注册仓库
    8. ```bash
    9. curl -XPUT ' http://localhost:9200/_snapshot/my_backup' -d '{
    10. "type":"fs",
    11. "settings":{
    12. "location":"/data/backups/my_backup",
    13. "compress":"true"
    14. }
    15. }'
    16. ```
    17. 3. 查看仓库信息
    18. ```bash
    19. curl -XGET ' http://localhost:9200/_snapshot/my_backup?pretty'
    20. {
    21. "my_backup" : {
    22. "type" : "fs",
    23. "settings" : {
    24. "compress" : "true", # 指定是否对快照文件进行压缩. 默认是 true.
    25. "location" : "/data/backups/my_backup" #指定快照的存储位置。必需要有
    26. }
    27. }
    28. }
    29. ```
    30. 4. 删除仓库
    31. ```bash
    32. curl -XDELETE 'localhost:9200/_snapshot/my_backup'
    33. ```
    34. 5. 建立快照
    35. 同一个集群中,一个仓库中能够存放多个快照。快照在集群中的名称是惟一的
    36. ```bash
    37. #全部索引进行快照:
    38. curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true?pretty"
    39. #某个索引进行快照:
    40. curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true?pretty" -d '{"indices":"customer"}'
    41. ```
    42. 6. 恢复快照
    43. (恢复的意思是经过接口删除节点中的索引,不是删除备份的快照)
    44. ```bash
    45. #恢复全部索引:
    46. curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true"
    47. #恢复某个索引:
    48. curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true" -d '{"indices":"customer","ignore_unavailable":"true"}'
    49. #注意:
    50. #快照恢复前须要关掉索引
    51. curl -XPOST "localhost:9200/my_index/_close"
    52. curl -XPOST "localhost:9200/customer/_close"
    53. #开启索引
    54. curl -XPOST "localhost:9200/my_index/_open"
    55. #查看索引状态
    56. curl 'localhost:9200/_cat/indices?v'
    57. curl -XGET "localhost:9200/_recovery/"
    58. ```
    59. 7. 查看快照状态信息
    60. ```bash
    61. #a.列出全部当前正在运行的快照以及显示他们的详细状态信息
    62. curl -XGET 'localhost:9200/_snapshot/_status?pretty'
    63. #b.查看指定仓库正在运行的快照以及显示他们的详细状态信息
    64. curl -XGET 'localhost:9200/_snapshot/my_backup/_status?pretty'
    65. #c.查看指定快照的详细状态信息即便不是正在运行
    66. curl -XGET 'localhost:9200/_snapshot/my_backup/snapshot_1/_status?pretty'
    67. #d.支持同时指定多个快照ID查看多个快照的信息
    68. curl -XGET 'localhost:9200/_snapshot/my_backup/snapshot_1,snapshot_2/_status?pretty'
    69. ```

     

    参考资料:

    Elasticsearch使用:Snapshot备份与恢复 - 腾讯云开发者社区-腾讯云

    ES集群数据迁移 - 简书

    https://www.elastic.co/guide/en/elasticsearch/reference/current/snapshot-restore.html

    https://www.cnblogs.com/koktlzz/p/14521583.html

  • 相关阅读:
    Linux磁盘管理
    SDRAM学习笔记(MT48LC16M16A2,w9812g6kh)
    【计算系统】5分钟了解超算,高性能计算,并行计算,分布式计算,网格计算,集群计算以及云计算的区别
    C#Assembly的使用
    数据结构—链表
    JAVA io理论
    景联文科技:针对敏感数据的安全转录服务,护航信息安全
    Linux Command htpasswd 创建密码文件
    美团大脑百亿级知识图谱的构建及应用进展
    靠“山寨”发家的名创优品,如今是什么模样
  • 原文地址:https://blog.csdn.net/qq_34777982/article/details/128135257