MySQL database backup/restore with Java Program ?



Hi!

This article is about creating a java program to backup/restore a MySQL database. The article is in two basic parts, namely Backup  & Restore.

1. Backup - Basically, the MYSQL command to backup database looks like follows.

mysqldump -u [username] -p[password] -B [dbname] -r [MYSQL file Path]

The following Code is for the java program.


public class Main{


 public void restoreDB(String path){

    String executeCmd = "C:/xampp/mysql/bin/mysqldump -u xyz -pxxyyzz -B myDB -r " + path;

    System.out.println(executeCmd);

    Process runtimeProcess;

    try
    {
       runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

       int processComplete = runtimeProcess.waitFor();

       System.out.println(processComplete);

       if(processComplete == 0)
       {
          System.out.println("Backup Created Successfully !");
       }
       else
       {
          System.out.println("Couldn't Create the backup !");
       }
    }
    catch(Exception ex)
    {
       ex.printStackTrace();
    }

   }

      public static void main(String[]args){

         new Main().restoreDB("C:/File.sql");
      }

}


2. Restore - Basically, the MYSQL command to restore database looks like follows.

mysql -u [username] -p[password] [databasename]<[MYSQL file Path]


In the query,

-u [username]    - is where you should type your username to access databases.

-p[password]      - is where you should type your password to the databases.

Then, you should type the name of the database which you need to backup tables to.

mysql -u xyz -pxxyyzz myDatabase<C:/file.sql

Then,Here's a java code to test run this MYSQL command.

public class Main{


 public void restoreDB(String path){

  String executeCmd = "C:/xampp/mysql/bin/mysql -u xyz -pxxyyzz myDB <" + path;

  System.out.println(executeCmd);

  Process runtimeProcess;

  try
  {
   runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });

   int processComplete = runtimeProcess.waitFor();

   System.out.println(processComplete);

   if(processComplete == 0)
   {
    System.out.println("Restored the Backup !");
   }
   else
   {
    System.out.println("Couldn't Restore the backup !");
   }
  }
  catch(Exception ex)
  {
   ex.printStackTrace();
  }

 }

 public static void main(String[]args){

  new Main().restoreDB("C:/File.sql");
 }

}
Ah, one more thing, You can set path to MYSQL in windows & adjust line 6 as follows.

String executeCmd = "mysql -u xyz -pxxyyzz myDB <" + path;

And, You need to have Process Class invoked to run cmd.exe & execute MYSQL command in line with Command Prompt.

19 comments:

Unknown said...

thank you very much. it's really working :D

Unknown said...

4 oomsortu

Unknown said...

thank you very muuch. it's really work :D

Unknown said...

thank you very much. it's work :D

Unknown said...

thank you very much. it's work :DFund

NithinSharma said...

A complete example on how to use these commands from JSP code can be found here
http://www.jvmhost.com/articles/mysql-postgresql-dump-restore-java-jsp-code

Harsha Wansooriya said...

Thanks Nithin for the info.

Anonymous said...

thank you so muchh Nithin.. it works

Zillas e Japeris said...

Don't work.

Unknown said...

Hey, its not working every time i get Couldn't Create the backup !
Please help

Unknown said...

Couldn't Create the backup ! ,I am getting this message every time ,please help

Harsha Wansooriya said...

I'm sorry People I was not available for several months. You can simply ask me any problem if you have ??

Anupam said...

Thanks Harsha
Backup is working
while restoring the backup it is printing else part i.e. "Couldn't Restore the Backup!"

Anupam said...

Thanks Harsha,
The backup is working well.
While restoring the backup it is displaying the else part "Couldn't Restore the Backup!"
Please help

Anupam said...

Than

Anupam said...

Thanks Harsha
The Backup code is working
while restoring the database the program is printing the else part i.e. "Couldn't Restore the backup !"

adil said...

this exactly what i looking for, searching ton of tutorials for restore the database, finally find this easy understandable simple code, you're the best sir...thank you very much!

adil said...

eureka!!! this what i looking for, searching ton of tutorials for restore the database, finally find this easy understandable simple code, you're the best sir! thank you very much!

Unknown said...

it gives me this error.
java.io.IOException: Cannot run program "mysqldump": CreateProcess error=2, The system cannot find the file specified
so what to do now please help