ElasticSearch备份工具

8/16/2022 ElasticSearch

# ElasticSearch备份工具介绍

# 安装elasticsearch-dump

[root@elasticsearch ~/soft]# git clone https://github.com/taskrabbit/elasticsearch-dump

# 2.安装elasticsearch-dump
[root@elasticsearch]# mv elasticsearch-dump/ /data/
[root@elasticsearch]# cd /data/elasticsearch-dump
[root@elasticsearch /data/elasticsearch-dump]# npm install elasticdump -g

# 3.查看版本
[root@elasticsearch /data/elasticsearch-dump]# elasticdump --version
6.62.1
1
2
3
4
5
6
7
8
9
10

# 可能报错

[root@elasticsearch /data/elasticsearch-dump]# npm install elasticdump
npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "elasticdump" under a package
npm ERR! also called "elasticdump". Did you name your project the same
npm ERR! as the dependency you're installing?
npm ERR! 
npm ERR! For more information, see:
npm ERR!     <https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm>

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2021-01-13T06_36_56_956Z-debug.log

报错内容翻译如下
错误的ERR! 代码ENOSELF
错误的ERR! 拒绝在包下安装名为“webpack”的包
错误的ERR! 也被称为“webpack”。 你的项目名称是否相同?
错误的ERR! 作为您正在安装的依赖项?
错误的ERR!
错误的ERR! 有关更多信息,请参阅:
错误的ERR!<https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm>

错误的ERR! 可以在以下位置找到此运行的完整日志:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 解决方案

  • 修改 package.jsonname,然后再次执行npm install
[root@elasticsearch /data/elasticsearch-dump]# vim package.json 
{
  "author": "Evan Tahler <evantahler@gmail.com>",
  "name": "elasticdump1",				#随便改就行
}
1
2
3
4
5

# 备份索引库

# 语法:elasticdump --input es地址/索引 --output 备份到某个路径
[root@elasticsearch ~]# mkdir /data/es-backer
[root@elasticsearch ~]# elasticdump --input http://172.31.80.1:9200/ajia --output /data/es-backer/ajia.json
Thu, 14 Jan 2021 02:57:08 GMT | starting dump
Thu, 14 Jan 2021 02:57:09 GMT | got 4 objects from source elasticsearch (offset: 0)
Thu, 14 Jan 2021 02:57:09 GMT | sent 4 objects to destination file, wrote 4
Thu, 14 Jan 2021 02:57:09 GMT | got 0 objects from source elasticsearch (offset: 4)
Thu, 14 Jan 2021 02:57:09 GMT | Total Writes: 4
Thu, 14 Jan 2021 02:57:09 GMT | dump complete
1
2
3
4
5
6
7
8
9

# 测试还原

# 删除索引

  • 在kibana中删除:Delete ajia

# 还原索引库

# 语法:elasticdump --input 备份文件路径 --output es地址/索引
[root@elasticsearch ~]# elasticdump --input /data/es-backer/ajia.json --output http://172.31.80.1:9200/ajia
Thu, 14 Jan 2021 03:04:45 GMT | starting dump
Thu, 14 Jan 2021 03:04:45 GMT | got 4 objects from source file (offset: 0)
Thu, 14 Jan 2021 03:04:50 GMT | sent 4 objects to destination elasticsearch, wrote 4
Thu, 14 Jan 2021 03:04:50 GMT | got 0 objects from source file (offset: 4)
Thu, 14 Jan 2021 03:04:50 GMT | Total Writes: 4
Thu, 14 Jan 2021 03:04:50 GMT | dump complete
1
2
3
4
5
6
7
8

# 两个es之间进行数据迁移

# 语法:elasticdump --input 要迁移es1地址/索引 --output 迁移到es2地址/索引
[root@elasticsearch ~]# elasticdump --input http://172.31.80.1:9200/ajia --output http://172.31.80.2:9200/ajia
Thu, 14 Jan 2021 03:08:24 GMT | starting dump
Thu, 14 Jan 2021 03:08:24 GMT | got 4 objects from source elasticsearch (offset: 0)
Thu, 14 Jan 2021 03:08:32 GMT | sent 4 objects to destination elasticsearch, wrote 4
Thu, 14 Jan 2021 03:08:32 GMT | got 0 objects from source elasticsearch (offset: 4)
Thu, 14 Jan 2021 03:08:32 GMT | Total Writes: 4
Thu, 14 Jan 2021 03:08:32 GMT | dump complete
1
2
3
4
5
6
7
8

# logstash

# logstash配置迁移全量数据

input{
    elasticsearch{
      # 源集群地址
      hosts => ["127.0.0.1:9200","127.0.0.1:9201","127.0.0.1:9202"]
      # 如果设置了访问ES服务的用户名密码
      # user => "yyyy"
      # password => "xxxxxx"
      # 表示所有索引,多个索引以英文以逗号(,)分隔。
      index => "*"
      # 设置为true,将会提取ES文档的元信息,例如index、type和id
      docinfo => true
      size => 5000
      scroll => "5m"
    }
}

filter {
  # 去掉一些Logstash自己加的字段。
  mutate {
    remove_field => ["@timestamp", "@version"]
  }
}

output{
    elasticsearch{
      # 目标集群地址
      hosts => ["xx.xx.xx.xx:9200","xx.xx.xx.xx:9201","47.xx.xx.xx.xx:9202"]
      # 目标端索引名称,以下配置表示索引与源端保持一致。
      index => "%{[@metadata][_index]}"
      # 目标端索引type,以下配置表示索引类型与源端保持一致。
      document_type => "%{[@metadata][_type]}"
      # 目标端数据的id,如果不需要保留原id,可以删除以下这行,删除后性能会更好。
      document_id => "%{[@metadata][_id]}"
    }
}
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

# 运行

  • 使用logstash -f logstash.conf -t检测配置是否正确,如果返回Configuration OK,则表示配置正确。
  • 运行命令logstash -f logstash.conf实现ES跨集群同步。

# logstash配置迁移增量数据

input{
    elasticsearch{
        # 源端ES地址。
        hosts =>  ["http://localhost:9200"]
        # 安全集群配置登录用户名密码。
        user => "xxxxxx"
        password => "xxxxxx"
        # 需要迁移的索引列表,多个索引使用英文逗号(,)分隔。
        index => "kibana_sample_data_logs"
        # 按时间范围查询增量数据,以下配置表示查询最近5分钟的数据。
        query => '{"query":{"range":{"@timestamp":{"gte":"now-5m","lte":"now/m"}}}}'
        # 定时任务,以下配置表示每分钟执行一次。
        schedule => "* * * * *"
        scroll => "5m"
        docinfo=>true
        size => 5000
    }
}

filter {
  # 去掉一些Logstash自己加的字段.
  mutate {
    remove_field => ["@timestamp", "@version"]
  }
}


output{
    elasticsearch{
        # 目标端ES地址,可在阿里云Elasticsearch实例的基本信息页面获取。
        hosts => ["http://es-cn-zvp2m4bko0009****.elasticsearch.aliyuncs.com:9200"]
        # 安全集群配置登录用户名密码.
        user => "elastic"
        password => "xxxxxx"
        # 目标端索引名称,以下配置表示索引与源端保持一致。
        index => "%{[@metadata][_index]}"
        # 目标端索引type,以下配置表示索引类型与源端保持一致。
        document_type => "%{[@metadata][_type]}"
        # 目标端数据的id,如果不需要保留原id,可以删除以下这行,删除后性能会更好。
        document_id => "%{[@metadata][_id]}"
        ilm_enabled => false
        manage_template => false
    }
}
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