ftp上传下载文件 | java编译运行有依赖的main方法

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

执行如上所示

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值