Guest User

Untitled

a guest
Feb 7th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. public class DatabaseBackup {
  2.  
  3. public static void main(String[] args) throws IOException, InterruptedException {
  4. Backupdbtosql();
  5. }
  6.  
  7. private static void Backupdbtosql() {
  8. try {
  9.  
  10. /*NOTE: Getting path to the Jar file being executed*/
  11. /*NOTE: YourImplementingClass-> replace with the class executing the code*/
  12. CodeSource codeSource = DatabaseBackup.class.getProtectionDomain().getCodeSource();
  13. File jarFile = new File(codeSource.getLocation().toURI().getPath());
  14. String jarDir = jarFile.getParentFile().getPath();
  15.  
  16.  
  17. /*NOTE: Creating Database Constraints*/
  18. String dbName = "XYZ";
  19. String dbUser = "root";
  20. String dbPass = "root";
  21.  
  22. /*NOTE: Creating Path Constraints for folder saving*/
  23. /*NOTE: Here the backup folder is created for saving inside it*/
  24. String folderPath = jarDir + "\\jogHarbackup";
  25.  
  26. /*NOTE: Creating Folder if it does not exist*/
  27. File f1 = new File(folderPath);
  28. f1.mkdir();
  29.  
  30. /*NOTE: Creating Path Constraints for backup saving*/
  31. /*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/
  32. String savePath = "\"" + jarDir + "\\jogHarbackup\\" + "backup.sql\"";
  33.  
  34. /*NOTE: Used to create a cmd command*/
  35. String executeCmd = "C:\\Program Files\\MySQL\\MySQL Server 5.7\\bin\\mysqldump -u" + dbUser + " -p" + dbPass + " " + dbName + " -r " + savePath;
  36.  
  37. /*NOTE: Executing the command here*/
  38. Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  39. int processComplete = runtimeProcess.waitFor();
  40.  
  41. /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  42. if (processComplete == 0) {
  43. System.out.println("Backup Complete");
  44. } else {
  45. System.out.println("Backup Failure");
  46. }
  47.  
  48. } catch (URISyntaxException | IOException | InterruptedException ex) {
  49. System.out.println(ex.getMessage());
  50. }
  51. }
  52. }
Add Comment
Please, Sign In to add comment