Friday 18 November 2016

Elasticsearch Backup And Restore

How To Take Elasticsearch Backup (Snapshot) And Restore

This post is about "How to take Elasticsearch backup and restore it back".

Video Tutorial:

Elasticsearch Installation

brew is a tool on mac to install stuff. As Elasticsearch requires Java, we need to install java first and than Elasticsearch.

brew update
brew cask install java
brew search elasticsearch


elasticsearch                    
homebrew/versions/elasticsearch24
homebrew/versions/elasticsearch17

brew install homebrew/versions/elasticsearch17

Check if elasticsearch has installed or not.

brew info elasticsearch

Creating first document with index

nameindex - name of the index
names - The "type" having similar data/info
myid - The id for the data
pretty - Simply means we want well-formatted output

curl -XPOST 'localhost:9200/nameindex/names/myid?pretty' -d '
{
    "name": "Ajeet",
    "email_address": "ajeet@example.com"
}'



If you insert another record with the same id "myid" than existing record will get updated.

Creating A Document without "id"

curl -XPOST 'localhost:9200/nameindex/names?pretty' -d '
{
    "name": "no_id",
    "email_address": "no_id@example.com"
}'



As you can see it has automatically generated the "id".

Backup & Resotre

For Backup we need to create two repository (logical and physical).
Let's create the physical repository


mkdir es_backup


This would be the parent directory which will have all backups.
Now we will tell elasticsearch, the path of our repository in config file.

vi /usr/local/Cellar/elasticsearch17/1.7.5/config/elasticsearch.yml


add the repository path:

path.repo: /Users/zombie/es_bakcup


save and exit.
Check if any snapshot is present 

curl localhost:9200/_snapshot


The output would be blank as we have not taken any snapshot yet.


Snapshot can be taken for few or a particular index or of the whole cluster. To take backup index-wise create sub-directory inside the parent repository.



Now we will create logical repository. Here we will take snapshot of particular index "nameindex"


Restart elasticsearch to take path.repo changes and run the following to register the repository:


curl -XPUT 'localhost:9200/_snapshot/nameindexbackup' -d '{
    "type": "fs",
    "settings": {
        "location": "nameindex",
        "compress": "true"
    }
}'




No need to specify the full path in "location" as we have already specified it in the config file path.repo.

This will create the logical repository which you can check in the parent folder. A sub directory must have been created with name "nameindex".




Now we will take the backup


CURL -XPUT 'localhost:9200/_snapshot/nameindexbackup/12nov16' -d '{
    "indices": "nameindex",
    "ignore_unavailable": true,
    "include_global_state": false
}'


indices field may have multiple index with comma separated values.
ignore_unavailable means don't stop the snapshot process if any shards is not available.
include_global_state means take snapshot of cluster status also. If the status is yellow than while restore the yellow status will be there. If you don't want this than set it to false.





To check if snapshot is completed or not fire a GET request

curl localhost:9200/_snapshot/nameindexbackup/12nov16





Restore the Snapshot

First delete the data from elasticsearch

curl -XDELETE localhost:9200/nameindex

Now if you throw a GET request you will get 404. 
Let's restore the index now.

curl -XPOST localhost:9200/_snapshot/nameindexbackup/12nov16/_restore




Now do a GET request if data is restored or not.





We have successfully restored the elasticsearch data.

During restore you can also provide certain information 

curl -XPOST 'localhost:9200/_snapshot/nameindexbackup/12nov16/_restore' -d '{
    "indices": "index_1,index_2",
    "ignore_unavailable": true,
    "include_global_state": true,
    "rename_pattern": "index_(.+)",
    "rename_replacement": "restored_index_$1"
}'

This is how snapshot and restore works in Elasticsearch. Any doubts or suggestions are welcome in the comment box.

14 comments:

  1. Hi,

    Same like how you explained as follwed is working in the same elasticsearch cluster. When I try same backup file in another cluster it is not wokring. Here I was using dockers. In first docker cluster I creatred back up , restore every thing working fine. But After recreate cluster , this backup not wokring.

    ReplyDelete
    Replies
    1. What's the error you are getting. Can you please share the error snippet.

      Delete
  2. How would I take complete backup?

    ReplyDelete
  3. "path.repo" seems to be unsupported in version 5.5.0 of elasticsearch.

    Is that so?

    How can be take backup in that case?

    ReplyDelete
    Replies
    1. This is in centos 'systemd' where settings are either given in "/usr/lib/systemd/system/elasticsearch.service" or "/etc/systemd/system/elasticsearch.service.d"

      Delete
  4. thanks first ,
    now I made a backup in my ES local version successfully, I would like to restore it on the hosting server; how can I do that ?
    thanks again !

    ReplyDelete
  5. Hi Ajit,

    I have some query, please help me to understand.

    what i did:

    1. I have Dev cluster running 2.4.2.3 and I took a backup.
    2. I have another QA cluster running 2.4.2.3.
    3. QA cluster has only four indices with few documents.
    4. Now I upgarded QA cluster to 5.1.2.6.
    Now when I tried to restore backup taken from CI, it is not restoring and not throwing any error, please suggest me what is the mistake.

    ReplyDelete
    Replies
    1. Please post the error you are getting.

      Delete
    2. thank you Ajit, I am not getting any error.


      Last login: Thu Dec 7 12:33:19 2017 from 10.30.196.220
      asadmin@c548nms:~> curl -XPUT 'https://demo-search-es.int.thomsonreuters.com/_snapshot/ES_backups' -d '{
      > "type": "fs",
      > "settings": {
      > "location": "/tools/novusRelease/ES_backups",
      > "compress": true
      > }
      > }'
      asadmin@c548nms:~> curl -XPOST https://demo-search-es.int.thomsonreuters.com/_snapshot/ES_backups/snapshot_1/_restore
      asadmin@c548nms:~>

      Delete
  6. I have Dev cluster running 2.4.X and I took a backup.
    curl -XPUT 'https://ci-search-es.int.thomsonreuters.com/_snapshot/ES_backups' -d '{
    "type": "fs",
    "settings": {
    "location": "/tools/novusRelease/ES_backups",
    "compress": true
    }
    }'


    curl -XPOST 'https://ci-search-es.int.thomsonreuters.com/_snapshot/ES_backups/_verify'

    curl -XPUT "https://ci-search-es.int.thomsonreuters.com/_snapshot/ES_backups/snapshot_2?wait_for_completion=true"


    I have another QA cluster running 2.4.X
    QA cluster has only four indices with few documents.
    Now I upgarded QA cluster to 5.1.X
    Now when I tried to restore backup taken from CI, it is not restoring and not throwing any error, please suggest me what is the mistake.

    Question is, can we restore 2.4.X backup snapshot from dev cluster to a demo cluster which is migrated from 2.4.x to 5.1.x?

    CI is still have 2.4.x, only another demo cluster I want to upgarde to 5.1.x and restore CI backup to it after upgradation?

    ReplyDelete
  7. hey, Thanks for the turtorial. I am new to ELK stack...

    We are moving ELK stack from containers to ECK...

    1) Take backup snapshot
    2) Provision the ECK setup
    3) Restore the backup from fs to ECK new set of PV's...

    am I right in htis procedure. Plz share me if have any blogs or videos links which will help me to understnad step by step procedure.

    Does all the pipelines will be taken in the snapshot backup or do we need to manually recreate all the pipelines with ports?

    ReplyDelete
  8. Hello, how do you restore a backup from an older version of Elasticserch. In the new instalation there is nothing created so after you create the Repo how do you restore all the data. Thank you

    ReplyDelete
  9. Thank you for the nice tutorial.

    Have you run into issues with restored snapshots into another cluster but now showing any documents?

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel