Skip to content
Merged
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
72 changes: 70 additions & 2 deletions src/helper/Site_Backup_Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,44 @@ private function pre_backup_check() {
EE::debug( 'Free space: ' . $free_space );

if ( $site_size > $free_space ) {
EE::error( 'Not enough disk space to take backup. Please free up some space and try again.' );
$error_message = $this->build_disk_space_error_message( 'backup', $site_size, $free_space );

$this->fs->remove( EE_BACKUP_DIR . '/' . $this->site_data['site_url'] . '.lock' );
EE::error( $error_message );
}
}

/**
* Build a disk space error message for backup/restore operations.
*
* @param string $operation The operation name ('backup' or 'restore').
* @param int $required_space The required disk space in bytes.
* @param int $free_space The available free space in bytes.
*
* @return string The formatted error message.
*/
private function build_disk_space_error_message( $operation, $required_space, $free_space ) {
$additional_space = $required_space - $free_space;
$required_formatted = $this->format_bytes( $required_space );
$available_formatted = $this->format_bytes( $free_space );
$needed_formatted = $this->format_bytes( $additional_space );

return sprintf(
"Not enough disk space to take %s.\n" .
"Required: %s (%s bytes)\n" .
"Available: %s (%s bytes)\n" .
"Additional space needed: %s (%s bytes)\n" .
"Please free up some space and try again.",
$operation,
$required_formatted,
number_format( $required_space ),
$available_formatted,
number_format( $free_space ),
$needed_formatted,
number_format( $additional_space )
);
}

private function check_and_install( $command, $name ) {
$status = EE::exec( "command -v $command" );
if ( ! $status ) {
Expand Down Expand Up @@ -669,9 +702,32 @@ private function pre_restore_check() {
EE::debug( 'Remote backup size: ' . $remote_size );

$free_space = disk_free_space( EE_BACKUP_DIR );
if ( false === $free_space ) {
EE::error( 'Unable to determine free disk space for backup directory.' );
}

if ( $remote_size > $free_space ) {
EE::error( 'Not enough disk space to restore backup. Please free up some space and try again.' );
$required_space = $remote_size;
$additional_space = $required_space - $free_space;
$required_formatted = $this->format_bytes( $required_space );
$available_formatted = $this->format_bytes( $free_space );
$needed_formatted = $this->format_bytes( $additional_space );

$error_message = sprintf(
"Not enough disk space to restore backup.\n" .
"Required: %s (%s bytes)\n" .
"Available: %s (%s bytes)\n" .
"Additional space needed: %s (%s bytes)\n" .
"Please free up some space and try again.",
$required_formatted,
number_format( $required_space ),
$available_formatted,
number_format( $free_space ),
$needed_formatted,
number_format( $additional_space )
);

EE::error( $error_message );
}


Expand Down Expand Up @@ -728,6 +784,18 @@ private function pre_restore_check() {
}


private function format_bytes( $bytes, $precision = 2 ) {
$units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];

$bytes = max( $bytes, 0 );
$pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) );
$pow = min( $pow, count( $units ) - 1 );

$size = $bytes / pow( 1024, $pow );

return round( $size, $precision ) . ' ' . $units[ $pow ];
}

private function dir_size( string $directory ) {
$size = 0;

Expand Down
Loading