LIH
LIH

Reputation: 933

mysql database backup using mysqldump

I'm trying to take a backup of a mysql database using the following code.

    public boolean backupDB() {

    String executeCmd = "mysqldump -u root -p 1234 --add-drop-database -B test -r D:\\backup\\aaa.sql";
    Process runtimeProcess;
    try {
        runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
            System.out.println("Backup created successfully");
            return true;
        } else {
            System.out.println("Could not create the backup");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return false;
}

But it always gives me this error:

CreateProcess error=2, The system cannot find the file specified

How can I resolve this? Thanks

Upvotes: 2

Views: 2119

Answers (2)

Shaikh Arbaaz
Shaikh Arbaaz

Reputation: 155

Before you use mysqldump please specify its path like this :

String executeCmd = "\""+mysqldump_path+"bin\\mysqldump.exe\" --host=localhost --port=3306 --user=root --password=pass database > \""+path+"\"";
 runtimeProcess = Runtime.getRuntime().exec(new String[]{"cmd.exe","/c",executeCmd});

       int processComplete = runtimeProcess.waitFor();
       System.out.println(processComplete);

       if(processComplete == 0) {
           JOptionPane.showMessageDialog(null, "Backup Created Successfully !");
           System.out.println("Backup Created Successfully !");
       }
       else{
          System.out.println("Couldn't Create the backup !");
          JOptionPane.showMessageDialog(null, "Couldn't Create the backup !");
       }

if it Creates error and goes of abnormally then this a pic of code that u can add to see the errors :

InputStream errorStream = runtimeProcess.getErrorStream();
byte[] buffer = new byte[errorStream.available()];
errorStream.read(buffer);
String str = new String(buffer);
System.out.println(str);

Upvotes: 0

Roman C
Roman C

Reputation: 1

Modify PATH external variable to contain a path to mysqldump.exe or use absolute path to the file.

set PATH=%PATH%;%MYSQL_HOME%\bin 

where MYSQL_HOME is a variable that contains a path to MySQL server installation folder.

When use with absolute path. Note, that this path depends on your comp and shouldn't be use in production code.

String executeCmd = "C:\\Programs Files\\MySQL\\MySQL Server 5.0\\bin\\mysqldump.exe -u root -p 1234 --add-drop-database -B test -r D:\\backup\\aaa.sql";

Upvotes: 1

Related Questions