Created
April 7, 2015 13:51
-
-
Save hakdogan/87e1a8eb782a41458a29 to your computer and use it in GitHub Desktop.
Illustrates the use of Elasticsearch Snapshot and Restore module with Java API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kodcu.es.util; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.elasticsearch.action.admin.cluster.repositories.delete.DeleteRepositoryResponse; | |
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse; | |
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; | |
import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse; | |
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest; | |
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; | |
import org.elasticsearch.client.Client; | |
import org.elasticsearch.cluster.metadata.RepositoryMetaData; | |
import org.elasticsearch.common.collect.ImmutableList; | |
import org.elasticsearch.common.settings.ImmutableSettings; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.snapshots.SnapshotInfo; | |
/** | |
* Created by hakdogan on 07/04/15. | |
*/ | |
public class SnaphotAndRestore { | |
private Logger logger = LogManager.getLogger(SnaphotAndRestore.class.getName()); | |
public boolean isRepositoryExist(Client client, String repositoryName){ | |
boolean result = false; | |
try { | |
ImmutableList<RepositoryMetaData> repositories = client.admin().cluster().prepareGetRepositories().get().repositories(); | |
if(repositories.size() > 0){ | |
for(RepositoryMetaData repo :repositories) | |
result = repositoryName.equals(repo.name())?true:false; | |
} | |
} catch (Exception ex){ | |
logger.error("Exception in getRepository method: " + ex.toString()); | |
} finally { | |
return result; | |
} | |
} | |
public PutRepositoryResponse createRepository(Client client, String repositoryName, | |
String path, boolean compress){ | |
PutRepositoryResponse putRepositoryResponse = null; | |
try { | |
if(!isRepositoryExist(client, repositoryName)) { | |
Settings settings = ImmutableSettings.settingsBuilder() | |
.put("location", path + repositoryName) | |
.put("compress", compress).build(); | |
putRepositoryResponse = client.admin().cluster().preparePutRepository(repositoryName) | |
.setType("fs").setSettings(settings).get(); | |
logger.info("Repository was created."); | |
} else | |
logger.info(repositoryName + " repository already exists"); | |
} catch(Exception ex){ | |
logger.error("Exception in createRepository method: " + ex.toString()); | |
} finally { | |
return putRepositoryResponse; | |
} | |
} | |
public DeleteRepositoryResponse deleteRepository(Client client, String repositoryName){ | |
DeleteRepositoryResponse deleteRepositoryResponse = null; | |
try { | |
if (isRepositoryExist(client, repositoryName)) { | |
deleteRepositoryResponse = client.admin().cluster().prepareDeleteRepository(repositoryName).execute().actionGet(); | |
logger.info(repositoryName + " repository has been deleted."); | |
} | |
} catch (Exception ex){ | |
logger.error("Exception in deleteRepository method: " + ex.toString()); | |
} finally { | |
return deleteRepositoryResponse; | |
} | |
} | |
public boolean isSnapshotExist(Client client, String repositoryName, String snapshotName){ | |
boolean result = false; | |
try { | |
ImmutableList<SnapshotInfo> snapshotInfo = client.admin().cluster().prepareGetSnapshots(repositoryName).get().getSnapshots(); | |
if(snapshotInfo.size() > 0){ | |
for(SnapshotInfo snapshot :snapshotInfo) | |
result = snapshotName.equals(snapshot.name())?true:false; | |
} | |
} catch (Exception ex) { | |
logger.error("Exception in getSnapshot method: " + ex.toString()); | |
} finally { | |
return result; | |
} | |
} | |
public CreateSnapshotResponse createSnapshot(Client client, String repositoryName, | |
String snapshotName, String indexName){ | |
CreateSnapshotResponse createSnapshotResponse = null; | |
try { | |
if(isSnapshotExist(client, repositoryName, snapshotName)) | |
logger.info(snapshotName + " snapshot already exists"); | |
else { | |
createSnapshotResponse = client.admin().cluster() | |
.prepareCreateSnapshot(repositoryName, snapshotName) | |
.setWaitForCompletion(true) | |
.setIndices(indexName).get(); | |
logger.info("Snapshot was created."); | |
} | |
} catch (Exception ex){ | |
logger.error("Exception in createSnapshot method: " + ex.toString()); | |
} finally { | |
return createSnapshotResponse; | |
} | |
} | |
public DeleteSnapshotResponse deleteSnapshot(Client client, String repositoryName, String snapshotName){ | |
DeleteSnapshotResponse deleteSnapshotResponse = null; | |
try { | |
if (isSnapshotExist(client, repositoryName, snapshotName)) { | |
deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot(repositoryName, snapshotName) | |
.execute().actionGet(); | |
logger.info(snapshotName + " snapshot has been deleted."); | |
} | |
} catch (Exception ex){ | |
logger.error("Exception in deleteSnapshot method: " + ex.toString()); | |
} finally { | |
return deleteSnapshotResponse; | |
} | |
} | |
public RestoreSnapshotResponse restoreSnapshot(Client client, String repositoryName, String snapshotName){ | |
RestoreSnapshotResponse restoreSnapshotResponse = null; | |
try { | |
if(isRepositoryExist(client, repositoryName) && isSnapshotExist(client, repositoryName, snapshotName)){ | |
RestoreSnapshotRequest restoreSnapshotRequest = new RestoreSnapshotRequest(repositoryName, snapshotName); | |
restoreSnapshotResponse = client.admin().cluster().restoreSnapshot(restoreSnapshotRequest).get(); | |
logger.info("Snapshot was restored."); | |
} | |
} catch (Exception ex){ | |
logger.error("Exception in restoreSnapshot method: " + ex.toString()); | |
} finally { | |
return restoreSnapshotResponse; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, your article given me a lot of inspiration!
Remind: A little part of code has out of version, needs us manual fixed.
Very good,thanks