快照是正在运行的Elasticsearch集群的备份。可以使用快照来:
默认情况下,集群的快照包含集群状态、所有常规数据流和所有常规索引
快照必须存储在存储库中,存储库的内容不能修改,否则会造成快照损坏或导致数据不一致等一系列问题。所以在创建快照之前,需要先创建存储库
PUT /_snapshot/my_repository
{
"type": "url",
"settings": {
"url": "https://pan.baidu.com/disk/main#/transfer/send?surl=ABkAAAAddddAABEHbw"
}
}
type 表示存储库的类型,存储库支持多种类型,我们示例是使用的 url 类型。
PUT /_snapshot/<repository>
POST /_snapshot/<repository>
repository:存储库的名称。
注意:除了 file 协议,其他协议需要 elasticsearch.yml 配置 repositories.url.allowed_urls,此设置为数组。
repositories.url.allowed_urls: ["https://pan.baidu.com/disk/main#/transfer/send?surl=ABkAAAAddddAABEHbw"]
url 方式的 file 协议,以及 fs 方式,需要配置地址的父级目录到 elasticsearch.yml 的 path.repo 。
PUT /_snapshot/my_fs_repo
{
"type": "fs",
"settings": {
"location": "/DATA/soft/bak/my_test_bak"
}
}
结果
{
"acknowledged": true
}
请先配置 elasticsearch.yml 的 path.repo: [“/DATA/soft/bak”]
本示例以及本文下面的示例都需要关闭其他节点,只启动一个节点来使用,配置的文件地址也是启动的节点的文件路径。这样做是为了简化讲解、简化示例,存储库在集群下的 fs 路径需要共享文件系统挂载到每个主节点和数据节点的同样的路径下且每个节点都能访问的。要在集群环境使用本示例,请至少使用 NFS 等共享文件系统。
# /_snapshot/<repository>/_verify
POST /_snapshot/my_fs_repo/_verify
结果:
{
"nodes": {
"AST-4rnFRdagsd0juerSaw": {
"name": "myNode2"
},
"oj5FEiVMRpuWV386zdwy3w": {
"name": "myNode1"
}
}
}
我们的示例是单节点的,所以结果应该只有一个节点。
# /_snapshot/<repository>
GET /_snapshot/my_fs_repo
{
"my_fs_repo": {
"type": "fs",
"settings": {
"location": "/DATA/soft/bak/my_test_bak"
}
}
}
# DELETE /_snapshot/<repository>
DELETE /_snapshot/my_fs_repo
# POST /_snapshot/<repository>/_cleanup
POST /_snapshot/my_fs_repo/_cleanup
PUT /_snapshot/<repository>/<snapshot>
POST /_snapshot/<repository>/<snapshot>
PUT /_snapshot/my_fs_repo/snapshot_test1
{
"indices":"*",
"include_global_state": false,
"metadata": {
"message": "测试快照"
}
}
结果
{
"accepted": true
}
GET /_snapshot/<repository>/<snapshot>
GET /_snapshot/my_fs_repo/snapshot_test1?index_details=true
结果
{
"snapshots": [ // 快照列表
{
"snapshot": "snapshot_test1", // 快照名称
"uuid": "vn2YN19iRr6aG8YJ97paTw", // 快照统一标识符
"repository": "my_fs_repo", // 存储库名称
"version_id": 8060299, // 用于创建快照的 es 版本id
"version": "8.6.2", // 用于创建快照的 es 版本号
"indices": [ // 快照包含的索引列表
".ds-.logs-deprecation.elasticsearch-default-2023.07.19-000002",
"users",
"person",
".ds-ilm-history-5-2023.07.19-000002",
"students",
".ds-ilm-history-5-2023.06.19-000001",
".ds-.logs-deprecation.elasticsearch-default-2023.06.19-000001",
"person1"
],
"index_details": { // 快照中索引的信息(分片数、索引总大小等)
".ds-.logs-deprecation.elasticsearch-default-2023.06.19-000001": {
"shard_count": 1, // 分片数
"size_in_bytes": 10572, // 索引索引分配的总大小(单位字节)
"max_segments_per_shard": 1 // 每个分片的最大段数
},
"students": {
"shard_count": 2,
"size_in_bytes": 450,
"max_segments_per_shard": 0
},
".ds-ilm-history-5-2023.06.19-000001": {
"shard_count": 1,
"size_in_bytes": 19887,
"max_segments_per_shard": 2
},
".ds-.logs-deprecation.elasticsearch-default-2023.07.19-000002": {
"shard_count": 1,
"size_in_bytes": 225,
"max_segments_per_shard": 0
},
".ds-ilm-history-5-2023.07.19-000002": {
"shard_count": 1,
"size_in_bytes": 23206,
"max_segments_per_shard": 2
},
"person1": {
"shard_count": 1,
"size_in_bytes": 5667,
"max_segments_per_shard": 1
},
"users": {
"shard_count": 1,
"size_in_bytes": 6123,
"max_segments_per_shard": 1
},
"person": {
"shard_count": 1,
"size_in_bytes": 62979852,
"max_segments_per_shard": 3
}
},
"data_streams": [ // 快照中的数据流
"ilm-history-5",
".logs-deprecation.elasticsearch-default"
],
"include_global_state": false, // 是否包含集群状态
"metadata": { // 创建快照时设置的信息
"message": "测试快照"
},
"state": "SUCCESS",
// 快照状态(IN_PROGRESS 快照当前正在运行)
// SUCCESS 快照已完成,所有分片已成功存储。
// FAILED 快照完成时出错,无法存储任何数据。
// PARTIAL 已存储全局群集状态,但至少一个分片的数据未成功存储
"start_time": "2023-08-02T06:11:55.210Z",// 快照创建的开始时间(时间戳)
"start_time_in_millis": 1690956715210,// 快照创建的开始时间(毫秒)
"end_time": "2023-08-02T06:11:58.212Z",// 快照创建的结束时间(时间戳)
"end_time_in_millis": 1690956718212,// 快照创建的结束时间(毫秒)
"duration_in_millis": 3002,// 快照创建所用时间(毫秒)
"failures": [],// 创建时的故障
"shards": { // 快照中的分片数
"total": 9, // 总分片数
"failed": 0, // 快照中未包含的分片数
"successful": 9 // 快照中包含的分片数
},
"feature_states": [] // 功能状态,需要快照包含功能状态时才有值
}
],
"total": 1,
"remaining": 0
}
PUT /_snapshot/<repository>/<source_snapshot>/_clone/<target_snapshot>
PUT /_snapshot/my_fs_repo/snapshot_test1/_clone/snapshot_test2
GET _snapshot/_status
GET _snapshot/<repository>/_status
GET _snapshot/<repository>/<snapshot>/_status
GET /_snapshot/my_fs_repo/snapshot_test1/_status
结果
{
"snapshots": [ // 快照列表
{
"snapshot": "snapshot_test1", // 快照名
"repository": "my_fs_repo", // 存储库名
"uuid": "vn2YN19iRr6aG8YJ97paTw", // 快照统一标识符
"state": "SUCCESS", // 当前状态
"include_global_state": false, //是否包含集群状态
"shards_stats": { // 分片状态
"initializing": 0, // 正在初始化的分片数
"started": 0, // 启动但尚未完成的分片数
"finalizing": 0, // 正在完成但尚未完成的分片数
"done": 9, // 成功初始化、启动和完成的分片数
"failed": 0,// 快照中未包含的分片数
"total": 9// 快照中包含的分片总数
},
"stats": {
"incremental": { //
"file_count": 39,
"size_in_bytes": 63045982
},
"total": { // 快照引用的总大小
"file_count": 39,
"size_in_bytes": 63045982
},
"start_time_in_millis": 1690956715210, // 快照创建的初始时间
"time_in_millis": 3002 // 快照创建总时间
},
"indices": {// 快照中包含的索引信息
".ds-ilm-history-5-2023.06.19-000001": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 7,
"size_in_bytes": 19887
},
"total": {
"file_count": 7,
"size_in_bytes": 19887
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 401
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 7,
"size_in_bytes": 19887
},
"total": {
"file_count": 7,
"size_in_bytes": 19887
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 401
}
}
}
},
"person1": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 5667
},
"total": {
"file_count": 4,
"size_in_bytes": 5667
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 401
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 5667
},
"total": {
"file_count": 4,
"size_in_bytes": 5667
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 401
}
}
}
},
".ds-ilm-history-5-2023.07.19-000002": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 7,
"size_in_bytes": 23206
},
"total": {
"file_count": 7,
"size_in_bytes": 23206
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 401
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 7,
"size_in_bytes": 23206
},
"total": {
"file_count": 7,
"size_in_bytes": 23206
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 401
}
}
}
},
"person": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 10,
"size_in_bytes": 62979852
},
"total": {
"file_count": 10,
"size_in_bytes": 62979852
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 2802
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 10,
"size_in_bytes": 62979852
},
"total": {
"file_count": 10,
"size_in_bytes": 62979852
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 2802
}
}
}
},
"students": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 2,
"failed": 0,
"total": 2
},
"stats": {
"incremental": {
"file_count": 2,
"size_in_bytes": 450
},
"total": {
"file_count": 2,
"size_in_bytes": 450
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 201
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 1,
"size_in_bytes": 225
},
"total": {
"file_count": 1,
"size_in_bytes": 225
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 201
}
},
"1": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 1,
"size_in_bytes": 225
},
"total": {
"file_count": 1,
"size_in_bytes": 225
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 201
}
}
}
},
".ds-.logs-deprecation.elasticsearch-default-2023.07.19-000002": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 1,
"size_in_bytes": 225
},
"total": {
"file_count": 1,
"size_in_bytes": 225
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 0
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 1,
"size_in_bytes": 225
},
"total": {
"file_count": 1,
"size_in_bytes": 225
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 0
}
}
}
},
".ds-.logs-deprecation.elasticsearch-default-2023.06.19-000001": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 10572
},
"total": {
"file_count": 4,
"size_in_bytes": 10572
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 601
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 10572
},
"total": {
"file_count": 4,
"size_in_bytes": 10572
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 601
}
}
}
},
"users": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 1,
"failed": 0,
"total": 1
},
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 6123
},
"total": {
"file_count": 4,
"size_in_bytes": 6123
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 601
},
"shards": {
"0": {
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 6123
},
"total": {
"file_count": 4,
"size_in_bytes": 6123
},
"start_time_in_millis": 1690956715410,
"time_in_millis": 601
}
}
}
}
}
}
]
}
POST /_snapshot/<repository>/<snapshot>/_restore
POST /_snapshot/my_fs_repo/snapshot_test1/_restore?wait_for_completion=true
{
"indices":"person,person1"
}
示例为恢复索引 person 和 person1
注意:在恢复索引之前 person 和 person1 必须关闭或者删除,否则将提示已经存在索引,无法执行恢复操作。关闭和删除的索引在恢复后会自动打开。
结果
{
"snapshot": {
"snapshot": "snapshot_test1",
"indices": [
"person1",
"person"
],
"shards": {
"total": 2,
"failed": 0,
"successful": 2
}
}
}
DELETE /_snapshot/<repository>/<snapshot>
示例
DELETE /_snapshot/my_fs_repo/snapshot_2,snapshot_3
多个快照用逗号隔开