快照生命周期管理 #
使用快照管理(SLM)API 自动创建快照。
创建快照策略 #
请求示例 #
- 每天上午 8 点自动创建一份快照,快照名称格式为
yyyy-MM-dd-HH:mm
,存储在 my_backup 快照仓库 - 每天凌晨 1 点自动删除最早 7 天前创建的快照、超过 21 个的快照以及保留至少 7 个快照
- 快照创建和删除的时间限制均为 1 小时
curl -XPOST -uadmin:admin -H 'Content-Type: application/json' 'https://localhost:9200/_slm/policies/daily-policy' -d '
{
"description": "每日快照策略",
"creation": {
"schedule": {
"cron": {
"expression": "0 8 * * *",
"timezone": "Asia/Shanghai"
}
},
"time_limit": "1h"
},
"deletion": {
"schedule": {
"cron": {
"expression": "0 1 * * *",
"timezone": "Asia/Shanghai"
}
},
"condition": {
"max_age": "7d",
"max_count": 21,
"min_count": 7
},
"time_limit": "1h"
},
"snapshot_config": {
"date_format": "yyyy-MM-dd-HH:mm",
"date_format_timezone": "Asia/Shanghai",
"indices": "*",
"repository": "my_backup",
"ignore_unavailable": "true",
"include_global_state": "false",
"partial": "true",
"metadata": {
"any_key": "any_value"
}
}
}'
示例响应 #
{
"_id": "daily-policy-sm-policy",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"sm_policy": {
"name": "daily-policy",
"description": "每日快照策略",
"schema_version": 17,
"creation": {
"schedule": {
"cron": {
"expression": "0 8 * * *",
"timezone": "Asia/Shanghai"
}
},
"time_limit": "1h"
},
"deletion": {
"schedule": {
"cron": {
"expression": "0 1 * * *",
"timezone": "Asia/Shanghai"
}
},
"condition": {
"max_age": "7d",
"min_count": 7,
"max_count": 21
},
"time_limit": "1h"
},
"snapshot_config": {
"indices": "*",
"metadata": {
"any_key": "any_value"
},
"ignore_unavailable": "true",
"date_format_timezone": "Asia/Shanghai",
"include_global_state": "false",
"date_format": "yyyy-MM-dd-HH:mm",
"repository": "my_backup",
"partial": "true"
},
"schedule": {
"interval": {
"start_time": 1685348095913,
"period": 1,
"unit": "Minutes"
}
},
"enabled": true,
"last_updated_time": 1685348095938,
"enabled_time": 1685348095909
}
}
获取策略 #
获取所有 SLM 策略
请求示例 #
curl -XGET -uadmin:admin 'https://localhost:9200/_slm/policies'
获取特定的 SLM 策略。
请求示例 #
curl -XGET -uadmin:admin 'https://localhost:9200/_slm/policies/daily-policy?pretty'
示例响应 #
{
"_id" : "daily-policy-sm-policy",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"sm_policy" : {
"name" : "daily-policy",
"description" : "每日快照策略",
"schema_version" : 17,
"creation" : {
"schedule" : {
"cron" : {
"expression" : "0 8 * * *",
"timezone" : "Asia/Shanghai"
}
},
"time_limit" : "1h"
},
"deletion" : {
"schedule" : {
"cron" : {
"expression" : "0 1 * * *",
"timezone" : "Asia/Shanghai"
}
},
"condition" : {
"max_age" : "7d",
"min_count" : 7,
"max_count" : 21
},
"time_limit" : "1h"
},
"snapshot_config" : {
"indices" : "*",
"metadata" : {
"any_key" : "any_value"
},
"ignore_unavailable" : "true",
"date_format_timezone" : "Asia/Shanghai",
"include_global_state" : "false",
"date_format" : "yyyy-MM-dd-HH:mm",
"repository" : "my_backup",
"partial" : "true"
},
"schedule" : {
"interval" : {
"start_time" : 1685348095913,
"period" : 1,
"unit" : "Minutes"
}
},
"enabled" : true,
"last_updated_time" : 1685348095938,
"enabled_time" : 1685348095909
}
}
更新策略 #
必须为更新请求提供 seq_no 和 primary_term 参数,其他同创建策略一样。
请求示例 #
PUT /_slm/policies/daily-policy?if_seq_no=2&if_primary_term=1
{
"description": "每日快照策略",
"creation": {
"schedule": {
"cron": {
"expression": "0 9 * * *",
"timezone": "Asia/Shanghai"
}
},
"time_limit": "1h"
},
"deletion": {
"schedule": {
"cron": {
"expression": "0 1 * * *",
"timezone": "Asia/Shanghai"
}
},
"condition": {
"max_age": "7d",
"max_count": 21,
"min_count": 7
},
"time_limit": "1h"
},
"snapshot_config": {
"date_format": "yyyy-MM-dd-HH:mm",
"date_format_timezone": "Asia/Shanghai",
"indices": "*",
"repository": "my_backup",
"ignore_unavailable": "true",
"include_global_state": "false",
"partial": "true",
"metadata": {
"any_key": "any_value"
}
}
}
解释 Explain #
_explain API 可以检查快照策略的设置并返回对其的解释。这对理解您的快照策略及其操作非常有用。
该 API 将返回以下内容的解释:
快照策略的创建设置,包括下次计划创建快照的确切日期和时间。
快照策略的删除设置,包括下次计划删除快照的确切日期和时间,以及会被删除的快照。
用于计算快照创建和删除时间的 Cron 表达式。
快照策略的所有其他设置,如快照设置、保留要求等。
获取索引的当前状态。您可以使用索引模式来获取多个索引的状态。
请求示例 #
curl -XGET -uadmin:admin 'https://localhost:9200/_slm/policies/daily*/_explain'
示例响应 #
{
"policies" : [
{
"name" : "daily-policy",
"creation" : {
"current_state" : "CREATION_START",
"trigger" : {
"time" : 1685404800000
}
},
"deletion" : {
"current_state" : "DELETION_START",
"trigger" : {
"time" : 1685379600000
}
},
"policy_seq_no" : 0,
"policy_primary_term" : 1,
"enabled" : true
}
]
}
删除策略 #
删除指定的 SLM 策略。
请求示例 #
curl -XDELETE -uadmin:admin 'https://localhost:9200/_slm/policies/daily-policy?pretty'
示例响应 #
{
"_index": ".easysearch-ilm-config",
"_type": "_doc",
"_id": "daily-policy-sm-policy",
"_version": 2,
"result": "deleted",
"forced_refresh": true,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 8,
"_primary_term": 1
}
启动策略 #
通过将策略的 enabled 标志设置为 true 来启动策略。
请求示例 #
我们先创建个每小时执行一次的快照策略
curl -XPOST -uadmin:admin -H 'Content-Type: application/json' 'https://localhost:9200/_slm/policies/hour-policy' -d '
{
"description": "每小时快照",
"creation": {
"schedule": {
"cron": {
"expression": "0 * * * *",
"timezone": "Asia/Shanghai"
}
},
"time_limit": "1h"
},
"deletion": {
"schedule": {
"cron": {
"expression": "0 1 * * *",
"timezone": "Asia/Shanghai"
}
},
"condition": {
"max_age": "7d",
"max_count": 21,
"min_count": 7
},
"time_limit": "1h"
},
"snapshot_config": {
"date_format": "yyyy-MM-dd-HH:mm",
"date_format_timezone": "Asia/Shanghai",
"indices": "*",
"repository": "my_backup",
"ignore_unavailable": "true",
"include_global_state": "false",
"partial": "true",
"metadata": {
"any_key": "any_value"
}
}
}'
启动一个策略 #
请求示例 #
curl -XPOST -uadmin:admin 'https://localhost:9200/_slm/policies/hour-policy/_start'
示例响应 #
{"acknowledged":true}
停止策略 #
请求示例 #
curl -XPOST -uadmin:admin 'https://localhost:9200/_slm/policies/hour-policy/_stop'
示例响应 #
{
"acknowledged" : true
}