elasticsearch备份和恢复

当elasticsearch存储的数据量比较大时,open_files数量直线上升,当此值达到瓶颈时,elasticsearch将无法正常运行,直接导致too many open files,

 当然可以适当调整用户的ulimit,但当ulimit也达到最大值时如何处理呢?

一个办法是增加集群节点数,使shards重新分配,从而降低每个节点的shards个数,也就减少了每个节点open_files数量;

另外一个办法是清除部分old index。清理方式可通过elasticsearch rest api进行,如:

 

curl -XDELETE 'http://127.0.0.1:9200/myOldIndex-2016.06*'

如果想要保留old index就需要用到snapshot功能了。

使用snapshot功能,需要以下几步操作:

1、yml文件配置repo路径

path.repo: ["/data/elasticsearch/snapshot"]

2、重启elasticsearch服务

通过rest api可以方便的查看snapshot配置:curl http://127.0.0.1:9200/_snapshot/_all

3、创建备份目录

mkdir -p /data/elasticsearch/snapshot

mkdir -p /data/elasticsearch/back

4、执行脚本

curl -XPUT 127.0.0.1:9200/_snapshot/backup -d '
{
"type":"fs",
"settings":{"location":"/data/elasticsearch/snapshot"}
}'

5、执行flush,保证elasticsearch所有的数据都刷到磁盘了

curl http://127.0.0.1:9200/_flush

6、执行backup,如下esbackup.sh脚本按日期备份,精确到小时

#!/bin/bash
filename=`date +%Y%m%d%H`
backesFile=es$filename.tar.gz
cd /data/elasticsearch/back
mkdir es_dump
cd es_dump
curl -XDELETE 127.0.0.1:9200/_snapshot/backup/$filename?pretty
echo 'sleep 30'
sleep 30
curl -XPUT 127.0.0.1:9200/_snapshot/backup/$filename?wait_for_completion=true&pretty
echo 'sleep 30'
sleep 30
cp /data/elasticsearch/snapshot/* /data/elasticsearch/back/es_dump -rf
cd ..
tar czf $backesFile es_dump/
rm es_dump -rf

至此,备份已完成,可以安心的删除elasticsearch中的index了

如何恢复呢?

执行以下esrestore.sh脚本即可

#!/bin/bash
#指定要恢复的时间点
filename='2016082909'
backesFile=es$filename.tar.gz
cd /data/elasticsearch/back
tar zxvf $backesFile
rm /data/elasticsearch/snapshot/* -rf
cp /data/elasticsearch/back/es_dump/* /data/elasticsearch/snapshot -rf

curl -XPOST 127.0.0.1:9200/myindex1/_close
#curl -XPOST 127.0.0.1:9200/myindex2/_close
echo 'sleep 5'
sleep 5
curl -XPOST 127.0.0.1:9200/_snapshot/backup/$filename/_restore?pretty -d '{
    "indices":"myindex1"
}'
#echo 'sleep 5'
#sleep 5
#curl -XPOST 127.0.0.1:9200/_snapshot/backup/$filename/_restore?pretty -d '{
#     "indices":"myindex2"
#}'
echo 'sleep 5'
sleep 5
curl -XPOST 127.0.0.1:9200/myindex1/_open
#curl -XPOST 127.0.0.1:9200/myindex2/_open 
rm es_dump -rf

猜你喜欢

转载自jimbean.iteye.com/blog/2320645