ElasticSearch备份与恢复

温馨提醒

一、备份

创建索引备份仓库 -> 备份索引快照到仓库 -> 查询索引备份仓库中快照列表 -> 从备份快照中恢复索引

三个概念:仓库(repository)、快照(snapshot)、索引(index)

1.1 创建快照仓库

修改 ElasticSearch 配置文件,添加备份仓库位置

1
2
3
vim /etc/elasticsearch/elasticsearch.yml

path.repo: /data/elasticsearch_data/es_backup

创建快照仓库,设置快照目录

1
curl -XPUT "http://localhost:9200/_snapshot/es_backup?verify=false" -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "/data/backup/es_backup", "compress": true, "max_restore_bytes_per_sec": 50m, "max_snapshot_bytes_per_sec": 30m } }'

参数:

location:快照的路径

compress:打开快照文件的压缩,压缩仅应用于元数据文件,数据文件未压缩,默认 true

max_restore_bytes_per_sec:每个节点的恢复速度,默认 40mb 每秒

max_snapshot_bytes_per_sec:每个节点的快照速率限制,默认 40mb 每秒

readonly:使用存储为只读,默认 false

1.2 创建快照

一个仓库可以包含多个快照,快照可以存多个索引、部分索引或一个单独的索引,还可以给索引指定一个唯一的名字作为备份

把所有索引备份到快照 snapshot_01 中,并存储到仓库 es_backup 下

1
curl -XPUT http://localhost:9200/_snapshot/es_backup/snapshot_01?wait_for_completion=true

把指定索引 index_1,index_2 备份到快照 snapshot_02 中,并存储到仓库 es_backup 下

1
2
curl -XPUT http://localhost:9200/_snapshot/es_backup/snapshot_02?wait_for_completion=true" -H 'Content-Type: application/json' -d '{ "indices": "index_1,index_2", "ignore_unavailable": true, "include_global_state": false 
}'

参数说明:

wait_for_completion:请求是在快照初始化后立即返回,还是等快照创建完成之后再返回

ignore_unavailable:该选项设置为 true 时,在创建快照时会忽略不存在的索引

include_global_state:默认情况下,如果如果一个快照中的一个或者多个索引没有所有主分片可用,整个快照创建会失败,该情况可以通过设置 partial 为 true 来改变

1.3 取消备份

1
curl -u elastic:123456 -XDELETE http://localhost:9200/_snapshot/es_backup/backup-20200629

1.4 删除快照

1
curl -u elastic:123456 -X DELETE "http://localhost:9200/_snapshot/es_backup/test_snapshot_2"

1.5 删除快照仓库

1
curl -u elastic:123456 -X DELETE http://localhost:9200/_snapshot/es_backup/

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

1
curl -X GET "localhost:9200/_snapshot/es_backup/_current"

1.7 查看所有快照仓库

1
curl -XGET http://localhost:9200/_snapshot/_all?pretty

1.8 查看快照仓库 backup_01 中的所有快照

1
curl -XGET http://localhost:9200/_snapshot/es_backup/_all?pretty

1.9 查看备份任务

1
curl -XGET http://localhost:9200/_snapshot/es_backup/user_data/_status

主要由以下几种状态:

INITIALIZING 集群状态检查,检查当前集群是否可以做快照,通常这个过程会非常快

STARTED 正在转移数据到仓库

FINALIZING 数据转移完成,正在转移元信息

DONE 完成

FAILED 备份失败

1.10 查看快照 snapshot_01 中的所有备份的索引

1
curl -XGET http://localhost:9200/_snapshot/backup_01/snapshot_01

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

IN_PROGRESS 正在创建快照

SUCCESS 快照创建成功

FAILED 快照创建完成,但是有错误,数据不会保存

PARTIAL 整个集群备份完成,但是至少有一个shard数据存贮失败,会有更具体报错信息

INCOMPATIBLE 创建快照的es版本和当前集群es版本不一致

二、恢复

2.1 创建快照仓库

需要先创建快照仓库,然后把备份的索引拷贝到/data/backup/elasticsearch/目录下

chown -R elasticsearch.elasticsearch /data/backup/elasticsearch

2.2 恢复快照中的索引

  • 恢复快照中所有的索引

    1
    
    curl -XPOST http://localhost:9200/_snapshot/es_backup/snapshot_01/_restore?wait_for_completion=true
  • 恢复快照中指定的索引

    1
    
    curl -u elastic:123456 -XPOST http://localhost:9200/_snapshot/es_backup/2023-05-31/_restore -H 'Content-Type: application/json' -d '{"indices": "test_20230520"}'

默认情况下,是恢复所有索引,也可以恢复快照中指定索引,以及重命名恢复的索引,可以避免覆盖所有的数据

1
2
3
4
5
6
7
8
curl -XPOST "http://localhost:9200/_snapshot/es_backup/snapshot_20150812/_restore" -H 'Content-Type: application/json' -d'
{
    "indices": "test_20230520",
    "ignore_unavailable": true,
    "include_global_state": true,
   	"rename_pattern": "index_(.+)",
   	"rename_replacement": "restored_index_$1"
}'

说明:

indices:指定要恢复的索引名,只恢复 test_20230520

ignore_unavailable:如果快照里缺少指定的索引,不会报错,而是跳过

include_global_state:恢复时包括集群的全局状态(比如模板、集群设置、别名等)

rename_pattern:正则表达式,用来匹配原索引名,表示重命名索引以 “index_” 开头的索引

rename_replacement:表示将所有的索引重命名为“restored_index_xxx”,如 test_20230520 会被重命名为 restored_index_1

查看恢复进度

1
2
3
4
5
6
7
8
# 查看所有索引的恢复进度
curl -XGET http://localhost:9200/_recovery/

# 查看索引restored_index_1的恢复进度
curl -XGET http://localhost:9200/_recovery/restored_index_1

# 取消恢复,只需要删除索引,即可取消恢复
curl -XDELETE http://localhost:9200/restored_index_1

三、其他命令

  • 修改一个索引的副本数

    1
    
    curl -i -XPUT ip:port/INDICES_NAME/_settings -H 'content-Type:application/json' -d '{"number_of_replicas": 1}'
  • 查询所有索引

    1
    
    curl -XGET 'localhost:9200/_cat/indices?v'
  • 查询指定索引

    1
    
    curl -XGET 'localhost:9200/_cat/indices/索引名'
  • 删除指定索引

    1
    
    curl -XDELETE 'localhost:9200/索引名?pretty'
  • 索引排序(按占用大小)

    1
    
    curl "localhost:9200/_cat/indices?v&s=store.size:desc"
  • 查看索引状态(所有索引)

    1
    
    curl -XGET 'localhost:9200/_settings?pretty'
  • 取消索引只读

    1
    
    curl -XPUT 'localhost:9200/_all/_settings'  -H 'Content-Type: application/json' -d '{"index.blocks.read_only_allow_delete": null}'
  • 设置索引分片为0

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    # 方式一
    curl -H "Content-Type: application/json" -XPUT 'localhost:9200/_settings' -d '{"索引名": {"number_of_replicas": "0"}}'
    
    # 方式二
    PUT test_20230520/_settings		
    
    # kibana上执行
    {
     "number_of_replicas":"0"
    }
  • 设置分片大小

    1
    2
    3
    4
    5
    6
    7
    8
    
    PUT /_cluster/settings
    {
      "persistent": {
        "cluster": {
          "max_shards_per_node":5000
        }
      }  
    }
  • 关闭自动分片

    1
    
    curl -u elastic:123456 -X PUT "http://localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d '{ "persistent": { "cluster.routing.allocation.enable": "none" } }'
  • 打开自动分片

    1
    
    curl -u elastic:123456 -X PUT "http://localhost:9200/_cluster/settings" -H 'Content-Type: application/json' -d '{ "persistent": { "cluster.routing.allocation.enable": "all" } }'