Example #1
0
 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;
 }