You are here

function dba_backup_table in Database Administration 5

Backup table to file.

Related topics

2 calls to dba_backup_table()
dba_auto_backup in ./dba.module
dba_verify_backup_form_submit in ./dba.module

File

./dba.module, line 1412
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_backup_table($table, $add_drop_table, $verbose = TRUE, $data = TRUE) {
  $output = "--\n";
  $output .= "-- Table structure for table '{$table}'\n";
  $output .= "--\n\n";
  if ($add_drop_table) {
    $output .= "DROP TABLE IF EXISTS {$table};\n";
  }
  $create = dba_show_create_table($table);
  $output .= $create['Create Table'];
  $output .= ";\n\n";
  if (!$data) {

    // Backup schema only for this table.
    return $output;
  }
  $output .= "--\n";
  $output .= "-- Dumping data for table '{$table}'\n";
  $output .= "--\n\n";
  if ($verbose) {
    echo $output;
    $output = NULL;
  }
  $result = db_query("select * from {$table}");
  $numrow = db_num_rows($result);
  $fields = dba_get_fields($table);
  $num_fields = sizeof($fields);
  while ($row = db_fetch_array($result)) {
    $line = "INSERT INTO {$table} VALUES(";
    $i = 0;
    foreach ($row as $value) {
      $value = db_escape_string($value);
      $value = ereg_replace("\n", "\\n", $value);
      $line .= isset($value) ? "\"{$value}\"" : "\"\"";
      $line .= ++$i < $num_fields ? ',' : ");\n";
    }
    $output .= $line;
    if ($verbose) {
      echo $output;
      $output = NULL;
    }
  }
  return $output;
}