非原創,只是留作自己查詢使用,轉自http://keenwon.com/1393.html
Elasticsearch的備份和恢復
備份
Elasticsearch的一大特點就是使用簡單,api也比較強大,備份也不例外。簡單來說,備份分兩步:1、創建一個倉庫。2、備份指定索引。下面一步一步來:
1、創建一個倉庫(creating the repository)
假如共享文件存儲介質掛載在/mount/backups/my_backup目錄下,需要在elasticsearch.yml添加如下配置:
path.repo: ["/mount/backups/my_backup"]
否則在注冊時,報如下錯誤:
{"error":"RepositoryException[[backup] failed to create repository]; nested: CreationException[Guice creation errors:\n\n1) Error injecting constructor, org.elasticsearch.repositories.RepositoryException: [backup] location [/mount/bak] doesn't match any of the locations specified by path.repo because this setting is empty\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: RepositoryException[[backup] location [/mount/bak] doesn't match any of the locations specified by path.repo because this setting is empty]; ","status":500}
備份數據之前,要創建一個倉庫來保存數據,倉庫的類型支持Shared filesystem, Amazon S3, HDFS和Azure Cloud。下面以文件系統為例:
- PUT http://127.0.0.1:9200/_snapshot/my_backup
- {
- "type": "fs",
- "settings": {
- "location": "/mount/backups/my_backup"
- }
- }
上面的代碼,我們創建了一個名叫my_backup
的備份,存放在本地的/mount/backups/my_backup
目錄下。除了location
參數外,還可以通過max_snapshot_bytes_per_sec
和max_restore_bytes_per_sec
來限制備份和恢復時的速度,如下:
- POST http://127.0.0.1:9200/_snapshot/my_backup/
- {
- "type": "fs",
- "settings": {
- "location": "/mount/backups/my_backup",
- "max_snapshot_bytes_per_sec" : "50mb",
- "max_restore_bytes_per_sec" : "50mb"
- }
- }
注意:第一段代碼用的是PUT
請求,用來創建repository,第二段代碼用的是POST
請求,來修改已經存在的repository。
2、備份索引
倉庫創建好之后就可以開始備份了。一個倉庫可以包含多個快照(snapshots),快照可以存所有的索引,部分索引或者一個單獨的索引。可以給索引指定一個唯一的名字:
- PUT http://127.0.0.1:9200/_snapshot/my_backup/snapshot_1
上面的代碼會將所有正在運行的索引,備份到my_backup倉庫下一個叫snapshot_1的快照中。上面的api會立刻返回,然后備份工作在后台運行。如果你想api同步執行,可以加wait_for_completion
標志:
- PUT http://127.0.0.1:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true
上面的方法會在備份完成后才返回,如果數據量大的話,會花很長時間。
如果只想備份部分索引的話,可以加上indices
參數:
- PUT http://127.0.0.1:9200/_snapshot/my_backup/snapshot_2
- {
- "indices": "index_1,index_2"
- }
3、刪除備份
不要手動刪除文件(Elasticsearch一貫主張使用api操作,尤其是大集群中),刪除snapshot_2:
- DELETE http://127.0.0.1:9200/_snapshot/my_backup/snapshot_2
如果備份正在后台進行,也可以直接刪除來取消此次備份。
4、查看備份信息
直接使用GET
請求即可:
- GET http://127.0.0.1:9200/_snapshot/my_backup/snapshot_2
返回類似下面的值:
- {
- "snapshots": [
- {
- "snapshot": "snapshot_2",
- "indices": [
- ".marvel_2014_28_10",
- "index1",
- "index2"
- ],
- "state": "SUCCESS",
- "start_time": "2014-09-02T13:01:43.115Z",
- "start_time_in_millis": 1409662903115,
- "end_time": "2014-09-02T13:01:43.439Z",
- "end_time_in_millis": 1409662903439,
- "duration_in_millis": 324,
- "failures": [],
- "shards": {
- "total": 10,
- "failed": 0,
- "successful": 10
- }
- }
- ]
- }
如果要查看所有索引的信息,使用如下api:
- GET http://127.0.0.1:9200/_snapshot/my_backup/_all
另外還有個一api可以看到更加詳細的信息:
- GET http://127.0.0.1:9200/_snapshot/my_backup/snapshot_3/_status
恢復
備份好后,恢復就更容易了,恢復snapshot_1里的全部索引:
- POST http://127.0.0.1:9200/_snapshot/my_backup/snapshot_1/_restore
這個api還有額外的參數:
- POST http://127.0.0.1:9200/_snapshot/my_backup/snapshot_1/_restore
- {
- "indices": "index_1",
- "rename_pattern": "index_(.+)",
- "rename_replacement": "restored_index_$1"
- }
參數indices
設置只恢復index_1索引,參數rename_pattern
和rename_replacement
用來正則匹配要恢復的索引,並且重命名。和備份一樣,api會立刻返回值,然后在后台執行恢復,使用wait_for_completion
標記強制同步執行。
另外可以使用下面兩個api查看狀態:
- GET http://127.0.0.1:9200/_recovery/restored_index_3
- GET http://127.0.0.1:9200/_recovery/
如果要取消恢復過程(不管是已經恢復完,還是正在恢復),直接刪除索引即可:
- DELETE http://127.0.0.1:9200/restored_index_3