1 23 24 package org.objectweb.cjdbc.controller.backup; 25 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 30 import org.objectweb.cjdbc.common.exceptions.BackupException; 31 import org.objectweb.cjdbc.common.log.Trace; 32 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 33 34 47 public class PostgreSQLPlainTextBackuper extends AbstractPostgreSQLBackuper 48 { 49 static Trace logger = Trace.getLogger(PostgreSQLPlainTextBackuper.class 51 .getName()); 52 53 58 public PostgreSQLPlainTextBackuper() 59 { 60 } 61 62 66 public String getDumpFormat() 67 { 68 return "PostgreSQL Plain Text Dump"; 69 } 70 71 77 public Date backup(DatabaseBackend backend, String login, String password, 78 String dumpName, String path, ArrayList tables) throws BackupException 79 { 80 81 String url = backend.getURL(); 83 String host = getHostFromURL(url); 84 String port = getPortFromURL(url); 85 String dbName = getDatabaseNameFromURL(url); 86 87 if (logger.isDebugEnabled()) 88 logger.debug("Backing up database '" + dbName + "' on host '" + host 89 + ":" + port + "'"); 90 91 try 92 { 93 File pathDir = new File (path); 95 if (!pathDir.exists()) 96 { 97 pathDir.mkdirs(); 98 pathDir.mkdir(); 99 } 100 101 String fullPath = getDumpPhysicalPath(path, dumpName) + ".sql"; 102 103 Runtime runtime = Runtime.getRuntime(); 105 String [] cmd = {"pg_dump", "-f", fullPath, "-h", host, "-p", port, "-U", 106 login, dbName}; 107 108 Process process = runtime.exec(cmd); 109 process.waitFor(); 110 if (process.exitValue() != 0) 111 throw new BackupException( 112 "pg_dump execution did not complete successfully!"); 113 } 114 catch (Exception e) 115 { 116 String msg = "Error while performing backup"; 117 logger.error(msg, e); 118 throw new BackupException(msg, e); 119 } 120 121 return new Date (System.currentTimeMillis()); 122 } 123 124 130 public void restore(DatabaseBackend backend, String login, String password, 131 String dumpName, String path, ArrayList tables) throws BackupException 132 { 133 String url = backend.getURL(); 135 String host = getHostFromURL(url); 136 String port = getPortFromURL(url); 137 String dbName = getDatabaseNameFromURL(url); 138 139 if (logger.isDebugEnabled()) 140 logger.debug("Restoring database '" + dbName + "' on host '" + host + ":" 141 + port + "'"); 142 143 String fullPath = getDumpPhysicalPath(path, dumpName) + ".sql"; 145 File dump = new File (fullPath); 146 if (!dump.exists()) 147 throw new BackupException("Backup '" + fullPath + "' does not exist!"); 148 149 try 150 { 151 Runtime runtime = Runtime.getRuntime(); 152 Process process = null; 153 154 if (logger.isDebugEnabled()) 155 logger.debug("Dropping database '" + dbName + "'..."); 156 157 String [] dropCmd = {"dropdb", "-h", host, "-p", port, "-U", login, dbName}; 159 process = runtime.exec(dropCmd); 160 process.waitFor(); 161 if (process.exitValue() != 0) 162 throw new BackupException( 163 "dropdb execution did not complete successfully!"); 164 165 if (optionsMap.containsKey("encoding")) 167 { 168 String encoding = (String ) optionsMap.get("encoding"); 169 170 if (logger.isDebugEnabled()) 171 logger.debug("Creating databse '" + dbName + "' with encoding '" 172 + encoding + "'"); 173 174 String [] createCmd = {"createdb", "-h", host, "-p", port, "-U", login, 175 "--encoding=" + encoding, dbName}; 176 process = runtime.exec(createCmd); 177 process.waitFor(); 178 if (process.exitValue() != 0) 179 throw new BackupException( 180 "createdb execution did not complete successfully!"); 181 } 182 else 183 { 184 if (logger.isDebugEnabled()) 185 logger.debug("Creating databse '" + dbName + "'"); 186 187 String [] createCmd = {"createdb", "-h", host, "-p", port, "-U", login, 188 dbName}; 189 process = runtime.exec(createCmd); 190 process.waitFor(); 191 if (process.exitValue() != 0) 192 throw new BackupException( 193 "createdb execution did not complete successfully!"); 194 } 195 196 if (logger.isDebugEnabled()) 197 logger 198 .debug("Rebuilding '" + dbName + "' from dump '" + dumpName + "'"); 199 200 String [] replayCmd = {"psql", "-h", host, "-p", port, "-U", login, "-f", fullPath, dbName}; 202 process = runtime.exec(replayCmd); 203 process.waitFor(); 204 if (process.exitValue() != 0) 205 throw new BackupException( 206 "psql execution did not complete successfully!"); 207 } 208 catch (Exception e) 209 { 210 String msg = "Error while performing backup"; 211 logger.error(msg, e); 212 throw new BackupException(msg, e); 213 } 214 } 215 } 216 | Popular Tags |