ElasticSearch备份与恢复

2022-10-27 15:11:31 浏览数 (1)

官方文档

挂载

挂载远程主机目录
代码语言:javascript复制
# CentOS
yum install epel-release
yum install fuse-sshfs

# ubuntu
apt install sshfs

sshfs root@nfs_ip:/data/backup/elasticsearch /data/backup/elasticsearch -o allow_other	# 如果没有allow_other参数,挂载过来的目录只有root才能访问
卸载目录
代码语言:javascript复制
fusermount -u /data/backup/elasticsearch

备份

创建快照备份目录
代码语言:javascript复制
mkdir /data/backup/elasticsearch
chown -R elasticsearch.elasticsearch /data/backup/elasticsearch
修改es配置,添加快照备份目录

vim /etc/elasticsearch/elasticsearch.yml

代码语言:javascript复制
path.repo: /data/backup/elasticsearch		# 如果是es集群,要确保所有集群可以访问到该目录
创建快照仓库
代码语言:javascript复制
curl -X PUT "http://localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "/data/backup/elasticsearch", "compress": true }}'

仓库名为my_backup,fs仓库类型为共享文件系统,location指定一个本机目录作为快照保存路径

创建完成后,查看所有仓库

_all替换具体的仓库名称

代码语言:javascript复制
curl -XGET localhost:9200/_snapshot/_all?pretty

备份快照
代码语言:javascript复制
curl -X PUT "localhost:9200/_snapshot/my_backup/news?wait_for_completion=true&pretty" -H 'Content-Type: application/json' -d'  { "indices":"indices1,indices2", "ignore_unavailable": true }'		# 只备份indices1和indices2索引

curl -XPUT localhost:9200/_snapshot/my_backup/news?wait_for_completion=true		# 备份所有索引到my_backup仓库下一个名为news快照中

备份快照

代码语言:javascript复制
curl -X PUT "localhost:9200/_snapshot/my_backup/news?wait_for_completion=true&pretty" -H 'Content-Type: application/json' -d'  { "indices":"indices1,indices2", "ignore_unavailable": true }'		# 只备份indices1和indices2索引

curl -XPUT localhost:9200/_snapshot/my_backup/news?wait_for_completion=true		# 备份所有索引到my_backup仓库下一个名为news快照中

仓库名my_backup,快照名news

备份完成后,查看快照
代码语言:javascript复制
curl -XGET localhost:9200/_snapshot/my_backup/_all?pretty

恢复

创建快照目录
代码语言:javascript复制
mkdir /data/backup/elasticsearch
chown -R elasticsearch.elasticsearch /data/backup/elasticsearch
修改es配置,添加备份目录

vim /etc/elasticsearch/elasticsearch.yml

代码语言:javascript复制
path.repo: /data/backup/elasticsearch		# 如果是es集群,要确保所有集群可以访问到该目录
创建快照仓库
代码语言:javascript复制
curl -X PUT "http://localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "/data/backup/elasticsearch", "compress": true }}'

恢复快照
代码语言:javascript复制
curl -XPOST http://localhost:9200/_snapshot/my_backup/news/_restore?wait_for_completion=true		# 默认会恢复news快照中所有的索引

0 人点赞