You are here

function dba_auto_backup in Database Administration 5

1 call to dba_auto_backup()
dba_cron in ./dba.module

File

./dba.module, line 339
Allows administrators direct access to their Drupal database. Written by Jeremy Andrews <jeremy@kerneltrap.org>, June 2004. PostgreSQL functionality provided by AAM <aam@ugpl.de> Major security audit, porting, and maintenance by Derek…

Code

function dba_auto_backup() {
  $backup_started = time();
  $path = variable_get('dba_auto_backup_path', file_directory_temp());

  // See what tables (if any) the admin wants us to only backup
  // the schema, not the actual data.  we need it as an array, so we
  // lookup the setting as a string, then split() it into an array.
  $exclude_tables_str = variable_get('dba_auto_backup_exclude_tables', DBA_BACKUP_EXCLUDE);
  $exclude_tables = split('[ ,]', $exclude_tables_str);

  // Make sure we have permission to save our backup file.
  if (file_check_directory($path, FILE_CREATE_DIRECTORY)) {
    $database = dba_get_database();
    $filename = format_date(time(), 'custom', 'Y-md-Hi_') . variable_get('dba_default_filename', 'backup.sql');
    $backup = "-- Drupal dba.module database dump\n";
    $backup .= "--\n";
    $backup .= "-- Database: {$database}\n";
    $backup .= "-- Date: " . format_date(time(), 'large') . "\n\n";
    $tables = dba_get_tables();
    foreach ($tables as $table) {
      $backup .= dba_backup_table($table, TRUE, FALSE, in_array($table, $exclude_tables) ? FALSE : TRUE);
    }

    // Optionally bzip2 compress auto-backup file.
    if (variable_get('dba_auto_backup_bzip2', 0)) {
      $backup = bzcompress($backup, 9);
      $filename = $filename . '.bz2';
    }
    else {
      if (variable_get('dba_auto_backup_gzip', 0)) {
        if (version_compare(phpversion(), '4.2', '>=')) {
          $backup = gzencode($backup, 9, FORCE_GZIP);
        }
        else {
          $backup = gzencode($backup, FORCE_GZIP);
        }
        $filename = $filename . '.gz';
      }
    }
    if ($fp = fopen($path . "/{$filename}", 'wb')) {
      fwrite($fp, $backup);
      fclose($fp);
      variable_set('dba_auto_backup_last', $backup_started);

      // If enabled, email a copy of the backup to the site administrator.
      if (variable_get('dba_auto_backup_mail', 0)) {
        $attachment = new stdClass();
        $attachment->path = $path . "/{$filename}";
        $attachment->filename = $filename;
        dba_mail_backup($attachment);
      }
    }
  }
}