Elasticsearch数据备份与恢复(基于HDFS)
Elasticsearch数据备份与恢复(基于HDFS)
1.(所有机子上)
安装es hdfs仓库插件repository-hdfs
# repository-hdfs一定要和es版本匹配
# 在线
bin/elasticsearch-plugin install repository-hdfs
# 离线
bin/elasticsearch-plugin install file:///xxx/repository-hdfs-x.x.x.zip.zip
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";
config/jvm.options追加
Djava.security.policy=file:////xxx/plugins/repository-hdfs/plugin-security.policy
3.(所有机子上)
设置JAVA安全管理器
$JAVA_HOME/jre/lib/security/java.policy追加
permission java.security.AllPermission;
重启Es集群
4. (一台机子上)
创建HDFS仓库
# 创建远程仓库,目录为/user/xxx/es/repository/es_hdfs_repository
# 在HDFS 50070查看
# uri需要是active的namenode节点
curl -XPUT 'http://host:esPort/_snapshot/es_hdfs_repository' -d '
{
"type":"hdfs",
"settings":{
"uri":"hdfs://host:hdfsPort/",
"path":"es/repository/es_hdfs_repository"
}
}'
5. (一台机子上)
备份
# 对所有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'
Tips:
恢复之前最好先把所有close,防止恢复期间还有新数据产生
如果是不存在的索引,不在考虑范围
7.其他
# 关闭索引
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'