how to backup and restore mysql database using java

会有一股神秘感。 提交于 2019-12-12 09:10:48

问题


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.


回答1:


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";



回答2:


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};



回答3:


Maybe its because of output stream assigned to the terminal... Is it an option to run your mysqldump command inside the cmd? for example

String command = "cmd /K mysqldump..."

you might be also interested to read this article:

Hope, this helps




回答4:


this really work for restore

 String comando = "C:\\MySQL\\bin\\mysql.exe  --host=localhost --port=3306 --user=root --password=123 < D:\\back.sql";
File f = new File("restore.bat");
FileOutputStream fos = new FileOutputStream(f);
fos.write(comando.getBytes());
fos.close();
Process run = Runtime.getRuntime().exec("cmd /C start restore.bat ");  

and for backup

  DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.forLanguageTag("ru"));
    java.util.Date currentDate = new java.util.Date();
    Process p = null;
    try {
        Runtime runtime = Runtime.getRuntime();
        p = runtime.exec("C:\\MySQL\\bin\\mysqldump.exe  --default-character-set=utf8 -uroot -p123 -c  -B shch2 -r " + "D:/" + dateFormat.format(currentDate) + "_backup" + ".sql");

//change the dbpass and dbname with your dbpass and dbname int processComplete = p.waitFor();

        if (processComplete == 0) {

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

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


    } catch (Exception e) {
        e.printStackTrace();
    }

but you need to convey the exact path to mysql.exe and mysqldump.exe



来源:https://stackoverflow.com/questions/8470760/how-to-backup-and-restore-mysql-database-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!