0

I've tried to back-up mysql database from my java app (using wamp server) but it doesn't work, it always display the message "can't create backup". here's my code which I took from this thread : Backup a mysql [xampp] database in java

    public static void saveBdd(){
         String path = null;
         String user = "root";
         Process p = null;


         JFileChooser fc = new JFileChooser();
         fc.setDialogTitle("Choisir l'emplacement de la sauvegarde");
         fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
         fc.setAcceptAllFileFilterUsed(false);
         fc.showOpenDialog(startPage);
         String date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());

        try {
            File f = fc.getSelectedFile();
            path = f.getAbsolutePath();
            path = path.replace('\\', '/');
            path = path+"/bcpbdd_"+date+".sql";


        } catch (Exception e) {
            e.printStackTrace();
        }
        try{
            Runtime runtime = Runtime.getRuntime();

            p=runtime.exec("C:/wamp64/bin/mysql/mysql5.7.23/bin/mysqldump -u " + user + " --add-drop-database -B bcpbdd -r "+path);

            int processComplete = p.waitFor();
            if (processComplete==0) {
                StartPage.afficheMessage("Backup Created Success!");
            } else {
                 StartPage.afficheMessage("Can't create backup.");
            }
        } catch (Exception e) {
            StartPage.afficheMessage(e.getMessage());
        }
    }
marui
  • 13
  • 4
  • Is your mysql protected by password? If it is you have to provide it on the command or it won't work – Ayrton Feb 20 '19 at 11:04
  • no it's not (not yet at least), that's why I removed -ppassword from the command line. – marui Feb 20 '19 at 11:09
  • To troubleshoot, print out the actual command you are executing, and try executing it manually from the command line. (I expect mysqldump to explaing what's wrong). Or, as others already suggested, print out the output of your process (see https://stackoverflow.com/questions/8149828/read-the-output-from-java-exec) – Daniele Feb 20 '19 at 11:16
  • ok I got this : mysqldump: Got error: 1049: Base 'bdd/bcpbdd_20-02-2019.sql' inconnue when selecting the database – marui Feb 20 '19 at 11:30
  • Problem solved, my destination folder contained a space wich was misinterpreted in the command line, thank you all ! – marui Feb 20 '19 at 11:33

2 Answers2

0

You can read the output of the process using p.getErrorStream() and p.getInputStream() This should be done in a new Thread and then you can write the output into a log file or into the console.

Guillaume
  • 14,306
  • 3
  • 43
  • 40
  • Problem solved, my destination folder contained a space wich was misinterpreted in the command line, thank you all ! – marui Feb 20 '19 at 11:35
0

Here is what I tried and working fine,

  1. more info on mysqldump command: https://stackoverflow.com/a/13484728/2987755
  2. You can provide any path where you want to create a backup file db_backup.sql.

    Process rt = Runtime.getRuntime().exec("mysqldump -P 3306 -h 127.0.0.1 -u root test");
    int exitCode = rt.waitFor();
    System.out.println("Process exited with : " + exitCode);
    BufferedReader in = new BufferedReader(new InputStreamReader(rt.getInputStream()));
    BufferedReader err = new BufferedReader(new InputStreamReader(rt.getErrorStream()));
    
    System.out.println("Backup file output:");
    String line;
    BufferedReader reader;
    if (exitCode != 0) {
        reader = err;
    } else {
        reader = in;
    }
    File file = new File("db_backup.sql");
    
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
        bufferedWriter.write(line);
        bufferedWriter.newLine();
    }
    bufferedWriter.flush();
    bufferedWriter.close();
    
dkb
  • 4,389
  • 4
  • 36
  • 54