使用python和snapshot备份ElasticSearch索引数据

本文介绍了一种使用Python脚本备份ElasticSearch索引数据的方法。首先,通过Elasticsearch API获取所有索引名称,排除以.开头的内置索引。接着,创建快照仓库,然后调用create函数执行快照备份操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值