我正在使用python访问elasticsearch集群。现在我想使用快照备份我的索引。 最困难的是:python-elasticsearch的doc只给我一个API描述。没有示例向我展示如何创建快照。我尝试了一些参数,但都失败了。任何人都可以使用python提供弹性搜索的快照示例吗?以下是我的代码:
from elasticsearch import Elasticsearch
es = Elasticsearch()
snapshot_body = {
"type": "url",
"settings": {
"url": "http://download.elasticsearch.org/definitiveguide/sigterms_demo/"
}
}
body = {"snapshot": snapshot_body}
es.snapshot.create_repository(repository='test', body=body)
答案 0 :(得分:3)
您的存储库创建几乎是正确的,您不需要行body = {"snapshot": snapshot_body}
,只需像这样创建您的存储库:
es.snapshot.create_repository(repository='test', body=snapshot_body)
现在,为了创建快照,您只需要这样做:
es.snapshot.create(repository='test', snapshot='my_snapshot')
如果你只想存储几个指数而不是全部,你也可以提供这样的身体:
index_body = {
"indices": "index_1,index_2"
}
es.snapshot.create(repository='test', snapshot='my_snapshot', body=index_body)
答案 1 :(得分:0)
将以下示例Python代码保存为Python文件,例如register-repo.py。客户端需要适用于Python的AWS开发工具包(Boto3),请求和request-aws4auth软件包。客户端包含其他快照操作的注释示例。
import boto3
import requests
from requests_aws4auth import AWS4Auth
host = '' # include https:// and trailing /
region = '' # e.g. us-west-1
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
# Register repository
path = '_snapshot/my-snapshot-repo-name' # the Elasticsearch API endpoint
url = host + path
payload = {
"type": "s3",
"settings": {
"bucket": "s3-bucket-name",
# "endpoint": "s3.amazonaws.com", # for us-east-1
"region": "us-west-1", # for all other regions
"role_arn": "arn:aws:iam::123456789012:role/TheSnapshotRole"
}
}
headers = {"Content-Type": "application/json"}
r = requests.put(url, auth=awsauth, json=payload, headers=headers)
print(r.status_code)
print(r.text)
# # Take snapshot
#
# path = '_snapshot/my-snapshot-repo/my-snapshot'
# url = host + path
#
# r = requests.put(url, auth=awsauth)
#
# print(r.text)
#
# # Delete index
#
# path = 'my-index'
# url = host + path
#
# r = requests.delete(url, auth=awsauth)
#
# print(r.text)
#
# # Restore snapshot (all indices except Kibana and fine-grained access control)
#
# path = '_snapshot/my-snapshot-repo/my-snapshot/_restore'
# url = host + path
#
# payload = {
# "indices": "-.kibana*,-.opendistro_security",
# "include_global_state": false
# }
#
# headers = {"Content-Type": "application/json"}
#
# r = requests.post(url, auth=awsauth, json=payload, headers=headers)
#
# # Restore snapshot (one index)
#
# path = '_snapshot/my-snapshot-repo/my-snapshot/_restore'
# url = host + path
#
# payload = {"indices": "my-index"}
#
# headers = {"Content-Type": "application/json"}
#
# r = requests.post(url, auth=awsauth, json=payload, headers=headers)
#
# print(r.text)
不要在美国东部1使用此功能,那么您必须使用此功能 重要 如果S3存储桶位于us-east-1区域,则必须使用“ endpoint”:“ s3.amazonaws.com”而不是“ region”:“ us-east-1”。
要为快照存储库启用由S3管理的密钥进行服务器端加密,请在“设置” JSON中添加“ server_side_encryption”:true。