A-A+

最新elasticsearch 索引备份,迁移、删除及恢复

2018年09月29日 17:25 学习笔记 暂无评论 共4464字 (阅读5,890 views次)

【注意:此文章为博主原创文章!转载需注意,请带原文链接,至少也要是txt格式!】

首先第一步,你需要先停止你的elasticsearch,执行如下命令。

systemctl stop elasticsearch.service

下一步建立相应的备份文件夹即赋予权限。这个文件夹留作备份使用。

mkdir /usr/local/esbackup
#然后是赋予文件夹所有权限
chmod 777 /usr/local/esbackup -R

然后编辑elasticsearch的配置文件,再最下面添加如下代码

path.repo: ["/usr/local/esbackup"]

然后保存 :wq!

然后启动ES,命令:

systemctl start elasticsearch.service

正式第一步,开始创建仓库。可以直接复制下面的命令然后再命令窗口执行。

curl -XPUT 'http://localhost:9200/_snapshot/my_backup' -H 'Content-Type: application/json' -d '{
  "type": "fs",
  "settings": {"compress" : "true",
    "location": "/usr/local/esbackup"
  }
}'

#######下面是执行上面的代码后返回的内容!!!##########
{"acknowledged":true}

然后查看仓库的命令是。(其实如果返回上面的值,基本就是建立成功了。)

curl -XGET 'http://0.0.0.0:9200/_snapshot?pretty'

#######下面是执行上面的代码后返回的内容!!!##########
{
  "my_backup" : {
    "type" : "fs",
    "settings" : {
      "compress" : "true",
      "location" : "/usr/local/esbackup"
    }
  }
}

备份索引,默认咱们就备份所有的吧。

curl -XPUT 'http://0.0.0.0:9200/_snapshot/my_backup/snapshot_1'

###还可以指定索引备份,这里如果你指定索引的备份方式命令如下:########
curl -XPUT 'http://0.0.0.0:9200/_snapshot/my_backup/snapshot_2' -d
{
  "indices": "index_1,index_2"  ##index_1就是你想指定的索引名字,多个索引用英文逗号间隔。
}

#######以上备份成功之后会显示如下结果############
{"accepted":true}

对上面的命令做一下解释吧。一个仓库可以包含多个快照。 每个快照跟一系列索引相关(比如所有索引,一部分索引,或者单个索引)。当创建快照的时候,你指定你感兴趣的索引然后给快照取一个唯一的名字。这个会备份所有打开的索引到 my_backup 仓库下一个命名为 snapshot_1 的快照里。这个调用会立刻返回,然后快照会在后台运行。||||||||||下面可能涉及同步异步备份的问题,其实也简单。通常你会希望你的快照作为后台进程运行,不过有时候你会希望在你的脚本中一直等待到完成。这可以通过添加一个 wait_for_completion 标记实现:

curl -XPUT 'http://0.0.0.0:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true'

如果使用上面的命令,这个会阻塞调用直到快照完成。注意大型快照会花很长时间才返回。

我们查看一下备份数据吧,查看的命令如下:

curl -XGET 'http://0.0.0.0:9200/_snapshot/my_backup/snapshot_1'
curl -XGET 'http://0.0.0.0:9200/_snapshot/my_backup/snapshot_1/_status' ###也可以用此命令查看备份进度呦

#####查看后如下结果##########
{
    "snapshots": [
        {
            "snapshot": "snapshot_1",
            "uuid": "ECoV25yYRBmTwRLn_SgY0g",
            "version_id": 6040199,
            "version": "6.4.1",
            "indices": [          //这里面包含的都是索引名字,这句注释是我自己加的哦。
                "stats_v3",  
                "fields_v2",
                "dstats_v3",
                "queries_v2",
                "users_v5",
                "history_v1-18w38",
                "files_v5",
                "sessions2-180929",
                "sequence_v2",
                "sessions2-180925"
            ],
            "include_global_state": true,
            "state": "SUCCESS",
            "start_time": "2018-09-29T08:29:55.754Z",
            "start_time_in_millis": 1538209795754,
            "end_time": "2018-09-29T08:29:57.882Z",
            "end_time_in_millis": 1538209797882,
            "duration_in_millis": 2128,
            "failures": [],
            "shards": {
                "total": 13,
                "failed": 0,
                "successful": 13
            }
        }
    ]
}

执行完上面的就已经完全的把索引备份到你centos7的/usr/local/esbackup目录下了呦。下一步就是恢复数据了,下面就是恢复备份的索引的命令:
这里也可以加wait_for_completion=true可以改成同步执行。。

####恢复整个快照索引命令:###
curl -XPOST 'http://192.168.1.10:9200/_snapshot/my_backup/snapshot_1/_restore'

####恢复单个快照索引命令:#####
curl -XPOST 'http://192.168.1.10:9200/_snapshot/my_backup/snapshot_1/_restore?wait_for_completion=true' -d
{
  "indices": "index_1,index_2", ##index_1就是你想指定恢复索引名字,多个索引用英文逗号间隔。
  "rename_pattern": "index_(.+)",  ##查找所提供的模式能匹配上的正在恢复的索引。
  "rename_replacement": "restored_index_$1"  ##然后把它们重命名成替代的模式。
}
####上面这个这个会恢复 index_1 到你及群里,但是重命名成了 restored_index_1 。
####这里还有两个隐藏参数
####ignore_unavailable设置为true,如果某些索引不可用,恢复过程还是会成功的。
####include_global_state设置为true,存储了集群的state,还会同时恢复一些template。


#####恢复后的显示结果如下#########
{"accepted":true}

最最后就是查看恢复状态:

curl -XGET "http://0.0.0.0:9200/_snapshot/my_backup/snapshot_1/_status"

都备份完了,那么可以删除没用的备份了,删除命令是:

curl -XDELETE 'http://0.0.0.0:9200/_snapshot/my_backup/snapshot_1'

资料来源参考官方:
https://www.elastic.co/guide/cn/elasticsearch/guide/current/backing-up-your-cluster.html
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_restoring_from_a_snapshot.html

网友遇到的问题整理合集:

问题一:::
如果你的是集群而且在创建仓库的时候没有配置共享文件夹那会报下面的错误

{
    "error": "RepositoryException[[my_backup] failed to create repository]; nested: CreationException[Guice creation errors:\n\n1) Error injecting constructor, org.elasticsearch.repositories.RepositoryException: [my_backup] location [/mnt/bak] doesn't match any of the locations specified by path.repo because this setting is empty\n  at org.elasticsearch.repositories.fs.FsRepository.(Unknown Source)\n  while locating org.elasticsearch.repositories.fs.FsRepository\n  while locating org.elasticsearch.repositories.Repository\n\n1 error]; nested: RepositoryException[[my_backup] location [/mnt/bak] doesn't match any of the locations specified by path.repo because this setting is empty]; ",
    "status": 500
}

解决方法::关闭支点服务器elasticsearch服务重新执行即可成功。
如果已经存在.kibana索引可以先关闭掉。
成功恢复数据后只有主节点服务器存在.kibana索引。我们希望所有的节点服务器都存在此索引时执行下面的命令

curl -XPUT 'http://0.0.0.0:9200/.kibanna/_settings' -d '
{
  "index" : {
    "number_of_replicas" : 1
  }
}'

问题二:::
恢复数据索引时报错,报错如下:

reason": "[my_backup:snapshot_entity/hxNnq8CsRAm3Yi8IvD0ZeA] cannot restore index [index_entity] because it's open"

解决方法::索引在被回复时需要先关闭,否则索引的写操作会影响恢复,于是关闭索引

curl -XPOST "http://0.0.0.0:9200/index_entity/_close"

再执行恢复命令,恢复好后记得打开索引。

curl -XPOST "http://0.0.0.0:9200/index_entity/_open"

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×
标签:

给我留言