Skip to content

Instantly share code, notes, and snippets.

@tatsunori-nishikori
Last active June 27, 2017 07:44
Show Gist options
  • Save tatsunori-nishikori/2eed2c2a143f41a9b074eedf4f6ae7f4 to your computer and use it in GitHub Desktop.
Save tatsunori-nishikori/2eed2c2a143f41a9b074eedf4f6ae7f4 to your computer and use it in GitHub Desktop.
ElasticSearch snapshot

1. elasticsearch.yml

# スナップショットを作成したいPathを指定する
path.repo: /usr/local/var/elasticsearch/snapshot

2. 設定読み込み

$ brew services restart elasticsearch
$ curl -XGET localhost:9200/_nodes/?pretty
...
       "path" : {
          "data" : [
            "/usr/local/var/elasticsearch/"
          ],
          "logs" : "/usr/local/var/log/elasticsearch",
          "home" : "/usr/local/Cellar/elasticsearch/5.4.0/libexec",
          "repo" : "/usr/local/var/elasticsearch/snapshot"
        },

3. snapshot リポジトリ登録

curl -XPUT localhost:9200/_snapshot/snapshot -d '
{
    "type": "fs",
    "settings": {
        "location": "/usr/local/var/elasticsearch/snapshot",
        "compress": true
    }
}
'

4. snapshot 作成

curl -XPUT localhost:9200/_snapshot/snapshot/bank-$(date +%Y-%m-%d)?wait_for_completion=true -d '
{
    "indices": "bank",
    "ignore_unavailable": true,
    "include_global_state": false
}'
{"snapshot":{"snapshot":"bank-2017-06-09","uuid":"-VkDP0CuTe-K4bN5tIbdag","version_id":5040099,"version":"5.4.0","indices":["bank"],"state":"SUCCESS","start_time":"2017-06-09T03:05:30.702Z","start_time_in_millis":1496977530702,"end_time":"2017-06-09T03:05:30.767Z","end_time_in_millis":1496977530767,"duration_in_millis":65,"failures":[],"shards":{"total":5,"failed":0,"successful":5}}}%

5. snapshot確認

curl -XGET localhost:9200/_snapshot/snapshot/bank-2017-06-09?pretty
{
  "snapshots" : [
    {
      "snapshot" : "bank-2017-06-09",
      "uuid" : "-VkDP0CuTe-K4bN5tIbdag",
      "version_id" : 5040099,
      "version" : "5.4.0",
      "indices" : [
        "bank"
      ],
      "state" : "SUCCESS",
      "start_time" : "2017-06-09T03:05:30.702Z",
      "start_time_in_millis" : 1496977530702,
      "end_time" : "2017-06-09T03:05:30.767Z",
      "end_time_in_millis" : 1496977530767,
      "duration_in_millis" : 65,
      "failures" : [ ],
      "shards" : {
        "total" : 5,
        "failed" : 0,
        "successful" : 5
      }
    }
  ]
}

6.リストア

statusがopenのindexに対してはリストア出来ない

curl -XPOST localhost:9200/bank/_close

たとえcloseにしても、すでにあるindexのnode数が違うとリストア出来ない

curl -XPOST localhost:9200/_snapshot/snapshot/bank-2017-06-09/_restore -d '
{
    "indices": "_bank",
    "ignore_unavailable": true,
    "include_global_state": false
}
'

別サーバから移行する場合、snapshotのディレクトリをまるごとコピーし、syncさせる。その上でリストアをする

  • snapshot一覧確認
curl -XGET "localhost:9200/_snapshot/snapshot/_all?pretty"

aliasの作成

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
  "actions" : [
    { "add" : { "index" : "category1", "alias" : "products" } },
    { "add" : { "index" : "category2", "alias" : "products" } }
  ]
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment