Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Yts 95
Added: Aug 6, 2020 1:29 PM
Views: 51
Tags: no tags
  1.  try {
  2.  
  3.         /*NOTE: Getting path to the Jar file being executed*/
  4.         /*NOTE: YourImplementingClass-> replace with the class executing the code*/
  5.         CodeSource codeSource = YourImplementingClass.class.getProtectionDomain().getCodeSource();
  6.         File jarFile = new File(codeSource.getLocation().toURI().getPath());
  7.         String jarDir = jarFile.getParentFile().getPath();
  8.  
  9.  
  10.         /*NOTE: Creating Database Constraints*/
  11.         String dbName = "YourDBName";
  12.         String dbUser = "YourUserName";
  13.         String dbPass = "YourUserPassword";
  14.  
  15.         /*NOTE: Creating Path Constraints for folder saving*/
  16.         /*NOTE: Here the backup folder is created for saving inside it*/
  17.         String folderPath = jarDir + "\\backup";
  18.  
  19.         /*NOTE: Creating Folder if it does not exist*/
  20.         File f1 = new File(folderPath);
  21.         f1.mkdir();
  22.  
  23.         /*NOTE: Creating Path Constraints for backup saving*/
  24.         /*NOTE: Here the backup is saved in a folder called backup with the name backup.sql*/
  25.          String savePath = "\"" + jarDir + "\\backup\\" + "backup.sql\"";
  26.  
  27.         /*NOTE: Used to create a cmd command*/
  28.          String executeCmd = "mysqldump -u" + dbUser + " -p" + dbPass + " --database " + dbName + " -r " + savePath;
  29.  
  30.         /*NOTE: Executing the command here*/
  31.         Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
  32.         int processComplete = runtimeProcess.waitFor();
  33.  
  34.         /*NOTE: processComplete=0 if correctly executed, will contain other values if not*/
  35.         if (processComplete == 0) {
  36.             System.out.println("Backup Complete");
  37.         } else {
  38.             System.out.println("Backup Failure");
  39.         }
  40.  
  41.     } catch (URISyntaxException | IOException | InterruptedException ex) {
  42.         JOptionPane.showMessageDialog(null, "Error at Backuprestore" + ex.getMessage());
  43.     }
  44.