프로젝트 모듈 - 백업 데이터베이스

3719 단어 JAVA
도입 의존

    com.jcraft
    jsch
    0.1.55

ExecuteShellUtil 도구 클래스
package me.zhengjie.modules.mnt.util;

import cn.hutool.core.io.IoUtil;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Vector;

/**
 *   shell  
 */
@Slf4j
public class ExecuteShellUtil {

	private Vector stdout;

	Session session;

	public ExecuteShellUtil(final String ipAddress, final String username, final String password,int port) {
		try {
			JSch jsch = new JSch();
			session = jsch.getSession(username, ipAddress, port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect(3000);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public int execute(final String command) {
		int returnCode = 0;
		ChannelShell channel = null;
		PrintWriter printWriter = null;
		BufferedReader input = null;
		stdout = new Vector();
		try {
			channel = (ChannelShell) session.openChannel("shell");
			channel.connect();
			input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
			printWriter = new PrintWriter(channel.getOutputStream());
			printWriter.println(command);
			printWriter.println("exit");
			printWriter.flush();
			log.info("The remote command is: ");
			String line;
			while ((line = input.readLine()) != null) {
				stdout.add(line);
				System.out.println(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}finally {
			IoUtil.close(printWriter);
			IoUtil.close(input);
			if (channel != null) {
				channel.disconnect();
			}
		}
		return returnCode;
	}

	public void close(){
		if (session != null) {
			session.disconnect();
		}
	}

	public String executeForResult(String command) {
		execute(command);
		StringBuilder sb = new StringBuilder();
		for (String str : stdout) {
			sb.append(str);
		}
		return sb.toString();
	}

}

 
Controller
    @PostMapping
    @ApiOperation(value = "     ")
    public ResponseEntityT create(@Validated @RequestBody YDatabaseBackupAdd resources){
        String date = DateUtils.getNewDate().getTime() + "";
        deployService.getBackupDatabase(resources.getBackupName(),date);
        return new ResponseEntityT<>(yDatabaseBackupService.create(resources,date),HttpStatus.CREATED);
    }

DeployServiceImpl
    @Override
	public String getBackupDatabase(String backup,String date) {
          
		ExecuteShellUtil executeShellUtil = getExecuteShellUtil(ip);

                //username      
                //password      
                //backupSQLUrl         
		 final String sql = " mysqldump -u"+ username +" -p" + password + " --all-databases | gzip >  " + backupSQLUrl + "" + backup +"_" + date + ".sql.gz";
		executeShellUtil.execute(sql);
		return "    ";
	}

    /**
	 *        ,              
	 * @param ip    ip
	 * @return
	 */
	private ExecuteShellUtil getExecuteShellUtil(String ip) {
		ServerDeployDto serverDeployDTO = serverDeployService.findByIp(ip);
		if (serverDeployDTO == null) {
			sendMsg("IP          :" + ip, MsgType.ERROR);
			throw new BadRequestException("IP          :" + ip);
		}
		return new ExecuteShellUtil(ip, serverDeployDTO.getAccount(), serverDeployDTO.getPassword(),serverDeployDTO.getPort());
	}

좋은 웹페이지 즐겨찾기