备份oracle数据库

在这里插入图片描述
在这里插入图片描述


	@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;
//	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值