Skip to content

Commit

Permalink
Issue 391 (#393)
Browse files Browse the repository at this point in the history
* First stab at filelist analyzer

* oops

* Misc fixes for #391

* Clear up confusion with tmp key

* filelist_analysis for the win fixes #391

* I am a loser. Fixes #391
  • Loading branch information
bwmarkle authored Oct 13, 2020
1 parent 0136fec commit f034a30
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 2 deletions.
15 changes: 14 additions & 1 deletion admin/class-boldgrid-backup-admin-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,8 @@ public function generate_archive_path( $extension = null ) {
public function archive_files( $save = false, $dryrun = false ) {
$this->archiving_files = true;

$this->logger->init( 'archive-' . time() . '.log' );
$log_time = time();
$this->logger->init( 'archive-' . $log_time . '.log' );
$this->logger->add( 'Backup process initialized.' );

$this->utility->bump_memory_limit( '1G' );
Expand Down Expand Up @@ -1806,6 +1807,18 @@ public function archive_files( $save = false, $dryrun = false ) {
]
);

if ( Boldgrid_Backup_Admin_Filelist_Analyzer::is_enabled() ) {
$this->logger->add_separator();
$this->logger->add( 'Starting to analyze filelist...' );
$this->logger->add_memory();

$filelist_analyzer = new Boldgrid_Backup_Admin_Filelist_Analyzer( $filelist, $log_time );
$filelist_analyzer->run();

$this->logger->add( 'Finished analyzing filelist!' );
$this->logger->add_memory();
}

/*
* Use the chosen compressor to build an archive.
* If the is no available compressor, then return an error.
Expand Down
168 changes: 168 additions & 0 deletions admin/class-boldgrid-backup-admin-filelist-analyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* File: class-boldgrid-backup-admin-filelist-analyzer.php
*
* @link https://www.boldgrid.com
* @since SINCEVERSION
*
* @package Boldgrid_Backup
* @subpackage Boldgrid_Backup/admin
* @copyright BoldGrid
* @version $Id$
* @author BoldGrid <[email protected]>
*/

/**
* Class: Boldgrid_Backup_Admin_Filelist
*
* @since SINCEVERSION
*/
class Boldgrid_Backup_Admin_Filelist_Analyzer {
/**
* The key used to save this setting in the Total Upkeep settings.
*
* @since SINCEVERSION
* @var string
*/
public static $settings_key = 'filelist_analysis';

/**
* The unix timestamp used for the parent backup's log file.
*
* This is passed in via the constructor, and is only used when creating the log file. We want
* the unixtime in the "backup" and "backup filelist" log fies to match so you know their is a
* relationship.
*
* @since SINCEVERSION
* @access private
* @var int
*/
private $log_time;

/**
* An array of files.
*
* This is passed in via the contructor. This is the same filelist that is passed to each compressor
* so they know which files to backup.
*
* @since SINCEVERSION
* @access private
* @var array
*/
private $filelist;

/**
* Constructor.
*
* @since SINCEVERSION
*
* @param array $filelist
* @param int $log_time
*/
public function __construct( $filelist = array(), $log_time ) {
$this->filelist = is_array( $filelist ) ? $filelist : array();
$this->log_time = ! empty( $log_time ) ? $log_time : time();
}

/**
* Whether or not the filelist analyer is enabled.
*
* @since SINCEVERSION
*
* @return bool
*/
public static function is_enabled() {
$core = apply_filters( 'boldgrid_backup_get_core', null );
$setting = $core->settings->get_setting( self::$settings_key );

return ! empty( $setting );
}

/**
* Run.
*
* Do all the magic and write the log file.
*
* @since SINCEVERSION
*/
public function run() {
$core = apply_filters( 'boldgrid_backup_get_core', null );
$logger = new Boldgrid_Backup_Admin_Log( $core );

$size_by_extension = array();
$count_by_extension = array();
$size_by_dir = array();

$logger->init( 'archive-' . $this->log_time . '-filelist.log' );

// Loop through each file.
foreach ( $this->filelist as $file ) {
$extension = pathinfo( $file[1], PATHINFO_EXTENSION );
$dir = dirname( $file[1] );

// Generate our stats.
$size_by_extension[ $extension ] = empty( $size_by_extension[ $extension ] ) ? $file[2] : $size_by_extension[ $extension ] + $file[2];
$count_by_extension[ $extension ] = empty( $count_by_extension[ $extension ] ) ? 1 : $count_by_extension[ $extension ] + 1;
$size_by_dir[ $dir ] = empty( $size_by_dir[ $dir ] ) ? $file[2] : $size_by_dir[ $dir ] + $file[2];
}

// Display top 100 files.
$limit = 100;
$to_show = count( $size_by_extension ) >= $limit ? $limit : count( $size_by_extension );
$key = 1;

$logger->add_separator();
$logger->add( 'LARGEST FILES' );

usort( $this->filelist, function ( $item1, $item2 ) {
return $item1[2] <= $item2[2] ? 1 : -1;
} );

foreach ( $this->filelist as $file ) {
$logger->add( '(' . $key . ') ' . $file[1] . ' - ' . size_format( $file[2], 2 ) );

$key++;
if ( $key > $to_show ) {
break;
}
}

// Display size by extension.
$limit = 30;
$to_show = count( $size_by_extension ) >= $limit ? $limit : count( $size_by_extension );
$key = 1;

$logger->add_separator();
$logger->add( 'SIZE BY EXTENSION' );

arsort( $size_by_extension );

foreach ( $size_by_extension as $extension => $size ) {
$logger->add( '(' . $key . ') .' . $extension . ' - ' . number_format( $count_by_extension[ $extension ] ) . ' files totaling ' . size_format( $size, 2 ) );

$key++;
if ( $key > $to_show ) {
break;
}
}

// Display size by directory.
$limit = 30;
$to_show = count( $size_by_dir ) >= $limit ? $limit : count( $size_by_dir );
$key = 1;

$logger->add_separator();
$logger->add( 'SIZE BY DIRECTORY' );

arsort( $size_by_dir );

foreach ( $size_by_dir as $directory => $size ) {
$logger->add( '(' . $key . ') ' . $directory . ' - ' . size_format( $size, 2 ) );

$key++;
if ( $key > $to_show ) {
break;
}
}
}
}
7 changes: 7 additions & 0 deletions admin/class-boldgrid-backup-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ private function update_settings() {
}
}

/*
* Save "backup filelist analysis" setting.
*
* @since SINCEVERSION
*/
$settings['filelist_analysis'] = ! empty( $_POST['filelist_analysis'] ) ? 1 : 0;

/*
* Save Compression Level Settings.
*
Expand Down
3 changes: 2 additions & 1 deletion admin/partials/boldgrid-backup-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
$nav = include BOLDGRID_BACKUP_PATH . '/admin/partials/boldgrid-backup-admin-nav.php';
$scheduler = include BOLDGRID_BACKUP_PATH . '/admin/partials/settings/scheduler.php';
$compressor = include BOLDGRID_BACKUP_PATH . '/admin/partials/settings/compressor.php';
$backup_logs = include BOLDGRID_BACKUP_PATH . '/admin/partials/settings/backup-logs.php';
$folders_include = include BOLDGRID_BACKUP_PATH . '/admin/partials/settings/folders.php';
$db = include BOLDGRID_BACKUP_PATH . '/admin/partials/settings/db.php';
$auto_backup = include BOLDGRID_BACKUP_PATH . '/admin/partials/settings/auto-backup.php';
Expand Down Expand Up @@ -57,7 +58,7 @@
[
'id' => 'section_process',
'title' => __( 'Backup Process', 'boldgrid-backup' ),
'content' => $compressor,
'content' => $compressor . $backup_logs,
],
[
'id' => 'connect_key',
Expand Down
48 changes: 48 additions & 0 deletions admin/partials/settings/backup-logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* File: backup-logs.php
*
* Backup logs on settings page.
*
* @link https://www.boldgrid.com
* @since SINCEVERSION
*
* @package Boldgrid_Backup
* @subpackage Boldgrid_Backup/admin/partials/settings
* @copyright BoldGrid
* @version $Id$
* @author BoldGrid <[email protected]>
*/

defined( 'WPINC' ) || die;

$checked = Boldgrid_Backup_Admin_Filelist_Analyzer::is_enabled() ? 'checked' : '';

ob_start();
?>
<div class="bg-box">
<div class="bg-box-top">
<?php esc_html_e( 'Backup Logs', 'boldgrid-backup' ); ?>
</div>
<div class="bg-box-bottom">

<table class="form-table">
<tr>
<th>
<strong><?php esc_html_e( 'Filelist Analysis', 'boldgrid-backup' ); ?></strong>
<p style="font-weight: normal;">
<?php esc_html_e( 'Include a filelist analysis log file with each backup. This log file will show you the largest files and directories that were added to your backup, and can be useful in troubleshooting.', 'boldgrid-backup' ); ?>
</p>
</th>
<td>
<input type="checkbox" name="filelist_analysis" value="1" <?php echo esc_attr( $checked ); ?>>
</td>
</tr>
</table>
</div>
</div>

<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
1 change: 1 addition & 0 deletions includes/class-boldgrid-backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ private function load_dependencies() {
require_once BOLDGRID_BACKUP_PATH . '/admin/class-boldgrid-backup-admin-restore-git.php';

require_once BOLDGRID_BACKUP_PATH . '/admin/class-boldgrid-backup-admin-filelist.php';
require_once BOLDGRID_BACKUP_PATH . '/admin/class-boldgrid-backup-admin-filelist-analyzer.php';

require_once BOLDGRID_BACKUP_PATH . '/admin/class-boldgrid-backup-admin-compressor.php';
require_once BOLDGRID_BACKUP_PATH . '/admin/class-boldgrid-backup-admin-compressors.php';
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'/admin/class-boldgrid-backup-admin-restore-helper.php',
'/admin/class-boldgrid-backup-admin-restore-git.php',
'/admin/class-boldgrid-backup-admin-filelist.php',
'/admin/class-boldgrid-backup-admin-filelist-analyzer.php',
'/admin/class-boldgrid-backup-admin-backup-dir.php',
'/admin/class-boldgrid-backup-admin-home-dir.php',
'/admin/class-boldgrid-backup-admin-compressors.php',
Expand Down

0 comments on commit f034a30

Please sign in to comment.