RSS

Restore MySql Backup file by using JAVA

28 Sep

First read my early blog post “Take the MySql DB backup in JAVA” and you can download the source from there


public boolean restoreDatabase(String dbUserName, String dbPassword, String source) {

        String[] executeCmd = new String[]{"mysql", "--user=" + dbUserName, "--password=" + dbPassword, "-e", "source " + source};

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

            if (processComplete == 0) {
                log.info("Backup restored successfully with " + source);
                return true;
            } else {
                log.info("Could not restore the backup " + source);
            }
        } catch (Exception ex) {
            log.error(ex, ex.getCause());
        }

        return false;

    }

Advertisements
 
4 Comments

Posted by on September 28, 2012 in java, mysql

 

Tags: , ,

4 responses to “Restore MySql Backup file by using JAVA

  1. GoesPoer

    May 8, 2015 at 10:46 am

    Reblogged this on Kidungsuwung.

     
  2. Noorullah

    April 7, 2016 at 9:34 am

    what is this source is this the saved sql file

     
  3. Vikshit

    October 6, 2017 at 9:34 am

    java.io.IOException: CreateProcess: “mysql ” –userroot –passwordadmin -e “source E:/backup.sql” error=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
    at java.lang.Runtime.exec(Runtime.java:591)
    at java.lang.Runtime.exec(Runtime.java:464)
    at test.testBackup.main(testBackup.java:169)

    it give me this error

    this are the changes which i done
    String source = “E:/backup.sql”;
    String username = “root”;
    String password = “admin”;
    String dbname = “ehms”;
    String[] restoreCmd = new String[]{“mysql “, “–user” + username, “–password” + password, “-e”, “source ” + source};

    Process runtimeProcess;
    try {

    runtimeProcess = Runtime.getRuntime().exec(restoreCmd);
    int processComplete = runtimeProcess.waitFor();

    if (processComplete == 0) {
    System.out.println(“Restored successfully!”);
    } else {
    System.out.println(“Could not restore the backup!”);
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }

     
  4. Vikshit

    October 6, 2017 at 10:12 am

    public static int restoreDB() throws IOException, InterruptedException {
    String path = “E:/backup.sql”;
    String username = “root”;
    String password = “admin”;
    String dbname = “ehms”;
    String executeCmd =”C:/\”Program Files (x86)\”/MySQL/\”MySQL Server 5.0\”/bin/mysql -u ” + username + ” -p” + password + ” ” + dbname + ” < " + path;
    Process runtimeProcess;
    try {
    System.out.println(executeCmd);//this out put works in mysql shell
    runtimeProcess = Runtime.getRuntime().exec(new String[] { "cmd.exe", "/c", executeCmd });
    System.out.println(executeCmd);

    // runtimeProcess = Runtime.getRuntime().exec(executeCmd);
    int processComplete = runtimeProcess.waitFor();
    System.out.println("processComplete "+processComplete);
    if (processComplete == 0) {
    System.out.println("Backup created successfully");
    return 1;
    } else {
    System.out.println("Could not create the backup");
    return 0;
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    return 0;
    }

    }

    finally i solve this this is working

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: