一.ES相關操作
1.ES啓動與關閉相關命令
su elastic # elastic用戶爲安裝ES時創建的用戶,es不能用root賬號啓動
cd /usr/local/elasticsearch-6.4.3/ # cd 進入es安裝目錄
./bin/elasticsearch -d -p pid # -p 選項可以將es進程PID記錄到pid文件中,該文件可以指定 -d 後臺運行
####
kill -SIGTERM PID # PID 爲ES進程的PID號
2.查看ES相關信息
]$ curl -XGET http://localhost:9200/
{
"name" : "SonoxO1",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "Oktq2zxmSRWcU8Fvhat2TA",
"version" : {
"number" : "6.4.3",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "fe40335",
"build_date" : "2018-10-30T23:17:19.084789Z",
"build_snapshot" : false,
"lucene_version" : "7.4.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
二.索引的相關操作
1.查看索引
]# curl localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open logstash-nq-coupon-2018.11.14 V2ilbJErQZGW2m9rBLXeEw 5 1 182396 0 24.9mb 24.9mb
green open .kibana llTJB8gQTS-r6oGjF8fTdg 1 0 3 0 17.6kb 17.6kb
2.刪除索引
]# curl -XDELETE http://localhost:9200/logstash-2018.11.14
{"acknowledged":true}
三.備份與恢復
1.創建備份倉庫
}# mkdir /data/elasticsearch/bakup # 創建備份目錄
}# chown elastic.elastic /data/elasticsearch/bakup # 更改目錄權限
# 在elasticsearch.yml配置文件中新增一行,並重新啓動ES
path.repo: /data/elasticsearch/bakup
#創建倉庫
}# curl -H 'Content-Type: application/json' -XPUT http://localhost:9200/_snapshot/es_bakup \ # es_bakup 可自定義
-d '{"type":"fs","settings":{"location":"/data/elasticsearch/bakup","compress": "true"}}'
{"acknowledged":true}
注:在6.X的版本中需要添加 -H 'Content-Type: application/json' 設置header,原因可參考:https://www.elastic.co/blog/strict-content-type-checking-for-elasticsearch-rest-requests
2.查看備份倉庫
]# curl -XGET http://127.0.0.1:9200/_snapshot?pretty
{
"es_bakup" : {
"type" : "fs",
"settings" : {
"compress" : "true",
"location" : "/data/elasticsearch/bakup"
}
}
}
3.備份索引
# 備份單個索引
]# curl -H'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/log_bak_2018.11.14 \ # log_bak_2018.11.14 快照名稱,可自定義
-d '{"indices":"logstash-2018.11.14"}'
# 備份多個索引
]# curl -H 'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/log_bak_2018.11.14 \
-d '{"indices": "idx_1,idx_2,idx_3"}'
# 備份所有索引
]# curl -H 'Content-Type: application/json' -XPUT http://127.0.0.1:9200/_snapshot/es_bakup/bakup_all_2018.11.16
4.查看備份的索引
]# curl -XGET http://127.0.0.1:9200/_snapshot/es_bakup/log_bak_2018.11.14?pretty
{
"snapshots" : [
{
"snapshot" : "log_bak_2018.11.14",
"uuid" : "wrDpuL8aQHe7kNn5l11WqA",
"version_id" : 6040399,
"version" : "6.4.3",
"indices" : [
"logstash-2018.11.14"
],
"include_global_state" : true,
"state" : "SUCCESS",
"start_time" : "2018-11-16T07:22:49.001Z",
"start_time_in_millis" : 1542352969001,
"end_time" : "2018-11-16T07:22:49.914Z",
"end_time_in_millis" : 1542352969914,
"duration_in_millis" : 913,
"failures" : [ ],
"shards" : {
"total" : 5,
"failed" : 0,
"successful" : 5
}
}
]
}
5.恢復索引
# 恢復單個索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/_snapshot/es_bakup/xp-coupon-2018.11.14/_restore \
-d '{"indices":"logstash-2018.11.14"}'
# 恢復所有索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/_snapshot/es_bakup/bakup_all_2018.11.16/_restore
# 恢復所有索引備份中的部分索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/_snapshot/es_bakup/bakup_all_2018.11.16/_restore \
-d '{"indices":"idx_1,idx_2"}'
6.其他
(1)備份目錄中的文件可以打包壓縮,恢復時解壓至備份目錄即可
(2)恢復索引時,如果該索引存在則需要先關閉該索引,恢復完成之後重新開啓該索引
# 關閉某個索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/logstash-2018.11.16/_close
# 打開某個索引
]# curl -H 'Content-Type: application/json' -XPOST http://127.0.0.1:9200/logstash-2018.11.16/_open