/** * Get affected tables * * @param string Table * @return string */ function get_affected_tables($table) { global $DB; $affected_tables = T_('Affected tables:') . ' '; if (is_array($table)) { $affected_tables .= implode(', ', aliases_to_tables($table)); } elseif ($table == '*') { $tables = array(); foreach ($DB->get_results('SHOW TABLES', ARRAY_N) as $row) { $tables[] = $row[0]; } $affected_tables .= implode(', ', $tables); } else { $affected_tables .= aliases_to_tables($table); } return $affected_tables; }
/** * Backup database * * @param string backup directory path */ function backup_database($backup_dirpath) { global $DB, $db_config, $backup_tables, $inc_path; echo '<h4>' . T_('Creating database backup...') . '</h4>'; evo_flush(); // Collect all included tables $ready_to_backup = array(); foreach ($this->backup_tables as $name => $included) { if ($included) { $tables = aliases_to_tables($backup_tables[$name]['table']); if (is_array($tables)) { $ready_to_backup = array_merge($ready_to_backup, $tables); } elseif ($tables == '*') { foreach ($DB->get_results('SHOW TABLES', ARRAY_N) as $row) { $ready_to_backup[] = $row[0]; } } else { $ready_to_backup[] = $tables; } } } // Ensure there are no duplicated tables $ready_to_backup = array_unique($ready_to_backup); // Exclude tables foreach ($this->backup_tables as $name => $included) { if (!$included) { $tables = aliases_to_tables($backup_tables[$name]['table']); if (is_array($tables)) { $ready_to_backup = array_diff($ready_to_backup, $tables); } elseif ($tables != '*') { $index = array_search($tables, $ready_to_backup); if ($index) { unset($ready_to_backup[$index]); } } } } // Create and save created SQL backup script $backup_sql_filename = 'db.sql'; $backup_sql_filepath = $backup_dirpath . $backup_sql_filename; // Check if backup file exists if (file_exists($backup_sql_filepath)) { // Stop tables backup, because backup file exists echo '<p style="color:red">' . sprintf(T_('Unable to write database dump. Database dump already exists: «%s»'), $backup_sql_filepath) . '</p>'; evo_flush(); return false; } $f = @fopen($backup_sql_filepath, 'w+'); if ($f == false) { // Stop backup, because it can't open backup file for writing echo '<p style="color:red">' . sprintf(T_('Unable to write database dump. Could not open «%s» for writing.'), $backup_sql_filepath) . '</p>'; evo_flush(); return false; } echo sprintf(T_('Dumping tables to «<strong>%s</strong>»...'), $backup_sql_filepath) . '<br/>'; evo_flush(); // Create and save created SQL backup script foreach ($ready_to_backup as $table) { // progressive display of what backup is doing echo sprintf(T_('Backing up table «<strong>%s</strong>» ...'), $table); evo_flush(); $row_table_data = $DB->get_row('SHOW CREATE TABLE ' . $table, ARRAY_N); fwrite($f, $row_table_data[1] . ";\n\n"); $values_list = array(); foreach ($DB->get_results('SELECT * FROM ' . $table, ARRAY_N) as $row) { $values = '('; $num_fields = count($row); for ($index = 0; $index < $num_fields; $index++) { if (isset($row[$index])) { $row[$index] = str_replace("\n", "\\n", addslashes($row[$index])); $values .= '\'' . $row[$index] . '\''; } else { // The $row[$index] value is not set or is NULL $values .= 'NULL'; } if ($index < $num_fields - 1) { $values .= ','; } } $values_list[] = $values . ')'; } if (!empty($values_list)) { fwrite($f, 'INSERT INTO ' . $table . ' VALUES ' . implode(',', $values_list) . ";\n\n"); } unset($values_list); // Flush the output to a file if (fflush($f)) { echo ' OK.'; } echo '<br />'; evo_flush(); } // Close backup file input stream fclose($f); if ($this->pack_backup_files) { // Pack created backup SQL script // Pack using 'zlib' extension and PclZip wrapper // Load PclZip class (PHP4): load_class('_ext/pclzip/pclzip.lib.php', 'PclZip'); $zip_filepath = $backup_dirpath . 'db.zip'; $PclZip = new PclZip($zip_filepath); $file_list = $PclZip->add($backup_dirpath . $backup_sql_filename, PCLZIP_OPT_REMOVE_PATH, no_trailing_slash($backup_dirpath)); if ($file_list == 0) { echo '<p style="color:red">' . sprintf(T_('Unable to create «%s»'), $zip_filepath) . '</p>'; evo_flush(); return false; } unlink($backup_sql_filepath); } return true; }