Index: ../../../../../../../../../../projects/drupal_modules/contributions/modules/backup_migrate/backup_migrate.module =================================================================== --- ../../../../../../../../../../projects/drupal_modules/contributions/modules/backup_migrate/backup_migrate.module (revision 1.3) +++ ../../../../../../../../../../projects/drupal_modules/contributions/modules/backup_migrate/backup_migrate.module Mon May 25 12:06:04 CEST 2009 @@ -183,6 +183,7 @@ function backup_migrate_backup() { $form = array(); $tables = _backup_migrate_get_table_names(); + $views = _backup_migrate_get_view_names(); $form['backup_migrate_exclude_tables'] = array( "#type" => "select", "#multiple" => true, @@ -199,6 +200,14 @@ "#default_value" => variable_get( "backup_migrate_nodata_tables", _backup_migrate_default_structure_only_tables() ), "#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['backup_migrate_exclude_views'] = array( + "#type" => "select", + "#multiple" => true, + "#title" => t("Exclude the following views altogether"), + "#options" => $views, + "#default_value" => variable_get( "backup_migrate_exclude_views", _backup_migrate_default_exclude_views() ), + "#description" => t( "The selected views will not be added to the backup file." ), + ); $form['backup_migrate_file_name'] = array( "#type" => "textfield", "#title" => t( "Backup file name" ), @@ -278,6 +287,7 @@ if ( $form_state['values']['backup_migrate_save_settings'] ) { variable_set( "backup_migrate_exclude_tables", $form_state['values']['backup_migrate_exclude_tables'] ); variable_set( "backup_migrate_nodata_tables", $form_state['values']['backup_migrate_nodata_tables'] ); + variable_set( "backup_migrate_exclude_views", $form_state['values']['backup_migrate_exclude_views']); variable_set( "backup_migrate_file_name", $form_state['values']['backup_migrate_file_name'] ); variable_set( "backup_migrate_destination", $form_state['values']['backup_migrate_destination'] ); variable_set( "backup_migrate_compression", $form_state['values']['backup_migrate_compression'] ); @@ -539,6 +549,7 @@ function _backup_migrate_get_dump_sql( $file, $exclude_tables, $nodata_tables ) { if ( $dst = fopen( $file, "w" ) ) { $exclude = variable_get( "backup_migrate_exclude_tables", _backup_migrate_default_exclude_tables() ); + $exclude_views = variable_get( "backup_migrate_exclude_views", _backup_migrate_default_exclude_views()); $nodata = variable_get( "backup_migrate_nodata_tables", _backup_migrate_default_structure_only_tables() ); fwrite( $dst, _backup_migrate_get_sql_file_header() ); $alltables = _backup_migrate_get_tables(); @@ -550,6 +561,12 @@ } } } + $allviews = _backup_migrate_get_views(); + foreach ($allviews as $view) { + if ( $view['Name'] && !isset($exclude_views[$view['Name']] ) ) { + fwrite( $dst, _backup_migrate_get_view_structure_sql($view) ); + } + } fwrite( $dst, _backup_migrate_get_sql_file_footer() ); fclose( $dst ); return true; @@ -577,6 +594,20 @@ } /** + * Get the sql for the structure of the given view + */ +function _backup_migrate_get_view_structure_sql($view) { + $out = ""; + $result = db_query("SHOW CREATE VIEW `". $view['Name'] ."`"); + if ($create = db_fetch_array($result)) { + $out .= "DROP VIEW IF EXISTS `". $view['Name'] ."`;\n"; + $out .= strtr( $create['Create View'], "\n", " " ); + $out .= ";\n"; + } + return $out; +} + +/** * Get the sql to insert the data for a given table */ function _backup_migrate_dump_table_data_sql_to_handle( $dst, $table ) { @@ -637,12 +668,29 @@ // 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; - } + $out[] = $table; + } + } return $out; } /** + * Get a list of views in the db. Works with MySQL, Postgres not tested. + */ +function _backup_migrate_get_views() { + $out = ""; + // get names of all views + $tables = db_query("show table status"); + while ($table = db_fetch_array($tables)) { + if(empty($table['Engine'])) { + $out[] = $table; + } + } + return $out; +} + +/** * Get the list of table names. */ function _backup_migrate_get_table_names( ) { @@ -650,11 +698,27 @@ // 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['Name']; - } + $out[$table['Name']] = $table['Name']; + } + } return $out; } +/** + * Get the list of table names. + */ +function _backup_migrate_get_view_names() { + $out = ""; + // Get auto_increment values and names of all views. + $tables = db_query("show table status"); + while ( $table = db_fetch_array($tables) ) { + if(empty($table['Engine'])) { + $out[$table['Name']] = $table['Name']; + } + } + return $out; +} /** @@ -1131,6 +1195,13 @@ } /** + * Views to ingore altogether. None by default. + */ +function _backup_migrate_default_exclude_views() { + return array(); +} + +/** * Return the default tables whose data can be ignored. These tables mostly contain * info which can be easily reproducted (such as cache or search index) * but also tables which can become quite bloated but are not necessarily extremely