扩展阅读-Elasticsearch 通过索引阻塞实现数据保护深入解析
warning:
这篇文章距离上次修改已过289天,其中的内容可能已经有所变动。
from datetime import datetime
from elasticsearch import Elasticsearch
# 假设Elasticsearch实例已经创建并连接到集群
es = Elasticsearch("http://localhost:9200")
# 创建一个新的索引阻塞策略
def create_new_index_block_policy(policy_name: str, repo_name: str, snapshot_name: str, index_patterns: list):
now = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
policy = {
"policy": {
"description": "Block all writes to specific indices",
"default_state": "block_writes",
"states": [
{
"name": "block_writes",
"actions": [
{
"replica_count": {
"action": "none"
}
},
{
"read_only": {
"action": "block"
}
},
{
"allocate": {
"number_of_replicas": 0,
"include": {
"box_type": ["hot", "warm"]
}
}
}
],
"transitions": [
{
"state_name": "delete",
"condition": {
"description": "Move to delete when snapshot completed",
"min_index_age": "1d"
}
}
]
},
{
"name": "delete",
"actions": [
{
"delete": {
"delete_searchable_snapshot": True
}
}
],
"transitions": []
}
],
"ism_template": {
"index_patterns": index_patterns,
"priority": 100,
"last_updated": now,
"schema_version": 1
}
}
}
# 创建策略
es.transport.perform_request('PUT', f'/_ism/policies/{policy_name}', body=policy)
# 调用函数创建一个新的索引阻塞策略
评论已关闭