MySql备份转储文件未恢复到MySQL数据库中

时间:2014-04-30 10:35:39

标签: java mysql

我使用Java代码创建了MySQL备份转储文件。在将转储文件恢复到MySQL Administrator时,它会给出MySQL错误:

  

所选文件由mysqldump生成,无法通过此应用程序恢复。

在我的代码下面创建MySQL转储文件:

public class MysqlBackup  {
    public boolean backupDataWithDatabase(String dumpExePath, String host, String port, String user, String password, String database, String backupPath) {
        boolean status = false;
        try {
            Process p = null;                
            DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            Date date = new Date();               
            String filepath = "backup(with_DB)-" + database + "-" + host + "-(" + dateFormat.format(date) + ").sql";               
            String batchCommand = "";              
            if (password != "") {                   
                batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + "< --password=" + password + " --add-drop-database -B " + database + " -r \"" + backupPath + "" + filepath + "\"";                   
            } else {                  
                batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --add-drop-database -B " + database + " -r \"" + backupPath + "" + filepath + "\"";
                             }

            Runtime runtime = Runtime.getRuntime();
            p = runtime.exec(batchCommand);             
            int processComplete = p.waitFor();             
            if (processComplete == 0) {                   
                FileInputStream in = new FileInputStream(backupPath + "" + filepath);                 
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream("D:\\desktop13\\serverdb.zip"));                   
                out.putNextEntry(new ZipEntry("serverdb.sql"));                 
                byte[] b = new byte[1024];
                int count;
                while ((count = in.read(b)) > 0) {                     
                    out.write(b, 0, count);
                }
                out.close();
                in.close();                  
                status = true;                   
            } else {
                status = false;                 
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return status;
    }
    public static void main(String[] args) {            
           MysqlBackup b = new MysqlBackup();
        b.backupDataWithDatabase("D:\\xampp1\\mysql\\bin\\mysqldump.exe", "localhost", "3306", "root", "", "serverdb", "D:\\desktop13");
    }
    }

0 个答案:

没有答案