1備份腳本
#!/bin/bash ip_addr=127.0.0.1 resposity_name=es_backup snapshot_location_path=/home/user1/program/backups/es_backup/$resposity_name snapshot_tar_path=/home/user1/program/backups/es_backup/tarPackage snapshot_name=test_snapshot port=9200 # filename=`date +%Y%m%d%H` 增量時使用 tarFileName="$resposity_name"_"$snapshot_name".tar.gz mkdir -p $snapshot_location_path mkdir -p $snapshot_tar_path chmod -R 777 $snapshot_location_path # 1創建快照倉庫 echo -e '\n*******create _snapshot resposity '$resposity_name'\n' curl -X PUT "$ip_addr:$port/_snapshot/$resposity_name" -H 'Content-Type: application/json' -d '{ "type": "fs", "settings": { "location": "'$snapshot_location_path'" } }' # curl -XPUT localhost:9200/_snapshot/es_snapshot -H 'Content-Type: application/json' -d '{"type":"fs","settings":{"location":"'/home/user1/program/backups/es_backup/es_backup'"}}' curl -X GET "$ip_addr:$port/_snapshot/$resposity_name/_current" echo -e '\n*******DELETE _snapshot '$snapshot_name', sleep 5\n' curl -XDELETE "$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name?" sleep 5 #全量備份 curl -X PUT "$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name?wait_for_completion=false" #索引備份 curl -X PUT "$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name?wait_for_completion=false" -H 'Content-Type: application/json' -d '{ "indices": "monitor_quota_20200311", "ignore_unavailable": true, "include_global_state": false }' sleep 10 # 5 查看備份情況 curl -X GET "$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name?pretty" cd $snapshot_location_path echo -e '\n******* tar package '$tarFileName' ,sleep 5\n' tar --warning=no-file-changed -czvf $tarFileName ./* mv $tarFileName $snapshot_tar_path rm ./* -rf echo -e '\nes data backup succeed!\n' echo -e '\n package in '$snapshot_tar_path' '$tarFileName' \n'
參數 wait_for_completion wait_for_completion=true
會一直等待備份結束。wait_for_completion=false
會立即返回,備份在后台進行,可以使用下面的api查看備份的進度:。
備份具體索引 通過設置 include_global_state 為 false,可以阻止集群全局狀態信息被保存為快照的一部分
(1)默認情況下,如果如果一個快照中的一個或者多個索引沒有所有主分片可用,整個快照創建會失敗,該情況可以通過設置 partial 為 true 來改變
(2)快照請求也支持 ignore_unavailable 選項,該選項設置為 true 時,在創建快照時會忽略不存在的索引。默認情況下,如果選項 ignore_unavailable 沒有設值,一個索引缺失,快照請求會失敗。
(3)快照名可以通過使用 date_math_expressions 來自動獲得,和創建新索引時類似。注意特殊字符需要 URI 轉碼處理。
2還原腳本
#!/bin/bash ip_addr=127.0.0.1 resposity_name=es_backup snapshot_name=test_snapshot port=9200 snapshot_location_path=/home/user1/program/backups/es_backup/$resposity_name snapshot_tar_path=/home/user1/program/backups/es_backup/tarPackage # filename=`date +%Y%m%d%H` 增量時使用 tarFileName="$resposity_name"_"$snapshot_name".tar.gz mkdir -p $snapshot_location_path mkdir -p $snapshot_tar_path chmod -R 777 $snapshot_location_path echo -e '\n*******create resposity:'$resposity_name' /n' curl -XPUT $ip_addr:$port/_snapshot/$resposity_name -H 'Content-Type: application/json' -d '{"type":"fs","settings":{"location":"'$snapshot_location_path'"}}' rm $snapshot_location_path/* -rf cp $snapshot_tar_path/$tarFileName $snapshot_location_path cd $snapshot_location_path echo -e '\n*******tar tarFileName: '$tarFileName' sleep5\n' echo ' ********tar -zxvf '$tarFileName'/n' tar -zxvf "$tarFileName" sleep 3 #查看快照 curl -X GET "$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name?pretty" #rm $snapshot_location_path/$tarFileName -rf #關閉索引 curl -XPOST $ip_addr:$port/_all/_close curl -XPOST $ip_addr:$port/monitor_quota_20200311/_close echo -e '\n*******_restore _snapshot sleep 5\n' curl -XPOST $ip_addr:$port/_snapshot/$resposity_name/$snapshot_name/_restore?
## 恢復單個索引: curl -XPOST 'http://$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name/_restore' -d '{"indices": "index_0101","rename_replacement": "index_0101"}'
sleep 5
#打開索引 curl -XPOST localhost:9200/monitor_quota_20200311/_open curl -XPOST $ip_addr:$port/_all/_open
curl -XPOST $ip_addr:$port/monitor_quota_20200311/_open
echo -e '\n es data restore succeed!'
# 查看恢復狀態:
curl -XGET "http://$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name/_status"
# 查看快照恢復進度:
# 查看所有索引的恢復進度 curl -XGET http://192.168.0.101:$port/_recovery/
注意:不同服務器備份和恢復的es版本需要保持一致
參考文檔
https://blog.csdn.net/ailiandeziwei/article/details/87857326
https://blog.51cto.com/svsky/2122521
命令行:
1 創建快照倉庫
#相對地址
curl -X PUT "localhost:9200/_snapshot/my_backup" -H 'Content-Type: application/json' -d'{ "type": "fs", "settings": { "location": "my_backup" } }'
#絕對地址
curl -X PUT "localhost:9200/_snapshot/es_snapshot" -H 'Content-Type: application/json' -d'{ "type": "fs", "settings": { "location": "/home/user1/program/backups/es_backup/snapshot/es_snapshot" } }'
2 查看倉庫信息
2.1查看已注冊的快照倉庫
curl -X GET "localhost:9200/_snapshot/es_backup"
2.2 獲取所有已注冊快照倉庫
curl -X GET "localhost:9200/_snapshot/_all?pretty"
3 創建快照備份
# 3.1 全部備份
curl -X PUT "localhost:9200/_snapshot/es_backup/test_snapshot?wait_for_completion=true"
##3.2 備份部分索引
curl -X PUT "localhost:9200/_snapshot/es_backup/test_snapshot_1?wait_for_completion=true" -H 'Content-Type: application/json' -d '{ "indices": "monitor_quota_20200310", "ignore_unavailable": true, "include_global_state": false }'
4 查看某倉庫下所有快照信息:
curl -X GET "localhost:9200/_snapshot/es_backup/_all?pretty"
curl -X GET "$ip_addr:$port/_snapshot/$resposity_name/$snapshot_name?pretty"
# 查看具體快照信息 curl -X GET "localhost:9200/_snapshot/es_backup/test_snapshot?pretty"
5 查看當前正在運行的快照:
curl -X GET "localhost:9200/_snapshot/my_backup/_current"
刪除快照 ,使用如下命令:curl -X DELETE "localhost:9200/_snapshot/es_backup/test_snapshot"
刪除倉庫,使用下面命令注銷倉庫:curl -X DELETE "localhost:9200/_snapshot/es_backup"
數據恢復
全量恢復 快照可以通過執行以下命令恢復:curl -X POST "node1:9200/_snapshot/es_backup/syslog/_restore"
.創建索引:
curl -XPUT 'localhost:9200/customer?pretty' or curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '{ "name": "John Doe"}'
6 查看索引
用_cat API檢測集群是否健康:curl 'localhost:9200/_cat/health?v'
查看全部:curl -XGET 'localhost:9200/_cat/indices?v'
單個:curl -XGET 'localhost:9200/monitor_quota_20200311/external/1?pretty'
7刪除索引
curl -XDELETE 'localhost:9200/monitor_quota_20200311?pretty'
8 索引的增刪改查有一個類似的格式,總結如下:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST風格的語法謂詞
<Node>:節點ip
<port>:節點端口號,默認9200
<Index>:索引名
<Type>:索引類型
<ID>:操作對象的ID號
9 導入示例數據集 curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
accounts.json: 其中每個數據都是如下格式:
{
"index":{"_id":"1"}
}
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbus Place",
"employer": "Euron",
"email": "bradshawmckenzie@euron.com",
"city": "Hobucken",
"state": "CO"
}
示我們已經成功批量導入數據索引到bank索引中: 查詢 curl 'localhost:9200/bank/_search?q=*&pretty' 其中 q=* 表示匹配索引中所有的數據。
10 查詢語言
匹配所有數據,但只返回1個:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}'
注意:如果siez不指定,則默認返回10條數據。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
返回從11到20的數據。(索引下標從0開始)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}'
上述示例匹配所有的索引中的數據,按照balance字段降序排序,並且返回前10條(如果不指定size,默認最多返回10條)。
11.執行搜索
下面例子展示如何返回兩個字段(account_number balance)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'