/** * Returns a list containing all the names of existing snapshots in the configured repository * * @return List of string with the snapshot names */ public List<String> showSnapshots() { ClusterAdminClient adminClient = this.clientFactory.client().admin().cluster(); GetSnapshotsRequestBuilder builder = new GetSnapshotsRequestBuilder(adminClient); builder.setRepository(repoConfig.getName()); GetSnapshotsResponse getSnapshotsResponse = builder.execute().actionGet(); return getSnapshotsResponse .getSnapshots() .stream() .map(SnapshotInfo::name) .collect(Collectors.toList()); }
/** * You cannot restore an open existing index, therefore we first check if an index exists that is * in the snapshot. If the index exists we close the index before we start the restore action. * * @param snapshot String containing the name of the snapshot in the configured repository to * restore */ public void restoreSnapshot(String snapshot) { ClusterAdminClient adminClient = this.clientFactory.client().admin().cluster(); // Obtain the snapshot and check the indices that are in the snapshot GetSnapshotsRequestBuilder builder = new GetSnapshotsRequestBuilder(adminClient); builder.setRepository(repoConfig.getName()); builder.setSnapshots(snapshot); GetSnapshotsResponse getSnapshotsResponse = builder.execute().actionGet(); // Check if the index exists and if so, close it before we can restore it. ImmutableList<String> indices = getSnapshotsResponse.getSnapshots().get(0).indices(); CloseIndexRequestBuilder closeIndexRequestBuilder = new CloseIndexRequestBuilder(this.clientFactory.client().admin().indices()); closeIndexRequestBuilder.setIndices(indices.toArray(new String[indices.size()])); closeIndexRequestBuilder.execute().actionGet(); // Now execute the actual restore action RestoreSnapshotRequestBuilder restoreBuilder = new RestoreSnapshotRequestBuilder(adminClient); restoreBuilder.setRepository(repoConfig.getName()).setSnapshot(snapshot); restoreBuilder.execute().actionGet(); logger.info("The snapshot {} is restored", snapshot); }