脚本内容如下:
#!/bin/bash
HOSTNAME="10.18.xxx.xxx"
PORT="xxxx"
USERNAME="xxxxx"
PASSWORD="xxxxx"
COPYDB="xxxxx"
DBNAME=$COPYDB"_"${1}
create_db_sql="CREATE DATABASE IF NOT EXISTS ${DBNAME} DEFAULT CHARACTER SET UTF8 COLLATE UTF8_GENERAL_CI"
mysql -h $HOSTNAME -P $PORT -u $USERNAME -p$PASSWORD -e "$create_db_sql"
mysqldump -h $HOSTNAME -P $PORT -u$USERNAME -p$PASSWORD $COPYDB | mysql -h $HOSTNAME -P $PORT -u$USERNAME -p$PASSWORD $DBNAME
grant_db_sql="grant select,insert,update,delete on $DBNAME.* to '授权user'@'%'"
mysql -h $HOSTNAME -P $PORT -u $USERNAME -p$PASSWORD -e "$grant_db_sql"
java代码:
调取脚本公共类
需要引入
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
package com.xxxx.admin.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 连接远程服务器 执行命令
*
* @author liutt
* @date 2020/11/27
*/
public class ConnectShellUtil {
private Connection conn;
private String ipAddr;
private String userName;
private String password;
private String charset = Charset.defaultCharset().toString();
public static Logger log = LoggerFactory.getLogger(ConnectShellUtil.class);
public ConnectShellUtil(String ipAddr, String userName, String password, String charset) {
this.ipAddr = ipAddr;
this.userName = userName;
this.password = password;
if (charset != null) {
this.charset = charset;
}
}
public boolean login() throws IOException {
conn = new Connection(ipAddr);
conn.connect();
// 认证
return conn.authenticateWithPassword(userName, password);
}
/**
* 执行命令
*
* @param shellCommand 命令
* @return 输出结果str
*/
public String excuteShellCommand(String shellCommand) {
InputStream in;
String str = "";
try {
if (this.login()) {
Session session = conn.openSession();
session.execCommand(shellCommand);
in = new StreamGobbler(session.getStderr());
str = this.processStdout(in, charset);
session.close();
in.close();
conn.close();
} else {
log.error("连接远程服务器 [{}] 失败,请检查连接信息", ipAddr);
}
} catch (IOException e1) {
e1.printStackTrace();
}
return str;
}
/**
* 获取服务器输出结果
*
* @param in 输出信息流
* @param charset 字符串 默认UTF8
* @throws UnsupportedEncodingException
*/
private String processStdout(InputStream in, String charset) throws UnsupportedEncodingException {
String line;
BufferedReader brs = new BufferedReader(new InputStreamReader(in, charset));
StringBuffer stringBuffer = new StringBuffer();
try {
while ((line = brs.readLine()) != null) {
stringBuffer.append(line + "\n");
}
} catch (IOException e) {
log.error("---转化出现异常---");
}
return stringBuffer.toString();
}
}
调取过程:
private static final String SHELL_COMMAND = "bash platform/copy.sh ";
private String copyDatabase(Integer productId) {
//使用byte
byte[] ip = {49, 48, 46, 49, xxx, 46, 50, 49, xxx, 46, 50, xxx, 53};
byte[] userName = {112, xxx, 97, 116, 102, xxx, 114, 109};
byte[] pwd = {112, xxx, 97, 116, 102, xxx, 114, 109, xxx, xxx};
ConnectShellUtil connectShellUtil = new ConnectShellUtil(new String(ip), new String(userName), new String(pwd), null);
return connectShellUtil.excuteShellCommand(SHELL_COMMAND + productId);
}
这样就实现了连接远程服务器,调取shell脚本 复制整个数据库过程!
亲测可用。