Skip to content

Instantly share code, notes, and snippets.

@naushadzaman
Created July 23, 2016 01:16
Show Gist options
  • Save naushadzaman/0ba7cb9958799bd46a01a559de4f9c3d to your computer and use it in GitHub Desktop.
Save naushadzaman/0ba7cb9958799bd46a01a559de4f9c3d to your computer and use it in GitHub Desktop.
Backup Data and Mapping from one Elastic Search server to another
import json
from elasticsearch import Elasticsearch
from elasticsearch import helpers
from elasticsearch.client import IndicesClient
source = '192.131.117.237:9200/' # example
dest = '192.226.457.547:9200/' # another example
es_source = Elasticsearch([source])
es_dest = Elasticsearch([dest])
_indices=es_source.indices.get_aliases().keys()
print _indices
for _ind in _indices:
_mapping = es_source.indices.get_mapping(index=_ind)
print _ind
mapping = {'mappings':_mapping[_ind]["mappings"]}
if es_dest.indices.exists(index=_ind):
print 'already exists'
es_dest.indices.delete(index=_ind)
es_dest.indices.create(index=_ind, body=mapping)
print 'mapping created'
print ''
for doc_type in mapping['mappings'].keys():
for each in helpers.scan(es_source, query={"query": {"match_all": {}}}, index=_ind, doc_type=doc_type):
res = es_dest.index(index=_ind, doc_type=doc_type, body=each['_source'])
print res[u'created'], _ind, doc_type, each['_id']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment