Create Snapshot API

edit

Use the Create Snapshot API to create a new snapshot.

Create Snapshot Request

edit

A CreateSnapshotRequest:

CreateSnapshotRequest request = new CreateSnapshotRequest();

Required Arguments

edit

The following arguments are mandatory:

request.repository(repositoryName); 

The name of the repository.

request.snapshot(snapshotName); 

The name of the snapshot.

Optional Arguments

edit

The following arguments are optional:

request.indices("test-index0", "test-index1"); 

A list of indices the snapshot is applied to.

request.indicesOptions(IndicesOptions.fromOptions(false, false, true, true)); 

Options applied to the indices.

request.partial(false); 

Set partial to true to allow a successful snapshot without the availability of all the indices primary shards. Defaults to false.

request.includeGlobalState(true); 

Set includeGlobalState to false to prevent writing the cluster’s global state as part of the snapshot. Defaults to true.

request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); 
request.masterNodeTimeout("1m"); 

Timeout to connect to the master node as a TimeValue.

Timeout to connect to the master node as a String.

request.waitForCompletion(true); 

Waits for the snapshot to be completed before a response is returned.

Synchronous Execution

edit

Execute a CreateSnapshotRequest synchronously to receive a CreateSnapshotResponse.

CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT);

Retrieve the SnapshotInfo from a CreateSnapshotResponse when the snapshot is fully created. (The waitForCompletion parameter is true).

SnapshotInfo snapshotInfo = response.getSnapshotInfo(); 

The SnapshotInfo object.

Asynchronous Execution

edit

The asynchronous execution of a create snapshot request requires both the CreateSnapshotRequest instance and an ActionListener instance to be passed as arguments to the asynchronous method:

client.snapshot().createAsync(request, RequestOptions.DEFAULT, listener); 

The CreateSnapshotRequest to execute and the ActionListener to use when the execution completes.

The asynchronous method does not block and returns immediately. Once it is completed the ActionListener is called back with the onResponse method if the execution is successful or the onFailure method if the execution failed.

A typical listener for CreateSnapshotResponse looks like:

ActionListener<CreateSnapshotResponse> listener =
    new ActionListener<CreateSnapshotResponse>() {
        @Override
        public void onResponse(CreateSnapshotResponse createSnapshotResponse) {
            
        }

        @Override
        public void onFailure(Exception exception) {
            
        }
    };

Called when the execution is successfully completed. The response is provided as an argument.

Called in case of a failure. The raised exception is provided as an argument.

Snapshot Create Response

edit

Use the CreateSnapshotResponse to retrieve information about the evaluated request:

RestStatus status = response.status(); 

Indicates the node has started the request.