欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

ElasticSearch7.x基于JavaAPI快照备份和恢复

时间:2023-07-11
快照备份

1、配置es环境
我这里用的是windowns版本的,在es官网下载压缩包
https://www.elastic.co/cn/downloads/elasticsearch
解压后在connfig找到elasticsearch.yml进行以下配置修改

服务名称 cluster.name: my-application节点名称 node.name: node-1快照本地路径 path.repo: [E:/es/data]地址network.host: localhost端口 http.port: 9200

在 bin 目录下,找到elasticsearch.bat 双击 启动es

2.创建好springboot项目进行测试

依赖说一下,避免找不到对应jar包里的方法,当使用springboot + Elasticsearch时,需要明确指定依赖.

7.9.3 org.elasticsearch.client elasticsearch-rest-high-level-client ${elasticsearch.version} org.elasticsearch elasticsearch ${elasticsearch.version} org.apache.logging.log4j log4j-core 2.17.0 org.apache.logging.log4j log4j-api 2.17.0 junit junit test com.fasterxml.jackson.core jackson-databind

创建快照仓库

RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200)) ); System.out.println("es已成功链接"); //创建快照仓库 PutRepositoryRequest request = new PutRepositoryRequest(); request.name("my_backup0001"); request.type(FsRepository.TYPE); request.masterNodeTimeout(Timevalue.timevalueMinutes(1)); request.verify(true); String locationKey = FsRepository.LOCATION_SETTING.getKey(); String locationValue = "my_fs_data_location"; String compressKey = FsRepository.COMPRESS_SETTING.getKey(); boolean compressValue = true; Map map = new HashMap<>(); map.put(locationKey, locationValue); map.put(compressKey, compressValue); request.settings(map); AcknowledgedResponse response = client.snapshot().createRepository(request, RequestOptions.DEFAULT); //确认是否响应 boolean acknowledged = response.isAcknowledged(); System.out.println("创建快照仓库是否成功:"+acknowledged); client.close(); System.out.println("es已关闭");

测试结果

创建快照

RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200)) ); System.out.println("es已成功链接"); //快照开始时间 System.out.println("快照开始时间:" + new Date()); //创建快照 CreateSnapshotRequest request = new CreateSnapshotRequest(); //快照仓库名称 request.repository("my_backup0001"); //快照名称 String snapshotName = "backup"+System.currentTimeMillis(); request.snapshot(snapshotName); //快照的索引 第一次全量备份,以后是增量备份 request.indices("shopping_demo", "test_demo"); request.indicesOptions(IndicesOptions.fromOptions(false, false, true, true)); request.partial(false); request.includeGlobalState(true); request.masterNodeTimeout("1m"); request.waitForCompletion(true); //同步请求客户端 CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT); //快照信息 SnapshotInfo snapshotInfo = response.getSnapshotInfo(); //快照索引信息 System.out.println(snapshotInfo.indices()); //快照结束时间 System.out.println("快照结束时间:" + snapshotInfo.endTime()); //快照创建响应状态 200 ok RestStatus status = response.status(); System.out.println("es快照响应状态:"+status); client.close(); System.out.println("es已关闭");

测试结果

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。