Advertisement
Ahmad_faisal

backup

Jul 7th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. public static void BackupDBtosql(String Failname) {
  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 = ?Main_Screen?.class.getProtectionDomain().getCodeSource();
  7. File jarFile = new File(codeSource.getLocation().toURI().getPath());
  8. String jarDir = "E:\\";
  9.  
  10.  
  11. /*NOTE: Creating Database Constraints*/
  12. String dbName = ?"electrical"?;
  13. String dbUser = ?"root"?;
  14. String dbPass = ?"root"?;
  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\\" + ?"mustafa.sql\"?";
  27. String savePath = Failname + ".sql";
  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. JOptionPane.showMessageDialog(null, "تم انشاء الملف بنجاح");
  38. } else {
  39. JOptionPane.showMessageDialog(null, "هناك مشكلة , قد تكون اسم الملف بحيث يجب أن يكون الأسم بالأنجليزي");
  40. }
  41.  
  42. } catch (URISyntaxException | IOException | InterruptedException ex) {
  43. JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement