diff -P -r -u -F '^function' dba_old/dba.module dba/dba.module --- dba_old/dba.module 2007-06-24 05:41:12.000000000 +0200 +++ dba/dba.module 2007-09-20 00:52:53.000000000 +0200 @@ -245,7 +245,7 @@ function dba_settings_form() { '#attributes' => $attributes, ); - if (function_exists('bzcompress')) { + if (function_exists('bzopen')) { $form['backup']['dba_auto_backup_bzip2'] = array( '#type' => 'checkbox', '#title' => t('Compress automatic backups'), @@ -255,7 +255,7 @@ function dba_settings_form() { '#attributes' => $attributes, ); } - else if (function_exists('gzencode')) { + else if (function_exists('gzopen')) { $form['backup']['dba_auto_backup_gzip'] = array( '#type' => 'checkbox', '#title' => t('Compress automatic backups'), @@ -346,45 +346,42 @@ function dba_auto_backup() { if (file_check_directory($path, FILE_CREATE_DIRECTORY)) { $database = dba_get_database(); $filename = format_date(time(), 'custom', 'Y-md-Hi_') . variable_get('dba_default_filename', 'backup.sql'); + $backup_file = $path .'/'. $filename; $backup = "-- Drupal dba.module database dump\n"; $backup .= "--\n"; $backup .= "-- Database: $database\n"; $backup .= "-- Date: ". format_date(time(), 'large') ."\n\n"; + dba_save_to_file($backup_file, $output); + $output = NULL; + $tables = dba_get_tables(); foreach ($tables as $table) { - $backup .= dba_backup_table($table, TRUE, FALSE, in_array($table, $exclude_tables) ? FALSE : TRUE); + dba_backup_table($table, TRUE, FALSE, in_array($table, $exclude_tables) ? FALSE : TRUE, $backup_file); } // Optionally bzip2 compress auto-backup file. if (variable_get('dba_auto_backup_bzip2', 0)) { - $backup = bzcompress($backup, 9); - $filename = $filename .'.bz2'; + $filename = $backup_file .'.bz2'; + dba_bzip2($backup_file, $filename); + unlink($backup_file); } // Otherwise, optionally gzip compress auto-backup file. else if (variable_get('dba_auto_backup_gzip', 0)) { - if (version_compare(phpversion(), '4.2', '>=')) { - $backup = gzencode($backup, 9, FORCE_GZIP); - } - else { - $backup = gzencode($backup, FORCE_GZIP); - } - $filename = $filename .'.gz'; + $filename = $backup_file .'.gz'; + dba_gzip($backup_file, 9, $filename); + unlink($backup_file); } - if ($fp = fopen($path ."/$filename", 'wb')) { - fwrite($fp, $backup); - fclose($fp); - variable_set('dba_auto_backup_last', time()); - - // If enabled, email a copy of the backup to the site administrator. - if (variable_get('dba_auto_backup_mail', 0)) { - $attachment = new stdClass(); - $attachment->path = $path ."/$filename"; - $attachment->filename = $filename; - dba_mail_backup($attachment); - } + variable_set('dba_auto_backup_last', time()); + + // If enabled, email a copy of the backup to the site administrator. + if (variable_get('dba_auto_backup_mail', 0)) { + $attachment = new stdClass(); + $attachment->path = $filename; + $attachment->filename = basename($filename); + dba_mail_backup($attachment); } } } @@ -1386,7 +1383,7 @@ function dba_describe_table($table, $ver /** * Backup table to file. */ -function dba_backup_table($table, $add_drop_table, $verbose = TRUE, $data = TRUE) { +function dba_backup_table($table, $add_drop_table, $verbose = TRUE, $data = TRUE, $filename = FALSE) { $output = "--\n"; $output .= "-- Table structure for table '$table'\n"; $output .= "--\n\n"; @@ -1401,6 +1398,10 @@ function dba_backup_table($table, $add_d if (!$data) { // Backup schema only for this table. + if ($filename) { + dba_save_to_file($filename, $output); + $output = NULL; + } return $output; } @@ -1413,6 +1414,11 @@ function dba_backup_table($table, $add_d $output = NULL; } + if ($filename) { + dba_save_to_file($filename, $output); + $output = NULL; + } + $result = db_query("select * from $table"); $numrow = db_num_rows($result); $fields = dba_get_fields($table); @@ -1432,10 +1438,74 @@ function dba_backup_table($table, $add_d echo $output; $output = NULL; } + if ($filename) { + dba_save_to_file($filename, $output); + $output = NULL; + } } return $output; } +function dba_save_to_file($filename, $data) { + if (empty($filename) || empty($data)) { + return; + } + $fh = fopen($filename, 'a'); + $data = "\n". $data; + fwrite($fh, $data); + fclose($fh); +} + +function dba_bzip2 ($src, $dst = FALSE) { + if($dst == FALSE){ + $dst = $src .'.gz'; + } + if (!file_exists ($src) || !is_readable ($src)) { + return FALSE; + } + if ((!file_exists ($dst) && !is_writeable (dirname ($dst)) || (file_exists($dst) && !is_writable($dst)) )) { + return FALSE; + } + + $src_handle = fopen ($src, "rb"); + $dst_handle = bzopen ($dst, "wb"); + + while (!feof ($src_handle)) { + $buffer = fgets ($src_handle, 4096); + bzwrite ($dst_handle, $buffer, 4096); + } + + fclose ($src_handle); + bzclose ($dst_handle); + + return TRUE; +} + +function dba_gzip($src, $level = 5, $dst = FALSE) { + if($dst == FALSE){ + $dst = $src .'.gz'; + } + if (!file_exists ($src) || !is_readable ($src)) { + return FALSE; + } + if ((!file_exists ($dst) && !is_writeable (dirname ($dst)) || (file_exists($dst) && !is_writable($dst)) )) { + return FALSE; + } + + $src_handle = fopen ($src, "rb"); + $dst_handle = gzopen($dst, "w$level"); + + while (!feof ($src_handle)) { + $buffer = fgets ($src_handle, 4096); + gzwrite ($dst_handle, $buffer, 4096); + } + + fclose($src_handle); + gzclose($dst_handle); + + return TRUE; +} + function dba_show_create_table($table) { if (_is_mysql()) { $query = "SHOW CREATE TABLE $table;";