Index: trunk/backup/WikiBackup.py |
— | — | @@ -169,8 +169,8 @@ |
170 | 170 | |
171 | 171 | class Runner(object): |
172 | 172 | |
173 | | - def __init__(self, public, private, dblist, privatelist, dbserver, |
174 | | - dbuser, dbpassword, wikidir, php="php", webroot="", |
| 173 | + def __init__(self, public, private, dblist, privatelist, dbserver=None, |
| 174 | + dbuser="", dbpassword="", wikidir="", php="php", webroot="", |
175 | 175 | template=dirname(realpath(sys.modules[__module__].__file__)), |
176 | 176 | tmp="/tmp", adminmail=None, mailfrom="root@localhost", |
177 | 177 | mailserver="localhost", bzip2="bzip2", sevenzip="7za"): |
— | — | @@ -178,7 +178,8 @@ |
179 | 179 | self.private = private |
180 | 180 | self.dblist = dblist |
181 | 181 | self.privatelist = privatelist |
182 | | - self.dbserver = dbserver |
| 182 | + self.dbserverOverride = dbserver |
| 183 | + self.dbserver = None |
183 | 184 | self.dbuser = dbuser |
184 | 185 | self.dbpassword = dbpassword |
185 | 186 | self.wikidir = wikidir |
— | — | @@ -325,6 +326,17 @@ |
326 | 327 | line = proc.fromchild.readline() |
327 | 328 | return proc.wait() |
328 | 329 | |
| 330 | + def runAndReturn(self, command): |
| 331 | + """Run a command and return the output as a string. |
| 332 | + Raises BackupError on non-zero return code.""" |
| 333 | + proc = popen2.Popen4(command, 64) |
| 334 | + output = proc.fromchild.read() |
| 335 | + retval = proc.wait() |
| 336 | + if retval: |
| 337 | + raise BackupError("Non-zero return code from '%s'" % command) |
| 338 | + else: |
| 339 | + return output |
| 340 | + |
329 | 341 | def debug(self, stuff): |
330 | 342 | print "%s: %s %s" % (prettyTime(), self.db, stuff) |
331 | 343 | |
— | — | @@ -341,12 +353,24 @@ |
342 | 354 | self.debug("Creating %s ..." % dir) |
343 | 355 | os.makedirs(dir) |
344 | 356 | |
| 357 | + def selectDatabaseServer(self): |
| 358 | + if self.dbserverOverride: |
| 359 | + self.dbserver = self.dbserverOverride |
| 360 | + else: |
| 361 | + self.dbserver = self.defaultServer() |
| 362 | + |
| 363 | + def defaultServer(self): |
| 364 | + command = "%s -q %s/maintenance/getSlaveServer.php %s" % shellEscape(( |
| 365 | + self.php, self.wikidir, self.db)) |
| 366 | + return self.runAndReturn(command).strip() |
| 367 | + |
345 | 368 | def doBackup(self): |
346 | 369 | self.makeDir(self.publicDir()) |
347 | 370 | self.makeDir(self.privateDir()) |
348 | 371 | |
349 | 372 | self.status("Starting backup of %s" % self.db) |
350 | 373 | self.lock() |
| 374 | + self.selectDatabaseServer() |
351 | 375 | |
352 | 376 | self.items = [PrivateTable("user", "User account data."), |
353 | 377 | PrivateTable("watchlist", "Users' watchlist settings."), |