elasticsearch备份和恢复

#elastic备份和恢复

0.备份准备

(1).首先查看es的用户,可以在/etc/passwd中查看,一般是elasticsearch用户
(2).然后修改脚本中给备份目录创建权限的chown为elasticsearch
(3).给脚本curl命令添加对应的账号密码 -u 用户:密码
(4).修改es端口为29200,改为自己环境对饮过的
(5).然后到es部署目录中执行脚本,/usr/share/elasticsearch目录

1.部署es

(1)创建数据目录

mkdir  /app/shell/es_bak/elastic/data 

chmod +777  /app/shell/es_bak/elastic/data

(2)启动es服务
docker run -dit --restart=always --memory=2G --network host --name es -p 29200:29200 -p 9300:9300 -v /etc/localtime:/etc/localtime:ro -v /app/shell/es_bak/elastic/data:/usr/share/elasticsearch/data -v /app/shell/es_bak/elastic/config:/usr/share/elasticsearch/config -v /app/shell/es_bak/elastic/bak:/home/elastic/bak 192.168.176.230:8090/public/elastic:7.1.1

(3)es设置密码

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
[root@devops elastic]# cat espasswd.sh
#!/bin/bash
#设置elastic密码
/usr/bin/expect <<-EOF
spawn docker exec -it es /bin/bash -c "elasticsearch-setup-passwords interactive"
expect "y/N"
send "y\n"
expect "elastic"
send "ytx@1234\n"
expect "elastic"
send "ytx@1234\n"
expect "apm_system"
send "ytx@1234\n"
expect "apm_system"
send "ytx@1234\n"
expect "kibana"
send "ytx@1234\n"
expect "kibana"
send "ytx@1234\n"
expect "logstash_system"
send "ytx@1234\n"
expect "logstash_system"
send "ytx@1234\n"
expect "beats_system"
send "ytx@1234\n"
expect "beats_system"
send "ytx@1234\n"
expect "remote_monitoring_user"
send "ytx@1234\n"
expect "remote_monitoring_user"
send "ytx@1234\n"
expect off
EOF

 

2.备份索引准备

(1)查看所有数据量

 

(2)导入测试数据

1
2
3
4
5
6
[root@devops data]# cat insert.sh
#!/bin/bash
#es_data.json 数据文件
#bank 索引名称
curl -H "Content-Type: application/json" -XPOST --user elastic:ytx@1234 "127.0.0.1:29200/bank/_bulk?pretty&refresh" --data-binary "@es_data.json"
curl -XGET -k --user elastic:ytx@1234 "http://127.0.0.1:29200/_cat/indices?v"

(3)测试数据文件es_data.json

(4)查看索引

 

(5)es配置文件添加备份路径

 

 (6)重启docker

 

(7)拷贝备份和恢复脚本到es容器中

docker cp es_bak.sh es:/usr/share/elasticsearch/

docker cp es_rok.sh es:/usr/share/elasticsearch/

 

3.备份指定索引

(1)备份脚本

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
[root@devops shell]# cat es_bak.sh
#!/bin/bash
currentUser=$(whoami)
if [ "$currentUser" != "root" ];then
    echo "当前用户为非root,请切换用户:${currentUser}"
    exit 1
fi
  
  
esDir="/usr/share/elasticsearch"    #es安装目录
es_backup="/home/elastic/bak"   #es备份数据目录
filename=`date +%Y%m%d%H`               #快照名称
ipAddress=127.0.0.1                     #IP
backupBeforeDelete=yes                  #备份之前是否删除旧备份
  
  
echo "*******************************"
echo "*       ES备份开始            *"
echo "*******************************"
  
#当前安装目录
currentpath=$(pwd)
echo "**当前安装目录:" ${currentpath}
  
#设置备份目录
echo "1.设置备份目录"
echo " * 修改elasticsearch.yml"
  
#查找配置文件是否存在字符串
if cat ${esDir}/config/elasticsearch.yml |grep "path.repo:"
then
    #找到
    echo " * elasticsearch.yml无需修改"
else
    #未找到
    #cat elasticsearch.yml |grep "path.repo:"
    #echo "path.repo: ["'${es_backup}'"]">> ${esDir}/config/elasticsearch.yml
    res=`echo $?`
    if [ $res = "1" ]; then
        echo -e "\033[31m修改elasticsearch.yml失败 \033[0m"
        exit 1
    fi
fi
  
echo " * 创建ES数据备份目录以及权限*"
mkdir -p  ${es_backup}
chmod 755 ${es_backup}
chown elasticsearch.elasticsearch ${es_backup}
res=`echo $?`
if [ $res = "1" ]; then
    echo -e "\033[31m创建ES数据备份目录失败 \033[0m"
    exit 1
fi
#docker rstart es #重启docker服务加载配置文件
  
  
#检测es访问地址是否有效
esStatus=$(curl -s -m 5 -IL --user elastic:ytx@1234 http://${ipAddress}:29200|grep 200)
if [ "$esStatus" != "" ];then
    echo $(date +'%Y-%m-%d %H:%M:%S') "es地址访问正常:http://${ipAddress}:29200,开始备份数据"
     
    #创建仓库
    #set -x  #打印上一步命令
    curl -H "Content-Type: application/json" -XPUT --user elastic:ytx@1234 'http://'${ipAddress}':29200/_snapshot/datasvr' -d ' {"type":"fs","settings":{"location":"'${es_backup}'","compress":true}}'
    #set +x
    res=`echo $?`
    if [ $res = "1" ]; then
        echo -e "\033[31m创建仓库失败 \033[0m"
        exit 1
    fi
    echo -e " \n创建仓库成功"
     
    #删除快照snapshot_1
    curl -X DELETE --user elastic:ytx@1234 "localhost:29200/_snapshot/datasvr/snapshot_1"
    echo -e " \n删除旧快照snapshot_1成功"
     
    #创建快照snapshot_1(备份索引)
    #curl -XPUT --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot/datasvr/snapshot_2' #备份所有索引
    curl -H "Content-Type:application/json" -XPUT --user elastic:ytx@1234 ''${ipAddress}':29200/_snapshot/datasvr/snapshot_1?wait_for_completion=true' -d '{"indices":"bank"}'
    res=`echo $?`
    if [ $res = "1" ]; then
        echo -e "\033[31m 创建快照snapshot_1失败 \033[0m"
        exit 1
    fi
    echo -e " \n创建快照snapshot_1成功"
else
    echo -e "\033[31m $(date +'%Y-%m-%d %H:%M:%S') es地址访问异常:http://${ipAddress}:29200 \033[0m"
    exit 1
fi
  
echo "*******************************"
echo "*       ES备份完成            *"
echo "*******************************"

(2)执行备份脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
cd /usr/share/elasticsearch
[root@devops elasticsearch]# ./es_bak.sh
*******************************
*       ES备份开始            *
*******************************
**当前安装目录: /usr/share/elasticsearch
1.设置备份目录
 * 修改elasticsearch.yml
path.repo: ["/home/elastic/bak"]
 * elasticsearch.yml无需修改
 * 创建ES数据备份目录以及权限*
2022-06-01 14:11:56 es地址访问正常:http://127.0.0.1:29200,开始备份数据
{"acknowledged":true}
创建仓库成功
{"error":{"root_cause":[{"type":"snapshot_missing_exception","reason":"[datasvr:snapshot_1] is missing"}],"type":"snapshot_missing_exception","reason":"[datasvr:snapshot_1] is missing"},"status":404}
删除旧快照snapshot_1成功
{"snapshot":{"snapshot":"snapshot_1","uuid":"v1sbmHO_Ql2N3eJcDLRZ3Q","version_id":7010199,"version":"7.1.1","indices":["bank"],"include_global_state":true,"state":"SUCCESS","start_time":"2022-06-01T06:11:56.606Z","start_time_in_millis":1654063916606,"end_time":"2022-06-01T06:11:57.179Z","end_time_in_millis":1654063917179,"duration_in_millis":573,"failures":[],"shards":{"total":1,"failed":0,"successful":1}}}
创建快照snapshot_1成功
*******************************
*       ES备份完成            *
*******************************

(3)查看备份目录

 

 (4)查看快照仓库

curl -XGET --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot?pretty'

(5)查看备份数据
curl -XGET --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot/datasvr/snapshot_1/_status'
curl -XGET --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot/datasvr/snapshot_1'

 

4.删除索引bank

curl -XDELETE -k --user elastic:ytx@1234 'http://127.0.0.1:29200/bank'

 

 

5.恢复bank索引

(1)恢复脚本

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
[root@devops shell]# cat es_rok.sh
#!/bin/bash
currentUser=$(whoami)
if [ "$currentUser" != "root" ];then
    echo "当前用户为非root,请切换用户:${currentUser}"
    exit 1
fi
  
ipAddress=127.0.0.1                 #IP
  
echo "*******************************"
echo "*    ES恢复备份开始            *"
echo "*******************************"
  
#当前安装目录
currentpath=$(pwd)
echo "**当前安装目录:" ${currentpath}
  
#检测es访问地址是否有效
esStatus=$(curl -s -m 5 -IL --user elastic:ytx@1234 http://${ipAddress}:29200|grep 200)
if [ "$esStatus" != "" ];then
    echo " * es地址访问正常:http://${ipAddress}:29200"
     
    #快照恢复
    #curl -H "Content-Type:application/json" -XPOST --user elastic:ytx@1234 ''${ipAddress}':29200/_snapshot/datasvr/snapshot_1/_restore'
    curl -H "Content-Type:application/json" -XPOST --user elastic:ytx@1234 ''${ipAddress}':29200/_snapshot/datasvr/snapshot_1/_restore?wait_for_completion=true' -d '{"indices":"bank"}'
    res=`echo $?`
    if [ $res = "1" ]; then
        echo -e "\033[31m * 快照恢复snapshot_1失败 \033[0m"
        exit 1
    fi
    echo -e " \n * 快照恢复snapshot_1成功"
else
    echo -e "\033[31m * es地址访问异常:http://${ipAddress}:29200 \033[0m"
    exit 1
fi
  
  
echo "*******************************"
echo "*    ES恢复备份完成           *"
echo "*******************************"

(2)恢复脚本执行

 

 

(3)完成数据恢复 

 

6.注意:

.security-7)索引里边包含了es的账户密码,所以在数据备份和恢复的时候不能删除它,因此我这边在做数据备份和恢复的时候也针对指定的索引去做,如果是多个索引可以使用脚本做循环。

 

 

7.报错处理

(1)备份报错

 (2)解决方法:

在es配置文件中添加如下行:

vim elasticsearch.yml

添加:

path.repo: "/home/backup/elk"

重启es服务

 

(3)恢复报错:

 (4)解决方法:

 没有把所有索引删除,因为备份是全量备份,所以需要删除所有索引

 curl -XDELETE -k --user elastic:ytx@1234 'http://127.0.0.1:29200/.security-7'

 

8.常用es命令汇总

_cat命令
#查询所有索引
curl -XGET -k --user elastic:ytx@1234 "http://127.0.0.1:29200/_cat/indices?pretty"
#查询ES全局状态
curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/_cluster/stats?pretty'

#查询es集群是否健康
curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/_cat/health?v'

#查询es集群index的整体数据量
curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/_cat/indices?v'

#查询集群线程池使用情况
curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/_cat/thread_pool?v'

查询_cat接口支持的所有的命令列表
curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/_cat/'

 

_nodes命令
查询指定ES实例的jvm参数 elastic是node_name
curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/_nodes/elastic/stats/jvm?pretty'

索引命令
关闭索引 my_index是索引名称
curl -XPOST -k --user elastic:ytx@1234 'http://127.0.0.1:29200/my_index/_close?pretty'

打开索引
curl -XPOST -k --user elastic:ytx@1234 'http://127.0.0.1:29200/my_index/_open?pretty'

删除索引,命令最后为索引名称,只有该索引的创建用户才可以删除索引
curl -XDELETE -k --user elastic:ytx@1234 'http://127.0.0.1:29200/bank'

查询索引mapping和settings

curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/my_index_name?pretty'

查询索引settings

curl -XGET -k --user elastic:ytx@1234 'http://127.0.0.1:29200/my_index_name/_settings?pretty'

修改索引刷新时间:

curl -XPUT --negotiate -k -u : 'https://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'{"refresh_interval" : "60s"}'

修改translog文件保留时长,默认为12小时

curl -XPUT --negotiate -k -u : 'https://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'{"index.translog.retention.age" : "30m"}'

设置索引副本:
curl -XPUT --negotiate -k -u : 'https://ip:port/my_index/_settings?pretty' -H 'Content-Type: application/json' -d'{"number_of_replicas" : 1}'

强制flush

curl -XPOST --negotiate --tlsv1.2 -k -u : 'https://ip:port/myindex/_flush'

强制refresh

curl -XPOST --negotiate --tlsv1.2 -k -u : 'https://ip:port/myindex/_refresh'

#解除只读权限
curl -XPUT "http://127.0.0.1:29200/_settings" -H 'Content-Type: application/json' -d '{"index.blocks.read_only_allow_delete":"false"}' --user elastic:ytx@1234

#查询当前索引当前类型中的所有文档
curl -XPOST --user elastic:ytx@1234 'http://127.0.0.1:29200/bank/_doc/_search'
#根据id查询数据,1是id
curl --user elastic:ytx@1234 'http://127.0.0.1:29200/bank/_doc/1'

#查看快照仓库
curl -XGET --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot?pretty'

#查看备份数据
curl -XGET --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot/datasvr/snapshot_1/_status'
curl -XGET --user elastic:ytx@1234 'http://0.0.0.0:29200/_snapshot/datasvr/snapshot_1'

 

9.参考文档

https://woj.app/3937.html

https://blog.csdn.net/liqfyiyi/article/details/120849116

posted @   Leonardo-li  阅读(2398)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示