如果ES是集群,那么需要使用共享存储,支持的存储有:
a、shared file system
b、S3
c、HDFS
我使用的是第一种,NFS共享文件系统。这里要说一下权限问题,ES一般是使用 elasticsearch 用户启动的,要保证共享目录对 elasticsearch 用户有读写权限,要不然创建仓库和快照的时候会报访问拒绝500错误。
在nfs-server上导出共享目录的权限配置,这里将所有连接用户都压缩为root权限:
1 2 3 4 |
<span class="hljs-comment"># vim /etc/exports /<span class="hljs-keyword">data02/es <span class="hljs-number">192.168.3.56(rw,sync,all_squash,anonuid=0,anongid=0,no_subtree_check) 192.168.3.57(rw,sync,all_squash,anonuid=0,anongid=0,no_subtree_check) 192.168.3.49(rw,sync,all_squash,anonuid=0,anongid=0,no_subtree_check) # /etc/init.d/nfs-kernel-server reload</span></span></span> |
1、创建挂载目录,并给予权限
1 2 3 |
<span class="hljs-meta"># mkidr -pv /nh/esbk/my_backup # chmod 755 /nh/esbk/ # chown elasticsearch.elasticsearch /nh/esbk/</span> |
2、挂载共享目录
1 2 3 4 5 |
<span class="hljs-comment"># vim /etc/fstab <span class="hljs-number">192.168.3.97<span class="hljs-symbol">:/data02/es /nh/esbk/my_backup nfs defaults 0 0 # mount -a # df -hT</span></span></span> |
3、修改ES的配置文件,添加仓库路径,重启服务
1 2 3 4 |
<span class="hljs-meta"># vim /etc/elasticsearch/elasticsearch.yml path.repo: [<span class="hljs-string">"/nh/esbk"] # /etc/init.d/elasticsearch restart</span></span> |
4、注册快照仓库到ES,这里是在 kibana 的 Dev Tools 上操作的,也可以使用 curl 发起请求。
1 2 3 |
Before any snapshot or <span class="hljs-keyword">restore operation can be performed, a snapshot repository should be registered in Elasticsearch. After all nodes are restarted, the following command can be used to register the shared file system repository with the name my_backup.</span> |
1 2 3 4 5 6 7 8 |
PUT <span class="hljs-regexp">/_snapshot/my_backup { <span class="hljs-string">"type": "fs", "settings": { "compress": <span class="hljs-literal">true, "location": "/nh/esbk/my_backup" } }</span></span></span> |
5、查看仓库信息
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="hljs-type">GET /_snapshot/my_backup # curl -u elastic -XGET <span class="hljs-symbol">'http:<span class="hljs-comment">//192.168.3.49:9200/_snapshot/my_backup?pretty' { <span class="hljs-string">"my_backup" : { "type" : "fs", "settings" : { "compress" : "true", "location" : "/nh/esbk/my_backup" } } }</span></span></span></span> |
6、创建快照
1 2 3 |
A repository can contain multiple snapshots <span class="hljs-keyword">of the same cluster. Snapshots are identified by unique names within the cluster. A snapshot with the name snapshot_1 in the repository my_backup can be created by executing the following command.</span> |
1 |
PUT <span class="hljs-regexp">/_snapshot/my_backup/snapshot_1</span> |
这里发起请求后,会立马返回 true,并在后台执行操作。
如果想等待执行完成之后再返回,可以加一个参数:
1 |
PUT /_snapshot/my_backup/snapshot_1?<span class="hljs-built_in">wait_<span class="hljs-keyword">for_completion=<span class="hljs-literal">true</span></span></span> |
7、查看刚才创建的快照的信息
1 |
Once a snapshot <span class="hljs-keyword">is created information about this snapshot can be obtained using the following command.</span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<span class="hljs-symbol">GET /_snapshot/my_backup/snapshot_1 { <span class="hljs-string">"snapshots": [ { "snapshot": "snapshot_1", "uuid": "xSMRNVMIRHmx_qlhX5fqfg", "version_id": <span class="hljs-number">5040199, "version": "5.4.1", "indices": [ ".monitoring-kibana-2-2017.07.05", ".monitoring-kibana-2-2017.07.11", "zixun-nginx-access-2017.07.12", ".monitoring-logstash-2-2017.07.07", ".monitoring-kibana-2-2017.07.07", "filebeat-2017.07.07", ".watcher-history-3-2017.07.04", ".watcher-history-3-2017.07.07", ".monitoring-es-2-2017.07.05", ".kibana", ".monitoring-data-2", ".watcher-history-3-2017.06.27", ".monitoring-logstash-2-2017.07.10", ".monitoring-kibana-2-2017.07.10", ".monitoring-es-2-2017.07.08", ".monitoring-logstash-2-2017.07.12", ".monitoring-es-2-2017.07.10", ".watcher-history-3-2017.07.06", ".monitoring-kibana-2-2017.07.09", ".watcher-history-3-2017.07.12", ".watcher-history-3-2017.07.03", ".monitoring-alerts-2", ".monitoring-logstash-2-2017.07.08", ".watcher-history-3-2017.07.01", ".watcher-history-3-2017.07.11", ".watcher-history-3-2017.07.05", ".watcher-history-3-2017.06.29", ".watcher-history-3-2017.06.28", ".monitoring-kibana-2-2017.07.08", ".security", ".monitoring-logstash-2-2017.07.11", ".monitoring-es-2-2017.07.11", ".watcher-history-3-2017.06.30", ".triggered_watches", ".watcher-history-3-2017.07.08", ".monitoring-es-2-2017.07.12", ".watcher-history-3-2017.07.09", ".monitoring-es-2-2017.07.09", ".monitoring-kibana-2-2017.07.12", ".monitoring-kibana-2-2017.07.06", ".watcher-history-3-2017.07.10", "test", ".monitoring-es-2-2017.07.07", ".monitoring-logstash-2-2017.07.09", ".watches", ".monitoring-es-2-2017.07.06", ".watcher-history-3-2017.07.02" ], "state": "SUCCESS", "start_time": "2017-07-12T04:19:08.246Z", "start_time_in_millis": 1499833148246, "end_time": "2017-07-12T04:20:04.717Z", "end_time_in_millis": 1499833204717, "duration_in_millis": 56471, "failures": [], "shards": { "total": 59, "failed": 0, "successful": 59 } } ] }</span></span></span> |
列出一个仓库里的所有快照
1 |
All snapshots currently <span class="hljs-keyword">stored in the repository can be listed using the following command:</span> |
1 |
GET <span class="hljs-regexp">/_snapshot/my_backup/_all</span> |
8、删除一个快照
1 |
A snapshot can be deleted <span class="hljs-keyword">from the repository using the following command:</span> |
1 |
DELETE <span class="hljs-regexp">/_snapshot/my_backup/snapshot_1</span> |
9、删除一个仓库
1 |
A repository can be deleted <span class="hljs-keyword">using the following command:</span> |
1 |
DELETE <span class="hljs-regexp">/_snapshot/my_backup</span> |
10、恢复一个快照(支持恢复部分数据以及恢复过程中修改索引信息,具体细节参考官方文档)
1 |
POST <span class="hljs-regexp">/_snapshot/my_backup/snapshot_1/_restore</span> |
11、查看快照状态信息(比如正在创建或者创建完成等)
a、列出所有当前正在运行的快照以及显示他们的详细状态信息
1 |
A list <span class="hljs-keyword">of currently running snapshots with their detailed status information can be obtained using the following command.</span> |
1 |
GET <span class="hljs-regexp">/_snapshot/_status</span> |
b、查看指定仓库的正在运行的快照以及显示他们的详细状态信息
1 |
GET <span class="hljs-regexp">/_snapshot/my_backup/_status</span> |
c、查看指定快照的详细状态信息即使不是正在运行
1 |
If both repository name <span class="hljs-keyword">and snapshot id are specified, this command will return detailed status information for the given snapshot even if it’s not currently running:</span> |
1 |
GET <span class="hljs-regexp">/_snapshot/my_backup/snapshot_1/_status</span> |
d、支持同时指定多个快照ID查看多个快照的信息
1 |
<span class="hljs-attribute">Multiple ids are also supported.</span> |
1 |
GET <span class="hljs-regexp">/_snapshot/my_backup/snapshot_1,snapshot_2/_status</span> |
12、如果要停止一个正在运行的snapshot任务(备份和恢复),将其删除即可。