快照备份
备份和还原的前提:在配置文件elasticsearch.yml中设置path.repo
path.repo: ["D:\\elasticsearch-6.8.23\\elasticsearch-6.8.23\\snapshot_data"]
创建快照仓库
语法:
PUT http://127.0.0.1:9200/_snapshot/快照仓库名
实例:创建一个名叫my_fs_backup的快照仓库
PUT http://127.0.0.1:9200/_snapshot/my_fs_backup
{
"type": "fs",
"settings": {
"location": "my_fs_backup_location", // 如果使用相对路径,则根据配置文件中的path.repo中的路径下创建文件夹
"compress": true // 是否启用压缩
}
}
查看快照仓库
GET http://127.0.0.1:9200/_snapshot/快照仓库名/
删除快照仓库
DELETE http://127.0.0.1:9200/_snapshot/快照仓库名/
开始快照备份
语法:
PUT http://127.0.0.1:9200/_snapshot/<快照仓库名>/快照备份名
?<wait_for_completion=false|true>&ignore_unavailable=false|true?include_global_state=false|true
# 快照的名称可以用这种方式,来创建名称中包含当前日期的快照,例如 snapshot-2018.05.11 :
# 日志格式详见:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/date-math-index-names.html
# PUT /_snapshot/my_backup/<snapshot-{now/d}>
# URL特殊字符要编码,上面的日志格式链接有详细说明。
PUT /_snapshot/my_backup/%3Csnapshot-%7Bnow%2Fd%7D%3E
wait_for_completion:是否等待快照备份完成,如果你的数据特别多会卡住很久的...
ignore_unavailable: 忽略不可用不存在的索引,默认为false,true则表示如果所有不存在,则会跳过而不是报错。
include_global_state:是否包含集群全局状态信息,默认为true。
实例:在my_fs_backup快照仓库中创建一个名为snapshot-1的快照,只备份completions,articles,news索引
PUT http://127.0.0.1:9200/_snapshot/my_fs_backup/snapshot-1
// 可以指定只备份那些索引
{
"indices": "completions,articles,news"
}
查看快照的备份、还原情况
语法:
GET http://127.0.0.1:9200/_snapshot/<快照仓库名>/<快照名,支持通配符查找多个>/
# 加上_status可以获取更完整的信息
GET http://127.0.0.1:9200/_snapshot/<快照仓库名>/<快照名,支持通配符查找多个>/_status
实例:查看my_fs_backup仓库下的snapshot-1快照的情况
GET http://127.0.0.1:9200/_snapshot/my_fs_backup/snapshot-1/_status
返回:
{
"snapshots": [
{
"snapshot": "snapshot-1", // 快照的名称
"repository": "my_fs_backup", // 快照所在的快照仓库
"uuid": "98Fv_sStR-ilW114aF768g",
"state": "SUCCESS", // 快照的备份状态
"include_global_state": true, // 该快照是否包含集群全局状态信息
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 9,
"failed": 0,
"total": 9
},
"stats": {
"incremental": {
"file_count": 96,
"size_in_bytes": 631455
},
"total": {
"file_count": 96,
"size_in_bytes": 631455
},
"start_time_in_millis": 1688029820217,
"time_in_millis": 86,
"number_of_files": 96,
"processed_files": 96,
"total_size_in_bytes": 631455,
"processed_size_in_bytes": 631455
},
"indices": { // 快照中的每个索引信息
"news": {
"shards_stats": {
"initializing": 0,
"started": 0,
"finalizing": 0,
"done": 3,
"failed": 0,
"total": 3
},
"stats": {...略....}
"shards": { // 所以的分片信息
"0": { // 第1个分片
"stage": "DONE",
"stats": {
"incremental": {
"file_count": 4,
"size_in_bytes": 3759
},
"total": {
"file_count": 4,
"size_in_bytes": 3759
},
"start_time_in_millis": 1688029820289,
"time_in_millis": 14,
"number_of_files": 4,
"processed_files": 4,
"total_size_in_bytes": 3759,
"processed_size_in_bytes": 3759
}
},
"1": { //第2个分片
"stage": "DONE",
"stats": {...略....}
},
"2": {
"stage": "DONE",
"stats": {...略....}
}
}
}
}
}
]
}
查看快照仓库下所有的快照
语法:
GET http://127.0.0.1:9200/_snapshot/<快照仓库名>/_all
实例:
{
"snapshots": [
{
"snapshot": "snapshot-1",
"uuid": "98Fv_sStR-ilW114aF768g",
"version_id": 6082399,
"version": "6.8.23",
"indices": [
"completions",
"articles",
"news"
],
"include_global_state": true,
"state": "SUCCESS",
"start_time": "2023-06-29T09:10:20.185Z",
"start_time_in_millis": 1688029820185,
"end_time": "2023-06-29T09:10:20.310Z",
"end_time_in_millis": 1688029820310,
"duration_in_millis": 125,
"failures": [],
"shards": {
"total": 9,
"failed": 0,
"successful": 9
}
},
{
"snapshot": "snapshot-2023.06.29",
"uuid": "j_TNZ3NvT5yX-cEGogJVuQ",
"version_id": 6082399,
"version": "6.8.23",
"indices": [
"completions",
"articles",
"news"
],
"include_global_state": true,
"state": "SUCCESS",
"start_time": "2023-06-29T09:28:58.952Z",
"start_time_in_millis": 1688030938952,
"end_time": "2023-06-29T09:28:59.033Z",
"end_time_in_millis": 1688030939033,
"duration_in_millis": 81,
"failures": [],
"shards": {
"total": 9,
"failed": 0,
"successful": 9
}
}
]
}
集群环境的下的快照备份
多节点集群如何配置才能实现快照?
第一:建立共享文件系统:如NFS共享,确定每一个节点挂载到指定路径,才能创建快照存储库。
第二:在所有的主节点、数据节点都要配置相同的path.repo。
快照还原
还原步骤
1、设置各个节点(集群环境的每台都要设置)elasticsearch.yml中的path.repo(如果是还原到其他es实例上,这一步要做。)
2、创建快照仓库,和备份快照一样,都需要先创建快照仓库。
3、复制备份的快照文件到快照仓库指定的文件夹下(如果是还原到其他es实例上,这里假设是没有使用ftp等共享方式的快照仓库)
4、执行还原快照的命令
POST /_snapshot/<快照仓库名>/<快照名>/_restore
// 还原的时候可以进行一些设置
{
"indices": "index_1,index_2", // 只还原指定的索引
"ignore_unavailable": true, // 忽略不可用索引
"include_global_state": true, // 包含es集群全局状态信息
// rename_pattern 和rename_replacement用于使用正则表达式重命名索引
"rename_pattern": "index_(.+)",
"rename_replacement": "restored_index_$1"
}
实例:
POST http://127.0.0.1:9201/_snapshot/my_fs_backup/snapshot-1/_restore
停止正在运行的快照和恢复操作
DELETE /_snapshot/<快照仓库名>/<快照名>
其他还原查看命令
查看还原信息
GET http://127.0.0.1:9200/_recovery/
查看正在运行的快照
GET /_snapshot/my_backup/_current
标签:127.0,快照,0.1,my,备份,ElasticSearch,snapshot,backup
From: https://www.cnblogs.com/juelian/p/17559735.html