草庐IT

es索引迁移Snapshot-迁移部分索引

赵承胜博客 2023-03-28 原文

Snapshot-迁移部分索引

源集群 192.168.40.180 192.168.40.181 192.168.40.182

目标集群 192.168.40.61 192.168.40.62 192.168.40.63

生产 需要额外,挂载硬盘

Snapshot API 是 Elasticsearch 用于对数据进行备份和恢复的一组 API 接口,可以通过 Snapshot API 进行跨集群的数据迁移,原理就是从源 Elasticsearch 集群创建数据快照,然后在目标 Elasticsearch 集群中进行恢复。

第一步:在源集群注册 Repository

创建快照前先要注册 Repository , 一个 Repository 可以包含多份快照文件, Repository 主要有以下几种类型:

fs: 共享文件系统,将快照文件存放于文件系统中
url: 指定文件系统的URL路径,支持协议:http,https,ftp,file,jar
s3: AWS S3对象存储,快照存放于S3中,以插件形式支持
hdfs: 快照存放于hdfs中,以插件形式支持
azure: 快照存放于azure对象存储中,以插件形式支持
gcs: 快照存放于google cloud对象存储中,以插件形式支持

1模拟数据

curl -H 'Content-type: application/json' -XPUT 'http://192.168.40.180:9200/_template/blog-template' -d '{
"index_patterns": [
"blog-*"
],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"dynamic_templates": [
{
"integers": {
"match": "int_*",
"mapping": {
"type": "integer"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword",
"ignore_above": 256
}
}
}
],
"properties": {
"title": {
"type": "text"
}
}
}
}'










curl -H 'Content-type: application/json' -XPUT 'http://192.168.40.180:9200/blog-test2' -d '{}'









curl -H 'Content-type: application/x-ndjson' -XPOST 'http://192.168.40.180:9200/blog-test/_bulk' -d '{"index":{}}
{"title":"elasticsearch best practice","author_firstname":"tony","author_lastname":"song","tags":"elasticsearch ,logstash","int_age":18,"locale":"zh,en"}
{"index":{}}
{"title":"elastic stack release V5","author_firstname":"shay","author_lastname":"banon","tags":"elk ,elasticsearch,release","int_age":28,"locale":"zhc,en"}
{"index":{}}
{"title":"kibana tutorial","author_firstname":"zen","author_lastname":"wei","tags":"","int_age":38,"locale":"zhc,en"}

'





curl -H 'Content-type: application/x-ndjson' -XPOST 'http://192.168.40.180:9200/blog-test2/_bulk' -d '{"index":{}}
{"title":"elasticsearch best practice","author_firstname":"tony","author_lastname":"song","tags":"elasticsearch ,logstash","int_age":18,"locale":"zh,en"}
{"index":{}}
{"title":"elastic stack release V5","author_firstname":"shay","author_lastname":"banon","tags":"elk ,elasticsearch,release","int_age":28,"locale":"zhc,en"}
{"index":{}}
{"title":"kibana tutorial","author_firstname":"zen","author_lastname":"wei","tags":"","int_age":38,"locale":"zhc,en"}

'

2查看索引信息

[es@master elasticsearch-7.2.0]$ curl --user elastic:1qaz1qaz -XGET 'http://192.168.40.180:9200/_cat/indices?pretty'
green open blog-test lREj4IwtS9-rHuIdiTMmwA 3 1 0 0 1.4kb 743b
green open emp ZAEKZhqJS5i7zt-L8MkB-Q 1 1 1 0 7.5kb 3.7kb
green open test10 ifIzAccwTc290xcfuxifJA 5 0 1 0 4.8kb 4.8kb
green open test 4655Yhv1QPGRb-W78qVm4g 1 1 2 3 18kb 9kb
green open blog-test2 znzIobHlQ5in6dxAr4L_eg 3 1 0 0 1.4kb 743b
green open test4 5XnbLGmoTo-mPzIT-FpFDA 3 1 0 0 1.6kb 849b
green open .security-7 iwX-Pv_MSRWb7I1MoP6j4Q 1 1 6 0 39.6kb 19.8kb
green open test3 KS7T5LvPQ4WhLyihz4AJ_g 3 1 0 0 1.6kb 849b

3安装sshfs

yum install -y epel-release
yum -y install fuse-sshfs
老集群挂载新集群目录

sshfs -o allow_other -o nonempty 192.168.40.61:/elastic/backup /elastic/backup
(所有节点执行)除了40.61

4修改elasticsearch配置文件

修改 elasticsearch.yml 配置文件,增加如下配置:

path.repo: ["/elastic/backup"]

mkdir -p /elastic/backup
chown es.es /elastic/backup -R

chmod 777 /elastic/ -R
chmod 777 /elastic/backup/ -R

5重启elasticsearch进程,查看创建的repo

curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.180:9200/_cluster/settings?include_defaults&filter_path=*.path.repo&pretty'
[es@master elasticsearch-7.2.0]$ curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.180:9200/_cluster/settings?include_defaults&filter_path=*.path.repo&pretty'
{
"defaults" : {
"path" : {
"repo" : [
"/elastic/backup"
]
}
}
}

6创建仓库

---会报错--忽略 -查看仓库(报错原因 需要共享文件服务"type": "fs",)我用的sshfs

curl --user elastic:1qaz1qaz -H "Content-Type: application/json" -XPUT http://192.168.40.182:9200/_snapshot/my_fs_backup3 -d '
{
"type": "fs",
"settings": {
"location": "/elastic/backup",
"max_restore_bytes_per_sec" : "50m",
"compress": true,
"max_snapshot_bytes_per_sec" : "50m"
}
}
'
7查看仓库

[root@node1 ~]# curl --user elastic:1qaz1qaz -XGET 'http://192.168.40.180:9200/_snapshot/_all?pretty'
{
"my_fs_backup" : {
"type" : "fs",
"settings" : {
"location" : "/elastic/backup",
"max_restore_bytes_per_sec" : "50m",
"compress" : "true",
"max_snapshot_bytes_per_sec" : "50m"
}
}
}


GET _snapshot/my_fs_backup
#输出结果:
{
"my_fs_backup" : {
"type" : "fs",
"settings" : {
"compress" : "true",
"location" : "/home/elastic/backup/my_fs_backup"
}
}
}
查看在哪个node上创建了关联:
[root@master backup]# curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XPOST 'http://192.168.40.180:9200/_snapshot/my_fs_backup/_verify?pretty' -d 'undefined'

{
"nodes" : {
"I-au_rvbQRKj1xrjvWZPnA" : {
"name" : "node2"
},
"opk7TQctTPaacJ42if-dbg" : {
"name" : "master"
},
"H55oT25BS0-K-Z_WadWxSg" : {
"name" : "node1"
}
}
}

8备份

emp开头索引,test开头索引,blog开头索引, 除了-.security

[root@master backup]# curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XPUT 'http://192.168.40.180:9200/_snapshot/my_fs_backup/es_bak_20220917' -d '{"indices": "emp*,test*,blog*,-.security*,","ignore_unavailable": "true","include_global_state": false}'

{"accepted":true}[root@master backup]#
查看所有快照信息

curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.180:9200/_snapshot/my_fs_backup/_all?pretty'
查看指定快照信息

[root@master backup]# curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.180:9200/_snapshot/my_fs_backup/es_bak*?pretty'
{
"snapshots" : [
{
"snapshot" : "es_bak_20220917",
"uuid" : "t4HY5qfZRu2wzSzXnn6NaA",
"version_id" : 7020099,
"version" : "7.2.0",
"indices" : [
"blog-test",
"emp",
"test10",
"test",
"blog-test2",
"test4",
"test3"
],
"include_global_state" : false,
"state" : "SUCCESS",
"start_time" : "2022-09-16T17:41:48.692Z",
"start_time_in_millis" : 1663350108692,
"end_time" : "2022-09-16T17:41:49.092Z",
"end_time_in_millis" : 1663350109092,
"duration_in_millis" : 400,
"failures" : [ ],
"shards" : {
"total" : 19,
"failed" : 0,
"successful" : 19
}
}
]
}
查看快照状态

curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.180:9200/_snapshot/my_fs_backup/es_bak_20220917/_status?pretty'

第二步:目标集群

path.repo: ["/elastic/backup"]

重启

查看repo

curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.61:9200/_cluster/settings?include_defaults&filter_path=*.path.repo&pretty'

1新集群仓库创建为只读

curl --user elastic:1qaz1qaz -H "Content-Type: application/json" -XPUT http://192.168.40.61:9200/_snapshot/my_fs_backupNEW -d '
{
"type": "fs",
"settings": {
"location": "/elastic/backup",
"max_restore_bytes_per_sec" : "50m",
"compress": true,
"max_snapshot_bytes_per_sec" : "50m",
"readonly": true
}
}
'
查看在那个node 上创建了关联

curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XPOST 'http://192.168.40.61:9200/_snapshot/my_fs_backupNEW/_verify?pretty' -d 'undefined'
{
"nodes" : {
"FmhibG9HTAqfd98tykZqEA" : {
"name" : "node1"
},
"SwOuMfpiRLKECWtfDx2ImA" : {
"name" : "node2"
},
"a5vPcQRSRR-f1fqnZxPIkQ" : {
"name" : "master"
}
}
}

2查看快照信息

[es@master ~]$ curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.61:9200/_snapshot/my_fs_backupNEW/_all?pretty'

{
"snapshots" : [
{
"snapshot" : "es_bak_20220917",
"uuid" : "t4HY5qfZRu2wzSzXnn6NaA",
"version_id" : 7020099,
"version" : "7.2.0",
"indices" : [
"blog-test",
"emp",
"test10",
"test",
"blog-test2",
"test4",
"test3"
],
"include_global_state" : false,
"state" : "SUCCESS",
"start_time" : "2022-09-16T17:41:48.692Z",
"start_time_in_millis" : 1663350108692,
"end_time" : "2022-09-16T17:41:49.092Z",
"end_time_in_millis" : 1663350109092,
"duration_in_millis" : 400,
"failures" : [ ],
"shards" : {
"total" : 19,
"failed" : 0,
"successful" : 19
}
}
]
}
[es@master ~]$

3恢复索引

curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XPOST 'http://192.168.40.61:9200/_snapshot/my_fs_backupNEW/es_bak_20220917/_restore' -d '{
"indices": "*",
"ignore_unavailable": true
}'

4查看索引 集群 节点

curl --user elastic:1qaz1qaz "http://192.168.40.61:9200/_cat/health?v"

curl --user elastic:1qaz1qaz "http://192.168.40.61:9200/_cat/nodes?v"
curl --user elastic:1qaz1qaz "http://192.168.40.61:9200/_cat/indices?v"

查看恢复进度
curl --user elastic:1qaz1qaz -H 'Content-type: application/json' -XGET 'http://192.168.40.61:9200/_recovery?pretty'
集群迁移部分索引成功

第三步:迁移索引模板

1导出集群中所有模板

curl --user elastic:1qaz1qaz http://192.168.40.61:9200/_cat/templates | awk '{print $1}'| while read line ;do curl -s -u elastic:xrRWYqr2QEXZ8h\!@6 -XGET http://192.168.40.61:9200/_template/$line?pretty >template-$line.json;done

导出集群中所有模板

2.模板json文件处理一遍,删除第二行和最后一行:

sed -i '2d' ./*json

sed -i '$d' ./*json

3新集群导入【需要迁移】的索引es模板:

ls | grep json | awk -F"template-" '{print $2}' | awk -F".json" '{print $1}' | while read line;do curl -XPUT http://localhost:9200/_template/$line -H 'Content-Type: application/json' -d@template-${line}.json;done

第四步

sshfs进程停掉

有关es索引迁移Snapshot-迁移部分索引的更多相关文章

  1. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  4. ES基础入门 - 2

    ES一、简介1、ElasticStackES技术栈:ElasticSearch:存数据+搜索;QL;Kibana:Web可视化平台,分析。LogStash:日志收集,Log4j:产生日志;log.info(xxx)。。。。使用场景:metrics:指标监控…2、基本概念Index(索引)动词:保存(插入)名词:类似MySQL数据库,给数据Type(类型)已废弃,以前类似MySQL的表现在用索引对数据分类Document(文档)真正要保存的一个JSON数据{name:"tcx"}二、入门实战{"name":"DESKTOP-1TSVGKG","cluster_name":"elasticsear

  5. ruby-on-rails - 协会的 Rails 索引 - 2

    我发现自己需要这个。假设cart是一个包含用户列表的模型。defindex_of_itemcart.users.each_with_indexdo|u,i|ifu==current_userreturniendend获取此类关联索引的更简单方法是什么? 最佳答案 indexArray上的方法与您的index_of_item方法相同,例如cart.users.index(current_user)返回数组中第一个对象的索引==给obj。如果未找到匹配项,则返回nil。 关于ruby-on-

  6. ruby - Rails -- :id attribute? 所需的数据库索引 - 2

    因此,当我遵循MichaelHartl的RubyonRails教程时,我注意到在用户表中,我们为:email属性添加了一个唯一索引,以提高find的效率方法,因此它不会逐行搜索。到目前为止,我们一直在根据情况使用find_by_email和find_by_id进行搜索。然而,我们从未为:id属性设置索引。:id是否自动索引,因为它在默认情况下是唯一的并且本质上是顺序的?或者情况并非如此,我应该为:id搜索添加索引吗? 最佳答案 大多数数据库(包括sqlite,这是RoR中的默认数据库)会自动索引主键,对于RailsMigration

  7. ruby-on-rails - 在现有数据库上进行 Rails 迁移 - 2

    我正在创建一个新的Rails3.1应用程序。我希望这个新应用程序重用现有数据库(由以前的Rails2应用程序创建)。我创建了新的应用程序定义模型,它重用了数据库中的一些现有数据。在开发和测试阶段,一切正常,因为它在干净的表数据库上运行,但是当尝试部署到生产环境时,我收到如下消息:PGError:ERROR:column"email"ofrelation"users"alreadyexists***[err::localhost]:ALTERTABLE"users"ADDCOLUMN"email"charactervarying(255)DEFAULT''NOTNULL但是我在迁移中有这

  8. ruby - 引用具有指定索引的枚举器值 - 2

    假设我有一个可枚举对象enum,现在我想获取第三个项目。我知道一种通用方法是转换成数组,然后使用索引访问,如:enum.to_a[2]但这种方式会创建一个临时数组,效率可能很低。现在我使用:enum.each_with_index{|v,i|breakvifi==2}但这非常丑陋和多余。执行此操作最有效的方法是什么? 最佳答案 你可以使用take剥离前三个元素,然后剥离last从take给你的数组中获取第三个元素:third=enum.take(3).last如果您根本不想生成任何数组,那么也许:#Ifenumisn'tanEnum

  9. ruby - 将 Logstash 中的时间戳时区转换为输出索引名称 - 2

    在我的场景中,Logstash收到的系统日志行的“时间戳”是UTC,我们在Elasticsearch输出中使用事件“时间戳”:output{elasticsearch{embedded=>falsehost=>localhostport=>9200protocol=>httpcluster=>'elasticsearch'index=>"syslog-%{+YYYY.MM.dd}"}}我的问题是,在UTC午夜,Logstash在外时区(GMT-4=>America/Montreal)结束前将日志发送到不同的索引,并且索引在20小时(晚上8点)之后没有日志,因为“时间戳”是UTC。我们已

  10. ruby-on-rails - Rails 迁移中的 PostgreSQL 点类型 - 2

    我想使用PostgreSQL中的point类型。我已经完成了:railsgmodelTestpoint:point最终的迁移是:classCreateTests当我运行时:rakedb:migrate结果是:==CreateTests:migrating====================================================--create_table(:tests)rakeaborted!Anerrorhasoccurred,thisandalllatermigrationscanceled:undefinedmethod`point'for#/hom

随机推荐