#!/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 转码处理。
#!/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:// i p a d d r : ip_addr: ipaddr:port/_snapshot/ r e s p o s i t y n a m e / resposity_name/ resposityname/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
https://www.cnblogs.com/personsiglewine/p/12510845.html
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 “ i p a d d r : ip_addr: ipaddr:port/_snapshot/ r e s p o s i t y n a m e / resposity_name/ resposityname/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风格的语法谓词
:节点ip
:节点端口号,默认9200
:索引名
:索引类型
:操作对象的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”: “[email protected]”,
“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”]
}’