# Scripts for fix unassigned shards

**URL:** https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019
**Category:** Elasticsearch
**Tags:** docker
**Created:** [September 8, 2022, 9:14pm UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019 "2022-09-08T21:14:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [September 8, 2022, 9:14pm UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/1 "2022-09-08T21:14:25Z")

</div>

Hello Can You point me out what's wrong in below short script

```auto
#!/bin/bash

NODE="es_data_1"
IFS=$'\n'
for line in $(curl -u elastic:pass 'http://10.220.111.200:9200/_cat/shards' | fgrep UNASSIGNED); do
  INDEX=$(echo $line | (awk '{print $1}'))
  SHARD=$(echo $line | (awk '{print $2}'))

  curl -u elastic:pass 'http://10.220.111.200:9200/_cluster/reroute' -d '{
     "commands": [
        {
            "allocate": {
                "index": "'$INDEX'",
                "shard": '$SHARD',
                "node": "'$NODE'",
                "allow_primary": true
          }
        }
    ]
  }'
done

```

at least I've got not desire output

```auto

{"error":{"root_cause":[{"type":"named_object_not_found_exception","reason":"[6:22] unknown field [allocate]"}],"type":"x_content_parse_exception","reason":"[6:22] [cluster_reroute] failed to parse field [commands]","caused_by":{"type":"named_object_not_found_exception","reason":"[6:22] unknown field [allocate]"}},"status":400}
------------------------------
UNASSIGNED

```

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [September 8, 2022, 9:34pm UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/2 "2022-09-08T21:34:01Z")

</div>

Try adding a `set -x` at the top to enable a bit of debugging.

However you should't need a script to fix things like this. If you are having issues with allocation, perhaps digging into it further will fix the underlying issue?

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [September 9, 2022, 5:37am UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/3 "2022-09-09T05:37:28Z")

</div>

> [@warkolm](#):
>
> set -x

Yes this script has some bug probably  
this is the output:

```auto
+ for line in '$(curl -u elastic:pass http://10.220.111.200:9203/_cat/shards | grep UNASSIGNED)'
++ echo UNASSIGNED
++ awk '{print $1}'
+ INDEX=UNASSIGNED
++ echo UNASSIGNED
++ awk '{print $2}'
+ SHARD=

```

I have a lot of unassigned shards some I need to fix it by reroute with allow\_primary": true parameter.

```auto
current_state" : "unassigned",
  "unassigned_info" : {
    "reason" : "CLUSTER_RECOVERED",
    "at" : "2022-09-08T17:15:35.604Z",
    "last_allocation_status" : "no_valid_shard_copy"
  },
  "can_allocate" : "no_valid_shard_copy",
  "allocate_explanation" : "cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster",

```

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [September 9, 2022, 1:26pm UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/4 "2022-09-09T13:26:59Z")

</div>

ok, I've found the bug we can close this case

---

<div class="post-metadata">

### Author: ![ropc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ropc/32/47022_2.png) [@ropc](https://discuss.elastic.co/u/ropc)
#### Post date: [September 10, 2022, 1:14am UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/5 "2022-09-10T01:14:24Z")

</div>

@INS - I agree with @warkolm , you should not need a script like this. Identifying the reason of these unassigned shards using the [cluster allocation explain API](https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-allocation-explain.html) should be the recommended approach. This will help you to identify why these shards are unassigned and you can address the problem accordingly.

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [September 11, 2022, 10:41pm UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/6 "2022-09-11T22:41:46Z")

</div>

> [@INS](#):
>
> ` "allocate_explanation" : "cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster",`

Have You already read all the topic?

It was pointed out why this shards have not been assigned " "allocate\_explanation" : "cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster"," Do You have any ideas, preferences ?

---

<div class="post-metadata">

### Author: ![ropc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ropc/32/47022_2.png) [@ropc](https://discuss.elastic.co/u/ropc)
#### Post date: [September 12, 2022, 1:20am UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/7 "2022-09-12T01:20:30Z")

</div>

@INS

> Have You already read all the topic?

Yes we did.

That is why we highlighted that you should dig into the allocation failure and address the root cause.

Elasticsearch reports that `no_valid_shard_copy ` and `cannot allocate because a previous copy of the primary shard existed but can no longer be found on the nodes in the cluster` - so the question would be to understand what happened to the primary shard? Has the corresponding node left the cluster? temporarily (e.g instability, performance issues) or definitively (hardware failure, node is forever gone, etc.)?

You would need to look as well whether your indices are configured with at least 1 replica shard. Indices without replica shards are not highly available.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 10, 2022, 1:20am UTC](https://discuss.elastic.co/t/scripts-for-fix-unassigned-shards/314019/8 "2022-10-10T01:20:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
