Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. private static String ip = "localhost";
  2. private static String port = "3306";
  3. private static String database = "IMS";
  4. private static String user = "root";
  5. private static String pass = "pass";
  6. private static String path = "/home/user/Desktop/project/";
  7.  
  8. public static void backup() {
  9. Date dateNow = new Date();
  10. SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
  11. String date_to_string = dateformatyyyyMMdd.format(dateNow);
  12. System.out.println("date into yyyyMMdd format: " + date_to_string);
  13. String ss = "IMS.sql";
  14. String fullName = path + " " + date_to_string + " " + ss;
  15. String dumpCommand = "mysqldump " + database + " -h " + ip + " -u " + user + " -p" + pass;
  16. Runtime rt = Runtime.getRuntime();
  17. File test = new File(fullName);
  18. PrintStream ps;
  19. try {
  20. Process child = rt.exec(dumpCommand);
  21. ps = new PrintStream(test);
  22. InputStream in = child.getInputStream();
  23. int ch;
  24. while ((ch = in.read()) != -1) {
  25. ps.write(ch);
  26. //System.out.write(ch); //to view it by console
  27. }
  28.  
  29. InputStream err = child.getErrorStream();
  30. while ((ch = err.read()) != -1) {
  31. System.out.write(ch);
  32. }
  33. } catch (Exception exc) {
  34. exc.printStackTrace();
  35. }
  36. }
  37.  
  38. String user = "root";
  39. String pass = "1234";
  40. String[] restoreCmd = new String[]{"mysql", "--user=" + user, "--password=" + pass, "-e", "source " + path};
  41. Process process;
  42. try {
  43.  
  44.  
  45. process = Runtime.getRuntime().exec(restoreCmd);
  46. int procCom = process.waitFor();
  47. if (procCom == 0) {
  48. lbl_restore.setText("Restore Succes!");
  49. } else {
  50. lbl_restore.setText("Can't Restore!");
  51. }
  52. } catch (Exception e) {
  53. JOptionPane.showMessageDialog(null, e);
  54. }
  55.  
  56. public boolean restoreDatabase(String dbUserName, String dbPassword, String source) {
  57.  
  58. String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source};
  59.  
  60. Process runtimeProcess;
  61.  
  62. try {
  63. runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  64. int processComplete = runtimeProcess.waitFor();
  65.  
  66. if (processComplete == 0) {
  67. log.info("Backup restored successfully with " + source);
  68. return true;
  69. } else {
  70. log.info("Could not restore the backup " + source);
  71. }
  72. } catch (Exception ex) {
  73. log.error(ex, ex.getCause());
  74. }
  75.  
  76. return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement