| 321 | /** \fn DBUtil::DoBackup(QString, QString) |
| 322 | * \brief Creates a backup of the database by executing the backupScript. |
| 323 | * |
| 324 | * This function executes the specified backup script to create a database |
| 325 | * backup. This is the preferred approach for creating the backup. |
| 326 | */ |
| 327 | bool DBUtil::DoBackup(QString backupScript, QString &filename) |
| 328 | { |
| 329 | DatabaseParams dbParams = gContext->GetDatabaseParams(); |
| 330 | QString dbSchemaVer = gContext->GetSetting("DBSchemaVer"); |
| 331 | QString backupDirectory = GetBackupDirectory(); |
| 332 | QString backupFilename = CreateBackupFilename(dbParams.dbName + "-" + |
| 333 | dbSchemaVer, ".sql"); |
| 334 | QString scriptArgs = gContext->GetSetting("BackupDBScriptArgs"); |
| 335 | |
| 336 | // Create a temporary file containing the database information |
| 337 | QString tempDatabaseConfFile = createTempFile( |
| 338 | "/tmp/mythtv_db_backup_conf_XXXXXX"); |
| 339 | FILE *fp; |
| 340 | if (!(fp = fopen(tempDatabaseConfFile.ascii(), "w"))) |
| 341 | { |
| 342 | VERBOSE(VB_IMPORTANT, LOC_ERR + QString("Unable to create temporary " |
| 343 | "configuration file for creating DB backup: %1") |
| 344 | .arg(tempDatabaseConfFile.ascii())); |
| 345 | VERBOSE(VB_IMPORTANT, LOC_ERR + "Attempting backup, anyway."); |
| 346 | tempDatabaseConfFile = ""; |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | chmod(tempDatabaseConfFile.ascii(), S_IRUSR); |
| 351 | fprintf(fp, QString("DBHostName=%1\n" |
| 352 | "DBPort=%2\n") |
| 353 | .arg(dbParams.dbHostName).arg(dbParams.dbPort)); |
| 354 | fprintf(fp, QString("DBUserName=%1\n" |
| 355 | "DBPassword=%2\n") |
| 356 | .arg(dbParams.dbUserName).arg(dbParams.dbPassword)); |
| 357 | fprintf(fp, QString("DBName=%1\n" |
| 358 | "DBSchemaVer=%2\n") |
| 359 | .arg(dbParams.dbName).arg(dbSchemaVer)); |
| 360 | fprintf(fp, QString("DBBackupDirectory=%1\n" |
| 361 | "DBBackupFilename=%2\n") |
| 362 | .arg(backupDirectory) |
| 363 | .arg(backupFilename)); |
| 364 | if (fclose(fp)) |
| 365 | VERBOSE(VB_IMPORTANT, LOC_ERR + QString("Error closing %1: %2") |
| 366 | .arg(tempDatabaseConfFile.ascii()).arg(strerror(errno))); |
| 367 | } |
| 368 | |
| 369 | if (!scriptArgs.isEmpty()) |
| 370 | { |
| 371 | scriptArgs.prepend(" "); |
| 372 | } |
| 373 | |
| 374 | QString command = backupScript + scriptArgs + " " + tempDatabaseConfFile; |
| 375 | VERBOSE(VB_IMPORTANT, QString("Backing up database with script: %1") |
| 376 | .arg(backupScript)); |
| 377 | uint status = system(command.ascii()); |
| 378 | |
| 379 | unlink(tempDatabaseConfFile.ascii()); |
| 380 | |
| 381 | if (status) |
| 382 | { |
| 383 | VERBOSE(VB_IMPORTANT, LOC_ERR + |
| 384 | QString("Error backing up database: %1 (%2)") |
| 385 | .arg(command.ascii()).arg(status)); |
| 386 | filename = "__FAILED__"; |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | VERBOSE(VB_IMPORTANT, "Database Backup complete."); |
| 391 | |
| 392 | QDir dir(backupDirectory, backupFilename + "*"); |
| 393 | uint numfiles = dir.count(); |
| 394 | if (numfiles < 1) |
| 395 | { |
| 396 | // If no file begins with the suggested filename, don't show the backup |
| 397 | // filename in the GUI message -- the script probably used some other |
| 398 | // filename |
| 399 | filename = ""; |
| 400 | VERBOSE(VB_FILE, LOC_ERR + QString("No files beginning with the " |
| 401 | "suggested database backup filename (%1) were found in %2.") |
| 402 | .arg(backupFilename).arg(backupDirectory)); |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | filename = dir.path() + "/" + dir[0];; |
| 407 | if (numfiles > 1) |
| 408 | VERBOSE(VB_FILE, LOC_ERR + QString("Multiple files beginning with " |
| 409 | "the suggested database backup filename (%1) were found " |
| 410 | "in %2. Assuming the first is the backup.") |
| 411 | .arg(backupFilename).arg(backupDirectory)); |
| 412 | } |
| 413 | if (!filename.isEmpty()) |
| 414 | VERBOSE(VB_IMPORTANT, QString("Backed up database to file: %1") |
| 415 | .arg(filename)); |
| 416 | return true; |
| 417 | } |
| 418 | |