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

快照备份

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

  1. 服务名称 cluster.name: my-application
  2. 节点名称 node.name: node-1
  3. 快照本地路径 path.repo: [E:/es/data]
  4. 地址network.host: localhost
  5. 端口 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
        
  1. 创建快照仓库

    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已关闭");
    

测试结果
ElasticSearch7.x基于Java API 快照备份和恢复_第1张图片

  1. 创建快照

    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已关闭");
    

测试结果
ElasticSearch7.x基于Java API 快照备份和恢复_第2张图片
3.快照恢复
先删除一个索引,请求方式delete,使用http的方式
http://localhost:9200/shopping_demo
这是删除前的es索引列表
ElasticSearch7.x基于Java API 快照备份和恢复_第3张图片
删除后的es索引列表
ElasticSearch7.x基于Java API 快照备份和恢复_第4张图片
shopping_demo这个索引已被删除,现在开始恢复索引

 RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );
        System.out.println("es已成功链接");
        //仓库名称
        String repositoryName = "my_backup0001";
        //快照名称
        String snapshotName = "backup1644456687246";
        //恢复快照
        RestoreSnapshotRequest request = new RestoreSnapshotRequest(repositoryName, snapshotName);
        //需要恢复的索引名称
        request.indices("shopping_demo");
        request.masterNodeTimeout("1m");
        request.waitForCompletion(true);
        request.partial(false);
        request.includeGlobalState(false);
        request.includeAliases(false);
        //同步
        RestoreSnapshotResponse response = client.snapshot().restore(request, RequestOptions.DEFAULT);
        RestoreInfo restoreInfo = response.getRestoreInfo();
        List indices = restoreInfo.indices();
        for (String s : indices){
            System.out.println("恢复索引名称:" + s);
        }
        client.close();
        System.out.println("es已关闭");

ElasticSearch7.x基于Java API 快照备份和恢复_第5张图片
索引再次恢复了,里面数据也还在,这些只是一些基本操作,写的不好的或者有好的建议请大家指正,谢谢!

你可能感兴趣的:(java,java,后端)