1.备份数据到ftp服务器上
java代码:
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* Created by Allen on 2017/8/31.
*/
public class CommandUtil {
private static final Logger log = LoggerFactory.getLogger( CommandUtil.class );
public static void main(String[] args) {
CommandUtil.createSnapshot("192.168.***.47","logstash-****-log-20180719","192.168.***.53","ftp***","123***e","/test/es_backup");
}
public static void createSnapshot(String ip, String index, String ftpip, String ftpusername, String ftppasswd, String ftppath) {
String src = "/usr/local/suninfo/siem/es_backup_script";
String commands = "/bin/bash " + src + "/backup_elasticsearch.sh " + ip + " " + index + " " + ftpip + " " + ftpusername + " " + ftppasswd + " " + ftppath;
log.error("备份##" + ip + "##下的##" + index + "##索引中......");
CommandUtil.executeCommand(src, commands);
}
/**
* 【描述】: linux下的shell脚本执行
* 【步骤】: 测试组ftp服务器:192.168.213.53:21 用户名:ftptest/123@qwe
* @param
* @return
* @throws
* @author Allen
* @date 2017/8/31 14:15
*/
public static void executeCommand(String src,String commands ){
Process process2=null;
try {
process2=Runtime.getRuntime().exec("chmod -R 777 " + src);
log.error( "commands:"+commands );
} catch (IOException e1) {
log.error("chmod -R 777 " + src + "失败!");
}finally{
if(process2!=null){
process2.destroy();
}
}
CommandLine cmdLine = CommandLine.parse(commands);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(cmdLine);
} catch (ExecuteException e ) {
log.error("执行完毕,"+e.getMessage());
} catch (IOException e) {
log.error("执行完毕,"+e.getMessage());
}
}
}
shell 脚本:
#!/bin/sh
#--------备份索引-----------------
echo "--------备份索引---------"
cd /usr/local/suninfo/node-v8.11.3-linux-x64
elasticdump \ --input=http://$1:9200/$2 \ --output=$ \ | gzip > /usr/local/suninfo/$2.json.gz
echo "--------完成备份-------------"
echo "--------开始上传-------------"
ftp -n<<!
open $3
user $4 $5
binary
cd $6
lcd /usr/local/suninfo
prompt
mput $2.json.gz
close
bye
!
echo "--------完成上传-------------"
rm -rf /usr/local/suninfo/$2.json.gz
编译java文件:javac -Djava.ext.dirs=./lib CommandUtil1.java
运行java文件:java -Djava.ext.dirs=./lib CommandUtil
2.ftp服务器恢复数据到本地
java代码:
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
* Created by Allen on 2017/8/31.
*/
public class CommandUtil1 {
private static final Logger log = LoggerFactory.getLogger( CommandUtil1.class );
public static void main(String[] args) {
CommandUtil1.restoreSnapshot("192.168.***.47","logstash-****-log-20180719","192.168.***.53","ftp***","123***e","/test/es_backup");
}
public static void restoreSnapshot(String ip, String index, String ftpip, String ftpusername, String ftppasswd, String ftppath) {
String src = "/usr/local/suninfo/siem/es_backup_script";
String commands = "/bin/bash " + src + "/restore_elasticsearch.sh " + ip + " " + index + " " + ftpip + " " + ftpusername + " " + ftppasswd + " " + ftppath;
log.error("恢复##" + ip + "##下的##" + index + "##索引中......");
CommandUtil1.executeCommand(src, commands);
}
/**
* 【描述】: linux下的shell脚本执行
* 【步骤】:
* @param
* @return
* @throws
* @author Allen
* @date 2017/8/31 14:15
*/
public static void executeCommand(String src,String commands ){
Process process2=null;
try {
process2=Runtime.getRuntime().exec("chmod -R 777 " + src);
log.error( "commands:"+commands );
} catch (IOException e1) {
log.error("chmod -R 777 " + src + "失败!");
}finally{
if(process2!=null){
process2.destroy();
}
}
CommandLine cmdLine = CommandLine.parse(commands);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
try {
executor.execute(cmdLine);
} catch (ExecuteException e ) {
log.error("执行完毕,"+e.getMessage());
} catch (IOException e) {
log.error("执行完毕,"+e.getMessage());
}
}
}
shell脚本:
#!/bin/sh
#--------恢复索引-----------------
echo "--------开始下载-------------"
ftp -n<<!
open $3
user $4 $5
binary
cd $6
lcd /usr/local/suninfo
prompt
get $2.json.gz
close
bye
!
echo "--------完成下载-------------"
echo "---------恢复索引--------"
cd /usr/local/suninfo
gzip -dv $2.json.gz
cd /usr/local/suninfo/node-v8.11.3-linux-x64
elasticdump --input /usr/local/suninfo/$2.json --output "http://$1:9200/$2"
echo ""
echo "---------恢复索引成功--------"
rm -rf /usr/local/suninfo/$2.json
执行如上所示