目的
ealsticsearchのsnapshotとrestore機能を利用してAサーバのindexデータをBサーバに移行してみましょう!
リポジトリ作成・確認・削除
アクセス可能なディレクトリを作成
chown elasticsearch:elasticsearch /data/es_snapshot/
スナップショットを作成する前に以下のようにしてリポジトリを作成する必要がある。
curl -XPUT 'http://localhost:9200/_snapshot/my_backup' -d '{ "type": "fs", "settings": { "location": "/home/s-kim/es_snapshot", "compress": true } }'
実行結果:
[2014-04-03 17:36:13,743][INFO ][repositories ] [Miles Warren] put repository [my_backup] {"acknowledged":true}
リポジトリの確認
curl -XGET 'http://localhost:9200/_snapshot'
実行結果:
curl -XGET 'http://localhost:9200/_snapshot' {"my_backup":{"type":"fs","settings":{"compress":"true","location":"/home/s-kim/es_snapshot"}}}
リポジトリ削除したい場合
curl -XDELETE "localhost:9200/_snapshot/my_backup?pretty"
リポジトリの種類
リポジトリのプラグイン
スナップショット作成・確認
curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true"
実行結果:
[2014-04-03 17:48:55,329][INFO ][snapshots ] [Miles Warren] snapshot [my_backup:snapshot_1] is done {"snapshot":{"snapshot":"snapshot_1","indices":["kibana-int","dictionary"],"state":"SUCCESS","start_time":"2014-04-03T08:48:43.693Z","start_time_in_millis":1396514923693,"end_time":"2014-04-03T08:48:55.332Z","end_time_in_millis":1396514935332,"duration_in_millis":11639,"failures":[],"shards":{"total":10,"failed":0,"successful":10}}}[root@vps01442 tmp]#
*wait_for_completionオプションはスナップショット終了を「待つ」、「待たない」の制御が出来る
作成されてるファイル確認
ls –la /data/es_snapshot -rw-rw-r-- 1 elasticsearch elasticsearch 31 Apr 3 17:48 index drwxrwxr-x 4 elasticsearch elasticsearch 4096 Apr 3 17:48 indices -rw-rw-r-- 1 elasticsearch elasticsearch 184 Apr 3 17:48 metadata-snapshot_1 -rw-rw-r-- 1 elasticsearch elasticsearch 195 Apr 3 17:48 snapshot-snapshot_1
*追記
僕の場合はスナップショットしたいindexを指定して実行した
実行例
curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_3" -d '{"indices": "elastic3","ignore_unavailable": "false","include_global_state":false}' {"accepted":true}
補足
elastic3というindexのデータをスナップショットした
ignore_unavailableがtrueであればindexが空の場合でも実行するがfalseであれば空ならエラーを返す
スナップショットしたものをディレクトリごとに移行先サーバへ移行する
参考ページ:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-snapshots.html
リストア
上記でスナップショットしたデータを移行先に持ってきたら以下のコマンドを打つ
curl -XPUT 'http://localhost:9200/_snapshot/my_backup' -d '{ "type": "fs", "settings": { "location": "/home/estore/es_snapshot", "compress": true } }'
僕の場合"/home/estore/es_snapshot"ディレクトリへ持ってきたので上記のようにlocation設定をした
で最後に以下のコマンドを打つと移行元のサーバにあったデータをコピーできるはず
curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_3/_restore"
だったが、うまくいかない。。理由は「エラー3」にまとめる
とりあえず以下のコマンドを利用してリストアーできた
curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_3/_restore?pretty" -d '{ "indices": "elastic3", "ignore_unavailable": "true", "include_global_state": false, "rename_pattern": "elastic3", "rename_replacement": "elastic3_restore" }'
エラー
[root@jmeter s-kim]# curl -XPUT 'http://localhost:9200/_snapshot/my_backup' -d '{ "type": "fs", "settings": { "location": "/home/s-kim/es_snapshot", "compress": true } }' {"error":"RepositoryException[[my_backup] failed to create repository]; nested: CreationException[Guice creation errors:\n\n1) Error injecting constructor, org.elasticsearch.common.blobstore.BlobStoreException: Failed to create directory at [/home/s-kim/es_snapshot]\n at org.elasticsearch.repositories.fs.FsRepository.<init>(Unknown Source)\n while locating org.elasticsearch.repositories.fs.FsRepository\n while locating org.elasticsearch.repositories.Repository\n\n1 error]; nested: BlobStoreException[Failed to create directory at [/home/s-kim/es_snapshot]]; ","status":500}[root@jmeter s-kim]#
対策:/home/s-kim/es_snapshotのディレクトリ権限を確認する
僕の場合s-kimディレクトリに書き込み・実行権限がなかったのでこのエラーが発生した。
エラー2
"state":"FAILED","reason":"Indices don’t have primary shards
[root@jmeter home]# curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true" {"snapshot":{"snapshot":"snapshot_1","indices":["dictionary","apache_access-201409","movies_","twitter","test","movies","dictionar2y","dictionayry","elastic3","kibana-int","elastic","cluster_elastic","elastic2"],"state":"FAILED","reason":"Indices don't have primary shards +[[twitter, apache_access-201409, dictionar2y, dictionayry, movies, elastic2, movies_, kibana-int, dictionary, cluster_elastic]]","start_time":"2014-10-20T04:16:31.271Z","start_time_in_millis":1413778591271,"end_time":"2014-10-20T04:16:31.763Z","end_time_in_millis":1413778591763,"duration_in_millis":492,"failures":[],"shards":{"total":0,"failed":0,"successful":0}}}[root@jmeter home]#
対策:
primary shardsが探せないということだが、なぜ出るのか不明
indicesオプションで自分がスナップショットしたいindexをはっきり指定しておいたら解消された。
curl -XPUT "localhost:9200/_snapshot/my_backup/snapshot_3" -d '{"indices": "elastic3","ignore_unavailable": "false","include_global_state":false}' {"accepted":true}
エラー3
curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_3/_restore"
{"error":"SnapshotRestoreException[[my_backup:snapshot_3] cannot restore index [elastic3] because it’s open]","status":500}
対策:
elastic3というindexが既にあるので失敗したという意味以下のようにrename_patternとrename_replacementオプションを利用すると回避できる
[root@v157-7-209-126 es_snapshot]# curl -XPOST "localhost:9200/_snapshot/my_backup/snapshot_3/_restore?pretty" -d '{ > "indices": "elastic3", > "ignore_unavailable": "true", > "include_global_state": false, > "rename_pattern": "elastic3", > "rename_replacement": "elastic3_restore" > }' { "accepted" : true }
http://inokara.hateblo.jp/entry/2014/02/15/094503
参考したページ:
http://inokara.hateblo.jp/entry/2014/02/09/005409
http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/modules-snapshots.html
http://inokara.hateblo.jp/entry/2014/02/09/005409
http://inokara.hateblo.jp/entry/2014/02/04/015029
http://chrissimpson.co.uk/elasticsearch-snapshot-restore-api.html