Published on

How to clone an Elasticsearch index periodically

Authors

Search engines for enterprise information search

PUT /my-index/_settings
{
   "settings": {
      "index.blocks.write": true
   }
}
POST /my-index/_clone/my-new-index
PUT /my-index/_settings
{
   "settings": {
      "index.blocks.write": false
   }
}
```json


```PowerShell
# Define variables
$elasticUrl = "http://your-elastic-url.com:9200/"
$indexToClone = "my-index"
$contentType = 'application/json'
 
$currentDate = Get-Date -UFormat "%Y%m%d"
 
$readOnlySettingsOff = '{
   "settings" : {
      "index.blocks.write" : false
   }
}'
 
$readOnlySettingsOn = '{
   "settings" : {
      "index.blocks.write" : true
   }
}'
 
# Make source index readonly
Invoke-WebRequest -Uri "${elasticUrl}${indexToClone}/_settings" -ContentType $contentType -Method PUT -Body $readOnlySettingsOn
 
# Clone
Invoke-WebRequest -Uri "${elasticUrl}${indexToClone}/_clone/${indexToClone}_${currentDate}" -Method POST
 
# Make source index writable
Invoke-WebRequest -Uri "${elasticUrl}${indexToClone}/_settings" -ContentType $contentType -Method PUT -Body $readOnlySettingsOff