db.mysql.inc in Backup and Migrate 8.2
File
includes/db.mysql.inc
View source
<?php
use Drupal\Core\Database\Database;
function backup_migrate_backup_db_to_file_mysql($file, $settings) {
$lines = 0;
$exclude = !empty($settings->exclude_tables) ? $settings->exclude_tables : array();
$nodata = !empty($settings->nodata_tables) ? $settings->nodata_tables : array();
if ($file
->open(TRUE)) {
$file
->write(_backup_migrate_get_sql_file_header_mysql());
$alltables = _backup_migrate_get_tables_mysql();
foreach ($alltables as $table) {
if (_backup_migrate_check_timeout()) {
return FALSE;
}
if ($table->name && !isset($exclude[$table->name])) {
$file
->write(_backup_migrate_get_table_structure_sql_mysql($table));
$lines++;
if (!in_array($table->name, $nodata)) {
$lines += _backup_migrate_dump_table_data_sql_to_file($file, $table);
}
}
}
$file
->write(_backup_migrate_get_sql_file_footer_mysql());
$file
->close();
return $lines;
}
else {
return FALSE;
}
}
function backup_migrate_restore_db_from_file_mysql($file, $settings) {
$num = 0;
if ($file
->open()) {
while ($line = $file
->read()) {
if (_backup_migrate_check_timeout()) {
return FALSE;
}
$line = trim($line);
if ($line) {
Database::getConnection()
->prepare($line)
->execute();
$num++;
}
}
$file
->close();
}
else {
drupal_set_message(t("Unable to open file %file to restore database", array(
"%file" => $file
->filepath(),
)), 'error');
$num = FALSE;
}
return $num;
}
function _backup_migrate_get_table_structure_sql_mysql($table) {
$out = "";
$result = db_query("SHOW CREATE TABLE `" . $table->name . "`");
if ($create = db_fetch_array($result)) {
$out .= "DROP TABLE IF EXISTS `" . $table->name . "`;\n";
$out .= strtr($create['create table'], array(
"\n" => " ",
'"' => '`',
));
if ($table->auto_increment) {
$out .= " AUTO_INCREMENT=" . $table->auto_increment;
}
$out .= ";\n";
}
return $out;
}
function _backup_migrate_dump_table_data_sql_to_file($file, $table) {
$lines = 0;
$data = db_query("SELECT * FROM `" . $table->name . "`");
while ($row = db_fetch_array($data)) {
$items = array();
foreach ($row as $key => $value) {
$items[] = is_null($value) ? "null" : "'" . mysql_escape_string($value) . "'";
}
if ($items) {
$file
->write("INSERT INTO `" . $table->name . "` VALUES (" . implode(",", $items) . ");\n");
$lines++;
}
}
return $lines;
}
function _backup_migrate_get_sql_file_header_mysql() {
return "\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */;\n\nSET NAMES utf8;\n";
}
function _backup_migrate_get_sql_file_footer_mysql() {
return "\n\n/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;\n/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;\n/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;\n/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;\n/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;\n/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;\n";
}
function _backup_migrate_get_tables_mysql() {
$out = array();
$tables = db_query("show table status");
foreach ($tables as $table) {
$out[$table->name] = $table;
}
return $out;
}
function _backup_migrate_get_table_names_mysql() {
$out = array();
foreach (_backup_migrate_get_tables_mysql() as $key => $table) {
$out[$key] = $table->name;
}
return $out;
}