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

author author     2022-12-11     189

关键词:

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进程停掉

elasticsearch数据迁移与集群容灾

...停止写入,可以采用诸如elasticsearch-dump、logstash、reindex、snapshot等方式进行数据迁移。实际上这几种工具大体上可以分为两类:如果是第二种场景,数据迁移过程中旧集群不能停止写入,需要根据实际的业务场景解决数据一致性... 查看详情

elasticsearch数据迁移

...址:https://github.com/elasticsearch-dump/elasticsearch-dump迁移单个索引:参数说明:--input:源地址,可为ES集群URL、文件或stdin,可指定索引,格式为:protocol://host:port/index--input-index:源ES集群中的索引--output:目标地址,可为ES集群地址URL、文... 查看详情

在 Sequelize 迁移中定义部分索引?

】在Sequelize迁移中定义部分索引?【英文标题】:DefinepartialindexinSequelizemigration?【发布时间】:2016-05-2807:14:24【问题描述】:我现在在我的模型定义中使用以下索引:name:\'unique_partner_id\',unique:true,fields:[\'partnerId\'],where:email:$ne:nul... 查看详情

es数据迁移_snapshot(不需要安装其他软件)(代码片段)

参考文章:三种常用的Elasticsearch数据迁移方案ES基于Snapshot(快照)的数据备份和还原CDH修改ElasticSearch配置文件不生效问题目录1、更改老ES和新ES的config/elasticsearch.yml2、重启老ES,在老ES执行Postman中创建备份目录... 查看详情

es数据库重建索引——reindex(数据迁移)(代码片段)

一、应用背景  ES在创建好索引后,mapping的properties属性类型是不能更改的,只能添加。如果说需要修改字段就需要重新建立索引然后把旧数据导到新索引。1、当你的数据量过大,而你的索引最初创建的分片数量不... 查看详情

[elasticsearch]es迁移笔记

ES在更改分词器或更改已有mapping结构后需要重新重新索引,当然复制也是一样的,相当于重新生成索引信息//es的head插件地址:http://localhost:9200/_plugin/head///新建索引PUT   /my_index_new//此处mapping复制的时候可以复制已有... 查看详情

使用elasticsearch-dump迁移es数据

...-type=data  不一样的地方就是--type=mapping,意思是把原始索引的mapping结构迁移给目标索引。然后在执行--type=data的6)就可以把数据迁移过去啦 如果索引很多,你还是懒得一个个去迁移,那么你可以改用这个命令:./elasticdump--i... 查看详情

elasticsearch部分常用操作(代码片段)

文章目录集群搭建7.4.1版本,配置linux优化索引映射创建,优化创建索引es_persist_3创建映射mappinges_persist_3创建索引es_persist_4创建mappinges_persist_4es的常用指令跨集群数据迁移reindex迁移reindex取消命令reindex查看进度(可以看到node_id:t... 查看详情

elasticsearch部分常用操作(代码片段)

文章目录集群搭建7.4.1版本,配置linux优化索引映射创建,优化创建索引es_persist_3创建映射mappinges_persist_3创建索引es_persist_4创建mappinges_persist_4es的常用指令跨集群数据迁移reindex迁移reindex取消命令reindex查看进度(可以看到node_id:t... 查看详情

elasticsearch集群数据索引迁移自动化脚本(代码片段)

  日常维护elasticsearch集群会出现新老集群数据迁移,这里使用的是snapshotapi是Elasticsearch用于对数据进行备份和恢复的一组api接口,可以通过snapshotapi进行跨集群的数据迁移,原理就是从源ES集群创建数据快照,然后在目标E... 查看详情

如何通过 Rails 迁移克隆数据库表?

...名称添加后缀,包括原始表中的所有索引。所以有一个“snapshots”表,我想创建“snapshots_temp”作为表的精确副本(不是数据,只是表架构,但包括索引)。我可以从sche 查看详情

MongoDB 将所有现有索引迁移到新数据库

】MongoDB将所有现有索引迁移到新数据库【英文标题】:MongoDBMigrateallexistingindexestonewdatabase【发布时间】:2018-04-1002:53:35【问题描述】:我有一个MongoDB开发集群,作为开发改进的一部分,我会随着时间的推移创建索引。在测试/生... 查看详情

迁移在指定列和索引列之后添加列

】迁移在指定列和索引列之后添加列【英文标题】:MigrationsAddColumnafterspecifiedcolumnandindexcolumn【发布时间】:2015-10-2014:40:59【问题描述】:我想在某个列也索引它之后通过迁移添加列(普通mysql索引不是唯一索引)。这是否可以... 查看详情

统计elasticsearch中月每天索引量的脚本

...近一段时间需要对生产环境中的elasticsearch集群中的历史索引数据做迁移,而在做迁移前需要对被迁移的elasticsearch索引数据做统计用于迁移后的验证统计,所以就写了一个脚本用于es数据中查询历史索引的量生成报表文件,而在... 查看详情

esrolloverandshrinkapi

...考技术A     ShrinkAPI允许我们将存在的索引转变为一个只包含主要分片的新索引。目标索引中请求的主要分片数量必须要为原索引中的因子(即原分片数量是新分片倍数),例如8个分片可以缩小到4、2、1个分片... 查看详情

带有索引的 SimpleRepository 自动迁移

】带有索引的SimpleRepository自动迁移【英文标题】:SimpleRepositoryautomigrationswithindexes【发布时间】:2010-05-3023:36:53【问题描述】:我在dev中使用subsonicsimplerepo和迁移,它使事情变得非常容易,但我一直遇到具有索引的nvarchar列的问... 查看详情

Rails 迁移返回索引太长

】Rails迁移返回索引太长【英文标题】:Railsmigrationreturningindextoolong【发布时间】:2021-09-0610:55:08【问题描述】:下面是我定义的迁移文件,用于重命名schooling_document_types_managing_areas表中的列,但问题是当我运行迁移时出现错误... 查看详情

添加索引迁移中的唯一性被忽略

】添加索引迁移中的唯一性被忽略【英文标题】:uniquenessinaddindexmigrationisignored【发布时间】:2021-12-1721:22:16【问题描述】:我正在尝试向现有索引添加唯一约束,因此我在迁移中删除并添加索引,一切正常,但是当我查看postgre... 查看详情