ELASTIC的备份与恢复
前言
elasticsearch官方并没有提供合适的备份工具,然而生产场景中备份却是的确需要的。
本文介绍了使用自己写的php脚本以及第三方工具来进行索引的备份,恢复以及删除等操作。
全量备份
恢复
删除索引
<?php
date_default_timezone_set('Asia/Shanghai'); $longopts = array(
'index:',
'query:',
'start:',
'end:',
);
$options = getopt('a', $longopts); if(!isset($options['index'])) {
fwrite(STDERR, "index必须设置索引地址\n");
exit(1);
} $components = parse_url($options['index']);
if(!isset($components['path'])) {
fwrite(STDERR, 'index不可为空');
exit(1);
} $host = "{$components['scheme']}://{$components['host']}:{$components['port']}";
$index = basename($components['path']); $query = isset($options['query']) ? $options['query'] : '{"query":{"match_all":{}}}';
if(isset($options['start'])) {
$start_time = strtotime($options['start']);
$start = date('Y-m-d', $start_time).'T00:00:00+0800';
if(isset($options['end'])) {
$end_time = strtotime($options['end']);
$end = date('Y-m-d', $end_time).'T00:00:00+0800';
} else {
$end = date('Y-m-d', $start_time+86400).'T00:00:00+0800';
} $field = strpos($index, 'analysis')!==false ? 'create_time' : 'date';
$query = '{"size":1000,"_source":false,"query":{"filtered":{"filter":{"range":{"'.$field.'":{"gte":"'.$start.'","lt":"'.$end.'"}}}}}}';
} $scroll_id = null;
$retry = 0;
$num = 0;
while(true) {
if(is_null($scroll_id)) {
$result = post("{$host}/{$index}/_search?scroll=2m", $query);
} else {
$result = get("{$host}/_search/scroll?scroll=2m&scroll_id={$scroll_id}");
} $json = json_decode($result, true);
if(!isset($json['_scroll_id'])) {
fwrite(STDERR, "查询失败:索引-{$index} 起始-{$start} 截止-{$end}\n");
sleep(5);
$retry++;
if($retry>10) {
exit(4);
}
} $scroll_id = $json['_scroll_id'];
$bulk = [];
foreach($json['hits']['hits'] as $row) {
unset($row['_score']);
$bulk[] = json_encode(['delete'=>$row]);
$num++;
} if(count($json['hits']['hits'])==0)
{
break;
} $check = post("{$host}/_bulk", implode("\n", $bulk)."\n");
fwrite(STDOUT, "{$num}\n");
usleep(100000);
} echo "deleted:{$num}\n"; function get($url)
{
$handle = curl_init();
//curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_URL, $url);
//curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
return curl_exec($handle);
} function post($url, $data)
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
return curl_exec($handle);
}
索引增量备份
#!/bin/bash #如果有参数,则第一个参数为备份日期,否则默认备份昨天数据
if [ -z "$1" ];then
start=$(date +%Y-%m-%d --date '-1 day 00:00:00')
end=$(date +%Y-%m-%d --date 'today 00:00:00')
else
start=$(date +%Y-%m-%d --date $)
end=$(date +%Y-%m-%d --date "$1 +1 day 00:00:00")
fi #如果是每月1号则加一天,解决时区导致的数据跨索引问题
if [ $(date +%d --date $end) -eq "" ]
then
end=$(date +%Y-%m-%d --date "$end +1 day 00:00:00")
fi php esdump.php --input=http://127.0.0.1:9200/logstash-event-$(date +%Y.%m --date $start) --start $start --end $end 2>> backup_error_$start.log | gzip > event/logstash-event-$(date +%Y.%m_%d --date $start).json.gz 2>> backup_error_$start.log &
<?php
date_default_timezone_set('Asia/Shanghai'); $longopts = array(
'input:',
'output:',
'query:',
'start:',
'end:',
);
$options = getopt('a', $longopts); if(!isset($options['input'])) {
fwrite(STDERR, "input必须设置索引地址\n");
exit(1);
} $check = get($options['input'].'/_count');
if($check===false) {
fwrite(STDERR, "input索引地址无效:{$options['input']}\n");
exit(2);
} $check = json_decode($check, true);
if(!isset($check['count'])) {
fwrite(STDERR, "input索引地址无效:{$options['input']}\n");
exit(3);
} $components = parse_url($options['input']);
$host = "{$components['scheme']}://{$components['host']}:{$components['port']}";
$index = basename($components['path']); $query = isset($options['query']) ? $options['query'] : '{"query":{"match_all":{}}}';
if(isset($options['start'])) {
$start_time = strtotime($options['start']);
$start = date('Y-m-d', $start_time).'T00:00:00+0800';
if(isset($options['end'])) {
$end_time = strtotime($options['end']);
$end = date('Y-m-d', $end_time).'T00:00:00+0800';
} else {
$end = date('Y-m-d', $start_time+86400).'T00:00:00+0800';
} $field = strpos($index, 'analysis')!==false ? 'create_time' : 'date';
$query = '{"size":1000,"sort":{"'.$field.'":{"order":"asc"}},"query":{"filtered":{"filter":{"range":{"'.$field.'":{"gte":"'.$start.'","lt":"'.$end.'"}}}}}}'; if(strpos($index, 'eventlogs')!==false) {
$query = '{"size":1000,"sort":{"'.$field.'":{"order":"asc"}},"query":{"filtered":{"filter":{"bool":{'.
'"must":[{"range":{"date":{"gte":"'.$start.'","lte":"'.$end.'"}}}],'.
'"must_not":[{"exists": {"field":"nsp3hq"}},{"exists": {"field":"q0i8u1"}},{"exists": {"field":"eyn916"}},{"exists": {"field":"20mqd8"}},'.
'{"exists": {"field":"wwbkux"}},{"exists": {"field":"r5ua96"}},{"exists": {"field":"easiz"}},{"exists": {"field":"dexusu"}},{"exists": {"field":"earts"}},'.
'{"exists": {"field":"ealu"}},{"exists": {"field":"ealf"}},{"exists": {"field":"eal"}},{"exists": {"field":"ears"}},{"exists": {"field":"ealuf"}},'.
'{"exists": {"field":"ealus"}},{"exists": {"field":"eaatf"}},{"exists": {"field":"enail"}},{"exists": {"field":"enuail"}},{"exists": {"field":"test"}}]'.
'}}}}}';
}
} $scroll_id = null;
$retry = 0;
$num = 0;
while(true) {
if(is_null($scroll_id)) {
$result = post("{$host}/{$index}/_search?scroll=2m", $query);
} else {
$result = get("{$host}/_search/scroll?scroll=2m&scroll_id={$scroll_id}");
} $json = json_decode($result, true);
if(!isset($json['_scroll_id'])) {
fwrite(STDERR, "查询失败:索引-{$index} 起始-{$start} 截止-{$end}\n");
sleep(5);
$retry++;
if($retry>10) {
exit(4);
}
} $scroll_id = $json['_scroll_id']; foreach($json['hits']['hits'] as $row) {
fwrite(STDOUT, json_encode($row)."\n");
$num++;
} if(count($json['hits']['hits'])==0)
{
break;
} usleep(100000);
} //校验条数是否一致
$query = json_decode($query, true);
unset($query['size'], $query['sort']);
$result = post("{$host}/{$index}/_count", json_encode($query));
$json = json_decode($result, true);
if(!isset($json['count']) or intval($json['count'])!==$num) {
fwrite(STDERR, "校验失败:索引-{$index} 起始-{$start} 截止-{$end} 记录条数-{$json['count']} 导出条数-{$num}\n");
} function get($url)
{
$handle = curl_init();
//curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_URL, $url);
//curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
return curl_exec($handle);
} function post($url, $data)
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_HEADER, 0);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
return curl_exec($handle);
}
备份时执行shell脚本即可备份昨天的增量数据
谢土豪
如果有帮到你的话,请赞赏我吧!
本文为kerwin原创,转载请注明出处。
http://www.cnblogs.com/kerwinC/p/6296675.html
ELASTIC的备份与恢复的更多相关文章
- Elastic认证考试,请先看这一篇
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/wojiushiwo987/article ...
- MySQL 数据库的备份与恢复
一.MySQL 常见的备份方式 1. 直接拷贝数据库文件(物理拷贝) 2. 使用 mysqldump 工具备份 3. 使用 mysqlhotcopy 工具备份 4. 使用 mysql 的主从同步复制, ...
- elastic search使用总结
1. elasticsearch安装 官方下载地址:https://www.elastic.co/downloads/elasticsearch 解压文件 elasticsearch-2.4.0.zi ...
- Assign an Elastic IP Address to Your Instance
By default, an instance in a nondefault VPC is not assigned a public IP address, and is private.You ...
- ZTE交换路由设备配置的备份与恢复
一.TFTP服务器搭建 使用用户计算机搭建TFTP服务器,交换路由设备作为TFTP客户端. 运行TFTPServer.exe,该程序所在的目录为TFTP的根目录. 请保证您的TFTP可以Ping通所要 ...
- mysql数据库的备份与恢复
假定我们的目标数据库是 test, 表是 user.假定mysql的用户名和密码均为 root. 备份与恢复所用的程序分别是mysql软件包提供的 mysqldump 命令和 mysql 命令.思想很 ...
- Oracle逻辑备份与恢复
1. 备份的类型 按照备份方式的不同,可以把备份分为两类: 1.1 逻辑备份:指通过逻辑导出对数据进行备份.将数据库中的用户对象导出到一个二进制文件中,逻辑备份使用导入导出工具:EXPDP/IMP ...
- How to ssh to your Amazon Elastic Beanstalk instance?
Well, if it's ec2 or a digital ocean server, it would be a lot easier- you do what you normally do f ...
- RMAN备份与恢复之初入茅庐
理解数据库备份 所谓备份实际上是把数据库复制到转储设备的过程. 从备份方式来看数据库备份分为物理备份和逻辑备份,物理备份是把构成数据库的所有文件拷贝到指定的位置的过程,而逻辑备份只是利用SQL语言从数 ...
随机推荐
- Redis 学习小记
由于是学习笔记,我就不来各种啰嗦,介绍这个介绍那个,也不上交给国家,或者各种对比,相信如果你真心用 redis 的话,就不会去跟 MySql,Memcached,MongoDB 等做对比了. 我原先用 ...
- 在Emacs中启用Fcitx输入法
安装fcitx输入法,在 ~/.xinitrc文件中添加如下内容 (我用startx启动图形环境,所以在~/.xinitrc中配置X会话) export LC_CTYPE="zh_CN.UT ...
- 第8月第16天 django pil
1. https://github.com/chaonet/forum/ sudo easy_install --find-links http://www.pythonware.com/produ ...
- [CEOI2015 Day2]世界冰球锦标赛 (双向搜索)
题目描述 [CEOI2015 Day2]世界冰球锦标赛译自 CEOI2015 Day2 T1「Ice Hockey World Championship」 今年的世界冰球锦标赛在捷克举行.Bobek ...
- NOI2001 方程的解数(双向搜索)
solution 一道非常经典的双向搜索题目,先将前3个未知数枚举一遍得到方程的前半部分所有可能的值,取负存入第一个队列中再将后3个未知数枚举一遍,存入第二个队列中.这样我们只要匹配两个队列中相同的元 ...
- Oracle 数据库和监听器开机自启动两种实现方法
数据库和监听器开机自启动 编辑oratab文件: 修改:orcl:/u01/app/oracle/product/11.2.0/db_1:N orcl:/u01/app/or ...
- Springboot 之 自定义配置文件及读取配置文件
本文章来自[知识林] 读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两 ...
- 修复 Tween.JS 的 onStop 设置无效
Tween.js 个人认为还是一个比较不错的 缓动动画库,给作为学渣的我实现一些酷酷的动画带来了极大的遍历. 但是,今天突然发现特么设置onStop的回调函数居然没反应...... 作为一个渣渣只能一 ...
- How to return plain text from AWS Lambda & API Gateway
With limited experience in AWS Lambda & API Gateway, it's struggling to find the correct way to ...
- yum命令简介
yum 一些较常见的用法 命令 功能 yum check-update 检查可更新的所有软件包 yum update 下载更新系统已安装的所有软件包 yum upgrade 大规模的版本升级,与yum ...