该python备份snapshot的索引数据脚本,通过Elasticsearch连接es,然后通过es.indices.get_alias函数获取所有索引名称,通过列表的startswith函数剔除.开头的自带索引名称,然后把所有索引名称放到字符串.
通过es.snapshot.create_repository创建快照仓库,
通过es.snapshot.create函数完成快照备份.
# -*- coding: utf-8 -*-
import time
from elasticsearch import Elasticsearch
import sys
reload(sys)
sys.setdefaultencoding('utf8')
es = Elasticsearch(["http://es-cn*******.public.elasticsearch.aliyuncs.com"],http_auth=('*****', '*********'),Transport=9200)
# 获取所有的索引名称,包含.moni开头和.kibana
indices = es.indices.get_alias().keys()
# 获取当前时间,精确到秒
time_string = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
indices_list = []
# 把所有索引获取后,去除.开头的索引,赋予indices_list列表
for i in indices:
new_indices = i
if new_indices.startswith('.'):
pass
else:
indices_list.append(new_indices)
# 把需要备份的索引列表转换成字符串
indices_string = ','.j