📣 极限科技诚招搜索运维工程师(Elasticsearch/Easysearch)- 全职/北京 👉 : 立即申请加入

在指定的快照仓库中创建快照,备份索引数据。

API 格式 #

POST /_snapshot/{repository}/{snapshot}
PUT /_snapshot/{repository}/{snapshot}

API 作用 #

该 API 用于创建快照备份:

  • 备份一个或多个索引到指定的快照仓库
  • 支持选择性备份(指定索引列表)
  • 可选包含集群全局状态
  • 支持增量快照

API 参数 #

路径参数 #

参数类型是否必填默认值描述
{repository}String-快照仓库名称
{snapshot}String-快照名称

查询参数 #

参数类型是否必填默认值描述
master_timeoutTime30s连接主节点的超时时间
wait_for_completionBooleanfalse是否等待快照完成后再返回响应

请求体参数 #

参数类型是否必填默认值描述
indicesArray["_all"]要包含在快照中的索引列表。支持多索引语法和别名。空数组或 ["*_all"] 表示备份所有打开的索引
ignore_unavailableBooleanfalse是否忽略不可用的索引(未找到或已关闭)
include_global_stateBooleantrue是否在快照中包含集群全局状态元数据
partialBooleanfalse是否允许部分快照(当某些索引的分片不可用时)
settingsObject{}仓库特定的设置(如压缩、块大小等)
metadataObjectnull附加到快照的用户自定义元数据(最大 1024 字节)

请求示例 #

创建所有索引的快照 #

POST /_snapshot/my_backup/snapshot_1
{
  "indices": "*",
  "include_global_state": true
}

创建指定索引的快照 #

POST /_snapshot/my_backup/daily_backup
{
  "indices": ["index1", "index2", "logs-*"],
  "ignore_unavailable": true,
  "include_global_state": false,
  "metadata": {
    "created_by": "admin",
    "description": "Daily backup"
  }
}

等待快照完成 #

POST /_snapshot/my_backup/full_backup?wait_for_completion=true
{
  "indices": ["*"],
  "include_global_state": true
}

创建部分快照 #

POST /_snapshot/my_backup/partial_snapshot
{
  "indices": ["index1", "index2"],
  "partial": true
}

响应示例 #

wait_for_completion=false(默认) #

202 Accepted
{
  "accepted": true
}

wait_for_completion=true(成功) #

200 OK
{
  "snapshot": {
    "snapshot": "snapshot_1",
    "uuid": "K8i7oP2rR4aK9sD5fG1hJ3mN6pQ",
    "version_id": 811419996,
    "version": "8.11.4",
    "indices": ["index1", "index2"],
    "data_streams": [],
    "state": "SUCCESS",
    "start_time": "2026-02-04T12:00:00.000Z",
    "start_time_in_millis": 1738644000000,
    "end_time": "2026-02-04T12:05:00.000Z",
    "end_time_in_millis": 1738644300000,
    "duration_in_millis": 300000,
    "include_global_state": true,
    "metadata": {
      "created_by": "admin"
    },
    "total_shards": 10,
    "successful_shards": 10,
    "failed_shards": 0,
    "failures": []
  }
}

wait_for_completion=true(部分失败) #

200 OK
{
  "snapshot": {
    "snapshot": "snapshot_1",
    "uuid": "K8i7oP2rR4aK9sD5fG1hJ3mN6pQ",
    "version_id": 811419996,
    "version": "8.11.4",
    "indices": ["index1", "index2"],
    "data_streams": [],
    "state": "PARTIAL",
    "reason": "some shards failed to snapshot",
    "start_time": "2026-02-04T12:00:00.000Z",
    "start_time_in_millis": 1738644000000,
    "end_time": "2026-02-04T12:05:00.000Z",
    "end_time_in_millis": 1738644300000,
    "duration_in_millis": 300000,
    "include_global_state": true,
    "total_shards": 10,
    "successful_shards": 8,
    "failed_shards": 2,
    "failures": [
      {
        "shard": 0,
        "index": "index1",
        "node": "node-1",
        "reason": "failed to snapshot shard",
        "status": "503"
      }
    ]
  }
}

响应状态码 #

状态码描述
202 Accepted快照操作已启动但未完成(wait_for_completion=false)
200 OK快照完成成功或部分成功(wait_for_completion=true)
500 Internal Server Error快照完全失败

响应字段说明 #

快照对象字段 #

字段类型描述
snapshotString快照名称
uuidString快照的唯一标识符
version_idInteger创建快照的 Easysearch 版本号
versionString创建快照的 Easysearch 版本字符串
indicesString[]快照中包含的索引列表
data_streamsString[]快照中包含的数据流列表
stateString快照状态(SUCCESS、PARTIAL、FAILED)
reasonString/null失败或部分失败的原因
start_timeString快照开始时间(ISO 格式)
start_time_in_millisLong开始时间(毫秒时间戳)
end_timeString快照结束时间(ISO 格式)
end_time_in_millisLong结束时间(毫秒时间戳)
duration_in_millisLong快照总耗时(毫秒)
include_global_stateBoolean是否包含集群全局状态
metadataObject附加的用户自定义元数据
total_shardsInteger快照的总分片数
successful_shardsInteger成功的分片数
failed_shardsInteger失败的分片数
failuresArray分片失败详情数组

命名规范 #

仓库名称 #

  • 必须是非空字符串
  • 小写
  • 不能包含空格
  • 不能包含逗号、井号
  • 不能以下划线开头

快照名称 #

  • 必须是非空字符串
  • 小写
  • 不能包含空格
  • 不能包含逗号、井号
  • 不能以下划线开头

使用场景 #

  1. 定期备份:按计划创建定期快照
  2. 全量备份:备份所有索引和集群状态
  3. 选择性备份:仅备份重要索引
  4. 迁移准备:在数据迁移前创建快照

注意事项 #

  1. 元数据大小:用户元数据限制为 1024 字节
  2. 并发限制:同一索引的并发快照会冲突
  3. 默认索引:未指定索引时备份所有打开的索引
  4. 增量快照:只备份自上次快照以来的变更
  5. 部分快照:使用 partial=true 允许部分分片失败时仍创建快照

快照状态说明 #

状态描述
SUCCESS所有分片快照成功
PARTIAL部分分片快照失败
FAILED快照完全失败
IN_PROGRESS快照正在进行中

相关操作 #

  • GET /_snapshot/{repository}/{snapshot}:查询快照信息
  • DELETE /_snapshot/{repository}/{snapshot}:删除快照
  • POST /_snapshot/{repository}/{snapshot}/_restore:恢复快照
  • GET /_snapshot/{repository}:查询仓库信息

实现文件 #

  • REST 处理器RestCreateSnapshotAction.java
  • 请求类CreateSnapshotRequest.java
  • 响应类CreateSnapshotResponse.java
  • Transport ActionTransportCreateSnapshotAction.java
  • 快照信息SnapshotInfo.java