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
依赖说一下,避免找不到对应jar包里的方法,当使用springboot + Elasticsearch时,需要明确指定依赖.
-
创建快照仓库
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);
-
创建快照
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);