最近在做一个功能,是通过Java实现数据库备份还原,查看网上存在的多个资源,现在把我实现功能的代码分享出来,希望可以帮到那些正在寻找实现数据库备份还原的人。
代码中的命令是通过CMD小黑窗测试通过的,主要 Runtime.getRuntime()执行命令来实现,当然也有Process的一些功能,代码如下,可以直接用。
import java.io.*; import java.util.Date; public class DatabaseBackupAndRestore { 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(); } } public static void DatabaseBackup(String filePath, String binPath, String username, String password, String databasename) { String dirfire = filePath; File file = new File(dirfire); if (!file.exists()) { file.mkdir(); } String fileName = "backup_" + new Date().getTime() + ".sql"; File datafile = new File(file + File.separator + fileName); if (datafile.exists()) { System.out.println("文件名已存在,请更换"); } //-u后的root为mysql数据库用户名,-p后接的root为该用户密码,注意不要有空格;dbName填写需要备份数据的数据库名称,大于号后接生成文件路径 //拼接cmd命令 windows下 cmd // Process exec = Runtime.getRuntime().exec("cmd /c /usr/bin/mysqldump -h" + host + " -P" + port + " -u " + username + " -p" + password + " " + databasename + " > " + datafile); try { Runtime rt = Runtime.getRuntime(); String os = System.getProperty("os.name"); Process process = null; //Windows环境下 if (os.toLowerCase().startsWith("win")) { String winCmdInfo = "cmd /c " + binPath + "mysqldump -u" + username + " -p" + password + " " + databasename + ">" + filePath + fileName; process = rt.exec(winCmdInfo); } else { //Linux环境下 Linux下 /bin/sh 提供思路未测试 String linuxCmdInfo = "/bin/sh " + binPath + "mysqldump -u" + username + " -p" + password + " " + databasename + ">" + filePath + fileName; process = rt.exec(new String[]{"sh", "-c", linuxCmdInfo}); } int i = process.waitFor(); process.destroy(); if (i == 0) { System.out.println("数据库备份成功"); //此处可以添加代码,把以上 *** 作的信息保存在相应的数据库中 } } catch (Exception e) { System.out.println("备份数据库失败"); } } public static void DatabaseRestore(String ip, String filePath, String binPath, String username, String password, String databasename) { try { Runtime rt = Runtime.getRuntime(); String os = System.getProperty("os.name"); Process process = null; // 调用 mysql 的 cmd: //Windows环境下 if (os.toLowerCase().startsWith("win")) { String winCmdInfo = binPath + "/mysql.exe" + " -h" + ip + " -u" + username + " -p" + password + " --default-character-set=utf8 " + databasename; process = rt.exec(winCmdInfo); } else { //Linux环境下 未测试 String linuxCmdInfo = "mysqldump -u" + username + " -p" + password + " " + databasename + ">" + filePath; process = rt.exec(new String[]{"sh", "-c", linuxCmdInfo}); } int i = process.waitFor(); process.destroy(); if (i == 0) { System.out.println("数据库还原成功"); } } catch (Exception e) { System.out.println("还原数据库失败"); //此处可以添加代码,把以上 *** 作的信息保存在相应的数据库中 } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)