功能概述

Elasticsearch 可以通过快照将指定 Index 甚至整个 Cluster 的数据存储到某远端仓库,并能从该远端仓库存储的快照中恢复数据。本应用的 Elasticsearch 可以通过 S3 Repository Plugin 与对象存储集成以便生成快照将数据存储到对象存储中,并可以在必要时从中恢复。

操作步骤

  1. 创建一个 repository

    PUT _snapshot/repo-stor
    {
      "type": "s3",
      "settings": {
        "endpoint": "s3.pek3a.stor.com",
        "access_key": "<YourAccessKey>",
        "secret_key": "<YourSecretKey>",
        "bucket": "my_stor_bucket"
      }
    }

    参数说明,详细参数说明请参考官方文档

    参数 说明

    repository

    名称。

    endpoint

    填写格式为 s3.<region_id>.stor.com,其中,<region_id> 为当前环境的 Region ID,请根据实际情况进行填写。

    access_key

    云平台账号关联的 access_key

    secret_key

    云平台账号关联的 secret_key

    bucket

    对象存储中 Bucket 名称。若填写的 Bucket 名不存在,则系统将创建该 Bucket。

  2. 通过如下命令查看、删除已有的 repository

    curl $ES_IP:9200/_snapshot/repo-stor    # 获取指定repository信息
    
    curl "$ES_IP:9200/_snapshot/repo*,*backup*" # 获取所有满足特定条件的repository信息
    
    curl $ES_IP:9200/_snapshot/_all             # 获取所有repository信息
    
    curl -XDELETE $ES_IP:9200/_snapshot/repo-stor # 删除repository
  3. 用如下命令创建快照。该快照将会存放在之前指定的对象存储的 Bucket 中,如示例 my_stor_bucket 中。

    # 创建包含集群所有index的snapshot
    curl -H "Content-Type: application/json" -XPUT "$ES_IP:9200/_snapshot/repo-stor/backup-2019.05.13?wait_for_completion=true"
    
    # 创建包含集群指定index(此处为index_1,index_2)的snapshot
    curl -H "Content-Type: application/json" -XPUT "$ES_IP:9200/_snapshot/repo-stor/backup-2019.05.13?wait_for_completion=true" -d'
    {
      "indices": "index_1,index_2",
      "ignore_unavailable": true,
      "include_global_state": false
    }
    '
    说明

    参数 wait_for_completiontrue 时表示该命令将会在快照创建完成返回,false 表示该命令将会在快照初始化完成就返回。

  4. 通过如下命令查看、删除快照。

    curl "$ES_IP:9200/_snapshot/repo-stor/backup-2019.05.13" # 查看指定repository中某snapshot信息
    
    curl "$ES_IP:9200/_snapshot/repo-stor/_all"              # 查看指定repository中所有snapshot信息
    
    curl -XDELETE "$ES_IP:9200/_snapshot/repo-stor/backup-2019.05.13" # 删除snapshot
  5. 通过如下命令恢复存储在对象存储的快照到 Elasticsearch 集群。

    # 恢复包含集群所有index的snapshot
    curl -H "Content-Type: application/json" -XPOST "$ES_IP:9200/_snapshot/repo-stor/backup-2019.05.13/_restore"
    
    # 恢复包含集群指定index(此处为index_1,index_2)的snapshot
    curl -H "Content-Type: application/json" -XPOST "$ES_IP:9200/_snapshot/repo-stor/backup-2019.05.13/_restore" -d'
    {
      "indices": "index_1,index_2",
      "ignore_unavailable": true,
      "include_global_state": false,
      "rename_pattern": "index_(.+)",
      "rename_replacement": "restored_index_$1"
    }
    '
    说明

    要恢复的 index 必须是集群中处于关闭状态的 index,处于打开状态的 index 将会提示无法恢复。

  6. 快照由于并没有和具体的集群信息绑定,所以也可以恢复到另一个不同的集群,用户可以用这种方法在不同集群之间通过对象存储导入导出数据。

    说明

    新集群的版本必须和老集群一致或者更新。

    1. 在目标集群中生成和源集群同样的 repository

    2. 将源集群的数据恢复到目标集群。通过步骤 5 中提到的命令,需要把 URL 地址改成目标集群里节点的地址。

      说明
      • 更详细的有关集群快照的生成和恢复的信息请参考 Elasticsearch 官方文档

      • 如果 ES 集群和对象存储位于同一区域进行数据迁移耗公网流量,如果不在同一区域则需要消耗公网流量,比如位于 北京3区-A 的 ES 集群可以选择同一区域的对象存储避免产生公网流量。