Skip to content

Instantly share code, notes, and snippets.

@vadirajks
Last active May 18, 2022 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadirajks/002a2772d216a08fbcb885cd3c52980c to your computer and use it in GitHub Desktop.
Save vadirajks/002a2772d216a08fbcb885cd3c52980c to your computer and use it in GitHub Desktop.
reindex elasticsearch indices
#!/bin/bash
if [ "$1" == "" ] || [ "$2" == "" ]; then
echo "Usage: ./reindex.sh [REMOTE_HOST:REMOTE_PORT] [INDEX_PATTERN] [LOCAL_HOST:LOCAL_PORT]"
exit 1
fi
REMOTE_HOST=$1
PATTERN=$2
if [ "$3" == "" ]; then
LOCAL_HOST="localhost:9200"
else
LOCAL_HOST=$3
fi
echo "---------------------------- NOTICE ----------------------------------"
echo "You must ensur you have the following setting in your local ES host's:"
echo "elasticsearch.yml config (the one re-indexing to):"
echo " reindex.remote.whitelist: $REMOTE_HOST"
echo "Also, if an index template is necessary for this data, you must create"
echo "locally before you start the re-indexing process"
echo "----------------------------------------------------------------------"
sleep 3
INDICES=$(curl --silent "$REMOTE_HOST/_cat/indices/$PATTERN?h=index")
TOTAL_INCOMPLETE_INDICES=0
TOTAL_INDICES=0
TOTAL_DURATION=0
INCOMPLETE_INDICES=()
for INDEX in $INDICES; do
TOTAL_DOCS_REMOTE=$(curl --silent "http://$REMOTE_HOST/_cat/indices/$INDEX?h=docs.count")
echo "Attempting to re-indexing $INDEX ($TOTAL_DOCS_REMOTE docs total) from remote ES server..."
SECONDS=0
curl -XPOST "http://$LOCAL_HOST/_reindex?wait_for_completion=true&pretty=true" -d "{
\"conflicts\": \"proceed\",
\"source\": {
\"remote\": {
\"host\": \"http://$REMOTE_HOST\"
},
\"index\": \"${INDEX}\"
},
\"dest\": {
\"index\": \"${INDEX}\"
}
}"
duration=$SECONDS
LOCAL_INDEX_EXISTS=$(curl -o /dev/null --silent --head --write-out '%{http_code}' "http://$LOCAL_HOST/$INDEX")
if [ "$LOCAL_INDEX_EXISTS" == "200" ]; then
TOTAL_DOCS_REINDEXED=$(curl --silent "http://$LOCAL_HOST/_cat/indices/$INDEX?h=docs.count")
else
TOTAL_DOCS_REINDEXED=0
fi
echo " Re-indexing results:"
echo " -> Time taken: $(($duration / 60)) minutes and $(($duration % 60)) seconds"
echo " -> Docs indexed: $TOTAL_DOCS_REINDEXED out of $TOTAL_DOCS_REMOTE"
echo ""
TOTAL_DURATION=$(($TOTAL_DURATION+$duration))
if [ "$TOTAL_DOCS_REMOTE" -ne "$TOTAL_DOCS_REINDEXED" ]; then
TOTAL_INCOMPLETE_INDICES=$(($TOTAL_INCOMPLETE_INDICES+1))
INCOMPLETE_INDICES+=($INDEX)
fi
TOTAL_INDICES=$((TOTAL_INDICES+1))
done
echo "---------------------- STATS --------------------------"
echo "Total Duration of Re-Indexing Process: $((TOTAL_DURATION / 60))m $((TOTAL_DURATION % 60))"
echo "Total Indices: $TOTAL_INDICES"
echo "Total Incomplete Re-Indexed Indices: $TOTAL_INCOMPLETE_INDICES"
if [ "$TOTAL_INCOMPLETE_INDICES" -ne "0" ]; then
printf '%s\n' "${INCOMPLETE_INDICES[@]}"
fi
echo "-------------------------------------------------------"
echo ""
#!/bin/bash
#ES_HOST="localhost"
#ES_PORT="9200"
TMP="_v2"
echo "**** Script Execution Started ****"
echo " "
echo "**** Fetching List of Indices Elasticsearch Reindexing ****"
curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc'
echo " "
sleep 5
indices_list=$(curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc' | awk '{print $3}' | sed -n '1!p')
#indices=$(curl -s "http://${ES_HOST}:${ES_PORT}/_cat/indices/abc_*?h=index" | egrep 'abc_[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{8}*')
# do for all abc elastic indices
count=0
echo "$indices_list"
for index in $indices_list
do
echo " "
echo "**** Index No: $count ****"
echo "**** Present Index we are iterating: ${index} ****"
count=`expr $count + 1`
echo " "
echo " "
echo "Reindex process starting for index: $index"
tmp_index=$index${TMP}
output=$(curl -s -X PUT "http://localhost:9200/$tmp_index" -H 'Content-Type: application/json' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 5,
"number_of_replicas" : 1
}
}
}')
echo " "
echo "Temporary index: $tmp_index created with output: $output"
echo "Starting reindexing elastic data from original index: $index to temporary index: $tmp_index"
output=$(curl -s -X POST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "'$index'"
},
"dest": {
"index": "'$tmp_index'"
}
}
')
echo " "
echo "Reindexing completed from original index: $index to temporary index: $tmp_index with output: $output"
echo " "
echo "Deleting $index"
output=$(curl -s -X DELETE "http://localhost:9200/$index")
echo "$index deleted with status: $output"
echo " "
echo "Creating index: $index"
output=$(curl -s -X PUT "http://localhost:9200/$index" -H 'Content-Type: application/json' -d'
{
"settings" : {
"index" : {
"number_of_shards" : 5,
"number_of_replicas" : 1
}
}
}')
echo " "
echo "Index: $index creation status: $output"
echo " "
echo "Starting reindexing elastic data from temporary index: $tmp_index to original index: $index"
output=$(curl -s -X POST "http://localhost:9200/_reindex" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "'$tmp_index'"
},
"dest": {
"index": "'$index'"
}
}
')
echo " "
echo "Reindexing completed from temporary index:$tmp_index to original index:$index with output: $output"
echo " "
echo "Deleting $tmp_index"
output=$(curl -s -X DELETE "http://localhost:9200/$tmp_index")
echo "$tmp_index deleted with status: $output"
echo " "
done
echo " "
sleep 5
echo "**** Fetching List of Indices After Elasticsearch Reindexing ****"
curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc'
echo " "
sleep 5
echo " "
echo "**** Original indices list ****"
echo "$indices_list"
echo "**** No. of indices in original list: $count ****"
echo " "
count1=0
MIndices_list=$(curl -s -X GET 'http://localhost:9200/_cat/indices/%2A?v=&s=index:desc' | awk '{print $3}' | sed -n '1!p')
echo " "
echo "**** Modified indices list ****"
echo "$MIndices_list"
for j in $MIndices_list
do
count1=`expr $count1 + 1`
done
echo " "
echo "**** No. of indices in modified list: $count1 ****"
echo "**** Script Execution Ended ****"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment