1 21 22 package org.continuent.sequoia.controller.backup.backupers; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.ArrayList ; 27 import java.util.Date ; 28 29 import org.continuent.sequoia.common.exceptions.BackupException; 30 import org.continuent.sequoia.common.log.Trace; 31 import org.continuent.sequoia.controller.backend.DatabaseBackend; 32 import org.continuent.sequoia.controller.backup.BackupManager; 33 import org.continuent.sequoia.controller.backup.DumpTransferInfo; 34 35 52 public class PostgreSQLTarBackuper extends AbstractPostgreSQLBackuper 53 { 54 static Trace logger = Trace 56 .getLogger(PostgreSQLTarBackuper.class 57 .getName()); 58 59 62 public static final String DUMP_FORMAT = "PostgreSQL Tar Dump"; 63 64 67 public String getDumpFormat() 68 { 69 return DUMP_FORMAT; 70 } 71 72 76 public Date backup(DatabaseBackend backend, String login, String password, 77 String dumpName, String path, ArrayList tables) throws BackupException 78 { 79 String url = backend.getURL(); 81 PostgreSQLUrlInfo info = new AbstractPostgreSQLBackuper.PostgreSQLUrlInfo( 82 url); 83 84 if (logger.isDebugEnabled()) 85 logger.debug("Backing up database '" + info.getDbName() + "' on host '" 86 + info.getHost() + ":" + info.getPort() + "'"); 87 88 try 89 { 90 File pathDir = new File (path); 92 if (!pathDir.exists()) 93 { 94 pathDir.mkdirs(); 95 pathDir.mkdir(); 96 } 97 String fullPath = getDumpPhysicalPath(path, dumpName) + ".tar"; 98 99 int exitValue = -1; 100 String dumpOptions = ""; 101 if (pgDumpFlags != null) 102 { 103 if (pgDumpFlags.indexOf("-F") >= 0 104 || pgDumpFlags.indexOf("--format=") >= 0) 105 { 106 logger.error("Invalid option in pgDumpFlags \"" + pgDumpFlags 107 + "\". You are not allowed to set the format of the dump!"); 108 } 109 else 110 { 111 dumpOptions = " " + pgDumpFlags + " "; 112 } 113 } 114 if (useAuthentication) 115 { 116 if (logger.isDebugEnabled()) 117 logger.debug("Performing backup using authentication"); 118 int timeout = -1; 119 if (dumpTimeout != null) 120 { 121 try 122 { 123 timeout = Integer.parseInt(dumpTimeout); 124 } 125 catch (NumberFormatException e) 126 { 127 logger.error("\"" + dumpTimeout 128 + "\" is not a valid dump-timeout value!"); 129 timeout = -1; 130 } 131 } 132 String [] expectFeed = makeExpectDialogueWithAuthentication("pg_dump", 133 info, " --format=t -f " + fullPath + dumpOptions, login, password, 134 timeout); 135 136 String [] cmd = makeExpectCommandReadingStdin(); 137 138 exitValue = executeNativeCommand(expectFeed, cmd); 139 } 140 else 141 { 142 if (pgDumpFlags != null) 143 { 144 if (pgDumpFlags.indexOf("-U") >= 0 || pgDumpFlags.indexOf("-W") >= 0) 145 { 146 logger 147 .error("Invalid option in pgDumpFlags \"" 148 + pgDumpFlags 149 + "\". Set \"authentication=true\" if you want to use authentication!"); 150 } 151 else 152 { 153 dumpOptions = " " + pgDumpFlags + " "; 154 } 155 } 156 157 String cmd = makeCommand("pg_dump", info, " --format=t -f " + fullPath 158 + dumpOptions, login); 159 exitValue = executeNativeCommand(cmd); 160 } 161 162 if (exitValue != 0) 163 { 164 printErrors(); 165 throw new BackupException( 166 "pg_dump execution did not complete successfully!"); 167 } 168 169 } 170 catch (Exception e) 171 { 172 String msg = "Error while performing backup"; 173 logger.error(msg, e); 174 throw new BackupException(msg, e); 175 } 176 177 return new Date (System.currentTimeMillis()); 178 } 179 180 184 public void restore(DatabaseBackend backend, String login, String password, 185 String dumpName, String path, ArrayList tables) throws BackupException 186 { 187 String url = backend.getURL(); 189 PostgreSQLUrlInfo info = new AbstractPostgreSQLBackuper.PostgreSQLUrlInfo( 190 url); 191 192 if (logger.isDebugEnabled()) 193 logger.debug("Restoring database '" + info.getDbName() + "' on host '" 194 + info.getHost() + ":" + info.getPort() + "'"); 195 196 String fullPath = getDumpPhysicalPath(path, dumpName) + ".tar"; 198 File dump = new File (fullPath); 199 if (!dump.exists()) 200 throw new BackupException("Backup '" + fullPath + "' does not exist!"); 201 202 try 203 { 204 if (useAuthentication) 205 { 206 if (logger.isInfoEnabled()) 207 logger.info("Performing database operations using authentication"); 208 209 if (logger.isDebugEnabled()) 211 logger.debug("Dropping database '" + info.getDbName() + "'"); 212 213 String [] expectFeed = makeExpectDialogueWithAuthentication("dropdb", 214 info, "", login, password, 60); 216 String [] cmd = makeExpectCommandReadingStdin(); 217 218 if (executeNativeCommand(expectFeed, cmd) != 0) 219 { 220 printErrors(); 221 throw new BackupException( 222 "dropdb execution did not complete successfully!"); 223 } 224 225 if (logger.isDebugEnabled()) 227 logger.debug("Re-creating '" + info.getDbName() + "'"); 228 229 expectFeed = makeExpectDialogueWithAuthentication("createdb", info, 230 encoding != null ? "--encoding=" + encoding + " " : "", login, 231 password, 60); 233 cmd = makeExpectCommandReadingStdin(); 234 235 if (executeNativeCommand(expectFeed, cmd) != 0) 236 { 237 printErrors(); 238 throw new BackupException( 239 "createdb execution did not complete successfully!"); 240 } 241 242 if (preRestoreScript != null) 244 { 245 if (logger.isDebugEnabled()) 246 logger.debug("Running pre-restore script '" + preRestoreScript 247 + "' on '" + info.getDbName() + "'"); 248 249 expectFeed = makeExpectDialogueWithAuthentication("psql", info, 250 "--pset pager -f " + preRestoreScript, login, password, 300); 254 cmd = makeExpectCommandReadingStdin(); 255 256 if (executeNativeCommand(expectFeed, cmd) != 0) 257 { 258 printErrors(); 259 throw new BackupException( 260 "psql execution did not complete successfully!"); 261 } 262 } 263 264 if (logger.isDebugEnabled()) 266 logger.debug("Rebuilding '" + info.getDbName() + "' from dump '" 267 + dumpName + "'"); 268 269 int timeout = -1; 270 if (restoreTimeout != null) 271 { 272 try 273 { 274 timeout = Integer.parseInt(restoreTimeout); 275 } 276 catch (NumberFormatException e) 277 { 278 logger.error("\"" + restoreTimeout 279 + "\" is not a valid restore-timeout value!"); 280 timeout = -1; 281 } 282 } 283 284 expectFeed = makeExpectDialogueWithAuthentication("pg_restore", info, 285 "--format=t -d " + info.getDbName() + " " + fullPath, login, 286 password, timeout); 288 cmd = makeExpectCommandReadingStdin(); 289 290 if (executeNativeCommand(expectFeed, cmd) != 0) 291 { 292 printErrors(); 293 throw new BackupException( 294 "pg_restore execution did not complete successfully!"); 295 } 296 297 if (postRestoreScript != null) 299 { 300 if (logger.isDebugEnabled()) 301 logger.debug("Running post-restore script '" + postRestoreScript 302 + "' on '" + info.getDbName() + "'"); 303 304 expectFeed = makeExpectDialogueWithAuthentication("psql", info, 305 "--pset pager -f " + postRestoreScript, login, password, 300); 309 cmd = makeExpectCommandReadingStdin(); 310 311 if (executeNativeCommand(expectFeed, cmd) != 0) 312 { 313 printErrors(); 314 throw new BackupException( 315 "psql execution did not complete successfully!"); 316 } 317 } 318 319 } 320 else 321 { 323 if (logger.isDebugEnabled()) 325 logger.debug("Dropping database '" + info.getDbName() + "'"); 326 327 String dropCmd = makeCommand("dropdb", info, "", login); 328 if (executeNativeCommand(dropCmd) != 0) 329 { 330 printErrors(); 331 throw new BackupException( 332 "dropdb execution did not complete successfully!"); 333 } 334 335 if (logger.isDebugEnabled()) 337 logger.debug("Re-creating '" + info.getDbName() + "'"); 338 339 String createCmd = makeCommand("createdb", info, encoding != null 340 ? "--encoding=" + encoding + " " 341 : "", login); 342 if (executeNativeCommand(createCmd) != 0) 343 { 344 printErrors(); 345 throw new BackupException( 346 "createdb execution did not complete successfully!"); 347 } 348 349 if (preRestoreScript != null) 351 { 352 if (logger.isDebugEnabled()) 353 logger.debug("Running pre-restore script '" + preRestoreScript 354 + "' on '" + info.getDbName() + "'"); 355 356 String preRestoreCmd = makeCommand("psql", info, "--pset pager -f " 357 + preRestoreScript, login); 358 if (executeNativeCommand(preRestoreCmd) != 0) 359 { 360 printErrors(); 361 throw new BackupException( 362 "psql execution did not complete successfully!"); 363 } 364 } 365 366 if (logger.isDebugEnabled()) 368 logger.debug("Rebuilding '" + info.getDbName() + "' from dump '" 369 + dumpName + "'"); 370 371 String replayCmd = makeCommand("pg_restore", info, "--format=t -d " 372 + info.getDbName() + " " + fullPath, login); 373 if (executeNativeCommand(replayCmd) != 0) 374 { 375 printErrors(); 376 throw new BackupException( 377 "pg_restore execution did not complete successfully!"); 378 } 379 380 if (postRestoreScript != null) 382 { 383 if (logger.isDebugEnabled()) 384 logger.debug("Running post-restore script '" + postRestoreScript 385 + "' on '" + info.getDbName() + "'"); 386 387 String postRestoreCmd = makeCommand("psql", info, "--pset pager -f " 388 + postRestoreScript, login); 389 if (executeNativeCommand(postRestoreCmd) != 0) 390 { 391 printErrors(); 392 throw new BackupException( 393 "psql execution did not complete successfully!"); 394 } 395 } 396 397 } 398 } 399 catch (Exception e) 400 { 401 String msg = "Error while performing backup"; 402 logger.error(msg, e); 403 throw new BackupException(msg, e); 404 } 405 } 406 407 411 public void fetchDump(DumpTransferInfo dumpTransferInfo, String path, 412 String dumpName) throws BackupException, IOException 413 { 414 BackupManager.fetchDumpFile(dumpTransferInfo, path, dumpName + ".tar"); 415 } 416 417 421 public void deleteDump(String path, String dumpName) throws BackupException 422 { 423 super.deleteDump(path, dumpName + ".tar"); 424 } 425 426 } 427 | Popular Tags |