Skip to content
Open
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
136 changes: 102 additions & 34 deletions src/helper/Site_Backup_Restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class Site_Backup_Restore {

private $restore_in_progress = false;

// Tables of the current DB that the backup's dump doesn't recreate; the import keeps them.
private $restore_extra_tables = [];

// WordPress root the running backup excludes core from ('' = core is archived).
private $backup_wp_root = null;

Expand Down Expand Up @@ -429,6 +432,7 @@ private function run_restore( $backup_dir ) {
} else {
$this->restore_site( $backup_dir );
}
$this->warn_about_extra_tables();

// Once per restore (BKP-13), after the commit like the restart below: it recreates the containers.
if ( $this->restore_custom_compose ) {
Expand Down Expand Up @@ -518,11 +522,14 @@ private function backup_site_details( $backup_dir ) {
$table_prefix = $this->run_wp_cli_command( 'config get table_prefix', true );

$query = 'SELECT COUNT(*) FROM ' . $table_prefix . 'posts WHERE post_type = "attachment"';
$query_file = EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] . '/app/htdocs/query.sql';
$this->fs->dumpFile( $query_file, $query );
$upload_count = $this->run_wp_cli_command( 'db query < /var/www/htdocs/query.sql --skip-column-names | tr -d \'[:space:]\'', true );
// In the root-checked scratch dir, never in the webroot (see get_db_size()).
$query_file = $this->write_scratch_file( 'query.sql', $query );
$upload_count = $this->run_wp_cli_command( 'db query < /var/www/' . self::RESTORE_STAGE_DIR . '/query.sql --skip-column-names | tr -d \'[:space:]\'', true );
$upload_count = empty( $upload_count ) ? 0 : $upload_count;
$this->fs->remove( $query_file );
if ( $this->path_exists( $query_file ) ) {
unlink( $query_file );
}
$this->remove_empty_scratch_dir();

$plugin_count = $this->run_wp_cli_command( 'plugin list --format=count' );
// if it is not a number, then make it -
Expand Down Expand Up @@ -1076,6 +1083,10 @@ private function set_wp_config_db_constants() {
$db_user = $this->site_data['db_user'];
$db_password = $this->site_data['db_password'];
$db_host = $this->site_data['db_host'];
// Same form as site creation's `wp config create --dbhost` (host:port).
if ( ! empty( $this->site_data['db_port'] ) && false === strpos( (string) $db_host, ':' ) ) {
$db_host .= ':' . $this->site_data['db_port'];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

// The restored wp-config.php still has the backed-up site's credentials. Each value is read from a file in the
// container (`\$(cat …)` runs there), so none of them passes the host shells of `ee shell --command` (BKP-15).
Expand Down Expand Up @@ -1126,10 +1137,18 @@ private function write_mysql_defaults_file() {
}
$this->mysql_defaults_files[] = $this->prepare_scratch_dir() . '/' . $name;

$this->write_scratch_file(
$name,
sprintf( "[client]\nuser=%s\npassword=%s\nhost=%s\n", $quote( $this->site_data['db_user'] ), $quote( $this->site_data['db_password'] ), $quote( $this->site_data['db_host'] ) )
);
// Same endpoint as DB_HOST, whose host:port form the client's host option can't take.
$host = (string) $this->site_data['db_host'];
$port = empty( $this->site_data['db_port'] ) ? '' : (string) $this->site_data['db_port'];
if ( false !== strpos( $host, ':' ) ) {
list( $host, $port ) = explode( ':', $host, 2 );
}
$content = sprintf( "[client]\nuser=%s\npassword=%s\nhost=%s\n", $quote( $this->site_data['db_user'] ), $quote( $this->site_data['db_password'] ), $quote( $host ) );
if ( '' !== $port ) {
$content .= sprintf( "port=%s\n", $quote( $port ) );
}

$this->write_scratch_file( $name, $content );

return '/var/www/' . self::RESTORE_STAGE_DIR . '/' . $name;
}
Expand Down Expand Up @@ -1731,11 +1750,11 @@ private function stage_db_dump( $sql_dir ) {
$dump = $sql_dir . '/' . $this->site_data['site_url'] . '.sql';
clearstatcache();
if ( ! $this->path_exists( $dump ) ) {
EE::error( sprintf( 'The backup has no database dump (sql/%s). The site was not changed.', basename( $dump ) ) );
EE::error( sprintf( 'The backup has no database dump (sql/%s).', basename( $dump ) ) );
}
// An empty dump imports as a no-op, which would restore the files over the current DB (older releases backed up a failed mysqldump).
if ( is_link( $dump ) || ! is_file( $dump ) || 0 === filesize( $dump ) ) {
EE::error( sprintf( 'The database dump in the backup (sql/%s) is empty or not a regular file. The site was not changed.', basename( $dump ) ) );
EE::error( sprintf( 'The database dump in the backup (sql/%s) is empty or not a regular file.', basename( $dump ) ) );
}

$this->rename_or_fail( $dump, $this->restore_txn['stage'] . '/db.sql' );
Expand Down Expand Up @@ -1783,7 +1802,7 @@ private function stage_legacy_wp_archive( $backup_app, $wp_root ) {

// Never the wp-content/uploads symlink entry some 7-Zip builds stored: restoring it made an uploads/uploads loop (BKP-10).
if ( ! $this->extract_to_stage( $backup_app, $new_root, [ 'wp-content/*' ], [ 'wp-content/uploads' ] ) ) {
EE::error( 'The backup archive has no wp-content directory. The site was not changed.' );
EE::error( 'The backup archive has no wp-content directory.' );
}

// wp-config.php was taken from one level above the WordPress root.
Expand Down Expand Up @@ -1850,18 +1869,18 @@ private function stage_wp_core( $wp_version, $wp_root, $legacy ) {
}
$real_existing = realpath( $existing );
if ( false === $real_existing || ( $real_existing !== $real_new && 0 !== strpos( $real_existing, $real_new . '/' ) ) ) {
EE::error( 'The WordPress root of the backup is outside the site. The site was not changed.' );
EE::error( 'The WordPress root of the backup is outside the site.' );
}

$this->fs->mkdir( $new_root );
$real_root = realpath( $new_root );
if ( false === $real_root || ( $real_root !== $real_new && 0 !== strpos( $real_root, $real_new . '/' ) ) ) {
EE::error( 'The WordPress root of the backup is outside the site. The site was not changed.' );
EE::error( 'The WordPress root of the backup is outside the site.' );
}
// Only the download dir goes to the site user; a fallback stage inside the scratch dir stays root's.
$core_dir = $this->prepare_scratch_dir() . '/core';
if ( ! mkdir( $core_dir, 0755 ) || ! $this->chown_to_site_user( $core_dir ) ) {
EE::error( 'Failed to prepare the WordPress core download. The site was not changed.' );
EE::error( 'Failed to prepare the WordPress core download.' );
}

// Same download, extraction and checksum gate as site creation, but into the private core dir.
Expand All @@ -1872,7 +1891,7 @@ private function stage_wp_core( $wp_version, $wp_root, $legacy ) {
'force' => true,
]
);
$result = $this->run_checked_shell_command( $core_download_command, 'Failed to download WordPress core for restore. The site was not changed.' );
$result = $this->run_checked_shell_command( $core_download_command, 'Failed to download WordPress core for restore.' );
// The output is captured, so pass on the checksum gate's warning (checksums unavailable from WordPress.org).
if ( preg_match_all( '/^Warning: Could not verify WordPress core checksums.*$/m', $result->stdout . "\n" . $result->stderr, $warnings ) ) {
foreach ( array_unique( $warnings[0] ) as $warning ) {
Expand All @@ -1885,12 +1904,12 @@ private function stage_wp_core( $wp_version, $wp_root, $legacy ) {
$downloaded = $this->restore_txn['stage'] . '/core';
$this->rename_or_fail( $core_dir, $downloaded );
if ( is_link( $downloaded ) || ! is_dir( $downloaded ) ) {
EE::error( 'The WordPress core download is not a directory. The site was not changed.' );
EE::error( 'The WordPress core download is not a directory.' );
}

$core_entries = array_diff( $this->list_dir( $downloaded ), [ 'wp-content' ] );
if ( ! in_array( 'wp-includes', $core_entries, true ) ) {
EE::error( 'The WordPress core download is incomplete. The site was not changed.' );
EE::error( 'The WordPress core download is incomplete.' );
}

foreach ( $core_entries as $entry ) {
Expand Down Expand Up @@ -1918,11 +1937,12 @@ private function apply_restore_transaction( $backup_dir ) {
$stage = $this->restore_txn['stage'];

if ( ! $this->chown_to_site_user( $this->restore_txn['new'], true ) || ( $this->fs->exists( $stage . '/uploads' ) && ! $this->chown_to_site_user( $stage . '/uploads', true ) ) ) {
EE::error( 'Failed to set the owner of the restored files. The site was not changed.' );
EE::error( 'Failed to set the owner of the restored files.' );
}

if ( ! empty( $this->restore_txn['has_db'] ) ) {
$this->snapshot_current_db();
$this->restore_extra_tables = array_values( array_diff( $this->get_dump_tables( $stage . '/db-before.sql' ), $this->get_dump_tables( $stage . '/db.sql' ) ) );

$this->move_db_dump( $stage . '/db.sql', $this->assert_scratch_dir() . '/db.sql' );
$this->restore_txn['db'] = 'importing';
Expand Down Expand Up @@ -1979,22 +1999,62 @@ private function snapshot_current_db() {

clearstatcache();
if ( $result->return_code || ! is_file( $scratch . '/db-before.sql' ) || 0 === filesize( $scratch . '/db-before.sql' ) ) {
EE::error( 'Could not save the current database before the restore. The site was not changed. ' . trim( $result->stderr ) );
EE::error( 'Could not save the current database before the restore. ' . trim( $result->stderr ) );
}

// Out of the container's reach; a rollback reads it through a hard link, so it never leaves the stage.
$saved = $this->restore_txn['stage'] . '/db-before.sql';
$this->move_db_dump( $scratch . '/db-before.sql', $saved );
// The site user could have left a symlink of that name; chown() and chmod() would follow it.
if ( is_link( $saved ) || ! is_file( $saved ) ) {
EE::error( 'The saved copy of the current database is not a regular file. The site was not changed.' );
EE::error( 'The saved copy of the current database is not a regular file.' );
}
chown( $saved, 0 );
chmod( $saved, 0600 );
$this->restore_txn['db'] = 'saved';
$this->write_restore_journal();
}

/**
* Tables and views a mysqldump file creates: it writes `DROP TABLE IF EXISTS` and `CREATE TABLE` lines for each
* (a view gets a stand-in table). Read in chunks, as a dump line can be megabytes long.
*
* @param string $file Dump on the host.
*
* @return string[]
*/
private function get_dump_tables( $file ) {
$tables = [];
$handle = is_file( $file ) ? fopen( $file, 'rb' ) : false;
if ( false === $handle ) {
return $tables;
}
$line_start = true;
while ( false !== ( $chunk = fgets( $handle, 65536 ) ) ) {
if ( $line_start && preg_match( '/^(?:DROP TABLE IF EXISTS|CREATE TABLE(?: IF NOT EXISTS)?) `((?:[^`]|``)+)`/', $chunk, $matches ) ) {
$tables[] = str_replace( '``', '`', $matches[1] );
}
$line_start = "\n" === substr( $chunk, -1 );
}
fclose( $handle );

return array_values( array_unique( $tables ) );
}

/**
* The import drops and recreates only the dump's own tables. Tables created after the backup stay: the DB can also
* hold tables the site doesn't own (another site sharing it, another table prefix), so they are named, not dropped.
*/
private function warn_about_extra_tables() {
$tables = $this->restore_extra_tables;
if ( empty( $tables ) ) {
return;
}
$shown = array_slice( $tables, 0, 20 );
$more = count( $tables ) > count( $shown ) ? sprintf( ' and %d more', count( $tables ) - count( $shown ) ) : '';
EE::warning( sprintf( 'The database has tables that are not in the backup; they were kept as they are: %s%s. Drop them if the restored site does not use them.', implode( ', ', $shown ), $more ) );
}

private function move_db_dump( $from, $to ) {
// Checked right before the rename: moving into or out of a replaced scratch dir would touch whatever it points to.
$scratch = $this->assert_scratch_dir();
Expand Down Expand Up @@ -2182,6 +2242,7 @@ private function rollback_unfinished_restore() {
$this->remove_scratch_dir();
$this->fs->remove( [ $this->get_restore_journal_file(), $this->restore_txn['stage'] ] );
$this->restore_txn = null;
// The only place this is said once the transaction has begun: errors raised inside it leave it out.
EE::log( 'The site was not changed.' );

return;
Expand Down Expand Up @@ -2651,18 +2712,20 @@ private function get_db_size() {
";


$query_file = EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] . '/app/htdocs/db_size_query.sql';
$this->fs->dumpFile( $query_file, $query );
// In the root-checked scratch dir, never in the webroot, which the site can serve or replace with a symlink.
$query_file = $this->write_scratch_file( 'db_size_query.sql', $query );


// No credential in the command: it passes two shells on the host (BKP-15).
$defaults_file = $this->write_mysql_defaults_file();
$command = sprintf( 'mysql --defaults-extra-file=%s --skip-ssl < /var/www/htdocs/db_size_query.sql', $defaults_file );
$command = sprintf( 'mysql --defaults-extra-file=%s --skip-ssl < /var/www/%s/db_size_query.sql', $defaults_file, self::RESTORE_STAGE_DIR );

$output = EE::launch( $this->get_ee_shell_command( $command ) );


$this->fs->remove( $query_file );
if ( $this->path_exists( $query_file ) ) {
unlink( $query_file );
}
$this->remove_mysql_defaults_files();


Expand Down Expand Up @@ -4020,7 +4083,7 @@ private function send_dash_success_callback( $ed_api_url, $backup_id, $verify_to

EE::debug( 'Payload being sent: ' . json_encode( array_merge( $payload, [ 'verify' => '******' ] ) ) );

$success = $this->send_dash_request( $endpoint, $payload );
$success = $this->send_dash_request( $endpoint, $payload, 'EasyEngine Dashboard callback sent successfully.' );

// A success that actually reached the EasyEngine Dashboard is the terminal callback; a failed
// attempt is not, so the failure path can still emit the failure callback.
Expand Down Expand Up @@ -4060,18 +4123,20 @@ private function send_dash_failure_callback( $ed_api_url, $backup_id, $verify_to
// Failure is terminal: mark before sending so a concurrent shutdown pass
// can never emit a second (duplicate) terminal callback.
$this->dash_callback_sent = true;
$this->send_dash_request( $endpoint, $payload );
// Not "sent successfully": printed right after a failed success callback, that read as if the backup was registered.
$this->send_dash_request( $endpoint, $payload, 'EasyEngine Dashboard failure callback sent.' );
}

/**
* Send HTTP request to EasyEngine Dashboard API with retry logic for 5xx errors and connection errors.
*
* @param string $endpoint The API endpoint URL.
* @param array $payload The request payload.
* @param string $sent_message Logged once the request went through.
*
* @return bool True if request succeeded, false otherwise.
*/
private function send_dash_request( $endpoint, $payload ) {
private function send_dash_request( $endpoint, $payload, $sent_message ) {
$max_retries = 3;
$retry_delay = 300; // 5 minutes in seconds
$max_attempts = $max_retries + 1; // 1 initial attempt + 3 retries = 4 total
Expand Down Expand Up @@ -4099,7 +4164,7 @@ private function send_dash_request( $endpoint, $payload ) {

// Check if request was successful
if ( ! $error && $http_code >= 200 && $http_code < 300 ) {
EE::log( 'EasyEngine Dashboard callback sent successfully.' );
EE::log( $sent_message );
EE::debug( 'EasyEngine Dashboard response: ' . $response_text );

return true; // Success
Expand Down Expand Up @@ -4201,7 +4266,8 @@ private function acquire_global_backup_lock() {
$lock_file = EE_BACKUP_DIR . '/backup-global.lock';
$max_wait = 86400; // 24 hours max wait
$waited = 0;
$interval = 60; // Check every 60 seconds
$interval = 5; // A queued backup starts within seconds of the lock's release.
$log_every = 60;

// Ensure backup directory exists
if ( ! $this->fs->exists( EE_BACKUP_DIR ) ) {
Expand Down Expand Up @@ -4233,12 +4299,14 @@ private function acquire_global_backup_lock() {
EE::error( 'Timeout waiting for another backup. Try again later.' );
}

// Read who has the lock
rewind( $this->global_backup_lock_handle );
$lock_info = stream_get_contents( $this->global_backup_lock_handle );
if ( 0 === $waited % $log_every ) {
// Read who has the lock
rewind( $this->global_backup_lock_handle );
$lock_info = stream_get_contents( $this->global_backup_lock_handle );

EE::log( sprintf( 'Another backup in progress (%s). Waiting... (%d/%d sec)',
trim( $lock_info ) ?: 'unknown', $waited, $max_wait ) );
EE::log( sprintf( 'Another backup in progress (%s). Waiting... (%d/%d sec)',
trim( $lock_info ) ?: 'unknown', $waited, $max_wait ) );
}

sleep( $interval );
$waited += $interval;
Expand Down
26 changes: 26 additions & 0 deletions src/helper/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = []
// Also when site_ssl is empty: SSL may have been turned off or lost while the files stayed, and nginx-proxy would keep matching them.
\EE\Site\Utils\remove_site_ssl_files( $site_url, $this->get_ssl_domains( $site_url ) );

$this->remove_backup_dir_leftovers( $site_url );

if ( Site::find( $site_url )->delete() ) {
\EE::log( 'Removed database entry.' );
} else {
Expand All @@ -413,6 +415,30 @@ protected function delete_site( $level, $site_url, $site_fs_path, $db_data = []
\EE::success( "Site $site_url deleted." );
}

/**
* Remove the files of the deleted site that EE kept in EE_BACKUP_DIR: the metadata copy of its last `ee site backup`
* and the DB dump `ee site update --ssl` takes before its search-replace. Other files there stay.
*
* @param string $site_url Site name.
*/
private function remove_backup_dir_leftovers( $site_url ) {
$dir = EE_BACKUP_DIR . '/' . $site_url;

try {
foreach ( [ EE_BACKUP_DIR . '/' . $site_url . '.metadata.json', $dir . '/' . $site_url . '.db' ] as $file ) {
if ( is_file( $file ) || is_link( $file ) ) {
$this->fs->remove( $file );
}
}
$entries = is_dir( $dir ) && ! is_link( $dir ) ? scandir( $dir ) : false;
if ( false !== $entries && 2 === count( $entries ) ) {
rmdir( $dir );
}
} catch ( \Exception $e ) {
\EE::debug( $e->getMessage() );
}
}

/**
* Supports updating and upgrading site.
*
Expand Down