public function setUp() { $this->size = new Site_Size(); $this->setup_test_data(); Path::get_instance()->set_path($this->test_data . '/tmp'); Path::get_instance()->set_root($this->test_data); $this->root = new \SplFileInfo(Path::get_root()); }
/** * Get the user defined excludes. * * @return array The array of excludes. */ public function get_user_excludes() { $excludes = $this->excludes; // If path() is inside root(), exclude it if (strpos(Path::get_path(), Path::get_root()) !== false && Path::get_root() !== Path::get_path()) { array_unshift($excludes, trailingslashit(Path::get_path())); } return $this->normalize($excludes); }
public function test_unreadable_directory_ignored() { chmod(Path::get_root() . '/exclude', 0220); if (is_readable(Path::get_root() . '/exclude')) { $this->markTestSkipped("directory was readable."); } $files = $this->backup->get_files(); $this->assertEquals(count($files), 1); }
/** * Override the common version of this test as `zip` does include unreadable directories, * it just doesn't include any of the files in the unreadable directory */ public function test_backup_with_unreadable_directory() { chmod(Path::get_root() . '/exclude', 0220); if (is_readable(Path::get_root() . '/exclude')) { $this->markTestSkipped("Directory was readable."); } $this->backup->backup(); $this->assertFileExists($this->backup->get_backup_filepath()); $this->assertArchiveNotContains($this->backup->get_backup_filepath(), array('exclude')); $this->assertArchiveFileCount($this->backup->get_backup_filepath(), 2); }
/** * Perform a Backup. * * ## OPTIONS * * [--files_only] * : Backup files only, default to off * * [--database_only] * : Backup database only, defaults to off * * [--destination] * : dir that the backup should be save in, defaults to your existing backups directory * * [--root] * : dir that should be backed up, defaults to site root. * * [--archive_filename] * : filename for the resulting zip file * * [--excludes] * : list of paths you'd like to exclude * * ## Usage * * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] * * @todo errors should be bubbled from Backup, Scheduled_Backup and the like instead of being repeated. */ public function backup($args, $assoc_args) { add_action('hmbkp_mysqldump_started', function () { \WP_CLI::line(__('Backup: Dumping database...', 'backupwordpress')); }); add_action('hmbkp_archive_started', function () { \WP_CLI::line(__('Backup: Zipping everything up...', 'backupwordpress')); }); if (!empty($assoc_args['destination'])) { Path::get_instance()->set_path($assoc_args['destination']); } Path::get_instance()->cleanup(); if (!empty($assoc_args['root'])) { Path::get_instance()->set_root($assoc_args['root']); } if (!is_dir(Path::get_path())) { \WP_CLI::error(__('Invalid backup path', 'backupwordpress')); return false; } if (!is_dir(Path::get_root()) || !is_readable(Path::get_root())) { \WP_CLI::error(__('Invalid root path', 'backupwordpress')); return false; } $filename = 'backup.zip'; if (isset($assoc_args['archive_filename'])) { $filename = $assoc_args['archive_filename']; } $hm_backup = new Backup($filename); if (!empty($assoc_args['files_only'])) { $hm_backup->set_type('file'); } if (!empty($assoc_args['database_only'])) { $hm_backup->set_type('database'); } if (!empty($assoc_args['excludes'])) { $hm_backup->set_excludes($assoc_args['excludes']); } $hm_backup->run(); if (file_exists($hm_backup->get_backup_filepath())) { \WP_CLI::success(__('Backup Complete: ', 'backupwordpress') . $hm_backup->get_backup_filepath()); } else { \WP_CLI::error(__('Backup Failed', 'backupwordpress')); } }
public function backup_database() { if ($this->status) { $this->status->set_status(__('Backing up database...', 'backupwordpress')); } $database_backup_engines = apply_filters('hmbkp_database_backup_engines', array(new Mysqldump_Database_Backup_Engine(), new IMysqldump_Database_Backup_Engine())); // Set the file backup engine settings if ($this->database_dump_filename) { foreach ($database_backup_engines as &$backup_engine) { $backup_engine->set_backup_filename($this->database_dump_filename); } } // Dump the database $database_dump = $this->perform_backup($database_backup_engines); if (is_a($database_dump, __NAMESPACE__ . '\\Backup_Engine')) { $this->database_dump_filepath = $database_dump->get_backup_filepath(); } // Fire up the file backup engines $file_backup_engines = apply_filters('hmbkp_file_backup_engines', array(new Zip_File_Backup_Engine(), new Zip_Archive_File_Backup_Engine())); // Set the file backup engine settings foreach ($file_backup_engines as &$backup_engine) { $backup_engine->set_backup_filename($this->backup_filename); $backup_engine->set_excludes(new Excludes(array('*.zip', 'index.html', '.htaccess', '.*-running'))); } // Zip up the database dump $root = Path::get_root(); Path::get_instance()->set_root(Path::get_path()); $file_backup = $this->perform_backup($file_backup_engines); Path::get_instance()->set_root($root); if (is_a($file_backup, __NAMESPACE__ . '\\Backup_Engine')) { $this->backup_filepath = $file_backup->get_backup_filepath(); } // Delete the Database Backup now that we've zipped it up if (file_exists($this->database_dump_filepath)) { unlink($this->database_dump_filepath); } }
/** * Check if a file is excluded, * i.e. excluded directly or is in an excluded folder. * * @param \SplFileInfo $file File to check if it's excluded. * * @return bool|null True if file is excluded, false otherwise. * Null - if it's not a file. */ public function is_file_excluded(\SplFileInfo $file) { $exclude_string = implode('|', $this->get_excludes_for_regex()); $file_path_no_root = str_ireplace(trailingslashit(Path::get_root()), '', wp_normalize_path($file->getPathname())); if ($exclude_string && preg_match('(' . $exclude_string . ')', $file_path_no_root)) { return true; } return false; }
function set_server_config_notices() { $notices = Notices::get_instance(); $messages = array(); if (!is_dir(Path::get_path())) { $messages[] = sprintf(__('The backups directory can\'t be created because your %s directory isn\'t writable. Please create the folder manually.', 'backupwordpress'), '<code>' . esc_html(dirname(Path::get_path())) . '</code>'); } if (is_dir(Path::get_path()) && !wp_is_writable(Path::get_path())) { $messages[] = __('The backups directory isn\'t writable. Please fix the permissions.', 'backupwordpress'); } if (Backup_Utilities::is_safe_mode_on()) { $messages[] = sprintf(__('%1$s is running in %2$s, please contact your host and ask them to disable it. BackUpWordPress may not work correctly whilst %3$s is on.', 'backupwordpress'), '<code>PHP</code>', sprintf('<a href="%1$s">%2$s</a>', __('http://php.net/manual/en/features.safe-mode.php', 'backupwordpress'), __('Safe Mode', 'backupwordpress')), '<code>' . __('Safe Mode', 'backupwordpress') . '</code>'); } if (defined('HMBKP_PATH') && HMBKP_PATH) { // Suppress open_basedir warning https://bugs.php.net/bug.php?id=53041 if (!path_in_php_open_basedir(HMBKP_PATH)) { $messages[] = sprintf(__('Your server has an %1$s restriction in effect and your custom backups directory (%2$s) is not within the allowed path(s): (%3$s).', 'backupwordpress'), '<code>open_basedir</code>', '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(@ini_get('open_basedir')) . '</code>'); } elseif (!file_exists(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom path does not exist', 'backupwordpress')); } else { if (!is_dir(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom backups directory %1$s doesn\'t exist and can\'t be created, your backups will be saved to %2$s instead.', 'backupwordpress'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(Path::get_path()) . '</code>'); } if (is_dir(HMBKP_PATH) && !wp_is_writable(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom backups directory %1$s isn\'t writable, new backups will be saved to %2$s instead.', 'backupwordpress'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(Path::get_path()) . '</code>'); } } } if (!is_readable(Path::get_root())) { $messages[] = sprintf(__('Your site root path %s isn\'t readable.', 'backupwordpress'), '<code>' . Path::get_root() . '</code>'); } if (!Requirement_Mysqldump_Command_Path::test() && !Requirement_PDO::test()) { $messages[] = sprintf(__('Your database cannot be backed up because your server doesn\'t support %1$s or %2$s. Please contact your host and ask them to enable them.', 'backupwordpress'), '<code>mysqldump</code>', '<code>PDO</code>'); } if (count($messages) > 0) { $notices->set_notices('server_config', $messages, false); } }
public function test_adding_files_to_existing_backup() { $this->backup->backup(); $filepath = $this->backup->get_backup_filepath(); $this->assertFileExists($this->backup->get_backup_filepath()); $this->assertArchiveContains($this->backup->get_backup_filepath(), array('test-data.txt', 'exclude', 'exclude/exclude.exclude')); $this->assertArchiveFileCount($this->backup->get_backup_filepath(), 3); // create a new file if (!file_put_contents(Path::get_root() . '/new.file', 'test')) { $this->markTestSkipped('new.file couldn\'t be created'); } $this->backup->backup(); $this->assertEquals($filepath, $this->backup->get_backup_filepath()); $this->assertFileExists($this->backup->get_backup_filepath()); $this->assertArchiveContains($this->backup->get_backup_filepath(), array('new.file')); $this->assertArchiveFileCount($this->backup->get_backup_filepath(), 4); }
function calculate_site_size() { $site_size = new Site_Size(); if (!$site_size->is_site_size_cached()) { $root = new \SplFileInfo(Path::get_root()); $site_size->filesize($root); } }
/** * @return string */ public static function test() { return Path::get_root(); }
</td> <td></td> </tr> </thead> <tbody> <?php if ($files) { foreach ($files as $size => $file) { $is_excluded = $is_unreadable = false; // Check if the file is excluded if ($exclude_string && preg_match('(' . $exclude_string . ')', str_ireplace(trailingslashit(Path::get_root()), '', wp_normalize_path($file->getPathname())))) { $is_excluded = true; } // Skip unreadable files if (!@realpath($file->getPathname()) || !$file->isReadable()) { $is_unreadable = true; } ?> <tr> <td> <?php if ($is_unreadable) { ?>
/** * Check if a backup is possible with regards to file * permissions etc. * * @return bool */ function is_backup_possible() { if (!wp_is_writable(Path::get_path()) || !is_dir(Path::get_path())) { return false; } if (!is_readable(Path::get_root())) { return false; } return true; }
public function testBackUpDirIsExcludedWhenBackUpDirIsInRoot() { $excludes = new Excludes(); $this->assertContains(Path::get_root(), Path::get_path()); $this->assertContains(str_replace(trailingslashit(Path::get_root()), '', Path::get_path()), $excludes->get_excludes()); }
/** * Convert the exclude rules to a format zip accepts * * @return string The exclude string ready to pass to `zip -x` */ public function get_exclude_string() { if (!$this->excludes) { return ''; } $excludes = $this->excludes->get_excludes(); foreach ($excludes as $key => &$rule) { $file = $absolute = $fragment = false; // Files don't end with / if (!in_array(substr($rule, -1), array('\\', '/'))) { $file = true; } elseif (in_array(substr($rule, 0, 1), array('\\', '/'))) { $absolute = true; } else { $fragment = true; } $rule = str_ireplace(Path::get_root(), '', untrailingslashit(wp_normalize_path($rule))); // Strip the preceeding slash if (in_array(substr($rule, 0, 1), array('\\', '/'))) { $rule = substr($rule, 1); } // Wrap directory fragments and files in wildcards for zip if ($fragment || $file) { $rule = '*' . $rule . '*'; } // Add a wildcard to the end of absolute url for zips if ($absolute) { $rule .= '*'; } } // Escape shell args for zip command $excludes = array_map('escapeshellarg', array_unique($excludes)); return implode(' -x ', $excludes); }
/** * Check if a backup is possible with regards to file * permissions etc. * * @return bool */ function is_backup_possible() { if (!wp_is_writable(Path::get_path()) || !is_dir(Path::get_path())) { return false; } if (!is_readable(Path::get_root())) { return false; } if (disk_space_low()) { return false; } if (!Requirement_Mysqldump_Command_Path::test() && !Requirement_PDO::test()) { return false; } if (!Requirement_Zip_Command_Path::test() && !Requirement_Zip_Archive::test()) { return false; } return true; }
<td> <code><?php echo esc_html(substr(sprintf('%o', fileperms(Path::get_root())), -4)); ?> </code> </td> <td> <code> <?php if (is_link(Path::get_root())) { esc_html_e('Symlink', 'backupwordpress'); } elseif (is_dir(Path::get_root())) { esc_html_e('Folder', 'backupwordpress'); } ?> </code> </td> <td></td> </tr> </thead> <tbody>