Repositories

必须先注册一个快照仓库,然后才能进行快照和恢复操作。建议为每个主版本创建一个新快照仓库。有效的仓库设置取决于仓库类型。如果多个集群注册同一个快照仓库,只有一个集群可以对仓库进行写访问,其他所有集群应该设置该仓库为 readonly 模式。

1. 注册创建快照仓库

  1. curl -X PUT "localhost:9200/_snapshot/仓库名称" -H 'Content-Type: application/json' -d'
  2. {
  3. "type": "fs",
  4. "settings": {
  5. "location": "my_backup_location"
  6. }
  7. }'

2. 查看已注册快照仓库

  1. curl -X GET "localhost:9200/_snapshot/仓库名称"

可以使用逗号间隔多个仓库,*通配符匹配仓库名字,下面示例返回仓库名以repo开头的和包含backup的仓库信息

  1. curl -X GET "localhost:9200/_snapshot/repo*,*backup*"

获取所有已注册快照仓库,省略仓库名或者使用_all

  1. curl -X GET "localhost:9200/_snapshot"

或者

  1. curl -X GET "localhost:9200/_snapshot/_all"

3. 查看快照仓库列表

  1. curl -X GET "localhost:9200/_cat/repositories?v"

4. Shared File System Repository

共享文件系统仓库(“type”: “fs”)使用共享文件系统存快照,如果要注册共享文件系统仓库,必须在所有master和data节点挂载相同的共享文件系统到同一个路径位置。这个路径位置(或者它的一个父目录)必须在所有master和data节点的path.repo设置上注册。
假设共享文件系统挂载到 /mount/backups/my_fs_backup_location,应该在elasticsearch.yml文件中添加如下配置:

  1. path.repo: ["/mount/backups", "/mount/longterm_backups"]

The path.repo setting supports Microsoft Windows UNC paths as long as at least server name and share are specified as a prefix and back slashes are properly escaped:

  1. path.repo: ["\\\\MY_SERVER\\Snapshots"]

所有节点启动之后,执行下面的命令注册名字为my_fs_backup的共享文件系统仓库:

  1. curl -X PUT "localhost:9200/_snapshot/my_fs_backup" -H 'Content-Type: application/json' -d'
  2. {
  3. "type": "fs",
  4. "settings": {
  5. "location": "/mount/backups/my_fs_backup_location",
  6. "compress": true
  7. }
  8. }'

如果仓库路径使用相对路径,该路径将根据在path.repo中定义的第一个路径决定:

  1. curl -X PUT "localhost:9200/_snapshot/my_fs_backup" -H 'Content-Type: application/json' -d'
  2. {
  3. "type": "fs",
  4. "settings": {
  5. "location": "my_fs_backup_location",
  6. "compress": true
  7. }
  8. }'

compress: Turns on compression of the snapshot files. Compression is applied only to metadata files (index mapping and settings). Data files are not compressed. Defaults to true.

5. 仓库确认

仓库被注册之后,会立即在所有master和data节点上被验证以确保对当前集群所有的节点有效。在注册或者更新仓库时,参数verify可以显示禁用仓库核实确认操作:

  1. curl -X PUT "localhost:9200/_snapshot/my_unverified_backup?verify=false" -H 'Content-Type: application/json' -d'
  2. {
  3. "type": "fs",
  4. "settings": {
  5. "location": "my_unverified_backup_location"
  6. }
  7. }
  8. '

验证过程可以通过下面的命令手动执行:

  1. curl -X POST "localhost:9200/_snapshot/my_unverified_backup/_verify"

上面返回一个仓库验证成功的节点列表或者验证失败时的错误信息。

Snapshot

创建快照

一个仓库可以拥有同一个集群的多个快照。在一个集群中快照拥有一个唯一名字作为标识。在仓库 my_backup 中创建名字为 snapshot_1 的快照,可以通过执行下面的命令来实现:

  1. curl -X PUT "localhost:9200/_snapshot/仓库名/快照名?wait_for_completion=true"

参数 wait_for_completion 决定请求是在快照初始化后立即返回(默认),还是等快照创建完成之后再返回。快照初始化时,所有之前的快照信息会被加载到内存,所以在一个大的仓库中改请求需要若干秒(甚至分钟)才能返回,即使参数 wait_for_completion 的值设置为 false。
默认情况下,创建一个快照会包含集群中所有打开和启动状态的索引。可以通过在创建快照的请求体中定义索引列表来改变这个默认处理:

  1. curl -X PUT "localhost:9200/_snapshot/my_backup/snapshot_2?wait_for_completion=true" -H 'Content-Type: application/json' -d'
  2. {
  3. "indices": "index_1,index_2",
  4. "ignore_unavailable": true,
  5. "include_global_state": false
  6. }
  7. '

查看快照

一旦快照创建完成,可以通过以下命令获取快照信息:

  1. curl -X GET "localhost:9200/_snapshot/仓库名/快照名"

这个命令返回快照的基本信息,包括开始合结束时间、创建快照的 ElasticSearch 版本、包含的索引列表、快照当前状态和快照期间产生的失败索引列表。快照的状态有:

状态 描述
IN_PROGRESS The snapshot is currently running.
SUCCESS The snapshot finished and all shards were stored successfully.
FAILED The snapshot finished with an error and failed to store any data.
PARTIAL The global cluster state was stored, but data of at least one shard wasn’t stored successfully. The failure section in this case should contain more detailed information about shards that were not processed correctly.
INCOMPATIBLE The snapshot was created with an old version of Elasticsearch and therefore is incompatible with the current version of the cluster.

跟仓库类似,在一次查询中可以查询多个快照的信息,也支持通配符:

  1. curl -X GET "localhost:9200/_snapshot/仓库名/snapshot_*,some_other_snapshot"

查看仓库当前存储的所有快照:

  1. curl -X GET "localhost:9200/_snapshot/仓库名/_all"

查看当前正在运行的快照:

  1. curl -X GET "localhost:9200/_snapshot/仓库名/_current"

删除快照

从仓库中删除一个快照,使用如下命令:

  1. curl -X DELETE "localhost:9200/_snapshot/仓库名/快照名"

当一个快照从仓库中删除,ElasticSearch 将删除该快照关联的但不被其他快照使用的所有文件。如果在快照创建的时候执行快照删除操作,此快照创建进程将终止且所有该进程已创建的文件也将被清理。所以,快照删除操作可以用来取消错误启动的长时间运行的快照操作。

删除仓库

可以使用下面命令注销仓库:

  1. curl -X DELETE "localhost:9200/_snapshot/仓库名"

当一个仓库被注销时,ElasticSearch 只删除仓库存储快照的引用位置,快照本身没有被删除并且在原来的位置。

Restore

快照可以通过执行以下命令恢复

  1. curl -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore"

默认情况下,快照中的所有索引将被恢复,集群状态不被恢复。可以通过在恢复请求中使用 indices 和 include_global_state 选项来指定要恢复的索引和允许恢复集群全局状态。索引列表支持多索引语法。rename_pattern 和 rename_replacement 选项在恢复时通过正则表达式来重命名索引。设置 include_aliases 为 false 可以防止与索引关联的别名被一起恢复。

  1. curl -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore" -H 'Content-Type: application/json' -d'
  2. {
  3. "indices": "index_1,index_2",
  4. "ignore_unavailable": true,
  5. "include_global_state": true,
  6. "rename_pattern": "index_(.+)",
  7. "rename_replacement": "restored_index_$1"
  8. }
  9. '

恢复操作可以在正常运行的集群上执行。已存在的索引只能在关闭状态下才能恢复,并且要跟快照中索引拥有相同数目的分片。还原操作自动打开关闭状态的索引,如果被还原索引在集群不存在,将创建新索引。如果集群状态通过 include_global_state (默认是 false)选项被还原,在集群中不存在的模板会被新增,已存在的同名模板会被快照中的模板替换。持久化设置会被添加到现有的持久化设置中。

部分还原

默认情况下,如果参与恢复操作的一个或者多个索引没有全部可用分片的快照,整个恢复操作将会失败。比如部分分片快照备份操作失败,上面的情况就会发生。这种情况依然可以通过设置 partial 为 true 来实现快照的恢复。注意在这种情况下,只有成功完成快照备份的分片才会被还原,而所有丢失的 其它分片将被创建成空分片。

恢复过程中修改索引设置

在恢复过程中,大部分的索引配置会被覆盖。例如,下面的命令将恢复索引 index_1,不创建任何副本并切换回默认的索引刷新时间间隔:

  1. curl -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore" -H 'Content-Type: application/json' -d'
  2. {
  3. "indices": "index_1",
  4. "index_settings": {
  5. "index.number_of_replicas": 0
  6. },
  7. "ignore_index_settings": [
  8. "index.refresh_interval"
  9. ]
  10. }
  11. '

请注意,有些设置不能再恢复操作时被修改,比如 index.number_of_shards。

快照状态

使用以下命令可以获取当前运行快照列表的详细状态信息:

  1. curl -X GET "localhost:9200/_snapshot/_status"

可以指定快照名,以获取指定快照的信息,即使它不是正在运行状态:

  1. curl -X GET "localhost:9200/_snapshot/仓库名/snapshot_1/_status"

也支持多个ID:

  1. curl -X GET "localhost:9200/_snapshot/仓库名/snapshot_1,snapshot_2/_status"