Skip to content

Instantly share code, notes, and snippets.

@kholisrag
Created August 28, 2024 16:09
Show Gist options
  • Save kholisrag/bdf375a0f1739dee5af827a3bdc32854 to your computer and use it in GitHub Desktop.
Save kholisrag/bdf375a0f1739dee5af827a3bdc32854 to your computer and use it in GitHub Desktop.
Elasticsearch Indicies Reindex Script
#!/bin/bash
# Elasticsearch credentials
ELASTIC_USER="elastic"
ELASTIC_PASSWORD="elasticpassword"
ELASTIC_URL="http://10.1.1.1:9200"
# File containing index names
INDEX_FILE="filtered-index.txt"
# Iterate through each line (index name) in the file
while read -r INDEX_NAME; do
echo "Processing index: $INDEX_NAME"
# Skip commented lines
if [[ $INDEX_NAME == \#* ]]; then
echo "Skipping index: $INDEX_NAME"
continue
fi
# Construct the new index name
NEW_INDEX_NAME="${INDEX_NAME}-new"
# Prepare the JSON payload for the _reindex API (without wait_for_completion)
PAYLOAD='{
"source": {
"index": "'"$INDEX_NAME"'"
},
"dest": {
"index": "'"$NEW_INDEX_NAME"'"
}
}'
# Make the cURL request to the _reindex API with wait_for_completion=false in the query parameter
echo "Triggering reindex from $INDEX_NAME to $NEW_INDEX_NAME..."
RESPONSE=$(curl -u "$ELASTIC_USER:$ELASTIC_PASSWORD" \
-XPOST "$ELASTIC_URL/_reindex?wait_for_completion=false" \
-H "kbn-xsrf: reporting" \
-H "Content-Type: application/json" \
-d "$PAYLOAD")
# Extract the task ID from the response
TASK_ID=$(echo "$RESPONSE" | jq -r '.task')
echo "Get Current Task ID: $TASK_ID"
# Check the cURL exit status and provide feedback
if [ $? -eq 0 ]; then
echo "Reindex from $INDEX_NAME to $NEW_INDEX_NAME triggered. Task ID: $TASK_ID"
# Wait for the task to complete
while true; do
TASK_STATUS=$(curl -u "$ELASTIC_USER:$ELASTIC_PASSWORD" \
"$ELASTIC_URL/_tasks/$TASK_ID")
if echo "$TASK_STATUS" | jq -e '.completed' >/dev/null; then
echo "Task $TASK_ID completed."
break
fi
sleep 5 # Wait for 5 seconds before checking again
done
else
echo "Reindex from $INDEX_NAME to $NEW_INDEX_NAME failed."
fi
done < "$INDEX_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment