本文基于es7.1版本。部分内容在较旧或较新版本可能不十分准确。
es提供对于运行中的集群的备份恢复功能,利用此功能可以备份整个集群(open或者started状态的分片,备份不了closed的index),或者备份指定的index。备份目标存储可以是网络文件系统NFS、Amazon S3、HDFS、Azure(将来可能不支持deprecated)、以及谷歌云存储。
对于不同es版本备份数据与恢复之间的版本兼容性,官方文档中有这样的描述:
A snapshot contains a copy of the on-disk data structures that make up an index. This means that snapshots can only be restored to versions of Elasticsearch that can read the indices:
- A snapshot of an index created in 6.x can be restored to 7.x.
- A snapshot of an index created in 5.x can be restored to 6.x.
- A snapshot of an index created in 2.x can be restored to 5.x.
- A snapshot of an index created in 1.x can be restored to 2.x.
意思就是说前一个major版本的备份,可以在后一个major的版本上被恢复。但是要注意,1.x的备份在2.x上恢复后,接着在2.x上对恢复的数据再备份,是不可以恢复到5.x上的。因为原始索引是在1.x下创建的,保留了1.x的特性,在从1.x恢复到2.x的过程中,特性仍然被保留,5.x的版本不能识别1.x创建的索引结构。When backing up your data prior to an upgrade, keep in mind that you won’t be able to restore snapshots after you upgrade if they contain indices created in a version that’s incompatible with the upgrade version.解决办法就是在2.x上restore后,reindex索引,然后delete掉旧索引。或者利用reindex-from-remote功能,直接从1.x建索引重建到2.x上。
要在es上进行备份,需要先创建repository,这里以备份到HDFS为例进行说明。这里所描述的内容在官方文档中也有讲述:https://www.elastic.co/guide/en/elasticsearch/plugins/7.3/repository-hdfs.html
1.首先安装hdfs插件。可以通过联机安装和离线安装。联机安装执行如下命令:
sudo bin/elasticsearch-plugin install repository-hdfs
离线安装,需要先下载对应版本的插件压缩包。比如我这里是es7.0.1版本,需要下载7.0.1的包,否则安装会报错。https://artifacts.elastic.co/downloads/elasticsearch-plugins/repository-hdfs/repository-hdfs-7.0.1.zip 对应版本的zip包只需要修改版本号即可。下载下来后,root用户cd到es bin目录下,执行下面命令安装,file后面的路径按照实际修改:
./elasticsearch-plugin install file:///bigdata/cluster1/repository-hdfs-7.0.1.zip
安装过程会弹出警告,忽略,选Y继续安装完成。结束后需要重启节点。集群中每个节点都照此步骤进行。因为repository会被持久化到cluster state,当节点online时,会尝试初