Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to backup and restore mysql database using java

Tags:

java

mysql

I have a small application which I am going to use to backup and restore a mysql database, coding for backup is as follows and it works properly,

....
String command = "mysqldump --host=" + dataBase.getHost() + " --user=" + dataBase.getUserName() + " --password=" + dataBase.getPassword() + " "
                + dataBase.getDatabaseName() + " -r " + dataBase.getBackupPath() + "/ofm_mnu_backup_" + bkDate + ".sql";
        System.out.println(command);
        Process p = null;
        try {


            Runtime runtime = Runtime.getRuntime();
            p = runtime.exec(command);

            int processComplete = p.waitFor();

            if (processComplete == 0) {

                System.out.println("Backup created successfully");

            } else {
                System.out.println("Could not create the backup");
            }
....

But when I try to restore the database using following codes it is not working, please help

  ........
    String command = "mysqldump --host=" + dataBase.getHost() + " --user="+dataBase.getUserName() + " --password= " + dataBase.getPassword()+""+dataBase.getDatabaseName() + "  " + dataBase.getBackupPath();
        Process p = null;

        try {
            System.out.println(command);
            Runtime runtime = Runtime.getRuntime();
            p = runtime.exec(command);
            int processComplete = p.waitFor();

            if (processComplete == 0) {
                System.out.println("Backup restored successfully");

            } else {
                System.out.println("Could not restore the backup");
            }
     ......

After so many google searches I found the answer for my matter,

  ......
  String[] executeCmd = new String[]{"mysql", [database], "--user=" + [username],"--password=" + [password], "-e", " source " + [absolute path to the sql file]};
  p = Runtime.getRuntime().exec(executeCmd);
  int processComplete = p.waitFor();
  ......

In the above code most important thing is ** " source " + [absolute path to the sql file] ** there shouldn't be a comma between 'source' word and the file path.

this worked for me I hope it'll work for you guys too.

like image 263
Harsha Avatar asked Jun 05 '26 17:06

Harsha


2 Answers

I don't think you are using the mysqldump command quite right. Shouldnt you use < or > characters to denote the backup/restore? This is discussed at length here. Something like this to backup:

String command = "mysqldump --host=" + dataBase.getHost() + " --user=" + dataBase.getUserName() + " --password=" + dataBase.getPassword() + " "
            + dataBase.getDatabaseName() + " > " + dataBase.getBackupPath() + "/ofm_mnu_backup_" + bkDate + ".sql";

and to restore:

String command = "mysqldump --host=" + dataBase.getHost() + " --user=" + dataBase.getUserName() + " --password=" + dataBase.getPassword() + " "
            + dataBase.getDatabaseName() + " < " + dataBase.getBackupPath() + "/ofm_mnu_backup_" + bkDate + ".sql";
like image 95
SuperTron Avatar answered Jun 08 '26 05:06

SuperTron


I would like to thank you guys for the support on how to restore mysql database with java code. Since none of the codes worked for me, but it gave me a clue to teak it to work.

Below is my final working code.

String[] executeCmd = new String[]{"C:\\xampp\\mysql\\bin\\mysql.exe",mydb, "--user=" + dbUserName, "--password=" + dbPassword, "-e", " source " + source};
like image 28
Ballans Avatar answered Jun 08 '26 06:06

Ballans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!