class backup_migrate_destination_db_mysql in Backup and Migrate 6.2
Same name and namespace in other branches
- 8.2 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql
- 8.3 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql
- 7.3 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql
- 7.2 includes/destinations.db.mysql.inc \backup_migrate_destination_db_mysql
A destination type for saving to a database server.
Hierarchy
- class \backup_migrate_item
Expanded class hierarchy of backup_migrate_destination_db_mysql
1 string reference to 'backup_migrate_destination_db_mysql'
- backup_migrate_backup_migrate_destination_types in includes/
destinations.inc - Implementation of hook_backup_migrate_destination_types().
File
- includes/
destinations.db.mysql.inc, line 16 - Functions to handle the direct to database destination.
View source
class backup_migrate_destination_db_mysql extends backup_migrate_destination_db {
function type_name() {
return t("MySQL Database");
}
/**
* Return a list of backup filetypes.
*/
function file_types() {
return array(
"sql" => array(
"extension" => "sql",
"filemime" => "text/x-sql",
"backup" => TRUE,
"restore" => TRUE,
),
"mysql" => array(
"extension" => "mysql",
"filemime" => "text/x-sql",
"backup" => TRUE,
"restore" => TRUE,
),
);
}
/**
* Declare any mysql databases defined in the settings.php file as a possible destination.
*/
function destinations() {
$out = array();
global $db_url;
$urls = is_array($db_url) ? $db_url : array(
'default' => $db_url,
);
foreach ((array) $urls as $key => $url) {
// Only mysql/mysqli supported by this destination.
if (strpos($url, 'mysql') === 0) {
if ($destination = backup_migrate_create_destination('mysql', array(
'url' => $url,
))) {
// Treat the default database differently because it is probably the only one available.
if ($key == 'default') {
$destination
->set_id('db');
$destination
->set_name(t('Default Database'));
// Dissalow backing up to the default database because that's confusing and potentially dangerous.
$destination
->remove_op('scheduled backup');
$destination
->remove_op('manual backup');
}
else {
$destination
->set_id('db:' . $key);
$destination
->set_name($key . ": " . $destination
->get_display_location());
}
$out[$destination
->get_id()] = $destination;
}
}
}
return $out;
}
/**
* Get the file type for to backup this destination to.
*/
function get_file_type_id() {
return 'mysql';
}
/**
* Backup the databases to a file.
*
* Returns a list of sql commands, one command per line.
* That makes it easier to import without loading the whole file into memory.
* The files are a little harder to read, but human-readability is not a priority
*/
function _backup_db_to_file($file, $settings) {
$lines = 0;
$exclude = !empty($settings->filters['exclude_tables']) ? $settings->filters['exclude_tables'] : array();
$nodata = !empty($settings->filters['nodata_tables']) ? $settings->filters['nodata_tables'] : array();
if ($file
->open(TRUE)) {
$file
->write($this
->_get_sql_file_header());
$alltables = $this
->_get_tables();
$allviews = $this
->_get_views();
foreach ($alltables as $table) {
if (_backup_migrate_check_timeout()) {
return FALSE;
}
if ($table['Name'] && empty($exclude[$table['Name']])) {
$file
->write($this
->_get_table_structure_sql($table));
$lines++;
if (empty($nodata[$table['Name']])) {
$lines += $this
->_dump_table_data_sql_to_file($file, $table);
}
}
}
foreach ($allviews as $view) {
if (_backup_migrate_check_timeout()) {
return FALSE;
}
if ($view['Name'] && !isset($exclude[$view['Name']])) {
$file
->write($this
->_get_view_create_sql($view));
}
}
$file
->write($this
->_get_sql_file_footer());
$file
->close();
return $lines;
}
else {
return FALSE;
}
}
/**
* Backup the databases to a file.
*/
function _restore_db_from_file($file, $settings) {
$num = 0;
if ($file
->open()) {
// Read one line at a time and run the query.
while ($line = $file
->read()) {
if (_backup_migrate_check_timeout()) {
return FALSE;
}
$line = trim($line);
if ($line) {
// Use the helper instead of the api function to avoid substitution of '{' etc.
_db_query($line);
$num++;
}
}
// Close the file with fclose/gzclose.
$file
->close();
}
else {
drupal_set_message(t("Unable to open file %file to restore database", array(
"%file" => $file
->filepath(),
)), 'error');
$num = FALSE;
}
return $num;
}
/**
* Get a list of tables in the database.
*/
function _get_table_names() {
$out = array();
foreach ($this
->_get_tables() as $table) {
$out[$table['Name']] = $table['Name'];
}
return $out;
}
/**
* Get a list of views in the database.
*/
function _get_view_names() {
$out = array();
foreach ($this
->_get_views() as $view) {
$out[$view['Name']] = $view['Name'];
}
return $out;
}
/**
* Lock the list of given tables in the database.
*/
function _lock_tables($tables) {
if ($tables) {
$tables_escaped = array();
foreach ($tables as $table) {
$tables_escaped[] = '`' . db_escape_table($table) . '` WRITE';
}
db_query('LOCK TABLES ' . implode(', ', $tables_escaped));
}
}
/**
* Unlock all tables in the database.
*/
function _unlock_tables($tables) {
db_query('UNLOCK TABLES');
}
/**
* Get a list of tables in the db.
*/
function _get_tables() {
$out = array();
// get auto_increment values and names of all tables
$tables = db_query("show table status");
while ($table = db_fetch_array($tables)) {
if (!empty($table['Engine'])) {
$out[$table['Name']] = $table;
}
}
return $out;
}
/**
* Get a list of views in the db.
*/
function _get_views() {
$out = array();
// get auto_increment values and names of all tables
$tables = db_query("show table status");
while ($table = db_fetch_array($tables)) {
if (empty($table['Engine'])) {
$out[$table['Name']] = $table;
}
}
return $out;
}
/**
* Get the sql for the structure of the given table.
*/
function _get_table_structure_sql($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'], "\n", " ");
if ($table['Auto_increment']) {
$out .= " AUTO_INCREMENT=" . $table['Auto_increment'];
}
$out .= ";\n";
}
return $out;
}
/**
* Get the sql for the structure of the given table.
*/
function _get_view_create_sql($view) {
$out = "";
// Switch SQL mode to get rid of "CREATE ALGORITHM..." what requires more permissions + troubles with the DEFINER user
$sql_mode = db_result(db_query("SELECT @@SESSION.sql_mode"));
db_query("SET sql_mode = 'ANSI'");
$result = db_query("SHOW CREATE VIEW `" . $view['Name'] . "`");
db_query("SET SQL_mode = '%s'", $sql_mode);
if ($create = db_fetch_array($result)) {
$out .= "DROP VIEW IF EXISTS `" . $view['Name'] . "`;\n";
$out .= "SET sql_mode = 'ANSI';\n";
$out .= strtr($create['Create View'], "\n", " ") . ";\n";
$out .= "SET sql_mode = '{$sql_mode}';\n";
}
return $out;
}
/**
* Get the sql to insert the data for a given table
*/
function _dump_table_data_sql_to_file($file, $table) {
$rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30);
$bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000);
$lines = 0;
$data = db_query("SELECT * FROM `" . $table['Name'] . "`");
$rows = $bytes = 0;
$line = array();
while ($row = db_fetch_array($data)) {
// DB Escape the values.
$items = array();
foreach ($row as $key => $value) {
$items[] = is_null($value) ? "null" : "'" . db_escape_string($value) . "'";
}
// If there is a row to be added.
if ($items) {
// Start a new line if we need to.
if ($rows == 0) {
$file
->write("INSERT INTO `" . $table['Name'] . "` VALUES ");
$bytes = $rows = 0;
}
else {
$file
->write(",");
}
// Write the data itself.
$sql = implode(',', $items);
$file
->write('(' . $sql . ')');
$bytes += strlen($sql);
$rows++;
// Finish the last line if we've added enough items
if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) {
$file
->write(";\n");
$lines++;
$bytes = $rows = 0;
}
}
}
// Finish any unfinished insert statements.
if ($rows > 0) {
$file
->write(";\n");
$lines++;
}
return $lines;
}
/**
* The header for the top of the sql dump file. These commands set the connection
* character encoding to help prevent encoding conversion issues.
*/
function _get_sql_file_header() {
return "/*!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\n";
}
/**
* The footer of the sql dump file.
*/
function _get_sql_file_footer() {
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";
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
backup_migrate_destination:: |
property | |||
backup_migrate_destination:: |
property | 2 | ||
backup_migrate_destination:: |
property |
Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
property |
Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
property | |||
backup_migrate_destination:: |
property | |||
backup_migrate_destination:: |
property |
Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
property |
Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
property |
Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function | Submit the settings form. Any values returned will be saved. | ||
backup_migrate_destination:: |
function | Get the form for the settings for this filter. | ||
backup_migrate_destination:: |
function | Determine if we can read the given file. | ||
backup_migrate_destination:: |
function | Determine if we can read the given file. | 1 | |
backup_migrate_destination:: |
function |
Create a new destination of the correct type. Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function |
Get the message to send to the user when confirming the deletion of the item. Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function | Delete the file with the given destination specific id. | 2 | |
backup_migrate_destination:: |
function | Retrieve the file list. | ||
backup_migrate_destination:: |
function | Retrieve the file list. | ||
backup_migrate_destination:: |
function | Cache the file list. | ||
backup_migrate_destination:: |
function | Check if a file exists in the given destination. | ||
backup_migrate_destination:: |
function |
Get the action links for a destination. Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function | Get the type name of this destination for display to the user. | ||
backup_migrate_destination:: |
function | Get the action links for a file on a given destination. | ||
backup_migrate_destination:: |
function |
Get the columns needed to list the type. Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function |
Get a row of data to be used in a list of items of this type. Overrides backup_migrate_item:: |
1 | |
backup_migrate_destination:: |
function |
Add the menu items specific to the destination type. Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function |
Get the name of the item. Overrides backup_migrate_item:: |
1 | |
backup_migrate_destination:: |
function | List all the available files in the given destination with their destination specific id. | 1 | |
backup_migrate_destination:: |
function | Load the file with the given destination specific id and return as a backup_file object. | 5 | |
backup_migrate_destination:: |
function | Does this destination support the given operation. | ||
backup_migrate_destination:: |
function | |||
backup_migrate_destination:: |
function | Remove the given op from the support list. | ||
backup_migrate_destination:: |
function | Get the form for the settings for this filter. | ||
backup_migrate_destination:: |
function | Get the form for the settings for this filter. | ||
backup_migrate_destination:: |
function | Submit the settings form. Any values returned will be saved. | ||
backup_migrate_destination:: |
function | Get the form for the settings for this filter. | ||
backup_migrate_destination:: |
function | |||
backup_migrate_destination:: |
function | Get the form for the settings for this destination type. | ||
backup_migrate_destination:: |
function | Get the form for the settings for this destination. | ||
backup_migrate_destination:: |
function | Submit the settings form. Any values returned will be saved. | ||
backup_migrate_destination:: |
function | Validate the form for the settings for this destination. | 1 | |
backup_migrate_destination:: |
function | |||
backup_migrate_destination:: |
function | |||
backup_migrate_destination:: |
function |
This function is not supposed to be called. It is just here to help the po extractor out. Overrides backup_migrate_item:: |
||
backup_migrate_destination:: |
function | Delete the file with the given destination specific id. | 2 | |
backup_migrate_destination:: |
function | List all the available files in the given destination with their destination specific id. | 3 | |
backup_migrate_destination:: |
function | Save the given file to the destination. | 2 | |
backup_migrate_destination_db:: |
property |
Overrides backup_migrate_destination:: |
||
backup_migrate_destination_db:: |
function |
Get the form for the settings for this destination. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_db:: |
function |
Get the form for the backup settings for this destination. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_db:: |
function | Backup from this source. | ||
backup_migrate_destination_db:: |
function |
Destination configuration callback. Overrides backup_migrate_destination_remote:: |
||
backup_migrate_destination_db:: |
function |
Validate the configuration form. Make sure the db info is valid. Overrides backup_migrate_item:: |
||
backup_migrate_destination_db:: |
function | Get a list of objects in the database. | ||
backup_migrate_destination_db:: |
function | Get a list of tables in the database. | ||
backup_migrate_destination_db:: |
function | Lock the database in anticipation of a backup. | ||
backup_migrate_destination_db:: |
function | Restore to this source. | ||
backup_migrate_destination_db:: |
function |
Save the info by importing it into the database. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_db:: |
function | Switch to the current database. Pass true to switch back to the previous db. | ||
backup_migrate_destination_db:: |
function | Unlock any tables that have been locked. | ||
backup_migrate_destination_db_mysql:: |
function | Declare any mysql databases defined in the settings.php file as a possible destination. | ||
backup_migrate_destination_db_mysql:: |
function |
Return a list of backup filetypes. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_db_mysql:: |
function |
Get the file type for to backup this destination to. Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_db_mysql:: |
function |
Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_db_mysql:: |
function |
Backup the databases to a file. Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_db_mysql:: |
function | Get the sql to insert the data for a given table | ||
backup_migrate_destination_db_mysql:: |
function | The footer of the sql dump file. | ||
backup_migrate_destination_db_mysql:: |
function | The header for the top of the sql dump file. These commands set the connection character encoding to help prevent encoding conversion issues. | ||
backup_migrate_destination_db_mysql:: |
function | Get a list of tables in the db. | ||
backup_migrate_destination_db_mysql:: |
function |
Get a list of tables in the database. Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_db_mysql:: |
function | Get the sql for the structure of the given table. | ||
backup_migrate_destination_db_mysql:: |
function | Get a list of views in the db. | ||
backup_migrate_destination_db_mysql:: |
function | Get the sql for the structure of the given table. | ||
backup_migrate_destination_db_mysql:: |
function | Get a list of views in the database. | ||
backup_migrate_destination_db_mysql:: |
function |
Lock the list of given tables in the database. Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_db_mysql:: |
function |
Backup the databases to a file. Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_db_mysql:: |
function |
Unlock all tables in the database. Overrides backup_migrate_destination_db:: |
||
backup_migrate_destination_remote:: |
function |
Submit the configuration form. Glue the url together and add the old password back if a new one was not specified. Overrides backup_migrate_item:: |
1 | |
backup_migrate_destination_remote:: |
function |
The location to display is the url without the password. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_remote:: |
function |
The location is a URI so parse it and store the parts. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_remote:: |
function | Glue a URLs component parts back into a URL. | ||
backup_migrate_destination_remote:: |
function |
Return the location with the password. Overrides backup_migrate_destination:: |
||
backup_migrate_destination_remote:: |
function | Break a URL into it's component parts. | ||
backup_migrate_destination_remote:: |
function | Get a url from the parts. | ||
backup_migrate_item:: |
property | |||
backup_migrate_item:: |
function | Get all of the given items. | ||
backup_migrate_item:: |
function | Decode a loaded db row (unserialize necessary fields). | ||
backup_migrate_item:: |
function | Delete the item from the database. | ||
backup_migrate_item:: |
function | Return as an exported array of values. | ||
backup_migrate_item:: |
function | Load an existing item from an array. | ||
backup_migrate_item:: |
function | Return a random (very very likely unique) string id for a new item. | ||
backup_migrate_item:: |
function | Get the member with the given key. | ||
backup_migrate_item:: |
function | Get the rendered action links for a destination. | ||
backup_migrate_item:: |
function | Get the default values for standard parameters. | 2 | |
backup_migrate_item:: |
function | Get the primary id for this item (if any is set). | ||
backup_migrate_item:: |
function | Get a table of all items of this type. | 1 | |
backup_migrate_item:: |
function | Get header for a lost of this type. | ||
backup_migrate_item:: |
function | Get the primary key field title from the schema. | ||
backup_migrate_item:: |
function | Get the schema for the item type. | ||
backup_migrate_item:: |
function | Return the fields which must be serialized before saving to the db. | ||
backup_migrate_item:: |
function | A particular item. | ||
backup_migrate_item:: |
function | Load an existing item from an database (serialized) array. | ||
backup_migrate_item:: |
function | Save the item to the database. | ||
backup_migrate_item:: |
function | Set the primary id for this item (if any is set). | ||
backup_migrate_item:: |
function | Return as an array of values. | ||
backup_migrate_item:: |
function | Merge parameters with the given defaults. | ||
backup_migrate_item:: |
function | Constructor, set the basic info pulled from the db or generated programatically. | 4 |