In this article, it explained that how take elasticsearch backup via Java.

ElasticSearch keep data on memory as a text. Therefore, it can be easily copy elasticsearch data folder and move. However this is not very good solution because some error may be occur and prefent from correctly work.
ElasticSearch include a backup system. This system restore index data and critical metadata. It is easily restored with this backup. In our example include very brief and basic example of backup.

Actually offical elasticsearch web page give very detailed information in this link. I explain this process with 3 step.

1) Create Reporsitory.

First of all, we have to create a reporsitory to keep restoring data on local server. Firstly we determine where are backup files save? I determine root/backup folder.
If you use sense, correct commands are

 PUT _snapshot/my_backup {“type”: “fs”, “settings”: { “location”: “/root/backup”, “compress”: true }}

In Java

JSONObject cred = new JSONObject();
JSONObject auth = new JSONObject();
JSONObject parent = new JSONObject();
auth.put(“compress”, true);
auth.put(“location”, “/root/backup”);
parent.put(“settings”, auth);
parent.put(“size”, “1000”);
parent.put(“type”, “fs”);
String category = “_snapshot/my_backup”; //or movies/movie/_search

After the first run, we do not need this method, only one time work is enought, now we can take backup.

If you want to check reporsitory you can use “/_snapshot/_all” methods as a  category on Java.

Step 2) Create Snapshot

Snapshut mean take information on this time. On the other words, take backup from elasticsearch.

Sense:  GET http://localhost:9200/_snapshot/my_backup?pretty

Please do not forget, Ubuntu Operating System do not accept UpperCase letter on document name. Unlike Others, Method of Connection must be “PUT”.

 JAVA: String category = “_snapshot/my_backup/snapshot-1?wait_for_completion”;
category = category.toLowerCase();

It can be only one backup in the same name. If name is conflict, old backup is valid.

Stem 3) Restore

Before the restore backup, Indexing must be close. If your elasticsearch include more than one index, you must close all of them.  And also it can be copy backup to elastic search backup reporsitory(Step1).

Close Indexing : Sense:  XPOST localhost:9200/my_index/_close

Sense: POST _snapshot/my_backup/snapshot_14/_restore

JAVA:     String category = “_snapshot/my_backup/backupname/_restore”; //or movies/movie/_search

After’da restore, all indexs automatically open by elasticsearch.There is a problem RestoreMethod in Java.
Normally all elastic process work as OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());     wr.write(parent.toString());   wr.flush();
However in Restore Method there must be only con.getOutputStream(); In other words, There is no parameter send with connection.

Other Articles in This Series.

  1. Security Properties of ElasticSearch – 1 – username password with JAVA
  2. ElasticSearch için Basit bir Java Programı (in Turkish)


My Sense Commands

PUT _snapshot/my_backup  {“type”: “fs”, “settings”: { “location”: “/root/backup”, “compress”: true }}
GET /_snapshot
GET /_snapshot/_all
GET _snapshot/my_backup?pretty { “my_backup” : { “type” : “fs”, “settings” : { “compress” : “true”, “/root/backup” } }}
PUT _snapshot/my_backup/snapshot_14?wait_for_completion=true
POST _snapshot/my_backup/11_sep_2014_03_44_16/_restore
POST /articles/_close
POST /articles/_open