Пример #1
0
 function addBackupJob($job)
 {
     $this->closeConnect();
     // Создаем новое задание
     $this->JOB = $job;
     mysql_select_db($this->JOB['db']);
     // Создаем список объектов и фильтр
     $filter = $object = array();
     $this->createFilters($this->JOB['obj'], $filter, $object);
     $queries = array(array('TABLE STATUS', 'Name', 'TA'));
     if (V_MYSQL > 50014) {
         $queries[] = array("PROCEDURE STATUS WHERE db='{$this->JOB['db']}'", 'Name', 'PR');
         $queries[] = array("FUNCTION STATUS WHERE db='{$this->JOB['db']}'", 'Name', 'FU');
         $queries[] = array('TRIGGERS', 'Trigger', 'TR');
         if (V_MYSQL > 50100) {
             $queries[] = array('EVENTS', 'Name', 'EV');
         }
     }
     $todo = $header = array();
     $tabs = $rows = 0;
     $only_create = explode(' ', $this->CFG['only_create']);
     foreach ($queries as $query) {
         $t = $query[2];
         if ($t == 'TA' && (!empty($object['TC']) || !empty($filter['TC']))) {
         } elseif (empty($object[$t]) && empty($filter[$t])) {
             continue;
         }
         $r = mysql_query('SHOW ' . $query[0]) or sxd_my_error();
         if (!$r) {
             continue;
         }
         $todo[$t] = array();
         $header[$t] = array();
         while ($item = mysql_fetch_assoc($r)) {
             $n = $item[$query[1]];
             switch ($t) {
                 case 'TA':
                 case 'TC':
                     if (V_MYSQL > 40101 && is_null($item['Engine']) && preg_match('/^VIEW/i', $item['Comment'])) {
                         if (sxd_check($n, $object['VI'], $filter['VI'])) {
                             $todo['VI'] = array();
                             $header['VI'] = array();
                         }
                         continue;
                     } elseif (sxd_check($n, $object['TA'], $filter['TA'])) {
                         $engine = V_MYSQL > 40101 ? $item['Engine'] : $item['Type'];
                         $t = in_array($engine, $only_create) ? 'TC' : 'TA';
                     } elseif (sxd_check($n, $object['TC'], $filter['TC'])) {
                         $t = 'TC';
                         $item['Rows'] = $item['Data_length'] = '';
                     } else {
                         continue;
                     }
                     $todo['TA'][] = array($t, $n, !empty($item['Collation']) ? $item['Collation'] : '', $item['Auto_increment'], $item['Rows'], $item['Data_length']);
                     $header['TA'][] = "{$n}`{$item['Rows']}`{$item['Data_length']}";
                     $tabs++;
                     $rows += $item['Rows'];
                     break;
                 default:
                     if (sxd_check($n, $object[$t], $filter[$t])) {
                         $todo[$t][] = array($t, $n, !empty($item['collation_connection']) ? $item['collation_connection'] : '');
                         $header[$t][] = $n;
                     }
             }
         }
     }
     if (V_MYSQL > 50014 && (!empty($object['VI']) || !empty($filter['VI']))) {
         // Бэкап обзоров, нужно отсортировать зависимые
         $r = mysql_query("SELECT table_name, view_definition /*!50121 , collation_connection */ FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '{$this->JOB['db']}'") or sxd_my_error();
         $views = $dumped = $views_collation = array();
         $re = "/`{$this->JOB['db']}`.`(.+?)`/";
         while ($item = mysql_fetch_assoc($r)) {
             preg_match_all($re, preg_replace("/^select.+? from/i", '', $item['view_definition']), $m);
             $used = $m[1];
             $views_collation[$item['table_name']] = !empty($item['collation_connection']) ? $item['collation_connection'] : '';
             $views[$item['table_name']] = $used;
         }
         while (count($views) > 0) {
             foreach ($views as $n => $view) {
                 $can_dumped = true;
                 foreach ($view as $k) {
                     if (isset($views[$k]) && !isset($dumped[$k])) {
                         $can_dumped = false;
                     }
                 }
                 if ($can_dumped) {
                     if (sxd_check($n, $object['VI'], $filter['VI'])) {
                         $todo['VI'][] = array('VI', $n, $views_collation[$n]);
                         $header['VI'][] = $n;
                     }
                     $dumped[$n] = 1;
                     unset($views[$n]);
                 }
             }
         }
         unset($dumped);
         unset($views);
         unset($views_collation);
     }
     $this->JOB['file_tmp'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.tmp';
     $this->JOB['file_rtl'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.rtl';
     $this->JOB['file_log'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.log';
     $this->JOB['file_stp'] = $this->CFG['backup_path'] . $this->JOB['job'] . '.stp';
     if (file_exists($this->JOB['file_stp'])) {
         unlink($this->JOB['file_stp']);
     }
     $this->fh_tmp = $this->openFile($this->JOB['file_tmp'], 'w');
     $this->JOB['file'] = sprintf('%s_%s.%s', isset($this->JOB['title']) ? $this->JOB['job'] : $this->JOB['db'], date('Y-m-d_H-i-s'), $this->JOB['file_ext']);
     $this->JOB['file_name'] = $this->CFG['backup_path'] . $this->JOB['file'];
     $this->JOB['todo'] = $todo;
     $this->saveJob($this->JOB['job'], $this->JOB);
     $fcache = implode('|', array('#SXD20', V_SXD, V_MYSQL, V_PHP, date('Y.m.d H:i:s'), $this->JOB['db'], $this->JOB['charset'], $tabs, $rows, mysql_escape_string($this->JOB['comment']))) . "\n";
     foreach ($header as $t => $o) {
         if (!empty($o)) {
             $fcache .= "#{$t} " . implode('|', $o) . "\n";
         }
     }
     $this->fh_rtl = fopen($this->JOB['file_rtl'], 'wb');
     $this->fh_log = fopen($this->JOB['file_log'], 'wb');
     $this->rtl = array(time(), time(), $rows, 0, '', '', '', 0, 0, 0, 0, TIMER, "\n");
     $fcache .= "#EOH\n\n";
     $this->write($fcache);
     $this->addLog(sprintf($this->LNG['backup_begin'], $this->JOB['db']));
     $this->runBackupJob();
 }
Пример #2
0
 function addBackupJob($job)
 {
     $this->closeConnect();
     $this->JOB = $job;
     mysql_select_db($this->JOB["db"]);
     $filter = $object = array();
     $this->createFilters($this->JOB["obj"], $filter, $object);
     $queries = array(array("TABLE STATUS", "Name", "TA"));
     if (V_MYSQL > 50014) {
         $queries[] = array("PROCEDURE STATUS WHERE db='{$this->JOB['db']}'", "Name", "PR");
         $queries[] = array("FUNCTION STATUS WHERE db='{$this->JOB['db']}'", "Name", "FU");
         $queries[] = array("TRIGGERS", "Trigger", "TR");
         if (V_MYSQL > 50100) {
             $queries[] = array("EVENTS", "Name", "EV");
         }
     }
     $todo = $header = array();
     $tabs = $rows = 0;
     $only_create = explode(" ", $this->CFG["only_create"]);
     foreach ($queries as $query) {
         $t = $query[2];
         if ($t == "TA" && (!empty($object["TC"]) || !empty($filter["TC"]))) {
         } elseif (empty($object[$t]) && empty($filter[$t])) {
             continue;
         }
         $r = mysql_query("SHOW " . $query[0]) or sxd_my_error();
         if (!$r) {
             continue;
         }
         $todo[$t] = array();
         $header[$t] = array();
         while ($item = mysql_fetch_assoc($r)) {
             $n = $item[$query[1]];
             switch ($t) {
                 case "TA":
                 case "TC":
                     if (V_MYSQL > 40101 && is_null($item["Engine"]) && preg_match("/^VIEW/i", $item["Comment"])) {
                         if (sxd_check($n, $object["VI"], $filter["VI"])) {
                             $todo["VI"] = array();
                             $header["VI"] = array();
                         }
                         continue;
                     } elseif (sxd_check($n, $object["TA"], $filter["TA"])) {
                         $engine = V_MYSQL > 40101 ? $item["Engine"] : $item["Type"];
                         $t = in_array($engine, $only_create) ? "TC" : "TA";
                     } elseif (sxd_check($n, $object["TC"], $filter["TC"])) {
                         $t = "TC";
                         $item["Rows"] = $item["Data_length"] = "";
                     } else {
                         continue;
                     }
                     $todo["TA"][] = array($t, $n, !empty($item["Collation"]) ? $item["Collation"] : "", $item["Auto_increment"], $item["Rows"], $item["Data_length"]);
                     $header["TA"][] = "{$n}`{$item['Rows']}`{$item['Data_length']}";
                     $tabs++;
                     $rows += $item["Rows"];
                     break;
                 default:
                     if (sxd_check($n, $object[$t], $filter[$t])) {
                         $todo[$t][] = array($t, $n, !empty($item["collation_connection"]) ? $item["collation_connection"] : "");
                         $header[$t][] = $n;
                     }
             }
         }
     }
     if (V_MYSQL > 50014 && (!empty($object["VI"]) || !empty($filter["VI"]))) {
         $r = mysql_query("SELECT table_name, view_definition /*!50121 , collation_connection */ FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '{$this->JOB['db']}'") or sxd_my_error();
         $views = $dumped = $views_collation = array();
         $re = "/`{$this->JOB['db']}`.`(.+?)`/";
         while ($item = mysql_fetch_assoc($r)) {
             preg_match_all($re, preg_replace("/^select.+? from/i", "", $item["view_definition"]), $m);
             $used = $m[1];
             $views_collation[$item["table_name"]] = !empty($item["collation_connection"]) ? $item["collation_connection"] : "";
             $views[$item["table_name"]] = $used;
         }
         while (count($views) > 0) {
             foreach ($views as $n => $view) {
                 $can_dumped = true;
                 foreach ($view as $k) {
                     if (isset($views[$k]) && !isset($dumped[$k])) {
                         $can_dumped = false;
                     }
                 }
                 if ($can_dumped) {
                     if (sxd_check($n, $object["VI"], $filter["VI"])) {
                         $todo["VI"][] = array("VI", $n, $views_collation[$n]);
                         $header["VI"][] = $n;
                     }
                     $dumped[$n] = 1;
                     unset($views[$n]);
                 }
             }
         }
         unset($dumped);
         unset($views);
         unset($views_collation);
     }
     $this->JOB["file_tmp"] = $this->CFG["backup_path"] . $this->JOB["job"] . ".tmp";
     $this->JOB["file_rtl"] = $this->CFG["backup_path"] . $this->JOB["job"] . ".rtl";
     $this->JOB["file_log"] = $this->CFG["backup_path"] . $this->JOB["job"] . ".log";
     $this->JOB["file_stp"] = $this->CFG["backup_path"] . $this->JOB["job"] . ".stp";
     if (!empty($this->JOB["outfile"])) {
         $this->JOB["file_buf"] = (preg_match("/^([a-z]:|\\/)/", $this->CFG["outfile_path"]) ? "" : strtr(dirname(__FILE__), "\\", "/") . "/") . $this->CFG["outfile_path"] . $this->JOB["job"] . ".buf";
     }
     if (file_exists($this->JOB["file_stp"])) {
         unlink($this->JOB["file_stp"]);
     }
     $this->fh_tmp = $this->openFile($this->JOB["file_tmp"], "w");
     $this->JOB["file"] = sprintf("%s_%s.%s", isset($this->JOB["title"]) ? $this->JOB["job"] : $this->JOB["db"], date("Y-m-d_H-i-s"), $this->JOB["file_ext"]);
     $this->JOB["file_name"] = $this->CFG["backup_path"] . $this->JOB["file"];
     $this->JOB["todo"] = $todo;
     $this->saveJob($this->JOB["job"], $this->JOB);
     $fcache = implode("|", array("#SXD20", V_SXD, V_MYSQL, V_PHP, date("Y.m.d H:i:s"), $this->JOB["db"], $this->JOB["charset"], $tabs, $rows, sxd_esc($this->JOB["comment"], false))) . "\n";
     foreach ($header as $t => $o) {
         if (!empty($o)) {
             $fcache .= "#{$t} " . implode("|", $o) . "\n";
         }
     }
     $this->fh_rtl = fopen($this->JOB["file_rtl"], "wb");
     $this->fh_log = fopen($this->JOB["file_log"], "wb");
     $this->rtl = array(time(), time(), $rows, 0, "", "", "", 0, 0, 0, 0, TIMER, "\n");
     $fcache .= "#EOH\n\n";
     $this->write($fcache);
     $this->addLog(sprintf($this->LNG["backup_begin"], $this->JOB["db"]));
     $this->runBackupJob();
 }