es集群主机分布
10.4.7.11 node1
10.4.7.12 node2
10.4.7.21 node3
10.4.7.22 nfs
1.在主机10.4.7.22部署nfs
[root@localhost ~]# yum install -y nfs-utils
[root@localhost ~]# systemctl enable rpcbind.service
[root@localhost ~]# systemctl enable nfs-server.service
[root@localhost ~]# systemctl start rpcbind.service
[root@localhost ~]# systemctl start nfs-server.service
#配置那些客户端可以访问
[root@localhost ~]# cat /etc/exports
/data/db/elasticsearch/backup 10.4.7.0/24(rw,sync,all_squash)
#创建nfs共享目录
[root@localhost ~]# mkdir -p /data/db/elasticsearch/backup
[root@localhost ~]# chmod -R 777 /data/db/elasticsearch/backup
#nfs生效
[root@localhost ~]# exportfs -r
#查看是否生效
[root@localhost ~]# exportfs -s
2.es集群的每个节点都要安装客户端
[root@localhost ~]# yum -y install showmount
#开启服务:
[root@localhost ~]# systemctl enable rpcbind.service
[root@localhost ~]# systemctl start rpcbind.service
#查看nfs挂载信息
[root@localhost ~]# showmount -e 10.4.7.22
#创建挂载目录
[root@localhost ~]# mkdir -p /data/es-backups/
[root@localhost ~]# chown -R es.es /data/es-backups/
[root@localhost ~]# chmod -R 777 /data/es-backups/
[root@localhost ~]# mount -t nfs 10.4.7.22:/data/db/elasticsearch/backup /data/es-backups/
#查看是否挂载成功,有需要要添加 /etc/fstab,因为这里挂载是临时的。重启服务器就失效
[root@localhost ~]# df -hT
#es的配置文件elasticsearch.yml需要添加下面内容,重启es服务
[root@localhost ~]# vi elasticsearch.yml
path.repo: ["/data/es-backups/"]
#重启es服务
[root@localhost ~]# ps aux|grep elasticsearch | grep -v 'grep elasticsearch'|awk '{print $2}'|xargs kill -9 2> /dev/null
[root@localhost ~]# su -c '/app/elasticsearch/bin/elasticsearch -d' es
3.创建快照仓库,可以在任何es节点操作,这里就在10.4.7.11节点操作,返回一个“{"acknowledged":true}” ,表示创建成功
[root@localhost ~]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XPUT http://10.4.7.11:9200/_snapshot/backup -d'
{
"type": "fs",
"settings": {
"location": "/data/es-backups/2022-01-21",
"compress": true,
"max_snapshot_bytes_per_sec" : "500mb", #如果不想限制就去掉
"max_restore_bytes_per_sec" : "500mb" #如果不想限制就去掉
}
}'
备注说明:
1.可在es任一节点操作
2.backup: 指定仓库名称为backup ,生成的备份文件存放路径为/data/es-backups/2022-01-21
3.max_snapshot_bytes_per_sec,max_restore_bytes_per_sec 限定备份和恢复的数据字节内容大小为50mb,
为了防止磁盘IO过高。数值越大,备份恢复速度越快。50mb为推荐值,IO性能高的机器可不限制
#查看backup仓库是否创建成功
[root@localhost ~]# curl -u elastic:Elastic123 http://10.4.7.11:9200/_cat/repositories?
backup fs
#如果要删除backup这个快照仓库
[root@localhost ~]# curl -u elastic:Elastic123 -XDELETE http://10.4.7.11:9200/_snapshot/backup
#自动会在/data/es-backups/目录下创建一个2022-01-21的目录,下面这个时候是没有任何文件的
[root@localhost ~]# ls -ld /data/es-backups/2022-01-21/
drwxr-xr-x 2 nfsnobody nfsnobody 6 Jan 20 22:40 /data/es-backups/2022-01-21/
#10.4.7.22的nfs主机上自然也多了一个2022-01-21的目录
[root@localhost ~]# ls -ld /data/db/elasticsearch/backup/2022-01-21/
4.创建快照备份
#针对全索引快照备份,返回一个 "accepted" : true"
[root@localhost ~]# curl -u elastic:Elastic123 -XPUT http://10.4.7.11:9200/_snapshot/backup/snapshot_all_indices?pretty
备注说明:
1.指定备份到快照仓库backup里,即备份目录为:/data/es-backups/2022-01-21/
2.快照名称为: snapshot_all_indices
#backup这个快照仓库备份到/data/es-backups/2022-01-21/目录里就有全索引快照备份的内容了
[root@localhost ~]# ll /data/es-backups/2022-01-21/
total 24
-rw-r--r-- 1 nfsnobody nfsnobody 450 Jan 20 22:55 index-0
-rw-r--r-- 1 nfsnobody nfsnobody 8 Jan 20 22:55 index.latest
drwxr-xr-x 3 nfsnobody nfsnobody 36 Jan 20 22:55 indices
-rw-r--r-- 1 nfsnobody nfsnobody 10175 Jan 20 22:55 meta-zpvPOoxnSveYdDoK8vez-Q.dat
-rw-r--r-- 1 nfsnobody nfsnobody 260 Jan 20 22:55 snap-zpvPOoxnSveYdDoK8vez-Q.dat
#创建一个索引 user_event_20220110的索引库,后面在head插件上操作更加方便。默认是一个分片,一个副本。
[root@localhost ~]# curl -u elastic:Elastic123 -XPUT http://10.4.7.11:9200/user_event_20220110
#在创建一个索引库fengge-2022,指定分片跟副本数量
[root@localhost ~]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XPUT http://10.4.7.11:9200/fengge-2022 -d '
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 2
}
}
}'
#查看创建的索引库信息
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/fengge-2022?pretty
{
"fengge-2022" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"creation_date" : "1642752684916",
"number_of_shards" : "2", #两个分片
"number_of_replicas" : "2", #两个副本
"uuid" : "5lff7GXrQBulvK21A_0Ayw",
"version" : {
"created" : "7090399"
},
"provided_name" : "fengge-2022"
}
}
}
}
#这个user_event_20220110索引添加一个文档,类型叫做 “blog” ,类型随便定义,只是一个标识符而已, 文档ID 是 “123” 或者不指定就是自增,那么这个索引请求就像这样
[root@localhost ~]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XPUT http://10.4.7.11:9200/user_event_20220110/blog/123 -d '
{
"title": {
"My first blog entry": {
"name": "fengge"}
},
"text": "Just trying this out",
"date": "2014/01/01"
}'
#在head可以查看
5.针对指定某个单独索引快照备份(为了区分不同索引备份目录,建议仓库用索引名称命名)
a.先针对索引创建仓库,返回一个"{"acknowledged":true}" 表示创建成功。
[root@localhost ~]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XPUT http://10.4.7.11:9200/_snapshot/user_event_20220110 -d'
{
"type": "fs",
"settings": {
"location": "/data/es-backups/user_event_20220110",
"compress": true,
"max_snapshot_bytes_per_sec" : "500mb",
"max_restore_bytes_per_sec" : "500mb"
}
}'
b.备份这个user_event_20220110这个索引到user_event_20220110快照仓库里。返回信息里有shards":{"total":1,"failed":0,"successful":1}}},没有error,表示成功。
[root@localhost ~]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XPUT http://10.4.7.11:9200/_snapshot/user_event_20220110/user_event_20220110?wait_for_completion=true -d '
{
"indices": "user_event_20220110", #这里可以指定多个需要备份的索引名字,它们之间用逗号间隔。
"ignore_unavailable": "true",
"include_global_state": false
}'
备注说明:
1.创建的仓库名为: user_event_20220110
2.存放的文件目录为: /data/es-backups/user_event_20220110
3.indices:指定索引源为: user_event_20220110
4.增加?wait_for_completion=true参数是为了执行完成返回结果状态
6.恢复快照备份数据到es集群
a.针对全索引快照备份的恢复操作,前提要恢复的索引在es集群是不存在的,否则恢复错误。
[root@localhost ~]# curl -u elastic:Elastic123 -XPOST http://10.4.7.11:9200/_snapshot/backup/snapshot_all_indices/_restore
备注说明:
1.指定要恢复仓库名称:backup
2.指定要恢复快照备份名称:snapshot_all_indices
b.针对某个指定索引的快照备份恢复操作,如果存在这个恢复的索引,需要先关闭这个索引,
否则索引写操作会影响恢复,于是关闭user_event_20220110这个索引
[root@localhost ~]# curl -u elastic:Elastic123 -XPOST http://10.4.7.11:9200/user_event_20220110/_close
针对索引user_event_20220110快照恢复
[root@localhost ~]# curl -u elastic:Elastic123 -XPOST http://10.4.7.11:9200/_snapshot/user_event_20220110/user_event_20220110/_restore
备注说明:
1.指定仓库名称: user_event_20220110
2.指定快照备份名称: user_event_20220110
#恢复索引后,需要打开这个索引,不然这个索引无法进行写数据
[root@localhost ~]# curl -u elastic:Elastic123 -XPOST http://10.4.7.11:9200/user_event_20220110/_open
c.再备份所有索引中恢复指定的索引来恢复
[root@localhost ~]# curl -u elastic:Elastic123 -H "Content-Type: application/json" -XPOST http://10.4.7.11:9200/_snapshot/backup/snapshot_all_indices/_restore?wait_for_completion=true -d '
{
"indices": "index_1,index_2", ##index_1就是你想指定恢复索引名字,多个索引用英文逗号间隔。
"rename_pattern": "index_(.+)", ##查找所提供的模式能匹配上的正在恢复的索引。
"rename_replacement": "restored_index_$1" ##然后把它们重命名成替代的模式。
}'
参数注解:
####上面这个这个会恢复 index_1,index_2 到你及群里,但是重命名成了分别为: restored_index_1 ,restored_index_2
####这里还有两个隐藏参数
####ignore_unavailable设置为true,如果某些索引不可用,恢复过程还是会成功的。
####include_global_state设置为true,存储了集群的state,还会同时恢复一些template。
#####恢复后的显示结果如下#########
{"accepted":true}
7.操作命令
#.查看已存在仓库
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_cat/repositories?
#查看已存在快照
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_snapshot?pretty
#删除user_event_20220110快照仓库里user_event_20220110快照
[root@localhost ~]# curl -u elastic:Elastic123 -XDELETE http://10.4.7.11:9200/_snapshots/user_event_20220110/user_event_20220110
#删除快照仓库user_event_20220110
[root@localhost ~]# curl -u elastic:Elastic123 -XDELETE http://10.4.7.11:9200/_snapshots/user_event_20220110
#查看快照仓库里快照备份的信息,里面有
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/_snapshot/user_event_20220110/user_event_20220110/_status?pretty
#如果在恢复某个索引的时候,你想查看恢复进度
[root@localhost ~]# curl -u elastic:Elastic123 -XGET http://10.4.7.11:9200/user_event_20220110/_recovery?pretty
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)