Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. import java.io.File;
  2.  
  3. public class NastyDbJobs {
  4.  
  5. private static String dbName = "tcs";
  6. private static String dbUserName = "root";
  7. private static String dbPassword = "password";
  8.  
  9. public static boolean backupDB(File file) {
  10. String path = file.getPath();
  11.  
  12. if (!path.contains(".sql")) {
  13. file = new File(path + ".sql");
  14. }
  15.  
  16. String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " --add-drop-database -B " + dbName + " -r " + file.getPath();
  17. Process runtimeProcess;
  18. try {
  19.  
  20. runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  21. int processComplete = runtimeProcess.waitFor();
  22.  
  23. if (processComplete == 0) {
  24. System.out.println("Backup created successfully");
  25. runtimeProcess.destroy();
  26. return true;
  27. } else {
  28.  
  29. System.out.println("Could not create the backup");
  30. }
  31.  
  32.  
  33. } catch (Exception ex) {
  34. ex.printStackTrace();
  35.  
  36.  
  37. }
  38.  
  39. return false;
  40. }
  41.  
  42. public static boolean restoreDB(File file) {
  43. String path = file.getPath();
  44. if (!path.contains(".sql")) {
  45. file = new File(path + ".sql");
  46. }
  47.  
  48. String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + file.getPath()};
  49.  
  50. Process runtimeProcess;
  51. try {
  52.  
  53. runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  54. int processComplete = runtimeProcess.waitFor();
  55.  
  56. if (processComplete == 0) {
  57. System.out.println("Backup restored successfully");
  58. return true;
  59. } else {
  60. System.out.println("Could not restore the backup");
  61. }
  62. } catch (Exception ex) {
  63. ex.printStackTrace();
  64. }
  65.  
  66. return false;
  67. }
  68. }
  69.  
  70. "/myPath/mysql5/mysqldump"
  71.  
  72. "c:\myPath\mysql5\mysqldump"
  73.  
  74. import java.io.File;
  75. import java.io.InputStream;
  76. import java.io.PrintStream;
  77. import java.util.*;
  78.  
  79. import java.util.Date;
  80. import java.text.DateFormat;
  81. import java.text.SimpleDateFormat;
  82. import java.util.Calendar;
  83. public class TableBackup_1 {
  84.  
  85. private static String ip="localhost";
  86. private static String port="3306";
  87. private static String database="hbni";
  88. private static String user="root";
  89. private static String pass="password";
  90. private static String path="/home/Admin/nil/";
  91. public static void export()
  92. {
  93. Date dateNow = new Date();
  94. SimpleDateFormat dateformatyyyyMMdd = new SimpleDateFormat("yyyyMMdd");
  95. String date_to_string = dateformatyyyyMMdd.format(dateNow);
  96. System.out.println("date into yyyyMMdd format: " + date_to_string);
  97. String ss="hbni.sql";
  98. String fullName = path + " " + date_to_string + " " + ss;
  99. String dumpCommand = "mysqldump " + database + " -h " + ip + " -u " + user +" -p" + pass;
  100. Runtime rt = Runtime.getRuntime();
  101. File test=new File(fullName);
  102. PrintStream ps;
  103. try{
  104. Process child = rt.exec(dumpCommand);
  105. ps=new PrintStream(test);
  106. InputStream in = child.getInputStream();
  107. int ch;
  108. while ((ch = in.read()) != -1) {
  109. ps.write(ch);
  110. //System.out.write(ch); //to view it by console
  111. }
  112.  
  113. InputStream err = child.getErrorStream();
  114. while ((ch = err.read()) != -1) {
  115. System.out.write(ch);
  116. }
  117. }catch(Exception exc) {
  118. exc.printStackTrace();
  119. }
  120. }
  121.  
  122. public static void main(String args[]){
  123. export();
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement