1 16 package org.outerj.daisy.backupTool.dbDump; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 public class MysqlDbDumper extends AbstractDbDumper { 24 25 private static String dumpExecutableName = "mysqldump"; 26 27 private static String restoreExecutableName = "mysql"; 28 29 public MysqlDbDumper(String dbName, String host, Integer port, String password, String username) { 30 super(dbName, host, port, password, username); 31 } 32 33 public void dump(File dumpFile) throws Exception { 34 try { 35 String command = dumpExecutableName + " --single-transaction" + " --databases " + " --user=" + this.username + " --password=" + this.password 36 + " --host=" + this.host + " --result-file=" + dumpFile.getPath() + (port.intValue() > 0 ? " --port=" + port : "") + " " + this.dbName; 37 Process dumpProcess = Runtime.getRuntime().exec(command); 38 39 handleRuntimeProcess(dumpProcess, null, System.out); 40 41 } catch (IOException e) { 42 throw new Exception ("The " + dumpExecutableName + " command was not found. Try putting this executable in your environments path variable."); 43 } 44 } 45 46 public void restore(File dumpFile) throws Exception { 47 int returnCode; 48 49 String dropCommand = "mysqladmin -f --user=" + username + " --password=" + password + " -h " + host + (port.intValue() > 0 ? " --port=" + port : "") + " drop " + dbName; 50 System.out.println("Running " + dropCommand); 51 Process dropProcess = Runtime.getRuntime().exec(dropCommand); 52 returnCode = dropProcess.waitFor(); 53 if (returnCode != 0) 54 throw new Exception ("Database drop command return with non-zero code: " + returnCode); 55 56 String createCommand = "mysqladmin -f --user=" + username + " --password=" + password + " -h " + host + (port.intValue() > 0 ? " --port=" + port : "") + " create " + dbName; 57 System.out.println("Running " + createCommand); 58 Process createProcess = Runtime.getRuntime().exec(createCommand); 59 returnCode = createProcess.waitFor(); 60 if (returnCode != 0) 61 throw new Exception ("Database create command return with non-zero code: " + returnCode); 62 63 String command = restoreExecutableName + " --user=" + this.username + " --password=" + this.password + " --host=" + this.host 64 + (port.intValue() > 0 ? " --port=" + port : "") + " " + this.dbName; 65 66 System.out.println("Running " + command); 67 System.out.println("and piping database script into it."); 68 Process restoreProcess = Runtime.getRuntime().exec(command); 69 70 InputStream fis = null; 71 72 try { 73 fis = new FileInputStream (dumpFile); 74 handleRuntimeProcess(restoreProcess, fis, System.out); 75 } finally { 76 if (fis != null) 77 fis.close(); 78 } 79 } 80 } | Popular Tags |