View source
<?php
function _backup_migrate_destination_complete_table_list($element, &$form_state) {
form_set_value($element, array_merge(array_fill_keys($element['#options'], 0), $element['#value']), $form_state);
}
class backup_migrate_destination_db extends backup_migrate_destination_remote {
var $supported_ops = array(
'scheduled backup',
'manual backup',
'configure',
'source',
);
function type_name() {
return t("Database");
}
function save_file($file, $settings) {
backup_migrate_include('files');
$settings
->set_source($this
->get_id());
$file = backup_migrate_perform_restore($this
->get_id(), $file, $settings);
return $file;
}
function edit_form() {
$form = parent::edit_form();
$form['scheme']['#title'] = t('Database type');
$form['scheme']['#options'] = array(
$GLOBALS['db_type'] => $GLOBALS['db_type'],
);
$form['scheme']['#description'] = t('The type of the database. Drupal only supports one database type at a time, so this must be the same as the current database type.');
$form['path']['#title'] = t('Database name');
$form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.');
$form['user']['#description'] = t('Enter the name of a user who has write access to the database.');
return $form;
}
function edit_form_validate($form, &$form_state) {
if (!preg_match('/[a-zA-Z0-9_\\$]+/', $form_state['values']['path'])) {
form_set_error('path', t('The database name is not valid.'));
}
parent::edit_form_validate($form, $form_state);
}
function backup_settings_default() {
$core = array(
'cache',
'cache_filter',
'cache_calendar_ical',
'cache_menu',
'cache_page',
'cache_views',
'cache_block',
'cache_update',
'cache_form',
'sessions',
'search_dataset',
'search_index',
'search_keywords_log',
'search_total',
'watchdog',
'accesslog',
'devel_queries',
'devel_times',
);
$nodata_tables = array_merge(array_combine($core, $core), module_invoke_all('devel_caches'));
return array(
'nodata_tables' => $nodata_tables,
'exclude_tables' => array(),
'utils_lock_tables' => FALSE,
);
}
function backup_settings_form($settings) {
$objects = $this
->get_object_names();
$form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so <strong>do not change these settings unless you know what you're doing</strong>.");
$form['exclude_tables'] = array(
"#type" => "select",
"#multiple" => TRUE,
"#title" => t("Exclude the following tables altogether"),
"#options" => $objects,
"#default_value" => array_filter($settings['exclude_tables']),
'#element_validate' => array(
'_backup_migrate_destination_complete_table_list',
),
"#description" => t("The selected tables will not be added to the backup file."),
);
$tables = $this
->get_table_names();
$form['nodata_tables'] = array(
"#type" => "select",
"#multiple" => TRUE,
"#title" => t("Exclude the data from the following tables"),
"#options" => $tables,
"#default_value" => array_filter($settings['nodata_tables']),
'#element_validate' => array(
'_backup_migrate_destination_complete_table_list',
),
"#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."),
);
$form['utils_lock_tables'] = array(
'#type' => 'checkbox',
'#title' => t('Lock tables during backup'),
'#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL,
'#description' => t('This can help reduce data corruption, but will make your site unresponsive.'),
);
return $form;
}
function backup_to_file($file, $settings) {
$file
->push_type($this
->get_file_type_id());
backup_migrate_filters_invoke_all('pre_backup', $this, $file, $settings);
$this
->switch_db();
$this
->lock_tables($settings);
$success = $this
->_backup_db_to_file($file, $settings);
$this
->unlock_tables($settings);
$this
->switch_db(TRUE);
backup_migrate_filters_invoke_all('post_backup', $this, $file, $settings, $success);
return $success ? $file : FALSE;
}
function restore_from_file($file, &$settings) {
$num = 0;
$type = $this
->get_file_type_id();
if ($file
->type_id() !== $this
->get_file_type_id() && $file
->type_id() !== 'sql') {
_backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array(
"%file" => $file
->filepath(),
'%type' => $file
->type_id(),
), 'error');
}
else {
backup_migrate_filters_invoke_all('pre_restore', $file, $settings);
$this
->switch_db();
$num = $this
->_restore_db_from_file($file, $settings);
$settings->performed_action = $num ? t('%num SQL commands executed.', array(
'%num' => $num,
)) : '';
$this
->switch_db(TRUE);
backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num);
}
return $num;
}
function _backup_db_to_file($file, $settings) {
}
function _restore_db_from_file($file, $settings) {
}
function get_object_names() {
$this
->switch_db();
$out = $this
->_get_table_names();
if (method_exists($this, '_get_view_names')) {
$out += $this
->_get_view_names();
}
$this
->switch_db(TRUE);
return $out;
}
function get_table_names() {
$this
->switch_db();
$out = $this
->_get_table_names();
$this
->switch_db(TRUE);
return $out;
}
function _get_table_names() {
return array();
}
function switch_db($switch_back = FALSE) {
static $db_stack = array();
global $db_url;
if ($switch_back && $db_stack) {
db_set_active(array_pop($db_stack));
return;
}
if ($url = $this
->get_location()) {
if (!is_array($db_url)) {
$db_url = array(
'default' => $db_url,
);
}
$db_url[$url] = $url;
$db_stack[] = db_set_active($url);
}
}
function lock_tables($settings) {
if ($settings->filters['utils_lock_tables']) {
$tables = array();
foreach ($this
->get_table_names() as $table) {
if (empty($settings->filters['exclude_tables']) || !in_array($table, (array) $settings->filters['exclude_tables'])) {
$tables[] = $table;
}
}
$this
->_lock_tables($tables);
}
}
function _lock_tables($tables) {
}
function unlock_tables($settings) {
if ($settings->filters['utils_lock_tables']) {
$this
->_unlock_tables();
}
}
function _unlock_tables($tables) {
}
function get_file_type_id() {
return 'sql';
}
}