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时,需要明确指定依赖.

   <properties>
        <elasticsearch.version>7.9.3</elasticsearch.version>
    </properties>
     <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.17.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
  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<String, Object> 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已关闭");
    

测试结果
在这里插入图片描述

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

测试结果
在这里插入图片描述
3.快照恢复
先删除一个索引,请求方式delete,使用http的方式
http://localhost:9200/shopping_demo
这是删除前的es索引列表
在这里插入图片描述
删除后的es索引列表
在这里插入图片描述
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<String> indices = restoreInfo.indices();
        for (String s : indices){
            System.out.println("恢复索引名称:" + s);
        }
        client.close();
        System.out.println("es已关闭");

在这里插入图片描述
索引再次恢复了,里面数据也还在,这些只是一些基本操作,写的不好的或者有好的建议请大家指正,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值