Skip to content

Instantly share code, notes, and snippets.

@jasonbartz
Created November 24, 2015 23:09
Show Gist options
  • Save jasonbartz/93f55f7c1765f1dca12c to your computer and use it in GitHub Desktop.
Save jasonbartz/93f55f7c1765f1dca12c to your computer and use it in GitHub Desktop.
How to recover elastic search shards
"""
Inspired by https://t37.net/how-to-fix-your-elasticsearch-cluster-stuck-in-initializing-shards-mode.html
3 node cluster, when 2 nodes have been terminated simultaneously
Not guaranteed to prevent data loss or fully recover lost data
"""
import requests
import json
HEALTHY_NODE = "10.65.15.5"
NEW_NODE_A = "10.65.14.5"
NEW_NODE_B = "10.65.13.5"
obj = requests.get("http://%s:9200/_cat/shards?format=json" % HEALTHY_NODE).json()
command_list = []
for item in obj:
if item["state"] == "UNASSIGNED" and item["prirep"] == "p":
if item["node"] == NEW_NODE_A:
node = NEW_NODE_B"
else:
node = NEW_NODE_A
command_list.append({
"allocate" : {
"index" : item["index"],
"shard" : item["shard"],
"node" : node,
"allow_primary" : True
}
})
requests.post("http://%s:9200/_cluster/reroute" % HEALTHY_NODE, data=json.dumps({"commands" : command_list}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment