MySQL backup and restore using JAVA

Backup:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;
executeCmd = “mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“Backup taken successfully”);

} else {

out.println(“Could not take mysql backup”);

}

Restore:

/******************************************************/
//Database Properties
/******************************************************/
String dbName = “dbName”;
String dbUser = “dbUser”;
String dbPass = “dbPass”;

/***********************************************************/
// Execute Shell Command
/***********************************************************/
String executeCmd = “”;

executeCmd = new String[]{“/bin/sh”, “-c”, “mysql -u” + dbUser+ ” -p”+dbPass+” ” + dbName+ ” < backup.sql” };

}
Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if(processComplete == 0){

out.println(“success”);

} else {

out.println(“restore failure”);

}

20 thoughts on “MySQL backup and restore using JAVA

  1. I am trying to take MySQL backup using java but I cant able to take.
    My Code:

    mport java.util.*;
    import java.io.*;
    public class backup
    {
    public static void main(String args[])
    {
    String dbName = “school”;
    String dbUser = “root”;
    String dbPass = “admin”;
    //path for the Mysql
    String executeCmd =”D:/ProgramFiles/MySQL/MySQL Server 5.1/bin/mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
    executeCmd =”mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;
    try
    {
    Process runtimeProcess =Runtime.getRuntime().exec(executeCmd);
    int processComplete = runtimeProcess.waitFor();
    if(processComplete == 0)
    {
    System.out.println(“Backup taken successfully”);
    } else
    {
    System.out.println(“Could not take mysql backup”);
    }
    }
    catch(Exception e)
    {
    System.out.println(“Not connected”+e);
    }

    }
    }

    It showing the error as
    Not connectedjava.io.IOException: Cannot run program “mysqldump”: CreateProcess error=2, The system cannot find the file specified.

    Can anyone help me to solve this?
    Thanks in advance
    Bhuvana

    1. Sorry for delay … But I was searching for the same thing you want I tried this:
      executeCmd += “mysqldump -u ” + dbUser + ” -p” + dbPass + ” ” + dbName + ” -r backup.sql”;

      instead of this:

      executeCmd =”mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;

      it worked to me !!

      1. Actually that line has to be commented
        //executeCmd =”mysqldump -u “+dbUser+” -p”+dbPass+” “+dbName+” -r backup.sql”;

        this line is additional

    1. Hi ,
      I need remote mysql db server beckup in local using java.. Actually DB is in Linux environment and production in windows.. I tried mysql dump command with host and port like String dumpCommand = mysqlpath+”mysqldump ” + dbName + ” -h ” + ip + ” –P ” + port + ” -u ” + dbUser +” -p” + dbPass;

      But it has a issue like this java.io.IOException: Cannot run program "\mysqldump": CreateProcess error=2, The system cannot find the file specified.. 
      

      I am struggling last 3 days for this, please help soon

      Thanks,
      Muthamizh karuppiah

    1. am stil stuck wth the code to backup my database cud u pliz inbox me on this email if figred it out:mobsweat@yahoo.com

    2. Its working fine..

      public class MySQLBackUp {

      public static void main(String[] args) {
      String databaseName = “exam”;
      String databaseUserName = “root”;
      String password = “root”;
      String executeCmd = “”;
      executeCmd = “mysqldump -u ” + databaseUserName + ” -p” + password + ” ” + databaseName + ” -r D:\\database\\backup.sql”;
      try {

      Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
      int processComplete = runtimeProcess.waitFor();
      if (processComplete == 0) {

      System.out.println(“Backup taken successfully”);

      } else {

      System.out.println(“Could not take mysql backup”);

      }
      } catch (Exception ex) {
      System.out.println(“Exception: ” + ex);
      }
      }
      }

  2. Great, its worked for me also, but not in Servlet program with tomcat 6 server and ubuntu 10.04 os. i know this program dont have any problem and its got some issue with “tomcat user access”. Can any one know how to do it for tomcat 6 ? Thanks for tutorial.

  3. hi,
    i wrote the code like:
    String databaseUserName = “root”;
    String password = “”;
    String executeCmd = “”;
    executeCmd = “mysqldump -u ” + databaseUserName + ” -p” + password + ” ” +
    databaseName + ” -r E:\backup.sql”;
    try {
    Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
    System.out.println(“”+runtimeProcess.waitFor());
    int processComplete = runtimeProcess.waitFor();
    if (processComplete == 0) {
    System.out.println(“Backup taken successfully”);
    } else {
    System.out.println(“Could not take mysql backup”);
    }
    } catch (Exception ex) {
    System.out.println(“Exception: ” + ex);
    }

    **no password for mysql
    but not get the backup and i couldnt close the program..there is creating backup.sql. while opening this file shows that the the application is using by another application..give a way

  4. hi,
    This is my code
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    public class Sample
    {
    public static void main(String[] args)
    {
    String databaseName = “root”;
    String databaseUserName = “root”;
    String password =”root”;
    //String executeCmd =””;
    String executeCmd = “mysqldump -u ” + databaseUserName + “-p” + password + ” ” + databaseName + ” -r C:\backup\backup.sql”;
    try {

        Process runtimeProcess = Runtime.getRuntime().exec(executeCmd);
        int processComplete = runtimeProcess.waitFor();
        if (processComplete == 0) {
    
        System.out.println("Backup taken successfully");
    
        } else {
    
        System.out.println("Could not take mysql backup");
    
        }
        } catch (Exception ex) {
        System.out.println("Exception: " + ex);
        }
        }
    
    
    }
    

    I got the output as:
    Could not take mysql backup
    can any one help me backup file is creating but there is no content in it .

Leave a comment