1 16 package org.outerj.daisy.backupTool.dbDump; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 21 public class PostgresqlDbDumper extends AbstractDbDumper { 22 private static String dumpExecutableName = "pg_dump"; 23 24 private static String restoreExecutableName = "psql"; 25 26 public PostgresqlDbDumper(String dbName, String host, Integer port, String password, String username) { 27 super(dbName, host, port, password, username); 28 } 29 30 public void dump(File dumpFile) throws Exception { 31 try { 32 System.out.println("Dumping database : " + dbName); 33 String command = dumpExecutableName + " -U " + this.username + " --host=" + this.host + " --file=" + dumpFile.getPath() 34 + (port.intValue() > 0 ? " --port=" + port : "") + " --create --oids --no-owner " + this.dbName; 35 36 Process dumpProcess = Runtime.getRuntime().exec(command); 37 handleRuntimeProcess(dumpProcess, null, System.out); 38 } catch (IOException e) { 39 throw new Exception ("The " + dumpExecutableName + " command was not found. Try putting this executable in your environments path variable."); 40 } 41 } 42 43 public void restore(File dumpFile) throws Exception { 44 try { 45 System.out.println("Restoring database : " + dbName + "\nYou may be prompted for the password of the following database user twice : " + username); 46 String dropCommand = "dropdb -U " + username + " --host " + host + (port.intValue() > 0 ? " --port=" + port : "") + " --quiet " + dbName; 47 Process dropProcess = Runtime.getRuntime().exec(dropCommand); 48 handleRuntimeProcess(dropProcess, null, System.out); 49 50 String command = restoreExecutableName + " --username " + this.username + " --host " + this.host + " --file " + dumpFile.getPath() 51 + (port.intValue() > 0 ? " --port " + port : "") + " --quiet template1"; 52 Process restoreProcess = Runtime.getRuntime().exec(command); 53 handleRuntimeProcess(restoreProcess, null, System.out); 54 } catch (IOException e) { 55 throw new Exception ("The " + restoreExecutableName + " command was not found. Try putting this executable in your environments path variable."); 56 } 57 } 58 } | Popular Tags |