从Elasticsearchdump备份方式转到es快照备份过程

本文介绍了如何使用Elasticsearch的快照功能来备份和恢复大型索引。首先,通过在Elasticsearch配置中设置路径并创建仓库,然后在主环境和备用环境执行快照操作。备份过程可以通过指定索引或全部索引完成,并使用`wait_for_completion=true`确保备份完成。恢复时,需在备用环境设置`readonly`属性以允许恢复操作。此外,还提供了备份和恢复的bash脚本示例。
摘要由CSDN通过智能技术生成

        本文只做记录

需求:之前采用Elasticsearchdump备份索引到备份环境,但是无奈索引越来越大,Elasticsearchdump只适合少量数据备份,恢复,于是就嘎嘎百度,了解了一下es快照备份,话不多说,开干

首先搭建一套es集群(无论是在docker还是k8s下或直接在服务器上启动,都差不多,所以就不在赘述了,我们这里以docker下的es集群为例,采用共享目录的方式进行快照存储)

第一:首先在启动es的时候要映射一下目录,我这里选择的是宿主机目录/data/sbapshot/映射到容器里面/home的路径下,我的主备环境的挂载的是目录是/data/sbapshot/,主备都可以访问)

第二:修改elasticsearch.yml的配置文件,加一行:

path.repo: '/home'

 添加好配置文件之后,重启启动docker,使配置生效。

之后进行在主环境创建仓库 http://ip:端口/_snapshot/es_backup

{

  "type": "fs",

  "settings": {

    "location": "/home",

    "compress": "true"

  }

}

备环境创建仓库 http://ip:端口/_snapshot/es_backup

{

  "type": "fs",

  "settings": {

    "location": "/home",

    "compress": "true"

    "readonly": "true"

  }

}

备环境(既要恢复索引的环境)必须设置"readonly": "true"  如果不设置,那就是默认为可readonly,不然无法正常恢复索引

 创建仓库完成之后进行查询,确认仓库已经创建完成 http://ip:端口/_snapshot/_all

 现在开始备份索引 http://ip:端口/_snapshot/es_backup/snapshot_23.04.13?wait_for_completion=true

{

    "indices": "索引名称-2023.04.19,索引名称-2023.04.18"

}

如果要备份全部索引,那就把body里面的清空,就可以备份集群中所有的索引了,

?wait_for_completion=true:代表备份完成之后在反应响应,在备份过程中一直会堵塞,等待响应成功

把索引进行还原http://ip:端口/_snapshot/es_backup/snapshot_2023.04.16/_restore?wait_for_completion=true

 更多es快照命令可以在csdn中进行搜索

es快照备份脚本,

#!/bin/bash

# 执行快照备份
time=$(date -d last-day +%Y.%m.%d)
logfile="/data/back/1.log"

curl -XPUT "http://10.206.16.86:9200/_snapshot/es_backup/snapshot_"${time} -H 'Content-Type: application/json' -d'
{
    "indices": "索引名称-'${time}',索引名称-'${time}'"
}'

# 快照备份需要一定时间,600s后检查备份情况
sleep 600


echo ""
curl -XGET "http://10.206.32.19:9200/_snapshot/es_backup/snapshot_"${time}   >> $logfile

if [ $? -eq 0 ]
then
  echo   "backup elasticsearch success" >> $logfile
else
  echo   "backup elasticsearch fail" >> $logfile
fi
echo ""


#恢复索引
curl -XPOST "http://10.206.32.19:9200/_snapshot/es_backup/snapshot_"${time}/_restore?wait_for_completion=true

#10分钟之后清楚7天前的索引
sleep 600

# 删除7天前的快照
delete_time=$(date "+%Y.%m.%d" -d " -7 day")
curl  -XDELETE "http://10.206.16.86:9200/_snapshot/es_backup/snapshot_"${delete_time} >> $logfile

Elasticsearchdump

导出备份

#!/bin/bash
# 集群地址

#PATH=/usr/local/node/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
source /etc/profile
ES=http://ip:9200
# 导出文件存入目录
ED=/data/backup/backupdata/es
mkdir /data/backup/backupdata/es -p

for index in `curl -s -XGET $ES/_cat/indices?h=i` 
do
    # settings, analyzer, data, mapping, alias, template
    echo "elasticdump --input=$ES/$index --output=$ED/$index"
    elasticdump --input=$ES/$index --output=${ED}/${index}_setting.json --limit=10000 --type=settings --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
    elasticdump --input=$ES/$index --output=${ED}/${index}_analyzer.json --limit=10000 --type=analyzer --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
    elasticdump --input=$ES/$index --output=${ED}/${index}_alias.json --limit=10000 --type=alias --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
    elasticdump --input=$ES/$index  --output=${ED}/${index}_template.json --limit=10000 --type=template --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
    elasticdump --input=$ES/$index --output=${ED}/${index}_mapping.json --limit=10000 --type=mapping --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
    elasticdump --input=$ES/$index --output=${ED}/${index}_data.json --limit=10000 --type=data --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
    echo ""
done
sleep 1h #睡眠1小时
#压缩
t=$(date +%Y%m%d%H%M%S)
cd /data/backup/backupdata/
tar -czvf es_$t.tar.gz es/

导入脚本

#!/bin/bash
#!/bin/bash
# 目标 ES 集群地址
ES=ip:9200

#ls es 这个es是要导入数据存储的目录,可以把导出的数据放在跟脚本同级的es目录里,下面的命令就不需要改
INDEXS=`ls es/|grep "analyzer"|awk -F"_a" '{print $1}'`
# analyzer restore
for index in ${INDEXS}
do
  echo ${index}
  elasticdump --input=`pwd`/es/${index}_setting.json --output=http://$ES/${index} --type=analyzer -limit=10000 --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
  elasticdump --input=`pwd`/es/${index}_analyzer.json --output=http://$ES/${index} --type=analyzer -limit=10000 --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
  elasticdump --input=`pwd`/es/${index}_alias.json --output=http://$ES/${index} --type=analyzer -limit=10000 --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
  elasticdump --input=`pwd`/es/${index}_mapping.json --output=http://$ES/${index} --type=mapping -limit=10000 --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
  elasticdump --input=`pwd`/es/${index}_data.json --output=http://$ES/${index} --type=data --limit=10000 --searchBody '{"query": { "match_all": {} }, "stored_fields": ["*"], "_source": true }'
done

结束!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值