public static function backup($database, $tables, $filename, $structure = true, $data = true, $drop = true, $compress = true, $full = false) { if (!is_array($tables) || empty($tables)) { trigger_error('No tables to backup', E_USER_WARNING); return false; } $crlf = "\n"; $esc = SQL_LAYER == 'postgresql' ? '--' : '#'; # doing some DOS-CRLF magic... # this looks better under WinX if (preg_match('#[^(]*\\((.*)\\)[^)]*#', $_SERVER['HTTP_USER_AGENT'], $regs)) { if (false !== stripos($regs[1], 'Win')) { $crlf = "\r\n"; } } if (GZIPSUPPORT) { while (ob_end_clean()) { } header('Content-Encoding: '); } else { $compress = false; } if ($compress) { $filename .= '.gz'; header("Content-Type: application/x-gzip; name=\"{$filename}\""); } else { header("Content-Type: text/x-delimtext; name=\"{$filename}\""); } header("Content-disposition: attachment; filename={$filename}"); DBCtrl::output("{$esc} ========================================================{$crlf}" . "{$esc}{$crlf}" . "{$esc} Database : {$database}{$crlf}" . "{$esc} " . _ON . " " . formatDateTime(time(), _DATESTRING) . " !{$crlf}" . "{$esc}{$crlf}" . "{$esc} ========================================================{$crlf}" . "{$crlf}", $compress); set_time_limit(0); if (SQL_LAYER == 'mysql') { $database = "`{$database}`"; } foreach ($tables as $table) { if ($structure) { DBCtrl::output("{$crlf}{$esc}{$crlf}" . "{$esc} Table structure for table '{$table}'{$crlf}" . "{$esc}{$crlf}{$crlf}", $compress); DBCtrl::output(SQLCtrl::get_table_struct($database, $table, $crlf, $drop) . ";{$crlf}{$crlf}", $compress); } if ($data) { DBCtrl::output("{$crlf}{$esc}{$crlf}" . "{$esc} Dumping data for table '{$table}'{$crlf}" . "{$esc}{$crlf}{$crlf}", $compress); SQLCtrl::get_table_content($database, $table, $crlf, false, true, $compress); } } if ($compress) { DBCtrl::output('', true, true); } exit; }
public static function backup($database, $tables, $filename, $structure = true, $data = true, $drop = true, $compress = true, $full = false) { global $db; $schema = $database; if (!is_array($tables) || empty($tables)) { trigger_error('No tables to backup', E_USER_WARNING); return false; } $crlf = "\n"; $current_user = $db->sql_ufetchrowset('SELECT CURRENT_USER', SQL_NUM); $current_user = $current_user[0][0]; //$search_path = $db->sql_ufetchrowset('SELECT current_schemas(true)'); //$search_path = preg_replace('#^{(.*?)}$#', '\\1', $search_path[0][0]); # doing some DOS-CRLF magic... # this looks better under WinX if (preg_match('#[^(]*\\((.*)\\)[^)]*#', $_SERVER['HTTP_USER_AGENT'], $regs)) { if (false !== stripos($regs[1], 'Win')) { $crlf = "\r\n"; } } if (GZIPSUPPORT) { while (ob_end_clean()) { } header('Content-Encoding: '); } else { $compress = false; } if ($compress) { $filename .= '.gz'; header("Content-Type: application/x-gzip; name=\"{$filename}\""); } else { header("Content-Type: text/x-delimtext; name=\"{$filename}\""); } header("Content-disposition: attachment; filename={$filename}"); $controls = "--{$crlf}-- PostgreSQL dump : {$database}{$crlf}" . "-- " . _ON . " " . formatDateTime(time(), _DATESTRING) . " !{$crlf}--{$crlf}{$crlf}" . "SET client_encoding = '" . pg_client_encoding() . "';{$crlf}" . "SET check_function_bodies = false;{$crlf}" . "SET SESSION AUTHORIZATION '{$current_user}';{$crlf}{$crlf}"; if ($full) { if ($drop) { $controls .= 'DROP SCHEMA ' . $schema . ' CASCADE;' . $crlf; } $controls .= "CREATE SCHEMA {$schema} AUTHORIZATION {$current_user};{$crlf}" . "REVOKE ALL ON SCHEMA {$schema} FROM PUBLIC;{$crlf}" . 'ALTER USER ' . $current_user . ' SET search_path TO ' . $schema . ";{$crlf}" . "{$crlf}"; } DBCtrl::output($controls, $compress); set_time_limit(0); if ($drop && !$full) { SQLCtrl::drop_table_struct($schema, $tables, $crlf, $compress); } if ($structure) { if ($full) { DBCtrl::output(SQLCtrl::get_function($schema, $crlf), $compress); } SQLCtrl::get_table_struct($schema, $tables, $crlf, $compress); } if ($data) { SQLCtrl::get_table_content($schema, $tables, $crlf, false, $compress); } if ($structure) { SQLCtrl::get_index($schema, $tables, $crlf, $compress); DBCtrl::output(SQLCtrl::get_sequence($schema, $tables, $crlf, $full), $compress); } DBCtrl::output($crlf . 'VACUUM ANALYZE;', $compress); if ($compress) { DBCtrl::output('', true, true); } exit; }