1 22 23 package org.continuent.sequoia.controller.backup.backupers; 24 25 import java.io.File ; 26 import java.io.FilenameFilter ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 31 import org.continuent.sequoia.common.exceptions.BackupException; 32 import org.continuent.sequoia.common.log.Trace; 33 import org.continuent.sequoia.controller.backend.DatabaseBackend; 34 import org.continuent.sequoia.controller.backup.BackupManager; 35 import org.continuent.sequoia.controller.backup.DumpTransferInfo; 36 37 74 public class PostgreSQLSplitPlainTextBackuper 75 extends AbstractPostgreSQLBackuper 76 { 77 static Trace logger = Trace 79 .getLogger(PostgreSQLSplitPlainTextBackuper.class 80 .getName()); 81 82 85 public static final String DUMP_FORMAT = "PostgreSQL Split Plain Text Dump"; 86 87 90 public String getDumpFormat() 91 { 92 return DUMP_FORMAT; 93 } 94 95 102 public Date backup(DatabaseBackend backend, String login, String password, 103 String dumpName, String path, ArrayList tables) throws BackupException 104 { 105 String url = backend.getURL(); 107 PostgreSQLUrlInfo info = new AbstractPostgreSQLBackuper.PostgreSQLUrlInfo( 108 url); 109 110 if (logger.isDebugEnabled()) 111 logger.debug("Backing up database '" + info.getDbName() + "' on host '" 112 + info.getHost() + ":" + info.getPort() + "'"); 113 114 try 115 { 116 117 File pathDir = new File (path); 119 if (!pathDir.exists()) 120 { 121 pathDir.mkdirs(); 122 pathDir.mkdir(); 123 } 124 125 String fullPath = getDumpPhysicalPath(path, dumpName); 126 127 int exitValue = -1; 128 if (useAuthentication) 129 { 130 if (logger.isDebugEnabled()) 131 logger.debug("Performing backup using authentication"); 132 133 String [] gCmd = makeSplitCommandWithAuthentication("pg_dump", info, 134 " | split -a 4 -b 10m - " + fullPath, login, password, false); 135 136 exitValue = executeNativeCommand(gCmd); 137 } 138 else 139 { 140 String [] gCmdArray = makeSplitCommand("pg_dump", info, 141 " | split -a 4 -b " + splitSize + " - " + fullPath, login); 142 143 exitValue = executeNativeCommand(gCmdArray); 144 } 145 146 if (exitValue != 0) 147 { 148 printErrors(); 149 throw new BackupException( 150 "pg_dump execution did not complete successfully!"); 151 } 152 153 } 154 catch (Exception e) 155 { 156 String msg = "Error while performing backup"; 157 logger.error(msg, e); 158 throw new BackupException(msg, e); 159 } 160 161 return new Date (System.currentTimeMillis()); 162 } 163 164 171 public void restore(DatabaseBackend backend, String login, String password, 172 String dumpName, String path, ArrayList tables) throws BackupException 173 { 174 String url = backend.getURL(); 176 PostgreSQLUrlInfo info = new AbstractPostgreSQLBackuper.PostgreSQLUrlInfo( 177 url); 178 179 if (logger.isDebugEnabled()) 180 logger.debug("Restoring database '" + info.getDbName() + "' on host '" 181 + info.getHost() + ":" + info.getPort() + "'"); 182 183 String fullPath = getDumpPhysicalPath(path, dumpName); 185 186 File dump = new File (fullPath + "aaaa"); if (!dump.exists()) 189 throw new BackupException("Backup '" + fullPath + "' does not exist!"); 190 191 try 192 { 193 if (useAuthentication) 194 { 195 if (logger.isInfoEnabled()) 196 logger.info("Performing database operations using authentication"); 197 198 if (logger.isDebugEnabled()) 200 logger.debug("Dropping database '" + info.getDbName() + "'"); 201 202 String [] dropCmd = makeCommandWithAuthentication("dropdb", info, "", 203 login, password, false); 204 if (executeNativeCommand(dropCmd) != 0) 205 { 206 printErrors(); 207 throw new BackupException( 208 "dropdb execution did not complete successfully!"); 209 } 210 211 if (logger.isDebugEnabled()) 213 logger.debug("Re-creating '" + info.getDbName() + "'"); 214 215 String [] createCmd = makeCommandWithAuthentication("createdb", info, 216 encoding != null ? "--encoding=" + encoding + " " : "", login, 217 password, false); 218 if (executeNativeCommand(createCmd) != 0) 219 { 220 printErrors(); 221 throw new BackupException( 222 "createdb execution did not complete successfully!"); 223 } 224 225 if (preRestoreScript != null) 227 { 228 if (logger.isDebugEnabled()) 229 logger.debug("Running pre-restore script '" + preRestoreScript 230 + "' on '" + info.getDbName() + "'"); 231 232 String [] preRestoreCmd = makeCommandWithAuthentication("psql", info, 233 " -f " + preRestoreScript, login, password, true); 234 235 if (executeNativeCommand(preRestoreCmd) != 0) 236 { 237 printErrors(); 238 throw new BackupException( 239 "psql execution did not complete successfully!"); 240 } 241 } 242 243 if (logger.isDebugEnabled()) 245 logger.debug("Rebuilding '" + info.getDbName() + "' from dump '" 246 + dumpName + "'"); 247 248 String [] replayCmd = makeSplitCommandWithAuthentication("cat" + " " 249 + fullPath + "* | psql", info, "", login, password, false); 250 251 if (executeNativeCommand(replayCmd) != 0) 252 { 253 printErrors(); 254 throw new BackupException( 255 "pg_restore execution did not complete successfully!"); 256 } 257 258 if (postRestoreScript != null) 260 { 261 if (logger.isDebugEnabled()) 262 logger.debug("Running post-restore script '" + postRestoreScript 263 + "' on '" + info.getDbName() + "'"); 264 265 String [] postRestoreCmd = makeCommandWithAuthentication("psql", info, 266 " -f " + postRestoreScript, login, password, true); 267 268 if (executeNativeCommand(postRestoreCmd) != 0) 269 { 270 printErrors(); 271 throw new BackupException( 272 "psql execution did not complete successfully!"); 273 } 274 } 275 } 276 else 277 { 279 if (logger.isDebugEnabled()) 281 logger.debug("Dropping database '" + info.getDbName() + "'"); 282 283 String dropCmd = makeCommand("dropdb", info, "", login); 284 if (executeNativeCommand(dropCmd) != 0) 285 { 286 printErrors(); 287 throw new BackupException( 288 "dropdb execution did not complete successfully!"); 289 } 290 291 if (logger.isDebugEnabled()) 293 logger.debug("Re-creating '" + info.getDbName() + "'"); 294 295 String createCmd = makeCommand("createdb", info, encoding != null 296 ? "--encoding=" + encoding + " " 297 : "", login); 298 if (executeNativeCommand(createCmd) != 0) 299 { 300 printErrors(); 301 throw new BackupException( 302 "createdb execution did not complete successfully!"); 303 } 304 305 if (preRestoreScript != null) 307 { 308 if (logger.isDebugEnabled()) 309 logger.debug("Running pre-restore script '" + preRestoreScript 310 + "' on '" + info.getDbName() + "'"); 311 312 String preRestoreCmd = makeCommand("psql", info, " -f " 313 + preRestoreScript, login); 314 if (executeNativeCommand(preRestoreCmd) != 0) 315 { 316 printErrors(); 317 throw new BackupException( 318 "psql execution did not complete successfully!"); 319 } 320 } 321 322 if (logger.isDebugEnabled()) 324 logger.debug("Rebuilding '" + info.getDbName() + "' from dump '" 325 + dumpName + "'"); 326 327 String [] cmdArray = makeSplitCommand("cat" + " " + fullPath 328 + "* | psql", info, "", login); 329 330 if (executeNativeCommand(cmdArray) != 0) 331 { 332 printErrors(); 333 throw new BackupException( 334 "psql execution did not complete successfully!"); 335 } 336 337 if (postRestoreScript != null) 339 { 340 if (logger.isDebugEnabled()) 341 logger.debug("Running post-restore script '" + postRestoreScript 342 + "' on '" + info.getDbName() + "'"); 343 344 String postRestoreCmd = makeCommand("psql", info, " -f " 345 + postRestoreScript, login); 346 if (executeNativeCommand(postRestoreCmd) != 0) 347 { 348 printErrors(); 349 throw new BackupException( 350 "psql execution did not complete successfully!"); 351 } 352 } 353 354 } 355 } 356 catch (Exception e) 357 { 358 String msg = "Error while performing backup"; 359 logger.error(msg, e); 360 throw new BackupException(msg, e); 361 } 362 } 363 364 368 public void fetchDump(DumpTransferInfo dumpTransferInfo, String path, 369 String dumpName) throws BackupException, IOException 370 { 371 BackupManager.fetchDumpFile(dumpTransferInfo, path, dumpName + ".sql"); 372 } 373 374 378 public void deleteDump(String path, String dumpName) throws BackupException 379 { 380 File dir = new File (path); 381 382 String [] files = dir.list(new DumpNameFilter(dumpName)); 384 385 for (int i = 0; i < files.length; i++) 386 { 387 File toRemove = new File (getDumpPhysicalPath(path, files[i])); 388 if (logger.isDebugEnabled()) 389 logger.debug("Deleting dump " + toRemove); 390 toRemove.delete(); 391 } 392 } 393 394 400 class DumpNameFilter implements FilenameFilter 401 { 402 private String dumpName; 403 404 409 public DumpNameFilter(String dumpName) 410 { 411 this.dumpName = dumpName; 412 } 413 414 420 public boolean accept(File dir, String theDump) 421 { 422 return (theDump.startsWith(dumpName) && theDump.length() == dumpName 423 .length() + 4); 424 } 425 } 426 427 } 428 | Popular Tags |