You are here

function db_maintenance_do_db_backup in DB Maintenance 5.2

Same name and namespace in other branches
  1. 6.2 db_maintenance.module \db_maintenance_do_db_backup()
1 call to db_maintenance_do_db_backup()
db_maintenance_cron in ./db_maintenance.module
Implementation of hook_cron().

File

./db_maintenance.module, line 408
Optimizes database tables during cron runs.

Code

function db_maintenance_do_db_backup() {
  global $db_url;
  $mysqldump = variable_get('db_maintenance_path_to_mysqldump', '/usr/bin/mysqldump');
  $backupdir = variable_get('db_maintenance_backup_directory', '/tmp');
  $dateformat = 'Ymd_H-i-s';
  $dbname = db_maintenance_get_db_info('dbname');
  $now = time();
  $date = date($dateformat, $now);
  $filename = "{$backupdir}/{$date}_{$dbname}_db.sql";
  $mysqloptions = db_maintenance_get_mysql_options();
  $mysqldumpexec = "{$mysqldump} {$mysqloptions} > {$filename}";
  if (is_dir($backupdir)) {
    exec($mysqldumpexec, $output, $return);

    // $return is return value of exec'd command (0 is okay status)
    if (!$return && !is_file($filename)) {
      watchdog('db_maintenance', $output, WATCHDOG_ERROR);
      return FALSE;
    }
    else {
      return $filename;
    }
  }
  else {
    watchdog('db_maintenance', t('backup directory does not exist'), WATCHDOG_ERROR);
    return FALSE;
  }
}