700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 达梦数据库备份还原

达梦数据库备份还原

时间:2018-07-12 05:01:05

相关推荐

达梦数据库备份还原

最近在做一个功能,是通过Java实现数据库备份还原,查看网上存在的多个资源。之前有写过MYSQL的备份与还原,现在把我达梦数据库实现备份还原的功能代码分享出来,达梦数据库实现 备份还原过程比较难,主要是对达梦数据库不太熟悉,以下的代码希望可以帮到那些正在寻找实现数据库备份还原的人。

import java.io.*;import java.util.Date;public class DmDatabaseBackupAndRestore {public static void resdStreamInfo(InputStream... inputStreams) {for (InputStream in : inputStreams) {new Thread(() -> {try {BufferedReader br = new BufferedReader(new InputStreamReader(in));String line = null;while ((line = br.readLine()) != null) {System.out.println("数据" + line);}} catch (IOException ex) {ex.printStackTrace();} finally {try {in.close();} catch (IOException Ex) {Ex.printStackTrace();}}}).start();}}/*** 达梦数据库备份* param filePath 文件备份路径 路径格式 D:/code/backup/ 以实际情况而定* param binPath 数据库bin路径 路径格式 D:/dm/bin/ 以实际情况而定* param ip 数据库登录IP 以实际情况而定* param databasePort 数据库登端口 以实际情况而定* param password 数据库密码 root 以实际情况而定* param databasename 数据库名称 以实际情况而定*/public static void DmDatabaseBackup(String filePath, String binPath, String ip, String databasePort, String password, String databasename) {String dirfire = filePath;File file = new File(dirfire);if (!file.exists()) {file.mkdir();}String filename = "backup_" + new Date().getTime() + ".dmp";File datafile = new File(file + File.separator + filename);if (datafile.exists()) {System.out.println("文件名已存在,请更换");}try {Runtime rt = Runtime.getRuntime();Process process = null;String IpAdd = ip + ":" + databasePort;//OWNER 对应的是要备份的用户名 String cmd = "cmd /c " + binPath + "/dexp " + databasename + "/" + password + "@" + IpAdd + " file=" + filename + " DIRECTORY=" + filePath + " OWNER=KMP";process = rt.exec(cmd);InputStream errorStream = process.getErrorStream();InputStream inputStream = process.getInputStream();resdStreamInfo(errorStream, inputStream);int i = process.waitFor();process.destroy();if (i == 0) {System.out.println("数据库备份成功");//此处可以添加代码,把以上操作的信息保存在相应的数据库中}} catch (Exception e) {System.out.println("备份数据库失败");}}/*** 达梦数据库还原* param ip 数据库连接的IP地址 如:127.0.0.1 以实际情况而定* param filePath 文件备份路径 路径格式 D:/code/backup/ 以实际情况而定* param binPath 数据库bin路径 路径格式 D:/dm/bin/ 以实际情况而定* param ip 数据库IP 以实际情况而定* param databasePort 数据库端口 以实际情况而定* param password 数据库密码 root 以实际情况而定* param databasename 数据库名称 以实际情况而定*/public static void DmDatabaseRestore(String filePath, String binPath, String filename, String ip, String databasePort, String password, String databasename) {try {Runtime rt = Runtime.getRuntime();String os = System.getProperty("os.name");String IpAdd = ip + ":" + databasePort;//达梦数据库还原 table_first=y table_exists_action=replace TABLE_EXISTS_ACTION参数有四个选项,REPLACE:先删除现有表,再导数据String cmd = "cmd /c " + binPath + "/dimp " + databasename + "/" + password + "@" + IpAdd + " file=" + filename + " DIRECTORY=" + filePath + " table_exists_action=replace OWNER=KMP";Process restoreData = rt.exec(cmd);InputStream errorStream = restoreData.getErrorStream();InputStream inputStream = restoreData.getInputStream();resdStreamInfo(errorStream, inputStream);int i = restoreData.waitFor();restoreData.destroy();if (i == 0) {System.out.println("数据库备份成功");//此处可以添加代码,把以上操作的信息保存在相应的数据库中}} catch (Exception e) {System.out.println("备份数据库失败");}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。