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());
        }
    }
5
  • Is your mysql protected by password? If it is you have to provide it on the command or it won't work Commented Feb 20, 2019 at 11:04
  • no it's not (not yet at least), that's why I removed -ppassword from the command line. Commented Feb 20, 2019 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 stackoverflow.com/questions/8149828/…) Commented Feb 20, 2019 at 11:16
  • ok I got this : mysqldump: Got error: 1049: Base 'bdd/bcpbdd_20-02-2019.sql' inconnue when selecting the database Commented Feb 20, 2019 at 11:30
  • Problem solved, my destination folder contained a space wich was misinterpreted in the command line, thank you all ! Commented Feb 20, 2019 at 11:33

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Problem solved, my destination folder contained a space wich was misinterpreted in the command line, thank you all !
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();
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.