What is it

Elastic Shell is an open source project that includes a set of command line utilities and run as a client to help you manage Elasticsearch. It is entirely written in Bash Shell.

“Elastic Shell 101” is a series of posts that tells you how to use it.

In this post, I will use snapshot command as example to show you how to run Elastic Shell in interactive mode using plain text UI.

Run interactively

Usually, we run Elastic Shell as a command line tool. It does not require user input when the command is running. However, Elastic Shell can also be run in interactive mode which provides context menu that allows user input to drive the way it goes. It is useful when you want to demonstrate, test, or investigate something with your Elasticsearch cluster. It clearly gives people the execution path.

To run Elastic Shell in interactive mode, you just need to add an option called --ui-text. Let’s choose snapshot command as an example.

##################################################
#
# Main Menu
#
##################################################
# Select a function:
1) cat
2) repo
3) create
4) list
5) restore
6) config
7) return
# Select a function:

You will see a welcome message followed by the main menu. Each menu item maps to the sub-command that is also available when run Elastic Shell in non-interactive mode. Input the number in front of each menu item to select the corresponding menu item. For example, if we input 1, it will take us to the sub-menu of command cat, which includes all Elasticsearch /_cat APIs supported by the Elasticsearch cluster. Select the last menu item on the menu at any level will go back to the menu at upper level. If it’s the main menu, it will exit from Elastic Shell.

Create repository

To backup or restore snapshots, let’s create a snapshot repository at first. From main menu, select 2, then 1. Elastic Shell will ask for a repository name, you can input a name or use the default one by pressing the Enter key.

##################################################
#
# Main Menu
#
##################################################
# Select a function:
1) cat
2) repo
3) create
4) list
5) restore
6) config
7) return
# Select a function:2

##################################################
#
# Repo
#
##################################################
# Select a function:
1) new
2) return
# Select a function:1

##################################################
#
# Repository Name
#
##################################################
# Input repository name(press Enter to use 'my_backup'):

##################################################
#
# Create Repository my_backup
#
##################################################
{
  "acknowledged": true
}

The returned response tells us it’s created successfully.

Here, the default repository name is actually defined in the main.properties file. This can also be verified by selecting config on main menu to run config command. It lists all the configurations that are available based on the context.

##################################################
#
# Main Menu
#
##################################################
# Select a function:
1) cat
2) repo
3) create
4) list
5) restore
6) config
7) return
# Select a function:6

##################################################
#
# Config
#
##################################################
# Available configuration:
1) net_host=http://elasticsearch:9200
2) net_use_ssl=false
3) net_ssl_no_validate=true
4) net_client_cert=./certs/admin.crt.pem
5) net_client_key=./certs/admin.key.pem
6) net_certificate=
7) snapshot_repository=my_backup
8) return
# Choose one item:

If you want to change it, input 7, then input the new value.

Another thing to note is the configuration file of the snapshot repository. It is called repo.json and located at snapshot folder under Elastic Shell config folder.

bash-4.4# cat elash/config/snapshot/repo.json 
{
  "type": "fs",
  "settings": {
    "location": "my_backup"
  }
}

Of course, you can modify it as you wish.

List snapshots

To list all snapshots that are available to the specified repository, let’s select list on main menu, then input the repository name. Here, we use the default name, my_backup, by pressing Enter key. You will see all snapshots that’ve been created previously in this repository.

##################################################
#
# List Snapshots
#
##################################################
{
  "snapshots": [
    {
      "snapshot": "github",
      "uuid": "KtoEO1IPSYiF5mBuUjF9dw",
      "version_id": 6030299,
      "version": "6.3.2",
      "indices": [
        "github"
      ],
      "include_global_state": true,
      "state": "SUCCESS",
      "start_time": "2019-01-18T13:22:01.491Z",
      "start_time_in_millis": 1547817721491,
      "end_time": "2019-01-18T13:22:01.996Z",
      "end_time_in_millis": 1547817721996,
      "duration_in_millis": 505,
      "failures": [],
      "shards": {
        "total": 5,
        "failed": 0,
        "successful": 5
      }
    }
 ]
}

Restore from snapshot

Let’s choose github snapshot, and restore from it. Select restore on main menu, press Enter key to use the default repository name, my_backup. Input the snapshot name, github.

##################################################
#
# Main Menu
#
##################################################
# Select a function:
1) cat
2) repo
3) create
4) list
5) restore
6) config
7) return
# Select a function:5

##################################################
#
# Repository Name
#
##################################################
# Input repository name(press Enter to use 'my_backup'):

##################################################
#
# Snapshot Name
#
##################################################
# Input snapshot name:github

##################################################
#
# Restore Snapshot github
#
##################################################
{
  "accepted": true
}

Then, run the cat indices command. You will see the github index and its documents are restored from the snapshot.

For more stories on Elastic Shell, stay tuned for next posts! Any question about this post or Elastic Shell, feel free to leave comments or drop email at morningspace@yahoo.com.

留下评论

您的电子邮箱地址并不会被展示。请填写标记为必须的字段。 *

正在加载...