Advertisement
Guest User

Untitled

a guest
Apr 13th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. public static void Backupdbtosql() {
  2. try {
  3.  
  4. /*NOTE: Getting path to the Jar file being executed*/
  5. /*NOTE: YourImplementingClass-> replace with the class executing the code*/
  6. CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();
  7. File jarFile = new File(codeSource.getLocation().toURI().getPath());
  8. String jarDir = jarFile.getParentFile().getPath();
  9.  
  10.  
  11. /*NOTE: Creating Database Constraints*/
  12. String dbName = "YourDBName";
  13. String dbUser = "YourUserName";
  14. String dbPass = "YourUserPassword";
  15.  
  16. /*NOTE: Creating Path Constraints for folder saving*/
  17. /*NOTE: Here the backup folder is created for saving inside it*/
  18. String folderPath = jarDir + "\backup";
  19.  
  20. /*NOTE: Creating Folder if it does not exist*/
  21. File f1 = new File(folderPath);
  22. f1.mkdir();
  23.  
  24. /*NOTE: Creating Path Constraints for backup saving*/
  25. /*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/
  26. String savePath = """ + jarDir + "\backup\" + "backup.sql"";
  27.  
  28. /*NOTE: Used to create a cmd command*/
  29. String executeCmd = "mysqldump -u" + dbUser + " -p" + dbPass + " --database " + dbName + " -r " + savePath;
  30.  
  31. /*NOTE: Executing the command here*/
  32. Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  33. int processComplete = runtimeProcess.waitFor();
  34.  
  35. /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  36. if (processComplete == 0) {
  37. System.out.println("Backup Complete");
  38. } else {
  39. System.out.println("Backup Failure");
  40. }
  41.  
  42. } catch (URISyntaxException | IOException | InterruptedException ex) {
  43. JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
  44. }
  45. }
  46.  
  47. public static void Restoredbfromsql(String s) {
  48. try {
  49. /*NOTE: String s is the mysql file name including the .sql in its name*/
  50. /*NOTE: Getting path to the Jar file being executed*/
  51. /*NOTE: YourImplementingClass-> replace with the class executing the code*/
  52. CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();
  53. File jarFile = new File(codeSource.getLocation().toURI().getPath());
  54. String jarDir = jarFile.getParentFile().getPath();
  55.  
  56. /*NOTE: Creating Database Constraints*/
  57. String dbName = "YourDBName";
  58. String dbUser = "YourUserName";
  59. String dbPass = "YourUserPassword";
  60.  
  61. /*NOTE: Creating Path Constraints for restoring*/
  62. String restorePath = jarDir + "\backup" + "\" + s;
  63.  
  64. /*NOTE: Used to create a cmd command*/
  65. /*NOTE: Do not create a single large string, this will cause buffer locking, use string array*/
  66. String[] executeCmd = new String[]{"mysql", dbName, "-u" + dbUser, "-p" + dbPass, "-e", " source " + restorePath};
  67.  
  68. /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  69. Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  70. int processComplete = runtimeProcess.waitFor();
  71.  
  72. /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  73. if (processComplete == 0) {
  74. JOptionPane.showMessageDialog(null, "Successfully restored from SQL : " + s);
  75. } else {
  76. JOptionPane.showMessageDialog(null, "Error at restoring");
  77. }
  78.  
  79.  
  80. } catch (URISyntaxException | IOException | InterruptedException | HeadlessException ex) {
  81. JOptionPane.showMessageDialog(null, "Error at Restoredbfromsql" + ex.getMessage());
  82. }
  83.  
  84. }
  85.  
  86. Session session = HibernateUtil.getSessionFactory().openSession();
  87. // for every table, have the bean implement Serializable and use the next 4 lines
  88. List <TblBean> tblCollection = session.createCriteria(TblBean.class).list();
  89. FileOutputStream backup = new FileOutputStream("backupOf"+TblBean.getClass().getName()+".dat");
  90. ObjectOutputStream backupWriter = new ObjectOutputStream(backup);
  91. backupWriter.write(tblCollection);
  92.  
  93. public static String getData(String host, String port, String user, String password, String db,String table) throws Exception {
  94.  
  95. //an "C:/xampp/mysql/bin/mysqldump" ---- location ito han mysqldump
  96.  
  97. Process run = Runtime.getRuntime().exec(
  98. "C:/xampp/mysql/bin/mysqldump --host=" + host + " --port=" + port +
  99. " --user=" + user + " --password=" + password +
  100. " --compact --databases --add-drop-table --complete-insert --extended-insert " +
  101. "--skip-comments --skip-triggers "+ db+" --tables "+table);
  102.  
  103. InputStream in = run.getInputStream();
  104. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  105. StringBuffer temp = new StringBuffer();
  106. int count;
  107. char[] cbuf = new char[BUFFER];
  108.  
  109. while ((count = br.read(cbuf, 0, BUFFER)) != -1)
  110. temp.append(cbuf, 0, count);
  111.  
  112. br.close();
  113. in.close();
  114.  
  115. return temp.toString();
  116. }
  117.  
  118. executeCmd = cmdRuta +" -u" + dbUser + " -p" + dbPass + " -h" + dbPort + " --databases " + dbName + " -r " + savePath;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement