zoukankan      html  css  js  c++  java
  • elasticsearch备份及导入索引脚本

    利用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)
  • 相关阅读:
    HDUOJ---------(1045)Fire Net
    HDUOJ----(1175)连连看
    HDUOJ-----(1072)Nightmare(bfs)
    deque容器的运用一点一点积累
    HDUOJ----(1016)Prime Ring Problem
    HDUOJ----Safecracker(1015)
    hduoj---Tempter of the Bone
    VC6.0设置注释快捷键
    nyoj------------找球号(一)
    set 容器 的全解(转)
  • 原文地址:https://www.cnblogs.com/hel7512/p/12350347.html
Copyright © 2011-2022 走看看