Skip to content

Instantly share code, notes, and snippets.

@radu-gheorghe
Created November 5, 2012 09:30
Show Gist options
  • Save radu-gheorghe/4016302 to your computer and use it in GitHub Desktop.
Save radu-gheorghe/4016302 to your computer and use it in GitHub Desktop.
copy one Elasticesarch index to another
#!/bin/bash
CURRENTINDEX="test"
NEWINDEX="newindex"
#where the indices are stored within the DATADIR
INDICESDIR=/var/lib/elasticsearch/elasticsearch/nodes/0/indices/
#get the metadata for the current index
curl localhost:9200/$CURRENTINDEX/_settings?pretty=true > /tmp/settings
curl localhost:9200/$CURRENTINDEX/_mapping?pretty=true > /tmp/mappings
#now let's hack our way to building a template for the new index
#in order to put the metadata
echo "{" > /tmp/template
#name of our template goes here
echo " \"template\": \"$NEWINDEX\"," >> /tmp/template
#add settings
grep -v -e "^{" -e "^}" -e "^\ \ }" -e "^\ \ \"" /tmp/settings >> /tmp/template
echo "," >> /tmp/template
#add mappings
sed -i s/^\ \ \"$CURRENTINDEX\"/\ \ \"mappings\"/ /tmp/mappings
grep -v "^{" /tmp/mappings >> /tmp/template
sleep 2
#put the template
curl -XPUT localhost:9200/_template/$NEWINDEX --data-binary @/tmp/template
#create the index
curl -XPUT localhost:9200/$NEWINDEX
sleep 2
#stop ES
/etc/init.d/elasticsearch stop
#copy all shards to the new index
for SHARD in `ls -1 $INDICESDIR/$CURRENTINDEX/ | grep -v "^_state$"`; do
cp -r $INDICESDIR/$CURRENTINDEX/$SHARD $INDICESDIR/$NEWINDEX/
done
#start ES
/etc/init.d/elasticsearch start
#optionally, delete the template we created
#curl -XDELETE localhost:9200/_template/$NEWINDEX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment