一、Repositories
在elasticsearch.yml文件中增加path.repo路径配置:
$ vim /etc/elasticsearch/elasticsearch.yml
path.repo: ["/home/ccx/es/backups", "/mount/longterm_backups"]
重启elasticsearch
$ service elasticsearch restart
创建文件夹作为repository的目录,并修改文件夹权限
$ mkdir -p /home/ccx/es/backups
$ chmod 755 /home/ccx/es/backups/
$ chown elasticsearch:elasticsearch /home/ccx/es/backups/
创建repository
$ curl -XPUT 'http://192.168.212.190:9200/_snapshot/my_backup' -d '{
"type": "fs",
"settings": {
"location": "/home/ccx/es/backups/my_backup",
"compress": true
}
}'
查看repository信息
$ curl -XGET 'http://192.168.212.190:9200/_snapshot/my_backup?pretty'
可以使用POST 请求,用来修改已经存在的repository
$ curl -XPOST 'http://192.168.212.190:9200/_snapshot/my_backup' -d '{
"type": "fs",
"settings": {
"location": "/home/ccx/es/backups/my_backup",
"max_snapshot_bytes_per_sec" : "50mb",
"max_restore_bytes_per_sec" : "50mb"
}
}'
二、Snapshot
创建快照
备份工作在后台运行
$ curl -XPUT 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_1'
同步执行,加wait_for_completion 标志,备份完成后才返回,如果数据量大的话,会花很长时间
$ curl -XPUT 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_2?wait_for_completion=true'
如果只想备份部分索引的话,可以加上indices 参数:
$ curl -XPUT 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_3' -d '{
"indices": "index_1,index_2",
"ignore_indices": "missing"
}'
查看备份信息
$ curl -XGET 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_2'
如果要查看所有索引的信息,使用如下api:
$ curl -XGET 'http://192.168.212.190:9200/_snapshot/my_backup/_all'
另外还有个一api可以看到更加详细的信息:
$ curl -XGET 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_2/_status'
删除备份
$ curl -XDELETE 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_2'
三、Restore
恢复snapshot_1里的全部索引:
$ curl -XPOST 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_1/_restore'
api额外的参数:
$ curl -XPOST 'http://192.168.212.190:9200/_snapshot/my_backup/snapshot_1/_restore' -d '{
"indices": "index_1",
"rename_pattern": "index_(.+)",
"rename_replacement": "restored_index_$1"
}'
参数indices 设置只恢复index_1索引,参数rename_pattern 和rename_replacement 用来正则匹配要恢复的索引,并且重命名。和备份一样,api会立刻返回值,然后在后台执行恢复,使用wait_for_completion 标记强制同步执行。
以使用下面两个api查看状态
$ curl -XGET 'http://192.168.212.190:9200/_recovery/'