利用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')