Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 23 additions & 11 deletions src/helper/Site_Backup_Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ public function __construct() {

public function backup( $args, $assoc_args = [] ) {
delem_log( 'site backup start' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, true, true, true );
$list_backups = \EE\Utils\get_flag_value( $assoc_args, 'list' );
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$list_backups = \EE\Utils\get_flag_value( $assoc_args, 'list' );
$dash_auth = \EE\Utils\get_flag_value( $assoc_args, 'dash-auth' );

// For --list or --dash-auth, we handle site disabled state specially
// --list is read-only and should work regardless of site state
// --dash-auth needs to send API callback instead of just exiting with error
if ( $list_backups || $dash_auth ) {
$this->site_data = get_site_info( $args, false, true, true );
} else {
$this->site_data = get_site_info( $args, true, true, true );
}

// Handle --list flag to display available backups
if ( $list_backups ) {
Expand All @@ -62,20 +71,13 @@ public function backup( $args, $assoc_args = [] ) {
}

// Handle --dash-auth flag for EasyDash integration
$dash_auth = \EE\Utils\get_flag_value( $assoc_args, 'dash-auth' );

if ( $dash_auth ) {
// Debug: Log the raw dash_auth value received
EE::debug( 'Received --dash-auth value: ' . $dash_auth );

// Parse backup-id:backup-verification-token format
$auth_parts = explode( ':', $dash_auth, 2 );
if ( count( $auth_parts ) !== 2 || empty( $auth_parts[0] ) || empty( $auth_parts[1] ) ) {
$this->capture_error(
'Invalid --dash-auth format. Expected: backup-id:backup-verification-token',
self::ERROR_TYPE_VALIDATION,
1001
);
EE::error( 'Invalid --dash-auth format. Expected: backup-id:backup-verification-token' );
}

Expand All @@ -86,6 +88,7 @@ public function backup( $args, $assoc_args = [] ) {
}

// Store dash auth info in class properties for shutdown handler
// Must be set before any capture_error calls so API callbacks work
$this->dash_auth_enabled = true;
$this->dash_backup_id = $auth_parts[0];
$this->dash_verify_token = $auth_parts[1];
Expand All @@ -98,6 +101,13 @@ public function backup( $args, $assoc_args = [] ) {

// Register shutdown handler to send failure callback if backup doesn't complete
register_shutdown_function( [ $this, 'dash_shutdown_handler' ] );

// Check if site is disabled - send API callback with error
if ( ! $this->site_data['site_enabled'] ) {
$error_msg = sprintf( 'Site %s is disabled. Enable it with `ee site enable %s` to create backup.', $this->site_data['site_url'], $this->site_data['site_url'] );
$this->capture_error( $error_msg, self::ERROR_TYPE_VALIDATION, 1003 );
EE::error( $error_msg );
}
}

// Acquire global lock to serialize backups (prevents OOM from concurrent backups)
Expand Down Expand Up @@ -158,6 +168,8 @@ public function backup( $args, $assoc_args = [] ) {
// Release global backup lock (also released by shutdown handler as safety net)
$this->release_global_backup_lock();

EE::success( 'Backup created successfully.' );

delem_log( 'site backup end' );
}

Expand Down Expand Up @@ -1564,7 +1576,7 @@ private function send_dash_request( $endpoint, $payload ) {
) );
}
sleep( $retry_delay );
$attempt++; // Increment at end of loop iteration
$attempt ++; // Increment at end of loop iteration
} else {
// Either not a retryable error, or we've exhausted all retries
if ( $error ) {
Expand Down
10 changes: 4 additions & 6 deletions src/helper/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -2498,9 +2498,8 @@ public function sync( $args, $assoc_args ) {
* $ ee site backup example.com --list
*/
public function backup( $args, $assoc_args ) {
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, true, true, true );
$backup_restore = new Site_Backup_Restore();
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$backup_restore = new Site_Backup_Restore();
$backup_restore->backup( $args, $assoc_args );
}

Expand All @@ -2525,9 +2524,8 @@ public function backup( $args, $assoc_args ) {
*
*/
public function restore( $args, $assoc_args ) {
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$this->site_data = get_site_info( $args, true, true, true );
$backup_restore = new Site_Backup_Restore();
$args = auto_site_name( $args, 'site', __FUNCTION__ );
$backup_restore = new Site_Backup_Restore();
$backup_restore->restore( $args, $assoc_args );
}

Expand Down
Loading