public function test_site_size_with_filescanner_complete_equals_database_plus_files() { $this->size->recursive_filesize_scanner(); $size_complete = $this->size; $size_database = new Site_Size('database'); $size_file = new Site_Size('file'); $this->assertNotEmpty($size_database->get_site_size()); $this->assertNotEmpty($size_file->get_site_size()); $this->assertEquals($size_complete->get_site_size(), $size_database->get_site_size() + $size_file->get_site_size()); }
<td> <input type="number" id="hmbkp_schedule_max_backups" name="hmbkp_schedule_max_backups" min="1" step="1" value="<?php echo esc_attr($schedule->get_max_backups()); ?> " /> <p class="description"> <?php printf(__('Past this limit older backups will be deleted automatically.', 'backupwordpress')); ?> <?php $site_size = new Site_Size($schedule->get_type(), $schedule->get_excludes()); if ($site_size->is_site_size_cached()) { printf(__('This schedule will store a maximum of %s of backups.', 'backupwordpress'), '<code>' . esc_html(size_format($site_size->get_site_size() * $schedule->get_max_backups())) . '</code>'); } ?> </p> </td> </tr> <?php foreach (Services::get_services($schedule) as $service) { $service->field(); }
/** * Returns a formatted string containing the calculated total site size or a message * to indicate it is being calculated. * * @param HM\BackUpWordPress\Scheduled_Backup $schedule * * @return string */ function get_site_size_text(Scheduled_Backup $schedule) { if (isset($_GET['hmbkp_add_schedule'])) { return ''; } $site_size = new Site_Size($schedule->get_type(), $schedule->get_excludes()); if ('database' === $schedule->get_type() || $site_size->is_site_size_cached()) { return sprintf('(<code title="' . __('Backups will be compressed and should be smaller than this.', 'backupwordpress') . '">%s</code>)', esc_html($site_size->get_formatted_site_size())); } return ''; }
/** * Receive the heartbeat and return backup status */ function heartbeat_received($response, $data) { $response['heartbeat_interval'] = 'fast'; if (!empty($data['hmbkp_schedule_id'])) { $schedule = new Scheduled_Backup(sanitize_text_field(urldecode($data['hmbkp_schedule_id']))); $status = new Backup_Status($schedule->get_id()); if (!empty($data['hmbkp_is_in_progress'])) { if (!$status->get_status()) { $response['hmbkp_schedule_status'] = 0; // Slow the heartbeat back down $response['heartbeat_interval'] = 'slow'; } else { $response['hmbkp_schedule_status'] = schedule_status($schedule, false); } } if (!empty($data['hmbkp_client_request'])) { $site_size = new Site_Size($schedule->get_type(), $schedule->get_excludes()); // Pass the site size to be displayed when it's ready. if ($site_size->is_site_size_cached()) { $response['hmbkp_site_size'] = $site_size->get_formatted_site_size(); ob_start(); require HMBKP_PLUGIN_PATH . 'admin/schedule-form-excludes.php'; $response['hmbkp_dir_sizes'] = ob_get_clean(); // Slow the heartbeat back down $response['heartbeat_interval'] = 'slow'; } } } return $response; }
/** * @return array */ public static function test() { $backup_sizes = array(); $schedules = Schedules::get_instance(); foreach ($schedules->get_schedules() as $schedule) { $site_size = new Site_Size($schedule->get_type(), $schedule->get_excludes()); if ($site_size->is_site_size_cached()) { $backup_sizes[$schedule->get_type()] = $site_size->get_formatted_site_size(); } } return $backup_sizes; }
/** * Return the contents of `$directory` as a single depth list ordered by total filesize. * * Will schedule background threads to recursively calculate the filesize of subdirectories. * The total filesize of each directory and subdirectory is cached in a transient for 1 week. * * @param string $directory The directory to list * * @todo doesn't really belong in this class, should just be a function * @return array returns an array of files ordered by filesize */ function list_directory_by_total_filesize($directory, Excludes $excludes) { $files = $files_with_no_size = $empty_files = $files_with_size = $unreadable_files = array(); if (!is_dir($directory)) { return $files; } $finder = new \Symfony\Component\Finder\Finder(); $finder->followLinks(); $finder->ignoreDotFiles(false); $finder->ignoreUnreadableDirs(); $finder->depth('== 0'); $site_size = new Site_Size('file', $excludes); $files = $finder->in($directory); foreach ($files as $entry) { // Get the total filesize for each file and directory $filesize = $site_size->filesize($entry); if ($filesize) { // If there is already a file with exactly the same filesize then let's keep increasing the filesize of this one until we don't have a clash while (array_key_exists($filesize, $files_with_size)) { $filesize++; } $files_with_size[$filesize] = $entry; } elseif (0 === $filesize) { $empty_files[] = $entry; } else { $files_with_no_size[] = $entry; } } // Sort files by filesize, largest first krsort($files_with_size); // Add 0 byte files / directories to the bottom $files = $files_with_size + array_merge($empty_files, $unreadable_files); // Add directories that are still calculating to the top if ($files_with_no_size) { // We have to loop as merging or concatenating the array would re-flow the keys which we don't want because the filesize is stored in the key foreach ($files_with_no_size as $entry) { array_unshift($files, $entry); } } return $files; }
<td> <input type="number" id="hmbkp_schedule_max_backups" name="hmbkp_schedule_max_backups" min="1" step="1" value="<?php echo esc_attr($schedule->get_max_backups()); ?> " /> <p class="description"> <?php printf(__('Past this limit older backups will be deleted automatically.', 'backupwordpress')); ?> <?php $site_size = new Site_Size(); if (Site_Size::is_site_size_cached()) { printf(__('This schedule will store a maximum of %s of backups.', 'backupwordpress'), '<code>' . esc_html(size_format($site_size->get_site_size($schedule->get_type(), $schedule->get_excludes()) * $schedule->get_max_backups())) . '</code>'); } ?> </p> </td> </tr> <?php foreach (Services::get_services($schedule) as $service) { $service->field(); }
_e('Here\'s a directory listing of all files on your site, you can browse through and exclude files or folders that you don\'t want included in your backup.', 'backupwordpress'); ?> </p> <?php // The directory to display $directory = Path::get_root(); if (isset($_GET['hmbkp_directory_browse'])) { $untrusted_directory = urldecode($_GET['hmbkp_directory_browse']); // Only allow real sub directories of the site root to be browsed if (false !== strpos($untrusted_directory, Path::get_root()) && is_dir($untrusted_directory)) { $directory = $untrusted_directory; } } $exclude_string = implode('|', $excludes->get_excludes_for_regex()); $site_size = new Site_Size(); // Kick off a recursive filesize scan $files = list_directory_by_total_filesize($directory); ?> <table class="widefat"> <thead> <tr> <th></th> <th scope="col"><?php _e('Name', 'backupwordpress'); ?> </th> <th scope="col" class="column-format"><?php
/** * File is NOT excluded directly (either in the root or any non-excluded sub-directory). * * Main test: Site_Size->filesize( $file ) * Expected: file size should be what it is. */ public function test_file_size_not_excluded_directly() { $file_path = $this->test_data . '/test-data.txt'; $file = new \SplFileInfo($file_path); // Check the file is created and its size is NOT 0. $this->assertContains($this->root->getPath(), $file->getPath()); $this->assertNotSame($file->getSize(), 0); // Check file size via BWP function. It should NOT be 0, should be the same size as via getSize(). $site_size = new Site_Size('file'); $this->assertNotSame($site_size->filesize($file), 0); $this->assertSame($site_size->filesize($file), $file->getSize()); }
/** * Returns a formatted string containing the calculated total site size or a message * to indicate it is being calculated. * * @param HM\BackUpWordPress\Scheduled_Backup $schedule * * @return string */ function get_site_size_text(Scheduled_Backup $schedule) { if (isset($_GET['hmbkp_add_schedule'])) { return ''; } $site_size = new Site_Size($schedule->get_type(), $schedule->get_excludes()); if ('database' === $schedule->get_type() || $site_size->is_site_size_cached()) { return sprintf('(<code title="' . __('Backups will be compressed and should be smaller than this.', 'backupwordpress') . '">%s</code>)', esc_attr($site_size->get_formatted_site_size())); } else { return sprintf('(<code class="calculating" title="' . __('this shouldn\'t take long…', 'backupwordpress') . '">' . __('calculating the size of your site…', 'backupwordpress') . '</code>)'); } }
?> </p> <?php // The directory to display. $directory = Path::get_root(); if (isset($_GET['hmbkp_directory_browse'])) { $untrusted_directory = urldecode($_GET['hmbkp_directory_browse']); // Only allow real sub directories of the site root to be browsed. if (false !== strpos($untrusted_directory, Path::get_root()) && is_dir($untrusted_directory)) { $directory = $untrusted_directory; } } $exclude_string = implode('|', $excludes->get_excludes_for_regex()); $site_size = new Site_Size('file'); $excluded_site_size = new Site_Size('file', $excludes); // Kick off a recursive filesize scan. $files = list_directory_by_total_filesize($directory, $excludes); ?> <table class="widefat"> <thead> <tr> <th></th> <th scope="col"><?php esc_html_e('Name', 'backupwordpress'); ?> </th> <th scope="col" class="column-format"><?php
/** * Check whether the server is low on disk space. * * @return bool Whether there's less disk space less than 2 * the entire size of the site. */ function disk_space_low($backup_size = false) { $disk_space = @disk_free_space(Path::get_path()); if (!$disk_space) { return false; } if (!$backup_size) { $site_size = new Site_Size('complete', new Excludes()); if (!$site_size->is_site_size_cached()) { return false; } $backup_size = $site_size->get_site_size() * 2; } return $backup_size >= $disk_space; }
?> </p> <?php // The directory to display $directory = Path::get_root(); if (isset($_GET['hmbkp_directory_browse'])) { $untrusted_directory = urldecode($_GET['hmbkp_directory_browse']); // Only allow real sub directories of the site root to be browsed if (false !== strpos($untrusted_directory, Path::get_root()) && is_dir($untrusted_directory)) { $directory = $untrusted_directory; } } $exclude_string = implode('|', $excludes->get_excludes_for_regex()); $site_size = new Site_Size('file'); $excluded_site_size = new Site_Size('file', $excludes); // Kick off a recursive filesize scan $files = list_directory_by_total_filesize($directory, $excludes); ?> <table class="widefat"> <thead> <tr> <th></th> <th scope="col"><?php _e('Name', 'backupwordpress'); ?> </th> <th scope="col" class="column-format"><?php