@Value("${username}")
private String username; //数据库用户
@Value("${password}")
private String password; //数据库密码
@Value("${url}")
private String url; //数据库连接url
@Value("${oracle.name}")
private String conName; //远程连接用户名
@Value("${oracle.pwd}")
private String conPwd; //远程连接密码
public void copy() {
DbOperate db = new DbOperate();
String backName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".dmp";
String localPath = "D:/";
String host = url.substring(url.lastIndexOf("@") + 1, url.indexOf(":1521"));
Boolean iss = db.exportDatabaseTool(localPath, backName, username, password, url, conName, conPwd);
new Thread(){
public void run(){
try {
Thread.sleep(10000);
if (iss) {
try {
//从远程服务器下载到本地
db.downloadSftpFile("/home/oracle", localPath, backName, host, conName, conPwd);
//上传文件
File sqlFile = new File(localPath + backName);
FileInputStream fileInputStream = null;
fileInputStream = new FileInputStream(sqlFile);
MultipartFile file = new MockMultipartFile(sqlFile.getName(), sqlFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
BladeFile bladeFile = ossBuilder.template("1").putFile(file.getOriginalFilename(), file.getInputStream());
if (bladeFile != null) {
sqlFile.delete();
} else {
throw new ServiceException("未上传成功,抛异常");
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) { }
}
}.start();
}
/**
* 备份工具类
*
*/
public class DbOperate {
private static Session session;
public boolean exportDatabaseTool(String localPath, String fileName, String userName, String passWord,
String url, String conName, String conPwd) {
String sid = url.substring(url.lastIndexOf(":") + 1);
String ip = url.substring(url.lastIndexOf("@") + 1, url.indexOf(":1521"));
String savePath = "~";
File saveFile = new File(savePath);
if (!saveFile.exists()) {
saveFile.mkdirs();
}
try {
//String command = "su - oracle " + "-c " + "\"" + "exp " + userName + "/" + passWord + "@" + sid + " tables=SMI_NEWS" +" file=" + savePath + "/" + fileName + "\"";
String command = "su - oracle " + "-c " + "\"" + "exp " + userName + "/" + passWord + "@" + sid +" file=" + savePath + "/" + fileName + "\"";
execCmd(command, ip, conName, conPwd, localPath, fileName);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* 执行相关命令
*
* @param command
* @param host
* @param conName
* @param conPwd
* @param localPath
* @param fileName
*/
public void execCmd(String command, String host, String conName, String conPwd, String localPath, String fileName) {
ChannelExec channelExec = null;
if (command != null) {
try {
connect(host, conName, conPwd);
channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand(command);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
channelExec.connect();
} catch (JSchException e) {
e.printStackTrace();
} finally {
if (null != channelExec) {
channelExec.disconnect();
}
}
}
}
/**
* 连接服务器
*
* @param host
* @param conName
* @param conPwd
*/
private static void connect(String host, String conName, String conPwd) {
int port = 22;
try {
JSch jsch = new JSch();
session = jsch.getSession(conName, host, port);
session.setPassword(conPwd);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
} catch (JSchException e) {
e.printStackTrace();
}
}
/**
* 下载
*
* @param ftpPath
* @param localPath
* @param fileName
* @param host
* @param conName
* @param conPwd
* @throws JSchException
*/
public static void downloadSftpFile(String ftpPath, String localPath, String fileName,
String host, String conName, String conPwd) throws JSchException {
connect(host, conName, conPwd);
Channel channel = session.openChannel("sftp");//打开传输通道
channel.connect();//通道连接
ChannelSftp chSftp = (ChannelSftp) channel;
String ftpFilePath = ftpPath + "/" + fileName; //服务器文件路径
String localFilePath = localPath + fileName; //拼接成下载后文件全路径
try {
chSftp.setFilenameEncoding("UTF-8");
chSftp.get(ftpFilePath, localFilePath);
} catch (Exception e) {
e.printStackTrace();
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
}
// public boolean impDatabaseTool(String fileName) {
// String userName = "sinomeic";
// String passWord = "sino3210";
// String savePath = "~";
// String SID = "orcl";
//
// try {
// String command = "su - oracle " + "-c " + "\"" + "imp " + userName + "/" + passWord + "@" + SID + " file=" + savePath + "/" + fileName + " full=y ignore=y" + "\"";
// execCmd(command);
// } catch (Exception e) {
// e.printStackTrace();
//
// }
// return true;
// }
}