Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions TestWPDBBackup.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

@require_once 'vfsStream/vfsStream.php';
define('DB_PASSWORD', 'test');
define('DB_NAME', 'test');
define('ABSPATH', 'test');
$_SERVER['SERVER_NAME'] = 'test';

/** WordPress-specific functions **/
function __($a) {
Expand Down Expand Up @@ -241,46 +244,6 @@ function wp_verify_nonce() {};
$this->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
*/
Expand Down
51 changes: 16 additions & 35 deletions wp-db-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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))
Expand Down