利用elastcdump命令備份es索引:
#!/bin/python
import subprocess
import os
import time
def dump_med(ip, index):
dumping = 'dumping %s'%index
print(dumping.center(80, '*'))
try:
subprocess.check_call('elasticdump --input=http://%s:9200/%s --output=./%s_data.json --type=data >> /dev/null 2>&1'%(ip, index, index), shell='true')
print('%s_data dumped success'%index)
except Exception as e:
print(e)
print('error: %s_data.json dump fail'%index)
time.sleep(1)
try:
subprocess.check_call('elasticdump --input=http://%s:9200/%s --output=./%s_mapping.json --type=mapping >> /dev/null 2>&1 '%(ip, index, index), shell='true')
print('%s_mapping dumped success'%index)
except Exception as e:
print(e)
print('error: %s_mapping.json dump fail'%index)
time.sleep(1)
ip = raw_input('input es ip:')
choice = raw_input('1:dump all or 2 dump a few >>')
os.mkdir('es_dump_index')
os.chdir('es_dump_index')
index_all = subprocess.check_output("curl http://%s:9200/_cat/indices 2>> /dev/null |awk '{print $3}'"%ip,shell='true')
if choice == '1':
for i in index_all.split():
dump_med(ip, i)
elif choice == '2':
indexs = raw_input("Enter the index name, separated by ','>>")
for i in indexs.split(','):
if i in index_all:
dump_med(ip, i)
else:
dumping = 'dumping %s'%i
print(dumping.center(80, '*'))
print('Check whether the index name is true')
else:
print('input error')
導入指定目錄下的所有索引,先導入mappig,在導入索引數據,maaping文件格式:test_mapping,json,data文件格式:test_data.json
#!/bin/python import subprocess import os import time import re def input_data(ip, index): dumping = 'dumping mapping of %s '%index print(dumping.center(80, '*')) try: subprocess.check_call('elasticdump --output=http://%s:9200/%s --input=./%s_data.json --type=data >> /dev/null 2>&1'%(ip, index, index), shell='true') print('%s_data dumped success'%index) except Exception as e: print(e) print('error: %s_data.json dump fail'%index) def input_mapping(ip, index): dumping = 'dumping data of %s'%index print(dumping.center(80, '*')) try: subprocess.check_call('elasticdump --output=http://%s:9200/%s --input=./%s_mapping.json --type=mapping >> /dev/null 2>&1 '%(ip, index, index), shell='true') print('%s_mapping dumped success'%index) except Exception as e: print(e) print('error: %s_mapping.json dump fail'%index) time.sleep(1) ip = raw_input('input es ip:') choice = raw_input("please input index abspath:") os.chdir(r'%s'%choice) mapping_list = [] data_list = [] for i in os.listdir('.'): if re.search('.*_mapping.*', i): mapping_list.append(i) elif re.search('.*_data.*', i): data_list.append(i) else: print("index'name format error:", i) for mapping in mapping_list: index = mapping.split('_mapping')[0] input_mapping(ip, index) time.sleep(1) for data in data_list: index = data.split('_data')[0] input_data(ip, index) time.sleep(1)
