ElasticSearch 7 快照备份与恢复

原创
2022/01/07 14:20
阅读数 562

ElasticSearch 7 快照备份与恢复

环境: ElasticSearch v7.7 + Kibana + win10

1. 建立索引(demo)

DELETE /customers_1

#定义索引 customers_1, 并定义别名 customers
PUT /customers_1
{
  "aliases": {
    "customers": {}
  }, 
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "text"
      }
    }
  }
}

#插入数据
POST /customers/_bulk
{"index":{"_id":1}}
{"id":1,"name":"aben1"}
{"index":{"_id":2}}
{"id":2,"name":"aben2"}
{"index":{"_id":3}}
{"id":3,"name":"aben3"}
{"index":{"_id":4}}
{"id":4,"name":"aben4"}
{"index":{"_id":5}}
{"id":5,"name":"aben5"}

#查询数据
GET /customers/_search

2. 仓库建立及管理

创建保存仓库的文件夹

#powershell
PS D:\elastic> mkdir -p D:/elastic/myrepo

修改es配置

修改 elasticsearch.yml, 设置path.repo:

path.repo: D:/elastic/myrepo

指定多个地址, 使用数组格式, 比如: path.repo: ["/mount/backups", "/mount/longterm_backups"]

启动es.

建立仓库

建立(或修改)名称为myrepo的仓库:

PUT /_snapshot/myrepo
{
  "type":"fs",
  "settings": {
    "location": "d:/elastic/myrepo",
    "compress": true
  }
}

管理仓库

查看所有仓库:

GET /_snapshot

查看仓库的定义:

GET /_snapshot/myrepo

删除一个仓库(不会删除已备份的快照):

DELETE /_snapshot/myrepo

清理仓库(删除过时的不再被快照引用的数据):

POST /_snapshot/myrepo/_cleanup

3. 快照创建及管理

创建快照

#在仓库`repo`下创建名称为`customers`的快照
PUT /_snapshot/myrepo/customers?wait_for_completion=true
{
  "indices": "customers",
  "ignore_unavailable": true,
  "include_global_state": true
}
  • 参数indices支持使用索引别名, 支持通配符表达式, 多个索引名称用逗号拼接.
  • 如果不指定indices, 则表示快照所有open状态的索引.
  • 参数include_global_state为true时, 会保存当前集群的全局状态到快照中.

查看快照的状态/进度 (包括索引信息)

GET /_snapshot/myrepo/customers/_status

管理快照

获取指定仓库中的所有快照:

#获取仓库repo下的所有快照
GET /_snapshot/myrepo/_all

获取某个特定快照的信息:

#获取仓库myrepo下快照customers的信息
GET /_snapshot/myrepo/customers

删除一个快照:

#删除仓库myrepo下名为customers的快照
DELETE /_snapshot/myrepo/customers

如果一个快照执行错误或者执行时间太长, 都可以使用DELETE来删除(或终止当前的快照操作,并删除文件).

定期备份策略, 更重要!

前面的快照策略只能执行一次, 而我们需要定期备份.

使用SLM(snapshot lifecycle management)来自动进行备份:

PUT /_slm/policy/daily-snapshots
{
  "schedule": "0 30 1 * * ?", 
  "name": "<daily-snap-{now/d}>", 
  "repository": "myrepo", 
  "config": { 
    "indices": ["customers"] 
  },
  "retention": { 
    "expire_after": "30d", 
    "min_count": 5, 
    "max_count": 50 
  }
}

任务的id是daily-snapshots.

  • 每天早上1:30执行一次快照
  • 快照名称使用日期格式, 比如: daily-snap-2022.01.07-任务id
  • 保存在仓库myrepo中
  • 参数indices可以使用通配符表达式. 如果使用*,则包含所有open(含以.开头的)索引
  • 快照数据保留30天, 至少保留5份, 最多50份.

手动执行任务:

POST /_slm/policy/daily-snapshots/_execute

查看任务执行记录:

GET /_slm/policy/daily-snapshots?human

查看所有任务:

GET /_slm/policy/

当前的7.7版本, 乃至7.16版本, 都未提供暂停单个任务的功能, 只能删除和编辑.

4. 使用快照恢复数据

这里我们先删除索引, 然后尝试恢复.

删除索引:

DELETE /customers_1

删除索引时, 会同时删除绑定的索引别名

例1、从快照恢复指定的索引(使用原索引名称):

POST /_snapshot/myrepo/customers/_restore
{
  "indices": "customers_1",
  "ignore_unavailable": true,
  "include_global_state": false
}

上面的例子, 指定了要恢复的索引是customers_1, 恢复后的索引名不变.

参数include_global_state如果设置为true, 则会把快照的设置, 索引模板, ingest pipelines 和 ILM 策略都保存到当前集群.

例2、使用正则: 恢复索引到指定的索引名称, 而不是使用原有的索引名称:

#恢复索引到指定的索引名
POST /_snapshot/myrepo/customers/_restore
{
  "indices": "customers_1",
  "ignore_unavailable": true,
  "include_global_state": false,
  "rename_pattern": "(.+)",
  "rename_replacement": "restored_$1",
  "include_aliases": false
}

使用正则时, 必须指定rename_patternrename_replacement, 即使只指定了一个索引.

说明

  • indices只能使用索引名称, 不能用索引别名
  • indices支持通配符表达式
  • 恢复时, 指定的索引名不能已存在
展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部