以下为背景
Elasticsearch 7.6.2单点,8.3.3单点
Docker 部署
当前使用场景:部分index,数据量较大,需要在跨版本的ES之间进行迁移
通常Elasticsearch备份有几种情况:
那么与之对应的解决方案有哪些呢?
什么样的方法适合什么样的场景呢?
基于以上说明,本地实验主要采取原生自带的备份snapshot与恢复restore的功能进行数据的迁移测试
curl https://registry.hub.docker.com/v1/repositories/elasticsearch/tags | tr -d '[\[\]" ]' | tr '}' '\n' | awk -F: -v image='elasticsearch' '{if(NR!=NF && $3 != ""){printf("%s:%s\n",image,$3)}}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 8250 0 8250 0 0 8620 0 --:--:-- --:--:-- --:--:-- 8611
elasticsearch:1
elasticsearch:1-alpine
elasticsearch:1.3.7
elasticsearch:1.3.8
elasticsearch:1.3.9
elasticsearch:1.4
elasticsearch:1.4.2
........
elasticsearch:8.1.2
elasticsearch:8.1.3
elasticsearch:8.2.0
elasticsearch:8.2.1
elasticsearch:8.2.2
elasticsearch:8.2.3
elasticsearch:8.3.1
elasticsearch:8.3.2
获取镜像
docker pull elasticsearch:7.6.2
docker pull elasticsearch:8.3.3
8.3.3: Pulling from library/elasticsearch
d7bfe07ed847: Pull complete
46789798a785: Pull complete
17d53e557d11: Pull complete
1044b0d3c7dc: Pull complete
6d3e6541b7e9: Pull complete
e56c78671a82: Pull complete
b3b71c18a4f1: Pull complete
5208b7309c64: Pull complete
067f1fbe449c: Pull complete
Digest: sha256:966.....2e1
Status: Downloaded newer image for elasticsearch:8.3.3
启动服务
docker run --name elasticsearch1 -p 9200:9201 -p 9300:9301 \
> -e "discovery.type=single-node" \
> -e ES_JAVA_OPTS="-Xms64m -Xmx256m" \
> -v ~/docker/elasticsearch1/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
> -v ~/docker/elasticsearch1/data:/usr/share/elasticsearch/data \
> -v ~/docker/elasticsearch1/plugins:/usr/share/elasticsearch/plugins \
> -v ~/docker/elasticsearch/backup:/usr/share/elasticsearch/backup \
> -d elasticsearch:7.6.2
docker run --name elasticsearch2 -p 9200:9201 -p 9300:9301 \
> -e "discovery.type=single-node" \
> -e ES_JAVA_OPTS="-Xms64m -Xmx256m" \
> -v ~/docker/elasticsearch2/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
> -v ~/docker/elasticsearch2/data:/usr/share/elasticsearch/data \
> -v ~/docker/elasticsearch2/plugins:/usr/share/elasticsearch/plugins \
> -v ~/docker/elasticsearch/backup:/usr/share/elasticsearch/backup \
> -d elasticsearch:8.3.3
确认服务
a@~:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9....b1 elasticsearch:8.3.3 "/usr/local/bin/dock…" 9 seconds ago Up 7 seconds 0.0.0.0:9201->9200/tcp, 0.0.0.0:9301->9300/tcp elasticsearch2
75....83 elasticsearch:7.6.2 "/usr/local/bin/dock…" 25 minutes ago Up 25 minutes 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch1
备份与恢复的版本说明
备份恢复时的版本兼容性:当前版本支持当前版本及升高一个的版本进行恢复不支持跨版本直接恢复。当然如果你想跨版本恢复,可以尝试版本递增滚动升级来达到目的。
反之则不可行,不能递减版本来恢复备份的数据。
在每个Elastcisearch节点的elasticsearch.yml的配置文件内加入:
path:
repo:
- /usr/share/elasticsearch/backup
a@~:~$ curl -H "Content-Type:application/json" -XPUT 'http://127.0.0.1:9200/_snapshot/{mybackup:这是备份的名称}' -d '{
> "type": "fs",
> "settings": {
> "location": "backupdata", #是对于elasticsearc.yml中地址的相对地址,也可以填完整地址
> "compress": "true" #压缩
> }
> }'
此外还可以选择的参数有:
出现以下内容则说明创建成功
{"acknowledged":true}
查看刚才建立的名为mybackup的仓库是否成功了
a@~:~$ curl -XGET 'http://127.0.0.1:9200/_snapshot'
{
"mybackup": {
"type":"fs",
"settings":{
"compress":"true","location":"backupdata"
}
}
}
以上查询到了就是成功了, 此外可以在es创建的时候外挂的数据目录内看到新建的“backupdata”目录
通过verify验证节点仓库是否在所有节点已生效
a@~:~$ curl -XPOST 'http://127.0.0.1:9200/_snapshot/mybackup/_verify'
{
"nodes":{
"VsZZX8i9Qk-o2YLwQbNPQQ":{
"name":"75707f972883"
}
}
}
a@~:~$ curl -H "Content-Type:application/json" -XPUT 'http://127.0.0.1:9200/_snapshot/{mybackup:备份仓库的名字}/{snapshot_1:备份的名字}?wait_for_completion=true' -d '{
> "indices": "sms_send_history_202208,users", #需要备份的index
> "ignore_unavailable": true,
> "include_global_state": false,
> "metadata": {
> "taken_by": "testuser",
> "taken_because": "test for backup."
> }
> }'
我们API选择参数等到其完成,因而API的结果最终会返回
{"snapshot":{
"snapshot":"snapshot_1",
"uuid":"8wrJ_ODOTQeiXN9uNamQRw",
"version_id":7060299,
"version":"7.6.2",
"indices":["sms_send_history_202208","users"],"include_global_state":false,
"metadata":{
"taken_by":"testuser",
"taken_because":"test for backup."
},
"state":"SUCCESS",
"start_time":"2022-08-08T09:43:24.082Z","start_time_in_millis":1659951804082,"end_time":"2022-08-08T09:43:24.484Z","end_time_in_millis":1659951804484,
"duration_in_millis":402,"failures":[],
"shards":{
"total":6,
"failed":0,
"successful":6
}
}
}
如果导出配置错误\使用完毕,可以将备份删除
DELETE /_snapshot/my_fs_backup/snapshot_1
DELETE /_snapshot/my_fs_backup/snapshot_*
能够指定一个、多个备份名称,或者通配符的方式将匹配的全部删除
前面配置两个Elasticsearch节点的时候我们将数据备份的目录挂载在了同一个
所以,我们只需要在Elasticsearch2的节点上也新建一下同名的仓库,路径名称与上面Elasticsearch1的一致,恢复的时候即可直接一键恢复,如果无法实现同一目录挂载,可能就要涉及数据的转移
a@~:~$ curl -H "Content-Type:application/json" -XPUT 'http://127.0.01:9201/_snapshot/{mybackup:与原仓库一致}' -d '{
> "type": "fs",
> "settings": {
> "location": "backupdata-与原仓库一致",
> "compress": "true"
> }
> }'
{"acknowledged":true}
否则会有报错:
{"error":{"root_cause":[{"type":"repository_missing_exception","reason":"[mybackup] missing"}],"type":"repository_missing_exception","reason":"[mybackup] missing"},"status":404}
直接全量恢复
curl -XPOST 'http://127.0.0.1:9201/_snapshot/mybackup/snapshot_1/_restore'
选择部分恢复:
curl -H "Content-Type:application/json" -XPOST 'http://127.0.0.1:9201/_snapshot/mybackup/snapshot_1/_restore' -d '
{
"indices": "user",
"ignore_unavailable": true,
"include_global_state": false, # include_global_state默认为true,是设置集群全局状态
"rename_pattern": "user_(.+)", # 重命名索引匹配规则,如: user_1
"rename_replacement": "re_user_$1",# 重命名索引为新的规则,如: re_user_1
"include_aliases": false
}'
正常返回值:
{
"accepted":true
}
通过原始指令查看index 和 索引数据
a@~:~$ curl http://127.0.0.1:9201/_cat/indices
yellow open users jSc-YVNhREu_at5b-Nm7vA 5 1 2 0 12.1kb 12.1kb
yellow open sms_send_history_202208 lefrl7cAQkmO69J2r-xO1g 1 1 9 0 63kb 63kb
a@~:~$ curl http://127.0.0.1:9201/sms_send_history_202208/_search?pretty
{
"took" : 58,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 9,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "sms_send_history_202208",
"_type" : "sms_send_history",
"_id" : "efb44600dbbe446789371ae146df654d",
"_score" : 1.0,
"_source" : {
"acctime" : 1659715200000,
"channelid" : 3,
"msgcount" : 1,
"msgbody" : "友情提示:1111111222222222220",
"sendcode" : "001",
"@timestamp" : "2022-08-08T0"
}
}]
}
}
后续可以再通过页面或其他渠道验证下数据