public/wordpress/wp-content/plugins/backupbuddy/classes/housekeeping.php
/*	trim_remote_send_stats()
	 *	
	 *	Handles trimming the number of remote sends to the most recent ones.
	 *	Recent transfer logs are kept for TRIPLE the max age as they are important for troubleshooting.
	 *
	 *	NOTE: Checks age first. Next max limit. Only deletes based on limit count if send is finished or failed.
	 *	
	 *	@return		null
	 */


Handles trimming the number of remote sends to the most recent ones.
Recent transfer logs are kept for TRIPLE the max age as they are important for troubleshooting.

Checks age first. Next max limit. Only deletes based on limit count if send is finished or failed.

@return         null
Handles trimming the number of remote sends to the most recent ones . Recent transfer logs are kept for TRIPLE the max age as they are important for troubleshooting . Checks age first . Next max limit . Only deletes based on limit count if send is finished or failed . @return null
public static function trim_remote_send_stats( $file_prefix = 'send-', $limit = '', $max_age = '', $purge_log = false ) {
		require_once( pb_backupbuddy::plugin_path() . '/classes/fileoptions.php' );
		pb_backupbuddy::status( 'details', 'Cleaning up remote send stats.' );
		if ( '' == $limit ) {
			$limit = (int)pb_backupbuddy::$options['max_send_stats_count']; 
		}
		if ( '' == $max_age ) {
			$max_age = (int)pb_backupbuddy::$options['max_send_stats_days'] * 60 * 60 * 24;
		}
		$send_fileoptions = pb_backupbuddy::$filesystem->glob_by_date( backupbuddy_core::getLogDirectory() . 'fileoptions/' . $file_prefix . '*.txt', $mode = 'mtime' );
		if ( ! is_array( $send_fileoptions ) ) {
			$send_fileoptions = array();
		}
		if ( count( $send_fileoptions ) <= $limit ) {
			return;
		}
		$i = 0;
		foreach( $send_fileoptions as $time => $send_fileoption ) {
			$i++;
			if ( ( time() - $time ) > $max_age ) {
				if ( false === @unlink( $send_fileoption ) ) {
					pb_backupbuddy::status( 'warning', 'Unable to delete old remote send fileoptions file `' . $send_fileoption . '`. You may manually delete it. Check directory permissions for future cleanup.' );
				} else { 
					@unlink( str_replace( '.txt', '.lock', $send_fileoption ) ); 
				}
				$i--; 
				continue;
			}
			if ( $i > $limit ) { 
				if ( ! file_exists( $send_fileoption ) ) {
					continue;
				}
				$send_fileoption_obj = new pb_backupbuddy_fileoptions( $send_fileoption, $read_only = true );
				if ( true !== ( $result = $send_fileoption_obj->is_ok() ) ) { 
					unset( $send_fileoption_obj );
					continue;
				} else {
					if ( ( 0 == $send_fileoption_obj->options['finish_time'] ) || ( ( -1 != $send_fileoption_obj->options['finish_time'] ) && ( ( time() - $send_fileoption_obj->options['finish_time'] ) < backupbuddy_constants::CLEANUP_FINISHED_FILEOPTIONS_AGE_DELAY ) ) ) { 
						unset( $send_fileoption_obj );
						continue;
					}
				}
				if ( false === @unlink( $send_fileoption ) ) {
					pb_backupbuddy::status( 'warning', 'Unable to delete old remote send fileoptions file `' . $send_fileoption . '`. You may manually delete it. Check directory permissions for future cleanup.' );
				} else { 
					@unlink( str_replace( '.txt', '.lock', $send_fileoption ) ); 
				}
				if ( true === $purge_log ) {
					$log_file = backupbuddy_core::getLogDirectory() . 'status-remote_send-' . $send_fileoption_obj->options['sendID'] . '_' . pb_backupbuddy::$options['log_serial'] . '.txt';
					@unlink( $log_file );
				}
				unset( $send_fileoption_obj );
			}
		}
		return;
	}
public static function trim_remote_send_stats ( $ file_prefix = 'send-' , $ limit = '' , $ max_age = '' , $ purge_log = false ) { require_once ( pb_backupbuddy :: plugin_path ( ) . '/classes/fileoptions.php' ) ; pb_backupbuddy :: status ( 'details' , 'Cleaning up remote send stats.' ) ; if ( '' == $ limit ) { $ limit = ( int ) pb_backupbuddy :: $ options [ 'max_send_stats_count' ] ; } if ( '' == $ max_age ) { $ max_age = ( int ) pb_backupbuddy :: $ options [ 'max_send_stats_days' ] * 60 * 60 * 24 ; } $ send_fileoptions = pb_backupbuddy :: $ filesystem -> glob_by_date ( backupbuddy_core :: getLogDirectory ( ) . 'fileoptions/' . $ file_prefix . '*.txt' , $ mode = 'mtime' ) ; if ( ! is_array ( $ send_fileoptions ) ) { $ send_fileoptions = array ( ) ; } if ( count ( $ send_fileoptions ) <= $ limit ) { return ; } $ i = 0 ; foreach ( $ send_fileoptions as $ time => $ send_fileoption ) { $ i ++ ; if ( ( time ( ) - $ time ) > $ max_age ) { if ( false === @ unlink ( $ send_fileoption ) ) { pb_backupbuddy :: status ( 'warning' , 'Unable to delete old remote send fileoptions file `' . $ send_fileoption . '`. You may manually delete it. Check directory permissions for future cleanup.' ) ; } else { @ unlink ( str_replace ( '.txt' , '.lock' , $ send_fileoption ) ) ; } $ i -- ; continue ; } if ( $ i > $ limit ) { if ( ! file_exists ( $ send_fileoption ) ) { continue ; } $ send_fileoption_obj = new pb_backupbuddy_fileoptions ( $ send_fileoption , $ read_only = true ) ; if ( true !== ( $ result = $ send_fileoption_obj -> is_ok ( ) ) ) { unset ( $ send_fileoption_obj ) ; continue ; } else { if ( ( 0 == $ send_fileoption_obj -> options [ 'finish_time' ] ) || ( ( - 1 != $ send_fileoption_obj -> options [ 'finish_time' ] ) && ( ( time ( ) - $ send_fileoption_obj -> options [ 'finish_time' ] ) < backupbuddy_constants :: CLEANUP_FINISHED_FILEOPTIONS_AGE_DELAY ) ) ) { unset ( $ send_fileoption_obj ) ; continue ; } } if ( false === @ unlink ( $ send_fileoption ) ) { pb_backupbuddy :: status ( 'warning' , 'Unable to delete old remote send fileoptions file `' . $ send_fileoption . '`. You may manually delete it. Check directory permissions for future cleanup.' ) ; } else { @ unlink ( str_replace ( '.txt' , '.lock' , $ send_fileoption ) ) ; } if ( true === $ purge_log ) { $ log_file = backupbuddy_core :: getLogDirectory ( ) . 'status-remote_send-' . $ send_fileoption_obj -> options [ 'sendID' ] . '_' . pb_backupbuddy :: $ options [ 'log_serial' ] . '.txt' ; @ unlink ( $ log_file ) ; } unset ( $ send_fileoption_obj ) ; } } return ; }
trim_remote_send_stats()
trim_remote_send_stats ()
// Maximum number of remote sends to keep track of. // Gets files, newest first. // Return if limit not yet met. // Deleted. // Remove lock file if exists. // Deleted so remove from limit counter. // Outside limit. Delete only if finished OR failed. // Make sure file still exists before processing it. // Don't delete if unfinished and not failed. // Could not open. Skip since we can't verify status. // Keep unfinished. Keep non-fails that are not too old. // Still unfinished OR ( NOT Failed AND finished too recently to delete ) // TODO: (maybe).. If 0==finish_time then check the filemtime of the fileoptions file. If no progress in a certain amount of time, consider timed out?
 // Made it here so must be finished or failed. // Deleted. // Remove lock file if exists.