当es集群的数据量较小的情况下elasticdump这个工具比较方便,但是当数据量达到一定级别比如上百G的时候,elasticdump速度就很慢了,此时我们可以使用快照的方法进行备份
elasticdump工具的使用可以参考 博文:elasticsearch5.0.1集群一次误删除kibana索引引发的血案 http://blog.csdn.net/reblue520/article/details/61925375
个人感觉这两个工具各有优缺点,和mysql的备份工具比起来:
elasticdump --> mysqldump# 都是逻辑备份需要将数据一条一条导出再导入,适合数据量小的场景
snapshot --> percona-xtrabackup # 物理备份,速度取决于磁盘IO能力,适合大数据量场景
一、使用snapshot恢复.kibana索引数据
1.修改配置文件:
vim elasticsearch.yml
path.repo: ["/data/bak"]
2.创建仓库
curl -XPUT http://10.25.177.47:9200/_snapshot/my_backup -d '
{
"type": "fs",
"settings": {
"location": "/data/bak",
"compress": true
}
}
'
# 返回true表示成功
{"acknowledged":true}
查看仓库
# curl -XGET 'http://10.25.177.47:9200/_snapshot/my_backup?pretty'
{
"my_backup" : {
"type" : "fs",
"settings" : {
"compress" : "true",
"location" : "/data/bak"
}
}
}
3、备份数据
curl -XPUT http://10.25.177.47:9200/_snapshot/my_backup/snapshot_20161207
执行上面的命令会快照ealsticsearch上所有的索引。
如果需要快照指定的索引
curl -XPUT http://10.25.177.47:9200/_snapshot/my_backup/snapshot_20170316 -d '
{
"indices": ".kibana"
}
'
{"accepted":true}
如果成功返回结果显示{"accepted":true}
4、查看备份
# curl -XGET http://10.25.177.47:9200/_snapshot/my_backup/snapshot_20170316?pretty
{
"snapshots" : [
{
"snapshot" : "snapshot_20170316",
"uuid" : "DhdKZkTaQ4G-eLAjJ68zGQ",
"version_id" : 5000199,
"version" : "5.0.1",
"indices" : [
".kibana"
],
"state" : "SUCCESS",
"start_time" : "2017-03-16T09:56:02.075Z",
"start_time_in_millis" : 1489658162075,
"end_time" : "2017-03-16T09:56:02.154Z",
"end_time_in_millis" : 1489658162154,
"duration_in_millis" : 79,
"failures" : [ ],
"shards" : {
"total" : 1,
"failed" : 0,
"successful" : 1
}
}
]
}
也可以使用下面的命令查看快照的状态
# curl -XGET http://10.25.177.47:9200/_snapshot/my_backup/snapshot_20170316/_status?pretty
{
"snapshots" : [
{
"snapshot" : "snapshot_20170316",
"repository" : "my_backup",
"uuid" : "DhdKZkTaQ4G-eLAjJ68zGQ",
"state" : "SUCCESS",
"shards_stats" : {
"initializing" : 0,
"started" : 0,
"finalizing" : 0,
"done" : 1,
"failed" : 0,
"total" : 1
},
"stats" : {
"number_of_files" : 4,
"processed_files" : 4,
"total_size_in_bytes" : 48362,
"processed_size_in_bytes" : 48362,
"start_time_in_millis" : 1489658162100,
"time_in_millis" : 41
},
"indices" : {
".kibana" : {
"shards_stats" : {
"initializing" : 0,
"started" : 0,
"finalizing" : 0,
"done" : 1,
"failed" : 0,
"total" : 1
},
"stats" : {
"number_of_files" : 4,
"processed_files" : 4,
"total_size_in_bytes" : 48362,
"processed_size_in_bytes" : 48362,
"start_time_in_millis" : 1489658162100,
"time_in_millis" : 41
},
"shards" : {
"0" : {
"stage" : "DONE",
"stats" : {
"number_of_files" : 4,
"processed_files" : 4,
"total_size_in_bytes" : 48362,
"processed_size_in_bytes" : 48362,
"start_time_in_millis" : 1489658162100,
"time_in_millis" : 41
}
}
}
}
}
}
]
}
5、删除备份
curl -XDELETE http://10.25.177.47:9200/_snapshot/my_backup/snapshot_20170316
{"acknowledged":true}
6、恢复备份测试
①删除.kibana索引,可以看到报表已经没有了
curl -XDELETE http://10.25.177.47:9200/.kibana
{"acknowledged":true}
②关闭.kibana索引
curl -XPOST http://10.25.177.47:9200/.kibana/_close
{"acknowledged":true}
③使用快照恢复
curl -XPOST http://10.25.177.47:9200/_snapshot/my_backup/snapshot_20170316/_restore
{"accepted":true}
curl '10.25.177.47:9200/_cat/indices?v'
可以看到成功恢复
二、生产环境实战(当老集群磁盘空间不足的情况)
1)旧的集群备份出来的数据,要拷贝到新集群中。
解决两个问题:
1.旧集群没有足够的空间存储这些备份出来的数据
2.备份出来都需要拷贝到新的集群中,此处想到一个方法,通过nfs或者sshfs将新集群机器的目录远程挂载(mount)到旧集群机器上。
2)挂载目录,2.1)和2.2)可以任选一种方式
2.1)使用sshfs进行挂载:
// 在每台机器上安装sshfs
yum install fuse sshfs -y
// 每台机器上创建Mount共享目录
mkdir /data/backup_es
// 旧集群的每台机器上挂载共享目录(分别挂载了新机器的/opt/data07目录到/opt/backup_es)
sshfs [email protected]:/opt/data07 /opt/backup_es -o allow_other
// 测试运行ES的用户是否有对共享目录的写权限
sudo -u elastic touch /opt/backup_es
// 在旧机器上将共享目录的权限付给ES的运行用户
chown elastic:elastic -R /opt/backup_es
2.2)使用Mount nfs进行挂载(推荐):
// 在新集群的机器上(10.25.177.47)添加共享的文件夹和客户端可以访问的IP地址
创建目录:
mkdir /data/voice
vim /etc/exports
/data/voice *(insecure,rw,no_root_squash,sync,anonuid=500,anongid=500)
// 查看共享文件夹
yum install -y exportfs
exportfs -rv
// 重启启动新集群机器的NFS服务
systemctl restart nfs
// 旧集群的每台机器上创建共享目录
mkdir /data/backup_es
// 旧集群每一台服务器上进行Mount挂载
mount -t nfs 10.25.177.47:/data/voice /data/backup_es -o proto=tcp -o nolock
列出nfs服务端共享的目录:
# showmount -e 10.25.177.47
Export list for 10.25.177.47:
/data/voice *
报错:
mount: wrong fs type, bad option, bad superblock on 10.25.177.47:/opt/data07,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.
In some cases useful info is found in syslog - try
dmesg | tail or so.
解决:
yum install -y nfs-utils
报错:
mount.nfs: access denied by server while mounting 10.25.177.47:/data/voice
解决:
nfs服务端
vim /etc/sysconfig/nfs
修改如下:
RPCNFSDARGS="-N 2 -N 3"
----->启用
# Turn off v4 protocol support
RPCNFSDARGS="-N 4" ---->启用
重启生效
systemctl restart nfs
// 在旧机器上将共享目录的权限付给ES的运行用户
chown yunva:yunva -R /data/backup_es
3)创建ES仓库
3.1创建ES仓库my_backup
修改配置文件:
vim elasticsearch.yml
# 添加如下配置(需要在旧集群的每个节点上添加):
path.repo: ["/data/backup_es"]
创建快照仓库命令:
curl -XPUT http://10.26.241.237:9200/_snapshot/my_backup -d '
{
"type": "fs",
"settings": {
"location": "/data/backup_es",
"compress": true
}
}
'
{"acknowledged":true}
查看仓库
# curl -XGET 'http://10.26.241.237:9200/_snapshot/my_backup?pretty'
{
"my_backup" : {
"type" : "fs",
"settings" : {
"compress" : "true",
"location" : "/data/backup_es"
}
}
}
4)创建快照备份
4.1创建所有索引的快照
curl -XPUT http://10.26.241.237:9200/_snapshot/my_backup/snapshot_voice20170316
执行上面的命令会快照ealsticsearch上所有的索引。
4.2创建指定索引的快照
curl -XPUT http://10.26.241.237:9200/_snapshot/my_backup/snapshot_voice20170316 -d '
{
"indices": "voice:mic:lose"
}
'
如果成功返回结果显示{"accepted":true}
4、查看备份
# curl -XGET http://10.26.241.237:9200/_snapshot/my_backup/snapshot_voice20170316?pretty
{
"snapshots" : [
{
"snapshot" : "snapshot_voice20170316",
"uuid" : "QBxu1worT3KDMf9GdG8svA",
"version_id" : 5000199,
"version" : "5.0.1",
"indices" : [
"voice:mic:lose"
],
"state" : "SUCCESS",
"start_time" : "2017-03-16T12:07:42.846Z",
"start_time_in_millis" : 1489666062846,
"end_time" : "2017-03-16T12:08:03.831Z",
"end_time_in_millis" : 1489666083831,
"duration_in_millis" : 20985,
"failures" : [ ],
"shards" : {
"total" : 5,
"failed" : 0,
"successful" : 5
}
}
]
}
也可以使用下面的命令查看快照的状态
# curl -XGET http://10.25.177.47:9200/_snapshot/my_backup/snapshot_voice20170316/_status?pretty
可以看到ES会同时进行几个分片的备份,而且显示备份的数据情况。
通过观察可以看到,旧集群上共享的两个目录/data/backup_es会均分备份出来的数据
{
"snapshots" : [
{
"snapshot" : "snapshot_voice20170316",
"repository" : "my_backup",
"uuid" : "42WjLbCLQwadjBeMTK0p2w",
"state" : "SUCCESS",
"shards_stats" : {
"initializing" : 0,
"started" : 0,
"finalizing" : 0,
"done" : 1,
"failed" : 0,
"total" : 1
},
"stats" : {
"number_of_files" : 107,
"processed_files" : 107,
"total_size_in_bytes" : 943093922,
"processed_size_in_bytes" : 943093922,
"start_time_in_millis" : 1489665958527,
"time_in_millis" : 52189
},
"indices" : {
"voice:mic:lose" : {
"shards_stats" : {
"initializing" : 0,
"started" : 0,
"finalizing" : 0,
"done" : 1,
"failed" : 0,
"total" : 1
},
"stats" : {
"number_of_files" : 107,
"processed_files" : 107,
"total_size_in_bytes" : 943093922,
"processed_size_in_bytes" : 943093922,
"start_time_in_millis" : 1489665958527,
"time_in_millis" : 52189
},
"shards" : {
"0" : {
"stage" : "DONE",
"stats" : {
"number_of_files" : 107,
"processed_files" : 107,
"total_size_in_bytes" : 943093922,
"processed_size_in_bytes" : 943093922,
"start_time_in_millis" : 1489665958527,
"time_in_millis" : 52189
}
}
}
}
}
}
]
}
6)耐心等待直到所有的的备份都完成。
备份完成后,查看旧集群每台机器的/data/backup_es目录,查看备份出的数据。
在所有旧集群服务器上取消挂载 umount /data/backup_es
7)在新集群中恢复
// 在新集群每台机器上将共享目录的权限付给ES的运行用户
chown yunva:yunva -R /data/voice
// 停止ES,在新集群的每台服务器上都添加设置elasticsearch.yml的参数
path.repo: ["/data/voice"]
8)恢复备份
①删除voice:mic:lose索引,可以看到节点已经没有了
curl -XDELETE http://10.25.177.47:9200/voice:mic:lose
{"acknowledged":true}
# curl '10.25.177.47:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .kibana OzGTpr9TSN6WGP2ynXBmaQ 1 1 40 0 47.2kb 47.2kb
# curl '10.25.177.47:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open voice:mic:lose 5vyq-0VYSnqfV9fN9dTNmQ 1 1 3070979 0 899.4mb 899.4mb
yellow open .kibana OzGTpr9TSN6WGP2ynXBmaQ 1 1 40 0 47.2kb 47.2kb
// 启动ES,在新集群创建仓库
curl -XPUT http://10.25.177.47:9200/_snapshot/my_backup -d '
{
"type": "fs",
"settings": {
"location": "/data/voice",
"compress": true
}
}
'
8)在新集群中恢复数据
// 使用RESTful API进行备份的恢复
使用快照恢复
curl -XPOST http://10.25.177.47:9200/_snapshot/my_backup/snapshot_voice20170316/_restore
{"accepted":true}
// 查看恢复的状态
curl -XGET http://10.25.177.47:9200/_snapshot/my_backup/snapshot_voice20170316/_status
curl '10.25.177.47:9200/_cat/indices?v'
9)耐心等待,直至恢复完成。