From e1babb733ebb5a18e38ee6d67fc09ef253070d6c Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Wed, 19 Dec 2012 21:07:04 +0100 Subject: [PATCH 1/2] Immediately compress the file instead of applying compression later This fixes out of memory errors while trying to read the whole backup file into memory at the end of the backup which resulted in backups not being delivered. --- TestWPDBBackup.php | 41 ------------------------------------- wp-db-backup.php | 51 +++++++++++++++------------------------------- 2 files changed, 16 insertions(+), 76 deletions(-) diff --git a/TestWPDBBackup.php b/TestWPDBBackup.php index 1b9e353..ca3c25c 100644 --- a/TestWPDBBackup.php +++ b/TestWPDBBackup.php @@ -1,6 +1,5 @@ assertTrue($this->_b->is_wp_secure_enough()); } - /** - * @dataProvider provider__perhaps_compress_file - */ - public function test__perhaps_compress_file($filename, $gzipped_filename, $file_exists, $gzipped_file_exists) - { - if (class_exists('vfsStream') === false) { - $this->markTestSkipped('vfsStream not installed.'); - } - - vfsStreamWrapper::register(); - $root = new vfsStreamDirectory('home'); - - $dir = dirname($filename); - if ( ! empty( $dir ) ) { - $root->addChild(new vfsStreamDirectory($dir)); - $root->getChild($dir)->addChild(vfsStream::newFile(basename($filename))->withContent('Backup SQL text')); - } - vfsStreamWrapper::setRoot($root); - - - $new_filename = $this->_b->perhaps_compress_file(vfsStream::url($filename)); - - if ( function_exists('gzencode') ) { - - $this->assertEquals($gzipped_file_exists, vfsStreamWrapper::getRoot()->hasChild($gzipped_filename)); - $this->assertEquals($file_exists, vfsStreamWrapper::getRoot()->hasChild($filename)); - } else { - $this->assertFalse(vfsStreamWrapper::getRoot()->hasChild($gzipped_filename)); - $this->assertTrue(vfsStreamWrapper::getRoot()->hasChild($filename)); - } - - - } - public function provider__perhaps_compress_file() - { - return array( - array('path/file.sql', 'path/file.sql.gz', false, true), - ); - } - /** * @dataProvider provider__send_mail */ diff --git a/wp-db-backup.php b/wp-db-backup.php index ded4d08..de135d2 100755 --- a/wp-db-backup.php +++ b/wp-db-backup.php @@ -91,7 +91,7 @@ function __construct() $table_prefix = ( isset( $table_prefix ) ) ? $table_prefix : $wpdb->prefix; $datum = gmdate("Ymd_B"); - $this->backup_filename = DB_NAME . "_$table_prefix$datum.sql"; + $this->backup_filename = DB_NAME . "_$table_prefix$datum.sql".($this->has_gz() ? '.gz' : ''); $this->backup_dir = trailingslashit(apply_filters('wp_db_b_backup_dir', WP_BACKUP_DIR)); $this->basename = 'wp-db-backup'; @@ -755,12 +755,24 @@ function backquote($a_name) { function open($filename = '', $mode = 'w') { if ('' == $filename) return false; - $fp = @fopen($filename, $mode); + if ($this->has_gz()) { + $fp = @gzopen($filename, $mode); + } else { + $fp = @fopen($filename, $mode); + } return $fp; } function close($fp) { - fclose($fp); + if ($this->has_gz()) { + gzclose($fp); + } else { + fclose($fp); + } + } + + function has_gz() { + return function_exists('gzencode'); } /** @@ -1099,43 +1111,12 @@ function send_mail($phpmailer = null, $to = '', $subject = '', $message = '', $d } - /** - * Try to gzip the file - * - * @param string $filepath The path to the file which we're trying to compress - * @return string The new filepath. - */ - function perhaps_compress_file( $filename = '' ) - { - if ( ! empty($filename) && function_exists('gzencode') ) { - $gz_filename = "{$filename}.gz"; - if ( function_exists('file_get_contents') ) { - $text = file_get_contents($filename); - } else { - $text = implode("", file($filename)); - } - $gz_text = gzencode($text, 9); - $fp = fopen($gz_filename, "w"); - if ( $fp ) { - fwrite($fp, $gz_text); - if ( fclose($fp) ) { - unlink($filename); - $filename = $gz_filename; - } - } - } - - return $filename; - } - function deliver_backup($filename = '', $delivery = 'http', $recipient = '', $location = 'main') { if ( empty( $filename ) ) { return false; } - - $diskfile = $this->perhaps_compress_file($this->backup_dir . $filename); - $filename = basename($diskfile); + $diskfile = $this->backup_dir . $filename; if ('http' == $delivery) { if (! file_exists($diskfile)) From 1d117455d1ff656673f1a7db24fb864d9b21677a Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Wed, 19 Dec 2012 21:08:32 +0100 Subject: [PATCH 2/2] Set constants and variables used by tests to dummy values This prevents errors in the test cases in my PHPUnit installation. --- TestWPDBBackup.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TestWPDBBackup.php b/TestWPDBBackup.php index ca3c25c..1cfca90 100644 --- a/TestWPDBBackup.php +++ b/TestWPDBBackup.php @@ -1,5 +1,9 @@