Elasticsearch数据备份与恢复(基于HDFS)
1.(所有机子上)
安装es hdfs仓库插件repository-hdfs
# repository-hdfs一定要和es版本匹配
#进入elasticsearch的目录有两种安装插件的方式(你彪哥建议离线安装)
# 在线
bin/elasticsearch-plugin install repository-hdfs
# 离线
bin/elasticsearch-plugin install file:///xxx/repository-hdfs-x.x.x.zip.zip
#file:///xxx/repository-hdfs-x.x.x.zip.zip指的是repository-hdfs的位置(这里是在提醒啥也不知道的憨憨)
#进入elasticsearch安装目录后发现多了一个plugins目录,此为安装成功
2. (所有机子上)
设置ES-HDFS仓库安全策略
plugins/repository-hdfs/plugin-security.policy追加
permission java.lang.RuntimePermission "accessDeclaredMembers";
permission java.lang.RuntimePermission "getClassLoader";
permission java.lang.RuntimePermission "shutdownHooks";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission javax.security.auth.AuthPermission "doAs";
permission javax.security.auth.AuthPermission "getSubject";
permission javax.security.auth.AuthPermission "modifyPrivateCredentials";
permission java.security.AllPermission;
permission java.util.PropertyPermission "*", "read,write";
permission javax.security.auth.PrivateCredentialPermission "org.apache.hadoop.security.Credentials * \"*\"", "read";
3.(所有机子上)
设置JAVA安全管理器
$JAVA_HOME/jre/lib/security/java.policy追加(此为jdk目录不多逼逼)
permission java.security.AllPermission;
重启es集群,注意需要滚动启动, 并行启动es你懂得~
4. (所有机子上)
修改HADOOP中配置文件
在hdfs-site.xml 文件中添加如下内容~
<property>
<name>dfs.permissions</name>
<value>false</value>
</property>
5.(一台机子上)
到namenode机器上创建hdfs目录
su - hdfs
hdfs dfs -ls /
hdfs dfs -mkdir /esbak
hdfs dfs -ls /
hdfs dfs -chmod +777 /esbak
6. (一台机子上)
创建HDFS仓库
# 创建远程仓库,目录为/eabak
# 在HDFS 50070查看
# uri需要是active的namenode节点
curl -XPUT 'http://host:esport/_snapshot/es_hdfs_repository' -d '
{
"type":"hdfs",
"settings":{
"uri":"hdfs://host:hdfsPort/",
"path":"/eabak"
}
}'
#host为IP地址
#esprot为es内部端口,一般为9200
#hdfsport为namenode内部端口,一般为8020
7. (一台机子上)
备份
# 对所有open的index进行备份,创建一个snapshot_1的备份
# wait_for_completion会堵塞到备份完成
curl -XPUT 'http://host:esPort/_snapshot/es_hdfs_repository/snapshot_1?wait_for_completion=true'
# 指定索引备份
curl -XPUT 'http://host:esPort/_snapshot/es_hdfs_repository/snapshot_1?wait_for_completion=true' -d '
{
"indices":["index1","index2"],
"ignore_unavailable":true,
"include_global_state":false,
"partial":true
}'
# 查看备份进度和结果
curl -XGET 'http://host:esPort/_snapshot/es_hdfs_repository/snapshot_1?pretty'
6.(一台机子上)
恢复
# 恢复指定索引
#include_global_state=true 集群状态也备份
#ignore_unavailable 部分索引不存在时也通过,否则认为不成功
#partial=true 部分失效也可以通过,否则认为不成功
curl -XPOST 'http://host:esPort/_snapshot/es_hdfs_repository/snapshot_1/_restore?wait_for_completion=true' -d '
{
"indices":["redisindex"],
"ignore_unavailable":true,
"include_global_state":false,
"partial":true
}'
# 恢复snapshot_1全部索引
curl -XPOST 'http://host:esPort/_snapshot/es_hdfs_repository/snapshot_1/_restore?wait_for_completion=true'
#恢复进度和结果
curl -XGET 'http://host:esPort/index1/_recovery?pretty'
注意!!!!
恢复之前最好先把所有close,防止恢复期间还有新数据产生
如果是不存在的索引,不在考虑范围
最后
# 关闭索引
curl -XPOST 'hhtp://host:esPort/eventindex/_close'
# 打开索引
curl -XPOST 'http://host:esPort/eventindex/_open'
# 删除备份
# 备份是增量备份的,所以不要轻易手动删,否则会出现索引损坏而无法恢复
# 正确的姿势
curl -XDELETE 'http://host:esPort/_snapshot/es_hdfs_repository/snapshot_1'