Skip to content
Open
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
32 changes: 28 additions & 4 deletions src/helper/Site_Backup_Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -2162,7 +2162,7 @@ public function rollback_restore_on_shutdown() {
$this->rollback_unfinished_restore();
} catch ( \Throwable $e ) {
// An exception here would also skip the shutdown handlers registered after this one.
$where = null === $this->restore_txn ? '' : sprintf( ' The previous site files are in %1$s/old and the previous database in %1$s/db-before.sql; the next `ee site restore` retries the rollback.', $this->restore_txn['stage'] );
$where = null === $this->restore_txn ? '' : sprintf( ' %s; the next `ee site restore` retries the rollback.', $this->describe_rollback_leftovers( $this->restore_txn ) );
EE::warning( 'The rollback stopped: ' . $e->getMessage() . $where );
}
$this->in_shutdown = false;
Expand All @@ -2187,15 +2187,39 @@ private function rollback_unfinished_restore() {
return;
}

$stage = $this->restore_txn['stage'];
$txn = $this->restore_txn;
EE::warning( 'The restore did not complete. Rolling the site back.' );
if ( $this->rollback_restore_transaction() ) {
EE::log( 'Rolled back: the site files and configuration are as they were before the restore, and the database was re-imported from the copy taken before it.' );
// The DB is put back only when the import had started; before that it was never changed.
EE::log( 'Rolled back: the site files and configuration are as they were before the restore' . ( 'importing' === $txn['db'] ? ', and the database was re-imported from the copy taken before it.' : '.' ) );
} else {
EE::warning( sprintf( 'The rollback did not complete. The previous site files are in %s/old and the previous database in %s/db-before.sql; the next `ee site restore` retries the rollback.', $stage, $stage ) );
EE::warning( sprintf( 'The rollback did not complete. %s; the next `ee site restore` retries the rollback.', $this->describe_rollback_leftovers( $txn ) ) );
}
}

/**
* @param array $txn Restore transaction.
*
* @return string Where an unfinished rollback left the previous site files, and the previous DB once its import had started.
*/
private function describe_rollback_leftovers( array $txn ) {
// Describe what is on disk now: the swap may not have started, and the dump may be gone.
$parts = [];
if ( ! empty( $this->list_dir( $txn['stage'] . '/old' ) ) ) {
$parts[] = sprintf( 'The previous site files still staged are in %s/old', $txn['stage'] );
}
if ( 'importing' === $txn['db'] ) {
$parts[] = is_file( $txn['stage'] . '/db-before.sql' )
? sprintf( 'the previous database is in %s/db-before.sql', $txn['stage'] )
: 'the copy of the previous database is missing';
}
if ( empty( $parts ) ) {
return sprintf( 'No previous site files are staged in %s', $txn['stage'] );
}

return ucfirst( implode( ' and ', $parts ) );
}

/**
* Roll back a restore that was killed before it finished (its journal is left in EE_BACKUP_DIR).
*/
Expand Down