Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. import java.io.*;
  2. import java.lang.*;
  3. import java.util.*;
  4.  
  5. class Backup
  6. {
  7. public static void main(String[] args)
  8. {
  9. String command = "D:\MySQL\bin\mysqldump.exe -u root -proot yatin -r D:/backup.sql";
  10. System.out.println(command);
  11. Process runtimeProcess;
  12. try
  13. {
  14. runtimeProcess = Runtime.getRuntime().exec(command);
  15. int processComplete = runtimeProcess.waitFor();
  16. if (processComplete == 0)
  17. {
  18. System.out.println("Backup created successfully");
  19. //return true;
  20. }
  21. else
  22. {
  23. System.out.println("Could not create the backup");
  24. }
  25. }
  26. catch (Exception ex)
  27. {
  28. ex.printStackTrace();
  29. }
  30. }
  31. }
  32.  
  33. import java.io.*;
  34. import java.lang.*;
  35. import java.util.*;
  36.  
  37. class Restore
  38. {
  39. public static void main(String[] args)
  40. {
  41. String command = "D:\MySQL\bin\mysql.exe -u root -proot mybackup< D:/backup.sql";
  42. //String [] command = new String[]{"mysql",mybackup,"D:\MySQL\bin\mysql.exe -u root -proot < D:/backup.sql";
  43. //String[] command = new String[]{"mysql", [database], "--user=" + [username], "--password=" + [password], "-e", " source " + [absolute path to the sql file]};
  44. //String[] command = new String[]{"mysql",mybackup, "-u=" +root, "-p="+root, "-e", " source " +D:/backup.sql};
  45. System.out.println(command);
  46. Process runtimeProcess;
  47. try
  48. {
  49. runtimeProcess = Runtime.getRuntime().exec(command);
  50. int processComplete = runtimeProcess.waitFor();
  51. if (processComplete == 0)
  52. {
  53. System.out.println("Restore created successfully");
  54. //return true;
  55. }
  56. else
  57. {
  58. System.out.println("Could not restore");
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. ex.printStackTrace();
  64. }
  65. }
  66. }
  67.  
  68. mysql> mysql -u root -proot mybackup < D:/backup.sql;
  69. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
  70. corresponds to your MySQL server version for the right syntax to use near 'mysql
  71. -u root -proot mybackup < D:/backup.sql' at line 1
  72.  
  73. public static void mysqlExport(String host, String port, String user, String password, String dbname, String table, String folder, String query) {
  74.  
  75. System.out.println("------ Exporting "+dbname+"."+table+" at "+folder+"---------------------------");
  76. try {
  77. String command = "mysqldump --host=" + host + " --port=" + port + " --user=" + user + " --password=" + password + " " + dbname + " " + table + " --where="" + query + "" > " + folder + table + ".sql";
  78. System.out.println(command);
  79. int returnValue = executeCommand(Arrays.asList("mysqldump", "--host="+host, "--port="+port, "--user="+user, "--password="+password, dbname, table, "--where="+query), folder + table + ".sql");
  80. } catch (Exception e) {
  81. System.out.println(e.getMessage());
  82. }
  83. }
  84.  
  85. public static void mysqlImport(String host, String port, String user, String password, String dbname, String table, String folder) {
  86.  
  87. System.out.println("------ Importing "+dbname+"."+table+" at "+folder+"---------------------------");
  88. try {
  89. String command = "mysql --host=" + host + " --port=" + port + " --user=" + user + " --password=" + password + " " + dbname + " " + table + " < " + folder + table + ".sql";
  90. System.out.println(command);
  91. int returnValue = executeCommand(new String[]{"mysql", "--host="+host, "--port="+port, "--user=" + user, "--password=" + password, dbname, "-e", "source " + folder + table + ".sql"});
  92. } catch (Exception e) {
  93. System.out.println(e.getMessage());
  94. }
  95. }
  96.  
  97. public static int executeCommand(String[] commands) throws IOException
  98. {
  99. System.out.println(commands.toString());
  100. Process process = Runtime.getRuntime().exec(commands);
  101. return dumpProcess(process);
  102. }
  103.  
  104. public static int executeCommand(List<String> commands, String folder) throws IOException
  105. {
  106. ProcessBuilder builder = new ProcessBuilder(commands);
  107. System.out.println(builder.command());
  108. builder.redirectOutput(new File(folder));
  109. Process process = builder.start();
  110. return dumpProcess(process);
  111. }
  112.  
  113. public static int dumpProcess(Process process) throws IOException
  114. {
  115. int returnValue = -1;
  116. try {
  117. String s = null;
  118. process.waitFor();
  119. returnValue = process.exitValue();
  120. if (returnValue == 0) {
  121. System.out.println("Command successful !!");
  122. BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
  123. System.out.println("Here is the standard output of the command:n");
  124. while ((s = stdInput.readLine()) != null) {
  125. System.out.println(s);
  126. }
  127. } else {
  128. System.out.println("Command failed. Exist Status code = "+returnValue);
  129. BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  130. System.out.println("Here is the standard error of the command (if any):n");
  131. while ((s = stdError.readLine()) != null) {
  132. System.out.println(s);
  133. }
  134. }
  135.  
  136. } catch (InterruptedException e) {
  137. e.printStackTrace();
  138. }
  139.  
  140. return returnValue;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement