1.先备份ES中的数据,重要!!!
运行准备的脚本,下面步骤中
2.创建新的存储路径
mkdir /home/elsearch/data/
3.修改elasticsearch.yml中的路径,将旧路径改成新的路径
重要:修改路径之前一定要先停掉ES的服务,不然找不到数据路径,会将Linux跑崩溃的, 血的教训
旧的数据路径: path.data: /home/data/elasticsearch
新的数据路径: path.data: /home/elsearch/data/
4. 赋予elasticsearch的账号(elsearch)权限
chown -R elsearch:elsearch /home/elsearch/data/
5. 移动nodes去新的路径下
/home/data/elasticsearch #mv * /home/elsearch/data/
6. 用账号elsearch去重启服务
可重启,查看数据正常是否
7. 测试新的路径备份数据遇到以下问题
{"error":{"root_cause":[{"type":"invalid_snapshot_name_exception","reason":"[backup:snapshot_all]Invalid snapshot name [snapshot_all], snapshot with the same name already exists"}],"type":"invalid_snapshot_name_exception","reason":"[backup:snapshot_all]Invalid snapshot name [snapshot_all], snapshot with the same name already exists"},"status":400}
问题原因: 存储路径改变,需要将仓库重新设置。
8. 修改数据存储后需要重新设置仓库快照
1) 在elasticsearch.yml中添加快照仓库设置
path.repo: "/home/elasticsearch/snapshot"
2) 通过postman设置,需要设置Hearders和Body两个部分,如下图。
命令:
curl -XPUT 'http://192.168.3.2:9200/_snapshot/backup' -d '{
"type": "fs",
"settings": {
"location": "/home/elasticsearch/snapshot",
"compress": "true"
}
}'
通过postman去设置仓库快
9. 备份脚本如下(现在生产就用到如下脚本)
注意此处路径必须存在:
/home/elasticsearch/back
/home/elasticsearch/snapshot
************************************************************************************************************************
#!/bin/bash
filename=`date +%Y%m%d%H`
backesFile=es$filename.tar.gz
cd /home/elasticsearch/back
mkdir es_dump
cd es_dump
curl -XDELETE http://192.168.3.2:9200/_snapshot/backup/$filename?pretty
echo 'sleep 5'
sleep 5
curl -XPUT http://192.168.3.2:9200/_snapshot/backup/snapshot_all?wait_for_completion=true&pretty
echo 'sleep 300'
sleep 300
cp /home/elasticsearch/snapshot/* /home/elasticsearch/back/es_dump -rf
cd ..
tar czf $backesFile es_dump/
rm es_dump -rf
cd /home/elasticsearch/snapshot
rm -rf *
************************************************************************************************************************
脚本成功如下:
10. 还原数据
1) 将解压文件放入/home/elasticsearch/snapshot,文件如下:
2) 还原命令如下:
curl -XPOST 192.168.3.2:9200/_snapshot/backup/snapshot_all/_restore