reason (timeout|not found|partially copied|not in backup|copied, old copy not removed). */ const OPTION_RESTORE_PART_WARNINGS = 'ocm_restore_part_warnings'; /** Persisted binary-preflight verdict for this restore's dump fingerprint. */ const OPTION_BINARY_PREFLIGHT = 'ocm_binary_preflight_verdict'; const OPTION_RESTORE_PLUGINS_ISOLATED = 'ocm_restore_plugins_isolated'; /** Hosts the staging URL rewrite collected so far, for a resumed rewrite. */ const OPTION_RESTORE_URL_HOSTS = 'ocm_restore_url_hosts'; /** Suffix of the untouched copy kept while a dump's table prefix is rewritten. */ const PRISTINE_DUMP_SUFFIX = '.ocm-orig'; /** A DB-import step cut off this many times in a row stops the restore. */ const MAX_DB_UNIT_ATTEMPTS = 3; public static function cancel_actions() { // Add nonce verification if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'one_click_migration_cancel_action')) { wp_die('Security check failed. Please try again.'); } // Check user capabilities if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to perform this action.'); } wp_safe_redirect(admin_url('tools.php?page=one-click-migration')); OCM_Debug_Mode::disable(); update_option('ocm_is_stopped', true, true); One_Click_Migration::$process_backup_single->cancel_all_process(); One_Click_Migration::$process_restore_single->cancel_all_process(); One_Click_Migration::$process_backup_single->cancel_scheduled_event(); One_Click_Migration::$process_restore_single->cancel_scheduled_event(); OCM_BackgroundHelper::delete_all_batch_process(); One_Click_Migration::cancel_all_process(); One_Click_Migration::stop_and_reset(); exit; } public static function start_backup() { // Verify nonce for security if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'one_click_migration_start_backup')) { wp_die('Security check failed. Please try again.'); } // Check user capabilities if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to perform this action.'); } // Claim ownership before touching any shared options or queues. The // background-process transient is only created after dispatch and its // real identifier includes the wp_ prefix, so it cannot close the // start/dispatch race by itself. if (get_option('ocm_action_start_backup') || get_option('ocm_action_start_restore') || get_site_transient('wp_ocm_background_backup_process_lock') ) { wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=backup_in_progress')); exit; } $run_id = self::claim_backup_run(); if (false === $run_id) { wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=backup_in_progress')); exit; } @set_time_limit(One_Click_Migration::get_timeout()); update_option('ocm_action_start_backup', true, true); update_option('ocm_is_stopped', false, true); update_option('ocm_upload_auto_try_nb_db', 0, true); update_option('ocm_upload_auto_try_nb_themes', 0, true); update_option('ocm_upload_auto_try_nb_plugins', 0, true); update_option('ocm_upload_auto_try_nb_uploads', 0, true); One_Click_Migration::delete_log_file(); One_Click_Migration::cancel_all_process(); self::reset_current_backup_steps(); self::reset_current_restore_steps(); self::delete_backup_options(); delete_option('ocm_log_merged_bucket'); One_Click_Migration::write_to_log('Backup started'); if (!One_Click_Migration::$process_backup_single->is_queue_empty()) { self::fail_backup('stale backup queue could not be cleared; use Stop & Reset before retrying'); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=backup_in_progress')); exit; } $username = self::get_username(); update_option('ocm_user_email', $username, true); $excluded_folders = self::get_excluded_folders(); if (!self::ensure_temporary_directory()) { self::fail_backup('temporary directory could not be created or is not writable'); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=write_permission')); exit; } $disk = self::check_backup_disk_space($excluded_folders); if (!$disk['ok']) { $disk_message = sprintf( 'Error: Not enough disk space to run backup. Need %s, only %s available.', self::convertToReadableSize($disk['required']), self::convertToReadableSize($disk['free']) ); One_Click_Migration::write_to_log($disk_message); self::fail_backup($disk_message); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=insufficient_disk_space')); exit; } // Register backup with Lambda → MySQL gateway. Lambda generates bucket_id, // restore code, and AES key. The restore code is emailed only after all // selected backup parts have finished uploading. $site_url = get_site_url(); $registration = OCM_S3::register_backup($username, $site_url); $retry_count = 0; $max_retries = 3; while (!$registration && $retry_count < $max_retries) { $retry_count++; One_Click_Migration::write_to_log("Retrying backup registration (attempt $retry_count/$max_retries)..."); sleep(2); $registration = OCM_S3::register_backup($username, $site_url); } if (!$registration) { One_Click_Migration::write_to_log('Error: Could not register backup after ' . ($retry_count + 1) . ' attempts'); self::fail_backup('could not register backup after ' . ($retry_count + 1) . ' attempts'); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=endpoint_failure')); exit; } $bucket_id = $registration->bucket_id; $aes_key = base64_decode($registration->aes_key_b64); $presigned_urls = $registration->urls; $restore_code = $registration->restore_code; update_option('ocm_bucket_id', $bucket_id, true); update_option('ocm_aes_key', base64_encode($aes_key), true); update_option('ocm_presigned_urls', $presigned_urls, true); update_option('ocm_restore_code_plain', $restore_code, false); self::set_backup_run_bucket($run_id, $bucket_id); // Publish the uncompressed part sizes into the log. The log is uploaded to // S3 with the backup and merged back on the restore side before its disk // preflight, which uses these numbers instead of guessing from zip sizes. if (!empty($disk['sizes'])) { $size_pairs = []; foreach ($disk['sizes'] as $part => $bytes) { $size_pairs[] = $part . '=' . (int) $bytes; } One_Click_Migration::write_to_log( ' SYSLOG: BACKUP_UNCOMPRESSED_SIZES bucket=' . $bucket_id . ' ' . implode(' ', $size_pairs) ); } if (!self::check_is_complete_backup_step('init', self::STEP_BACKUP_CHILD_INITIATE)) { One_Click_Migration::write_to_log('Backup started'); $all_plugins = get_option('active_plugins', []); $all_plugins = is_array($all_plugins) ? $all_plugins : []; $other_count = count($all_plugins) - 1; One_Click_Migration::write_to_log('Other active plugins: ' . max(0, $other_count)); OCM_Debug_Mode::enable(); One_Click_Migration::write_to_log(' SYSLOG: Debug mode enabled for backup run'); $excluding = self::get_excluded_folders(); // Check and convert to array if necessary if (empty($excluding)) { $excluding = []; // Handle empty or null values } elseif (!is_array($excluding)) { $excluding = [$excluding]; // Convert to array if it's not already } $excluding_str = implode(", ", $excluding); One_Click_Migration::write_to_log(sprintf('Excluding "%s"', $excluding_str)); self::save_system_data_to_log_file($username, $bucket_id); One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Backup worker time budget: %ds', One_Click_Migration::get_worker_time_budget() )); // Server configuration diagnostics One_Click_Migration::write_to_log(' SYSLOG: Server IP: ' . ($_SERVER['SERVER_ADDR'] ?? 'unknown')); One_Click_Migration::write_to_log(' SYSLOG: WP_HTTP_BLOCK_EXTERNAL: ' . (defined('WP_HTTP_BLOCK_EXTERNAL') ? (WP_HTTP_BLOCK_EXTERNAL ? 'true' : 'false') : 'not set')); One_Click_Migration::write_to_log(' SYSLOG: WP_ACCESSIBLE_HOSTS: ' . (defined('WP_ACCESSIBLE_HOSTS') ? WP_ACCESSIBLE_HOSTS : 'not set')); One_Click_Migration::write_to_log(' SYSLOG: Admin URL: ' . admin_url('admin-ajax.php')); foreach ($presigned_urls as $key => $generated_url) { One_Click_Migration::write_to_log("URL generated: $key"); } self::set_complete_backup_step('init', self::STEP_BACKUP_CHILD_INITIATE); } if(!(in_array('db', $excluded_folders))){ if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_INITIATE_DB_BACKUP)) { One_Click_Migration::write_to_log('db added to queue'); One_Click_Migration::$process_backup_single->push_to_queue(array('db', $run_id)); } } if(!(in_array('themes', $excluded_folders))){ if (!self::check_is_complete_backup_step('themes', self::STEP_BACKUP_CHILD_INITIATE_BACKUP_THEMES)) { One_Click_Migration::write_to_log('themes added to queue'); One_Click_Migration::$process_backup_single->push_to_queue(array('themes', $run_id)); } } if(!(in_array('plugins', $excluded_folders))){ if (!self::check_is_complete_backup_step('plugins', self::STEP_BACKUP_CHILD_INITIATE_BACKUP_PLUGINS)) { One_Click_Migration::write_to_log('plugins added to queue'); One_Click_Migration::$process_backup_single->push_to_queue(array('plugins', $run_id)); } } if(!(in_array('uploads', $excluded_folders))){ if (!self::check_is_complete_backup_step('uploads', self::STEP_BACKUP_CHILD_INITIATE_BACKUP_UPLOADS)) { One_Click_Migration::write_to_log('uploads added to queue'); One_Click_Migration::$process_backup_single->push_to_queue(array('uploads', $run_id)); } } // Plugin isolation is intentionally disabled while restore is being debugged. // The function still runs to clear any stale mu-plugin shim from disk. One_Click_Migration::start_backup_plugin_isolation(); One_Click_Migration::$process_backup_single->save(); self::dispatch_backup_worker_with_diagnostics(); wp_die('Backup started'); } private static function dispatch_backup_worker_with_diagnostics() { if (!self::admin_ajax_loopback_check_passed()) { // Async dispatch would be sent into the void; run the worker inline // instead (the same "maximum compatibility" path restore uses). update_option('ocm_dispatch_blocked', 1, false); One_Click_Migration::write_to_log('Notice: This host blocks loopback requests to admin-ajax.php; running the backup in direct processing mode.'); One_Click_Migration::$process_backup_single->trigger_processing(); return; } delete_option('ocm_dispatch_blocked'); One_Click_Migration::write_to_log(' SYSLOG: Dispatching backup worker'); $response = One_Click_Migration::$process_backup_single->dispatch(); self::log_backup_worker_dispatch_response($response); if (false === $response || is_wp_error($response)) { One_Click_Migration::write_to_log('Notice: Backup worker dispatch failed; falling back to direct processing.'); One_Click_Migration::$process_backup_single->trigger_processing(); } } private static function admin_ajax_loopback_check_passed() { $url = add_query_arg( array( 'action' => 'ocm_loopback_check', 'ocm_ts' => time(), ), admin_url('admin-ajax.php') ); One_Click_Migration::write_to_log(' SYSLOG: Checking admin-ajax loopback before backup worker dispatch'); $response = wp_remote_get($url, array( 'timeout' => 8, 'redirection' => 3, 'sslverify' => apply_filters('https_local_ssl_verify', false), )); if (is_wp_error($response)) { One_Click_Migration::write_to_log(sprintf( ' SYSLOG: admin-ajax loopback check failed before backup worker dispatch: %s', $response->get_error_message() )); return false; } $code = (int) wp_remote_retrieve_response_code($response); $message = wp_remote_retrieve_response_message($response); $body = trim((string) wp_remote_retrieve_body($response)); $body_preview = substr(preg_replace('/\s+/', ' ', $body), 0, 160); if (200 !== $code || false === strpos($body, '"success":true')) { One_Click_Migration::write_to_log(sprintf( ' SYSLOG: admin-ajax loopback check returned HTTP %d %s before backup worker dispatch. Body: %s', $code, $message, $body_preview )); return false; } One_Click_Migration::write_to_log(sprintf( ' SYSLOG: admin-ajax loopback check passed with HTTP %d %s', $code, $message )); return true; } private static function log_backup_worker_dispatch_response($response) { if (false === $response) { One_Click_Migration::write_to_log(' SYSLOG: Backup worker dispatch was not attempted because the process is already running or dispatch was cancelled.'); return; } if (is_wp_error($response)) { One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Backup worker dispatch returned WP_Error: %s', $response->get_error_message() )); return; } $code = (int) wp_remote_retrieve_response_code($response); $message = wp_remote_retrieve_response_message($response); if (0 === $code) { // The dispatch is a fire-and-forget request, so 0 just means "sent". // Whether the worker actually started is confirmed by its heartbeat. One_Click_Migration::write_to_log(' SYSLOG: Backup worker dispatch request sent (async)'); return; } One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Backup worker dispatch HTTP response: %d %s', $code, $message )); } public static function start_restore() { // Verify nonce for security if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], 'one_click_migration_start_restore')) { wp_die('Security check failed. Please try again.'); } // Check user capabilities if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to perform this action.'); } if (get_option('ocm_action_start_backup') || get_option('ocm_action_start_restore')) { wp_send_json_error(array('message' => 'Another operation started. Wait for it to finish or stop it first.'), 409); return; } $restore_code = self::get_restore_code(); $url_plan = OCM_Restore_Precheck::consume( $_POST['precheck_token'] ?? '', $restore_code, $_GET['selected'] ?? '', isset($_POST['domain_ids']) ? wp_unslash($_POST['domain_ids']) : array() ); if (is_wp_error($url_plan)) { wp_send_json_error(array('message' => $url_plan->get_error_message()), 400); return; } update_option('ocm_download_auto_try_nb_db', 0, true); update_option('ocm_download_auto_try_nb_themes', 0, true); update_option('ocm_download_auto_try_nb_plugins', 0, true); update_option('ocm_download_auto_try_nb_uploads', 0, true); One_Click_Migration::cancel_all_process(); self::release_backup_run(self::get_stored_backup_run_id()); delete_option(self::BACKUP_UPLOADED_PARTS_OPTION); self::reset_current_backup_steps(); self::reset_current_restore_steps(); self::delete_restore_options(); if (!update_option(OCM_Restore_Precheck::OPTION, $url_plan, false)) { wp_send_json_error(array('message' => 'The address choices could not be saved. Check the backup again before restoring.'), 500); return; } // Clear stale presigned URLs from a prior migration so early write_to_log // calls don't create a local log file keyed to a dead bucket (which would // later block the merge-in of the backup log from the new bucket). delete_option('ocm_presigned_urls'); delete_option('ocm_log_merged_bucket'); delete_option(self::OPTION_RESTORE_DB_PART_FAILED); One_Click_Migration::delete_log_file(); $exluded_folders = self::get_excluded_folders(); // cancel_all_process() above emptied the queue; any item still left // belongs to an older run and its worker drops it (#96). $run_id = self::claim_restore_run(); One_Click_Migration::$process_restore_single->push_to_queue(array($restore_code, $run_id)); // Save the queue and use direct processing for maximum reliability One_Click_Migration::$process_restore_single->save(); One_Click_Migration::write_to_log(' SYSLOG: Using direct restore processing for maximum compatibility'); One_Click_Migration::$process_restore_single->trigger_processing(); } public static function start_restore_process($restore_code) { global $wpdb; @set_time_limit(One_Click_Migration::get_timeout()); update_option('ocm_action_start_restore', true, true); update_option('ocm_is_stopped', false, true); // Cleared at restore start so a prior failed attempt cannot block a new run. // Mid-restore db skip paths set this; user-excluded db never does. // Do NOT clear ocm_backup_*_parts here: a worker restart after the backup // log was merged (ocm_log_merged_bucket set) would wipe exclusions while // skipping re-read, causing excluded parts to be requested again. Fresh // restores already clear those via delete_restore_options() at kickoff. if (!self::check_is_complete_restore_step('init', self::STEP_RESTORE_CHILD_INITIATE)) { delete_option(self::OPTION_RESTORE_DB_PART_FAILED); delete_option(self::OPTION_BINARY_PREFLIGHT); delete_option(self::OPTION_RESTORE_PART_WARNINGS); delete_option(self::OPTION_RESTORE_PLUGINS_ISOLATED); delete_option(self::OPTION_RESTORE_WAS_WARNED); self::$db_restore_refused_binary = false; self::$binary_preflight_passed = false; } $content_dir = self::get_restore_work_path(); $excluded_restore_files = get_option('ocm_excluded_folders', []); if (!self::check_is_complete_restore_step('init', self::STEP_RESTORE_CHILD_INITIATE)) { $redeem = OCM_S3::redeem_restore_code($restore_code); $status = is_object($redeem) ? (int) ($redeem->_status ?? 0) : 0; $has_error = is_object($redeem) && !empty($redeem->error); $has_payload = is_object($redeem) && !empty($redeem->bucket_id) && !empty($redeem->aes_key_b64) && !empty($redeem->urls); if ($status !== 200 || $has_error || !$has_payload) { $error = $has_error ? $redeem->error : 'malformed_response'; One_Click_Migration::write_to_log(sprintf( 'Error: Restore code lookup failed (status=%d error=%s payload_ok=%s)', $status, $error, $has_payload ? 'yes' : 'no' )); One_Click_Migration::reset_actions_start_mark(); update_option('ocm_is_stopped', true, true); self::end_restore_run(); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=invalid_restore_code')); exit; } $url_plan = get_option(OCM_Restore_Precheck::OPTION, array()); if ($url_plan && ($url_plan['bucket_id'] !== $redeem->bucket_id || $url_plan['destination'] !== site_url())) { One_Click_Migration::write_to_log('Error: Restore preparation no longer matches this backup or destination. Check the backup again.'); update_option('ocm_is_stopped', true, true); self::end_restore_run(); exit; } $bucket_id = $redeem->bucket_id; $aes_key = base64_decode($redeem->aes_key_b64); $presigned_urls = $redeem->urls; $username = $redeem->email ?? ''; update_option('ocm_bucket_id', $bucket_id, true); update_option('ocm_aes_key', base64_encode($aes_key), true); update_option('ocm_presigned_urls', $presigned_urls, true); if ($username) { update_option('ocm_user_email', $username, true); } // Pull backup-side log from S3 and prepend to whatever the restore worker // has already written locally (e.g. "*** RESTORE TASK STARTED ***"). One_Click_Migration::merge_backup_log_from_s3(); $backup_missing_parts = get_option('ocm_backup_missing_parts', []); if (is_array($backup_missing_parts) && $backup_missing_parts) { One_Click_Migration::write_to_log(sprintf( 'Notice: This backup does not include: %s. These parts were skipped when the backup was created, so the restored site will be missing them.', implode(', ', $backup_missing_parts) )); } // Promote backup-log =excluded parts into ocm_excluded_folders so the // rest of restore matches 3.1.18 selective exclude (no download, and // for db: complete_restore / no import / no URL rewrite). // Call AFTER get_excluded_folders() so a $_GET selected list cannot wipe them. $excluded_for_check = self::get_excluded_folders(); self::apply_backup_excluded_parts_to_restore_exclusions(); $backup_excluded_for_disk = get_option('ocm_backup_excluded_parts', array()); $backup_excluded_for_disk = is_array($backup_excluded_for_disk) ? $backup_excluded_for_disk : array(); $excluded_for_check = array_values(array_unique(array_merge( is_array($excluded_for_check) ? $excluded_for_check : array(), get_option('ocm_excluded_folders', array()) ?: array(), $backup_excluded_for_disk ))); $disk = self::check_restore_disk_space($presigned_urls, $excluded_for_check); if (!$disk['ok']) { if (!empty($disk['reason']) && 'write_probe_failed' === $disk['reason']) { One_Click_Migration::write_to_log( 'Error: Restore workspace is not writable or disk quota is exhausted. Please free space or increase the hosting account quota, then try again.' ); } else { One_Click_Migration::write_to_log(sprintf( 'Error: Not enough disk space to run restore. Need %s, only %s available.', self::convertToReadableSize($disk['required']), self::convertToReadableSize($disk['free']) )); } One_Click_Migration::write_to_log('Restore failed: Not enough disk space or the hosting disk quota is exhausted (see previous log line).'); One_Click_Migration::reset_actions_start_mark(); update_option('ocm_is_stopped', true, true); self::end_restore_run(); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=insufficient_disk_space')); exit; } One_Click_Migration::write_to_log('Restore started'); $all_plugins = get_option('active_plugins', []); $all_plugins = is_array($all_plugins) ? $all_plugins : []; $other_count = count($all_plugins) - 1; One_Click_Migration::write_to_log('Other active plugins: ' . max(0, $other_count)); // Plugin deactivation waits until binary preflight / db-part checks // pass (gate_restore_before_file_replace). Stops that say // "No changes were made" must not have deactivated anything. OCM_Debug_Mode::enable(); One_Click_Migration::write_to_log(' SYSLOG: Debug mode enabled for restore run'); One_Click_Migration::enable_maintenance_mode(); $excluded_restore_files = get_option('ocm_excluded_folders', []); // Item A: backup-skipped / failed db → stop unless db is excluded for // this restore (user selection OR backup-log db=excluded files-only). $excluded_restore_files = get_option('ocm_excluded_folders', []); self::stop_if_db_unavailable_for_restore($excluded_restore_files); $exluding = implode(", ", $excluded_restore_files); // One_Click_Migration::write_to_log(sprintf('Excluding "%s"', $exluding)); self::save_system_data_to_log_file($username, $bucket_id); One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Restore worker time budget: %ds', One_Click_Migration::get_worker_time_budget() )); foreach ($presigned_urls as $key => $generated_url) { $writable_file_path = OCM_PLUGIN_WRITABLE_PATH . $key . '.zip'; if(file_exists($writable_file_path)){ One_Click_Migration::write_to_log("URL generated: $key"); } } $attempted_dir = ''; $content_dir = self::prepare_restore_workspace($attempted_dir); if ('' === $content_dir) { One_Click_Migration::write_to_log(sprintf( 'Downloading restore files, Error: Directory "%s" could not be prepared. Please check the write permission for the parent directory "%s".', $attempted_dir, dirname($attempted_dir) )); exit; } One_Click_Migration::write_to_log('Downloading restore files. Please wait.'); self::set_complete_restore_step('init', self::STEP_RESTORE_CHILD_INITIATE); self::set_complete_restore_step('init', self::STEP_RESTORE_CHILD_PRESIGNED_URLS, $presigned_urls); } $presigned_urls = self::get_restore_step_value('init', self::STEP_RESTORE_CHILD_PRESIGNED_URLS); // Resume path: get_excluded_folders may no-op (no $_GET); still re-apply // backup exclusions so a resumed files-only restore never re-requests db. $excluded_folders = self::get_excluded_folders(); self::apply_backup_excluded_parts_to_restore_exclusions(); // Process db first so the binary preflight can run after DB extract and // before themes/plugins/uploads replace destination files. $presigned_urls = self::presigned_urls_db_first($presigned_urls); // Download each of the zip files foreach ($presigned_urls as $key => $download_url) { // A worker left over from a failed, stopped or replaced run must not // log or touch the restore workspace again (#96). if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } // Don't try and download the log file $excluded_restore_files = get_option('ocm_excluded_folders',[]); $keyZipCrypt = $key . '.zip.crypt'; $file_path = $content_dir . '/' . $keyZipCrypt; $extract_path = $content_dir . "/$key"; if(!in_array($key, $excluded_restore_files)){ if (in_array($key, array('log', 'log_download'))) { continue; } // Never request a part the backup log marked =excluded (including // db=excluded files-only moves). if (self::maybe_skip_backup_excluded_restore_part($key, $excluded_restore_files)) { continue; } $excluded_restore_files = get_option('ocm_excluded_folders',[]); $skipped_restore_files = get_option('ocm_skipped_folders', []); $eexcluded_restore_files = get_option('ocm_eexcluded_folders', []); if(is_dir($content_dir)){ $retry_count = get_option('ocm_restore_download_retry_' . $key, 0); if(!$retry_count){ update_option('ocm_restore_download_retry_' . $key, 1, true); } if($retry_count){ if($retry_count <= self::MAX_TRANSFER_RETRIES){ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD)) { One_Click_Migration::write_to_log(sprintf('Downloading %s Retry %d', $key, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_restore_download_retry_' . $key, $retry_count, true); } }else{ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD)) { One_Click_Migration::write_to_log(sprintf('Skipping ' .$key. ' Download')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$key. ' Decrypting')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$key. ' Extraction')); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); self::capture_skipped_restore_part($excluded_restore_files, $skipped_restore_files, $key); continue; } } } if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD)) { // Reset time limit for file download @set_time_limit(One_Click_Migration::get_timeout()); One_Click_Migration::write_to_log(sprintf('Downloading file: "%s.zip.crypt"', $key)); self::$restore_download_requests[] = $key; $downloadTmpPathFile = self::download_restore_file($download_url, $file_path, $key); if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } if(is_wp_error($downloadTmpPathFile)){ $error_message = $downloadTmpPathFile->get_error_message(); if($error_message === 'Not Found'){ $not_found = self::handle_restore_part_download_not_found( $key, $keyZipCrypt, $excluded_restore_files, $eexcluded_restore_files ); if ($not_found === 'fail_restore') { return self::fail_restore(sprintf( 'Error: Restore failed because "%s" should exist in this backup but is missing from storage. Create a fresh backup and restore with the new code.', $keyZipCrypt )); } continue; } if ($downloadTmpPathFile->get_error_code() === 'ocm_partial_download') { One_Click_Migration::write_to_log($error_message); One_Click_Migration::write_to_log("Process is Restarting"); One_Click_Migration::$process_restore_single->restart_task(); } return self::fail_restore(sprintf( 'Error: Restore failed while downloading "%s": %s', $keyZipCrypt, $error_message )); } // The file is complete: record it before handing over, or the // next worker would redo the step and, once its retries ran // out, skip the part. self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD_TMP_PATH, $downloadTmpPathFile); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD); if (One_Click_Migration::$process_restore_single->time_exceeded()) { One_Click_Migration::write_to_log("Process is Restarting"); One_Click_Migration::$process_restore_single->restart_task(); } } } if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } if(is_dir($content_dir)){ $retry_count = get_option('ocm_restore_decrypt_retry_' . $key, 0); if(!$retry_count){ update_option('ocm_restore_decrypt_retry_' . $key, 1, true); } if($retry_count){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT)) { One_Click_Migration::write_to_log(sprintf('Decrypting %s Retry %d', $key, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_restore_decrypt_retry_' . $key, $retry_count, true); } }else{ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT)) { One_Click_Migration::write_to_log(sprintf('Skipping ' .$key. ' Decrypting')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$key. ' Extraction')); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); self::capture_skipped_restore_part($excluded_restore_files, $skipped_restore_files, $key); continue; } } } if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT)) { // Reset time limit for file decryption @set_time_limit(One_Click_Migration::get_timeout()); One_Click_Migration::write_to_log(sprintf('Decrypting file: "%s.zip.crypt"', $key)); $downloadTmpPathFile = self::get_restore_step_value($key, self::STEP_RESTORE_CHILD_DOWNLOAD_TMP_PATH); if (!is_string($downloadTmpPathFile) || !file_exists($downloadTmpPathFile)) { return self::fail_restore(sprintf( 'Error: Restore failed because downloaded file for "%s.zip.crypt" was missing.', $key )); } if ($downloadTmpPathFile !== $file_path && !copy($downloadTmpPathFile, $file_path)) { return self::fail_restore(sprintf( 'Error: Restore failed while copying "%s.zip.crypt" into the restore workspace. Please check disk quota and write permissions.', $key )); } $aes_key_b64 = get_option('ocm_aes_key'); $aes_key = $aes_key_b64 ? base64_decode($aes_key_b64) : ''; $unencrypted_zip = self::decryptZipFile($file_path, $aes_key); if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } if (!$unencrypted_zip) { clearstatcache(true, $file_path); if (!file_exists($file_path)) { // The file vanished mid-step; that's not a quota or permission problem. return self::fail_restore(sprintf( 'Error: Restore failed because downloaded file for "%s.zip.crypt" was missing.', $key )); } return self::fail_restore(sprintf( 'Error: Restore failed while decrypting "%s.zip.crypt". Please check disk quota and write permissions.', $key )); } // Decrypted: record it before handing over (see download above). self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_UNENCRYPTED_ZIP, $unencrypted_zip); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT); // Later steps only need the decrypted zip; drop the encrypted copy // now so the part never occupies 3x its size on disk at once. @unlink($file_path); if (is_string($downloadTmpPathFile) && $downloadTmpPathFile !== $file_path && file_exists($downloadTmpPathFile)) { @unlink($downloadTmpPathFile); } if (One_Click_Migration::$process_restore_single->time_exceeded()) { One_Click_Migration::write_to_log("Process is Restarting"); One_Click_Migration::$process_restore_single->restart_task(); } } } if (class_exists('ZipArchive') != true) { One_Click_Migration::write_to_log('Error: Class ZipArchive not found. Please see this page for installation instructions or ask your hosting provider to enable the zip extension'); exit; } if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } if(is_dir($content_dir)){ $retry_count = get_option('ocm_restore_extract_retry_' . $key, 0); if(!$retry_count){ update_option('ocm_restore_extract_retry_' . $key, 1, true); } if($retry_count){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT)) { One_Click_Migration::write_to_log(sprintf('Extracting %s Retry %d', $key, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_restore_extract_retry_' . $key, $retry_count, true); } }else{ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT)) { One_Click_Migration::write_to_log(sprintf('Skipping ' .$key. ' Extraction')); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); self::capture_skipped_restore_part($excluded_restore_files, $skipped_restore_files, $key); continue; } } } } if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT)) { // Reset time limit for file extraction @set_time_limit(One_Click_Migration::get_timeout()); One_Click_Migration::write_to_log(sprintf('Extracting file: "%s.zip.crypt"', $key)); $unencrypted_zip = self::get_restore_step_value($key, self::STEP_RESTORE_CHILD_UNENCRYPTED_ZIP); if (!is_array($unencrypted_zip) || empty($unencrypted_zip['filename']) || !is_string($unencrypted_zip['filename']) || !file_exists($unencrypted_zip['filename'])) { return self::fail_restore(sprintf( 'Error: Restore failed because decrypted zip for "%s.zip.crypt" was missing.', $key )); } $zip = new ZipArchive; $res = $zip->open($unencrypted_zip['filename']); if ($res !== true) { return self::fail_restore(sprintf( 'Error: Zip file "%s" could not be opened for extraction.', $unencrypted_zip['filename'] )); } $zip_uncompressed = 0; for ($i = 0; $i < $zip->numFiles; $i++) { $stat = $zip->statIndex($i); if ($stat !== false) { $zip_uncompressed += (int) $stat['size']; } } $free_before_extract = self::get_free_disk_space($content_dir); if ($free_before_extract > 0 && $zip_uncompressed > 0 && $free_before_extract < $zip_uncompressed) { $zip->close(); return self::fail_restore(sprintf( 'Error: Not enough disk space to extract "%s.zip": need %s free, only %s available. Free space or increase the hosting account quota, then run the restore again.', $key, self::convertToReadableSize($zip_uncompressed), self::convertToReadableSize($free_before_extract) )); } $extract_result = $zip->extractTo($extract_path); $zip->close(); if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } if (!$extract_result || !is_dir($extract_path)) { return self::fail_restore(sprintf( 'Error: Zip file could not be extracted at "%s". Please check disk quota and write permissions.', $extract_path )); } // Extracted: record it before handing over (see download above). self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); // Extraction succeeded, so the zip is no longer needed; free its // space before the next part starts downloading. @unlink($unencrypted_zip['filename']); if (One_Click_Migration::$process_restore_single->time_exceeded()) { One_Click_Migration::write_to_log("Process is Restarting"); One_Click_Migration::$process_restore_single->restart_task(); } } } if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } if(is_dir($content_dir)){ $retry_count = get_option('ocm_restore_child_delete_retry_' . $key, 0); if(!$retry_count){ update_option('ocm_restore_child_delete_retry_' . $key, 1, true); } // Until the DB phase is done, themes/uploads only wait for their // deferred swap: a worker passing by (the DB import can span // several) is not a failed swap attempt. $swap_waits_for_db = in_array($key, array('themes', 'uploads'), true) && !in_array('db', $excluded_restore_files, true) && !self::check_is_complete_restore_step('db', self::STEP_RESTORE_CHILD_RESTORE); if($retry_count && !$swap_waits_for_db){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW)) { One_Click_Migration::write_to_log(sprintf('Deleting old %s Retry %d', $key, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_restore_child_delete_retry_' . $key, $retry_count, true); } }else{ if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW)) { One_Click_Migration::write_to_log(sprintf('Skipping %s restore after repeated timeouts (folder replace incomplete).', $key)); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); self::record_restore_part_warning($key, 'timeout'); self::capture_skipped_restore_part($excluded_restore_files, $skipped_restore_files, $key); continue; } } } if (!self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW)) { // themes/uploads/plugins file swaps wait until DB import AND // options/prefix validation succeed (or until files-only path). // Mark db/plugins extract-side steps only; deferred swap owns // themes/uploads DELETE_OLD_AND_MOVE_NEW. if (in_array($key, array('db', 'plugins'), true)) { self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); } } // After DB extract (db-first ordering), refuse before any later // themes/plugins/uploads replacement in this or a resumed run. if ($key === 'db' && self::check_is_complete_restore_step('db', self::STEP_RESTORE_CHILD_EXTRACT) ) { self::gate_restore_before_file_replace(); } } } if(!is_dir($content_dir)){ return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; update_option('ocm_is_stopped', true, true); die(); } if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } // Mid-restore db download/decrypt/extract failure must never fall through // to complete_restore() (which would replace nothing but still report success). if (get_option(self::OPTION_RESTORE_DB_PART_FAILED)) { self::stop_restore_after_db_part_failure(); } if(in_array('db', $excluded_restore_files)){ // Files-only: no DB import — swap extracted files now, then complete. self::apply_deferred_file_swaps($presigned_urls); self::complete_restore(); }else{ self::finish_restore_db_phase(); } exit; } /** * Reduce a detailed "Error: ..." message to a short single-line reason * suitable for the terminal "Backup failed:" / "Restore failed:" markers. */ private static function short_failure_reason($message) { $reason = trim(preg_replace('/^\s*Error:\s*/i', '', (string) $message)); $reason = preg_replace('/\s+/', ' ', $reason); if (strlen($reason) > 160) { $reason = substr($reason, 0, 160) . '…'; } return '' !== $reason ? $reason : 'unknown error (see previous log lines)'; } /** * Terminal failure marker for a backup run. Support tooling and the * Mailgun alerter key off the literal "Backup failed:" prefix (mirroring * "Backup completed." on success) to tell a dead run from a hung one. * Does not exit; callers keep their own exit/redirect behaviour. */ public static function fail_backup($message) { One_Click_Migration::write_to_log('Backup failed: ' . self::short_failure_reason($message)); One_Click_Migration::reset_actions_start_mark(); update_option('ocm_is_stopped', true, true); self::release_backup_run(self::get_stored_backup_run_id()); } public static function fail_part_upload($part, $message) { $skipped = get_option('ocm_skipped_folders', array()); self::capture_failed_folder(is_array($skipped) ? $skipped : array(), $part, 'ocm_skipped_folders'); self::log_backup_parts_manifest(self::get_backup_missing_parts()); self::fail_backup(sprintf('%s upload failed: %s', $part, $message)); return OCM_BackgroundBackup::RETURN_TYPE_END_PROCESSS; } /** * Hand the backup over to a new worker once this one has used up its time * budget. Call it only after the finished step is recorded: the next worker * then moves on instead of redoing the step against its retry limit. */ private static function hand_over_if_backup_time_exceeded() { $process = One_Click_Migration::$process_backup_single; if (is_object($process) && method_exists($process, 'time_exceeded') && $process->time_exceeded()) { One_Click_Migration::write_to_log("Process is Restarting"); $process->restart_task(); } } private static function fail_restore($message) { // This run already ended (another worker failed it, or a new run took // over): its failure is not news and must not reach the log (#96). if (self::restore_run_ended()) { return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } One_Click_Migration::write_to_log($message); // Terminal marker, same contract as "Backup failed:" above. One_Click_Migration::write_to_log('Restore failed: ' . self::short_failure_reason($message)); One_Click_Migration::disable_maintenance_mode(); OCM_Debug_Mode::disable(); One_Click_Migration::reset_actions_start_mark(); update_option('ocm_is_stopped', true, true); self::end_restore_run(); // Nothing was swapped in yet and a retry starts from scratch. Don't // leave the plaintext SQL dump under wp-content, where many servers // serve .sql files to anyone. self::remove_extracted_restore_sql(); return OCM_BackgroundRestore::RETURN_TYPE_STOP_PROCESSS; } public static function complete_restore(){ delete_option(OCM_Restore_Precheck::OPTION); $presigned_urls = get_option('ocm_presigned_urls'); self::post_restore_cleanup(); One_Click_Migration::write_to_log('Cleaning up.'); // Add a small delay to ensure all file operations complete before cleanup sleep(2); // Delete the restore workspace (deleteDir() logs and stops on failure). if(WP_DEBUG !== true){ self::delete_restore_workspaces('Cleaning up the restore workspace', $presigned_urls); } self::print_folders_skipped(); self::print_folders_not_found(); $backup_missing_parts = get_option('ocm_backup_missing_parts', []); if (is_array($backup_missing_parts) && $backup_missing_parts) { One_Click_Migration::write_to_log('Notice: Restore finished without: ' . implode(', ', $backup_missing_parts) . ' (these were not included in the backup).'); } One_Click_Migration::disable_maintenance_mode(); OCM_Debug_Mode::disable(); // Durable flag so the admin UI can show the success modal after // post-restore logout / re-login (session cookies often invalidate). // Hard failures never reach here — fail_restore() / db-part stop / // finish_restore_db_phase() exit first. self::finalize_restore_completion_status(); // Partial address-update completion was removed: URL rewrite errors now // stop before promote with LOG_RESTORE_STOPPED_IMPORT. delete_option('ocm_restore_partial_address_updates'); delete_option('ocm_restore_failed_address_pairs'); One_Click_Migration::reset_actions_start_mark(); self::reset_current_restore_steps(); update_option('ocm_is_stopped', true, true); self::end_restore_run(); } /** * Write the restore terminal marker and arm UI flags. * Clean runs: "Restore completed." Warned runs name skipped parts when known. * Public for unit tests. */ public static function finalize_restore_completion_status() { $missing_parts = get_option('ocm_backup_missing_parts', []); $skipped_folders = get_option('ocm_skipped_folders', []); $not_found_folders = get_option('ocm_eexcluded_folders', []); $backup_excluded_parts = get_option('ocm_backup_excluded_parts', []); $backup_excluded_parts = is_array($backup_excluded_parts) ? $backup_excluded_parts : array(); $part_warnings = get_option(self::OPTION_RESTORE_PART_WARNINGS, array()); $part_warnings = is_array($part_warnings) ? $part_warnings : array(); // Defense in depth: never warn about parts the backup marked =excluded. // Must not hide a 404 on an included part (those are not in this list). foreach ($backup_excluded_parts as $excluded_part) { $excluded_part = strtolower((string) $excluded_part); if ($excluded_part !== '') { unset($part_warnings[$excluded_part]); } } // Derive warnings from legacy lists when a reason was not recorded. foreach (array( 'timeout' => $skipped_folders, 'not found' => $not_found_folders, 'not in backup' => $missing_parts, ) as $reason => $list) { if (!is_array($list)) { continue; } foreach ($list as $part) { if (!is_string($part) || $part === '' || $part === 'log' || $part === 'log_download') { continue; } $part = strtolower($part); if (in_array($part, $backup_excluded_parts, true)) { continue; } if (!isset($part_warnings[$part])) { $part_warnings[$part] = $reason; } } } ksort($part_warnings); if ($part_warnings !== array()) { $line = self::format_restore_warnings_terminal_line($part_warnings); One_Click_Migration::write_to_log( 'Notice: Restore finished with warnings for: ' . implode(', ', array_keys($part_warnings)) . '.' ); One_Click_Migration::write_to_log($line); self::apply_restore_success_flags(true, 'warnings'); return; } One_Click_Migration::write_to_log('Restore completed.'); // When the database was not restored for any intentional reason // (backup db=excluded, or the user unticked db for this restore on a // db=ok backup), take the files-only path: no review CTA / success // email until a later release reactivates other plugins. Warnings and // failures already returned above. $user_excluded = get_option('ocm_excluded_folders', array()); $user_excluded = is_array($user_excluded) ? $user_excluded : array(); $db_not_restored = in_array('db', $backup_excluded_parts, true) || in_array('db', $user_excluded, true); if ($db_not_restored) { self::apply_restore_success_flags(true, 'files-only'); return; } self::apply_restore_success_flags(false, ''); } /** * Build a warned terminal line that never contains the substring "Restore completed." * (period after completed). Example: * "Restore completed with warnings: uploads skipped (timeout), themes partially copied." * * @param array $part_warnings * @return string */ public static function format_restore_warnings_terminal_line(array $part_warnings) { $chunks = array(); foreach ($part_warnings as $part => $reason) { $part = strtolower((string) $part); $reason = strtolower(trim((string) $reason)); if ($part === '') { continue; } if ($reason === 'partially copied') { $chunks[] = $part . ' partially copied'; } elseif ($reason === 'copied, old copy not removed') { $chunks[] = $part . ' copied, old copy not removed'; } elseif ($reason === 'not in backup') { $chunks[] = $part . ' skipped (not in backup)'; } elseif ($reason === 'not found') { $chunks[] = $part . ' skipped (not found)'; } elseif ($reason === 'timeout') { $chunks[] = $part . ' skipped (timeout)'; } else { $chunks[] = $part . ' skipped (' . ($reason !== '' ? $reason : 'see log') . ')'; } } if ($chunks === array()) { return self::LOG_RESTORE_COMPLETED_WARNINGS_PREFIX . ' some parts were skipped (see log).'; } return self::LOG_RESTORE_COMPLETED_WARNINGS_PREFIX . ' ' . implode(', ', $chunks) . '.'; } /** * @param string $part * @param string $reason */ public static function record_restore_part_warning($part, $reason) { $part = strtolower(trim((string) $part)); $reason = strtolower(trim((string) $reason)); if ($part === '' || $part === 'log' || $part === 'log_download') { return; } $current = get_option(self::OPTION_RESTORE_PART_WARNINGS, array()); $current = is_array($current) ? $current : array(); // Prefer the more specific "partially copied" / "copied, old copy not // removed" over a generic timeout for the same part. if (isset($current[$part]) && $reason === 'timeout' && ($current[$part] === 'partially copied' || $current[$part] === 'copied, old copy not removed') ) { return; } $current[$part] = $reason !== '' ? $reason : 'see log'; update_option(self::OPTION_RESTORE_PART_WARNINGS, $current, false); } /** * Set restore-success modal + review CTA flags. Warned / missing-parts / * partial-address runs never arm the Review ask or increment the clean * count, so a later clean restore can still show Leave a review once. * * @param bool $is_warned * @param string $reason Short reason for the review_ask log line. */ public static function apply_restore_success_flags($is_warned, $reason = '') { update_option('ocm_show_restore_success', time(), false); $is_partial = (is_string($reason) && false !== strpos($reason, 'partial')); // files-only is a clean Restore completed. — not a warned UI state. $is_files_only = ($reason === 'files-only'); $is_warned_ui = $is_warned && !$is_partial && !$is_files_only; update_option('ocm_restore_was_partial', $is_partial ? 1 : 0, false); update_option(self::OPTION_RESTORE_WAS_WARNED, $is_warned_ui ? 1 : 0, false); if ($is_warned) { update_option('ocm_show_review_cta', 0, false); // files-only: do not consume ocm_clean_restore_count (flag kept). One_Click_Migration::write_to_log(sprintf( 'SYSLOG: review_ask: none (%s)', $reason !== '' ? $reason : 'warned' )); return; } $clean_count = (int) get_option('ocm_clean_restore_count', 0) + 1; update_option('ocm_clean_restore_count', $clean_count, false); $dismissed = (bool) get_option('ocm_review_ask_dismissed', false); $show_cta = (!$dismissed && $clean_count >= 1); update_option('ocm_show_review_cta', $show_cta ? 1 : 0, false); if ($show_cta) { One_Click_Migration::write_to_log('SYSLOG: review_ask: restore-success with review CTA'); } else { One_Click_Migration::write_to_log('SYSLOG: review_ask: none (dismissed or not eligible)'); } } /** * Finish the destructive phase of the restore: DB restore (including staging * URL rewrite + promote) + deferred file swaps + cleanup. * Called inline at the end of start_restore_process() once downloads complete. */ public static function finish_restore_db_phase() { global $wpdb; $presigned_urls = get_option('ocm_presigned_urls'); $excluded_folders = get_option('ocm_excluded_folders', []); //cater for the database restore. if(!in_array('db', $excluded_folders)){ if (!self::check_is_complete_restore_step('db', self::STEP_RESTORE_CHILD_RESTORE)) { self::$db_restore_refused_binary = false; self::$db_restore_refused_options = false; if (!self::restore_db($presigned_urls, $wpdb)) { if (self::$db_restore_refused_binary) { // Terminal marker already written by stop_restore_during_db_import() // or the early binary preflight path. Do not add generic // import-failure lines. } elseif (self::$db_restore_refused_options) { // LOG_RESTORE_STOPPED_OPTIONS already written; do not add // generic import-failure lines (those arm proc_open advice). } elseif (self::$import_oom_stop_written) { // LOG_RESTORE_STOPPED_IMPORT already written by the import // OOM shutdown guard. Do not add a second stop line. } else { // Any other import-stage failure before the swap maps to // RESTORE-STOPPED-IMPORT (live site untouched). One_Click_Migration::write_to_log(self::LOG_RESTORE_STOPPED_IMPORT); } self::cleanup_after_restore_no_change_stop(); exit; } self::set_complete_restore_step('db', self::STEP_RESTORE_CHILD_RESTORE); // active_plugins is now the backup's list (just restored from the options table). // Leave it alone — URL rewrite already ran on staging tables inside // restore_db() (before promote), and we want the user's original // plugin set to be active again after restore. } // DB+options gate passed — only now deactivate other plugins and swap files. self::apply_deferred_file_swaps($presigned_urls); } // URL search-replace runs on temp-prefixed staging tables inside // restore_db() before the atomic RENAME / file swap. A failure there // stops with LOG_RESTORE_STOPPED_IMPORT and never reaches this point. self::complete_restore(); exit; } /** * Log each failed approved address rewrite as `old → new` (optional table.column). * Never logs row payloads or secrets. * * @param array $failed_pairs */ public static function log_failed_address_pairs($failed_pairs) { if (!is_array($failed_pairs)) { return; } foreach ($failed_pairs as $pair) { if (!is_array($pair) || empty($pair['search'])) { continue; } $line = sprintf( 'Address update failed: %s → %s', (string) $pair['search'], isset($pair['replace']) ? (string) $pair['replace'] : '' ); if (!empty($pair['table']) && !empty($pair['column'])) { $line .= sprintf(' (%s.%s)', (string) $pair['table'], (string) $pair['column']); } One_Click_Migration::write_to_log($line); } } public static function initiate_db_backup($presigned_urls, $aes_key) { global $wpdb; $work_path = self::get_backup_work_path(); // Reset logging flags for this backup session OCM_SQL_Utils::reset_backup_logging_flags(); One_Click_Migration::write_to_log('Database backup started'); $db_size_mb = self::DBSize(); One_Click_Migration::write_to_log('DB Size '. $db_size_mb.'MB'); // Pre-flight: if DB ≳ memory_limit, warn and force streamed dump (never hold the SQL in RAM). OCM_SQL_Utils::preflight_db_memory((int) ((float) $db_size_mb * 1024 * 1024), 'dump'); // Pre-flight disk space check: need ~2x DB size for SQL dumps + ZIP if (!self::ensure_temporary_directory()) { return OCM_BackgroundBackup::RETURN_TYPE_END_PROCESSS; } // A worker that only resumes the upload needs no room for a new dump, and // the encrypted archive it uploads already takes part of the space. $needs_dump_space = !self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_COMPRESS); $free_bytes = self::get_free_disk_space($work_path); if ($needs_dump_space && $free_bytes > 0 && $db_size_mb > 0) { $required_bytes = $db_size_mb * 2 * 1024 * 1024; if ($free_bytes < $required_bytes) { $disk_message = sprintf( 'Error: Not enough disk space to complete backup. Need %sMB, only %sMB available.', round($required_bytes / 1024 / 1024), round($free_bytes / 1024 / 1024) ); One_Click_Migration::write_to_log($disk_message); self::fail_backup($disk_message); exit; } } $skipped_restore_files = get_option('ocm_skipped_folders', []); $retry_count = get_option('ocm_backup_compress_retry_db', 0); if($retry_count == 0){ update_option('ocm_backup_compress_retry_db', 1, true); } if($retry_count){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_COMPRESS)) { One_Click_Migration::write_to_log(sprintf('Compressing %s Retry %d','db', $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_backup_compress_retry_db' , $retry_count, true); } }else{ if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_COMPRESS)) { One_Click_Migration::write_to_log(sprintf('Skipping DB compression')); One_Click_Migration::write_to_log(sprintf('Skipping DB encrypting')); One_Click_Migration::write_to_log(sprintf('Skipping DB upload')); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_COMPRESS); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, 'db', 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File DB was skipped due to timeout"); } } } // If file exists already, delete it if (file_exists($work_path . 'db_backup.sql')) { unlink($work_path . 'db_backup.sql'); } if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_COMPRESS)) { $tables = $wpdb->get_results('SHOW TABLES'); // If tmp directory doesn't exist, create it (in wp-content/tmp when // the private work dir isn't writable here) self::ensure_temporary_directory(); $work_path = self::get_backup_work_path(); if (!is_dir($work_path . 'db/')) { if (!mkdir($concurrentDirectory = $work_path . 'db/', 0700, true) && !is_dir($concurrentDirectory)) { One_Click_Migration::write_to_log(sprintf('Database backup started, Error: Directory "%s" was not created', $concurrentDirectory)); One_Click_Migration::write_to_log(sprintf( 'Database backup started, Error: Directory "%s" was not created. Please check the write permission for the parent directory "%s".', $concurrentDirectory, WP_CONTENT_DIR )); self::fail_backup('database backup directory could not be created (write permission)'); exit; } } $url_found = array(); $collect_urls = function ($value) use (&$url_found) { OCM_Extra_Domains::collect_from_text($url_found, $value); }; foreach ($tables as $table) { foreach ($table as $t) { $file = $work_path . "db/$t.sql"; if(file_exists($work_path . "db")){ // Suppressed: generates one log line per table, causing excessive log noise // One_Click_Migration::write_to_log(sprintf(' SYSLOG: Table being backed up: "%s"', $t)); $exported = OCM_SQL_Utils::export([$file], [ 'tables' => $t, 'quick' => null, 'ocm_collect_urls' => $collect_urls, ]); if ($exported === false) { return self::fail_part_upload('db', sprintf('SQL dump write failed for table "%s"', $t)); } }else{ return OCM_BackgroundBackup::RETURN_TYPE_END_PROCESSS; } } } self::publish_url_manifest($url_found); $zip_filepath = self::createZipFile($work_path . 'db/', $aes_key, $presigned_urls); if (!$zip_filepath) { self::fail_backup('could not create the database archive (see previous error line)'); exit; } // The encrypted archive is complete: record it before handing over, or // the next worker would dump the database again and, once its retries // ran out, skip the part. One_Click_Migration::write_to_log('Database ZIP file created.'); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_COMPRESS); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_ZIP_FILE_PATH, $zip_filepath); // Only the encrypted zip is uploaded; drop the per-table SQL dumps and // the plain zip now so they don't stack under later parts' disk needs. self::deleteDirectoryBestEffort($work_path . 'db/'); @unlink($work_path . 'db.zip'); self::hand_over_if_backup_time_exceeded(); } $retry_count = get_option('ocm_backup_upload_retry_db', 0); if(!$retry_count){ update_option('ocm_backup_upload_retry_db', 1, true); } if($retry_count){ if($retry_count <= self::MAX_TRANSFER_RETRIES){ One_Click_Migration::write_to_log(sprintf('Uploading %s Retry %d', 'db', $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_backup_upload_retry_db' , $retry_count, true); }else{ One_Click_Migration::write_to_log(sprintf('Skipping DB upload')); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, 'db', 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File DB was skipped due to timeout"); } } if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_UPLOAD)) { One_Click_Migration::write_to_log(sprintf('%s upload has been started.', 'db')); if (empty($zip_filepath)) { $zip_filepath = self::get_backup_step_value('db', self::STEP_BACKUP_CHILD_ZIP_FILE_PATH); } if (empty($zip_filepath)) { // Fallback if path was not saved after encryption. $zip_filepath = $work_path . 'db.zip.crypt'; } if (!is_string($zip_filepath) || !is_file($zip_filepath) || !is_readable($zip_filepath)) { return self::fail_part_upload('db', 'encrypted archive is missing or unreadable'); } try { $return = OCM_S3::upload_zip($zip_filepath); } catch (\Throwable $e) { return self::fail_part_upload('db', $e->getMessage()); } if ($return !== true) { return self::fail_part_upload('db', 'storage did not confirm the encrypted archive upload'); } // A confirmed PUT is terminal success even if the request consumed the // worker's time budget. Persist success before any restart decision so // this part can never later be classified as skipped. self::mark_backup_part_uploaded('db'); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_UPLOAD); if (is_string($zip_filepath)) { @unlink($zip_filepath); } return false; } } public static function createZipFile($filepath, $aes_key, $presigned_urls) { // Suppress PHP 8+ compatibility warnings from vendor library $old_error_reporting = error_reporting(); error_reporting($old_error_reporting & ~E_DEPRECATED & ~E_NOTICE); // Reset time limit for DB zip creation @set_time_limit(One_Click_Migration::get_timeout()); $filename = self::get_backup_work_path() . 'db.zip'; $skipped_restore_files = get_option('ocm_skipped_folders', []); if (!self::create_native_zip_from_directory($filepath, $filename)) { // The DB dump is bounded in size, so the in-process streaming // zip is the last resort even after the monitored system zip // failed (#69); only its own failure ends the backup. if (self::$archive_backend_failed) { One_Click_Migration::write_to_log(' SYSLOG: Trying in-process streaming zip as the last database archive failover.'); } @unlink($filename); $zip = new ZipFile(); $dir = new DirectoryIterator($filepath); foreach ($dir as $fileinfo) { if (!$fileinfo->isDot()) { $zip->addFile($fileinfo->getPathname(), '/' . $fileinfo->getFilename(), ZipCompressionMethod::STORED); } } try { $zip->saveAsFile($filename); } catch (\Throwable $e) { @unlink($filename); One_Click_Migration::write_to_log(sprintf( 'Error: In-process zip failover could not save "%s": %s (free: %d bytes)', basename($filename), $e->getMessage(), (int) self::get_free_disk_space(dirname($filename)) )); self::fail_backup('database archive could not be created by ZipArchive, system zip or in-process zip: ' . $e->getMessage()); exit; } $zip->close(); delete_option('ocm_zip_close_in_progress_' . md5($filename)); One_Click_Migration::write_to_log(sprintf(' SYSLOG: Created "%s" with in-process streaming zip.', basename($filename))); } $retry_count = get_option('ocm_backup_encrypt_retry_db', 0); if(!$retry_count){ update_option('ocm_backup_encrypt_retry_db', 1, true); } if($retry_count){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_ENCRYPT)) { One_Click_Migration::write_to_log(sprintf('Encrypting %s Retry %d', 'db', $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_backup_encrypt_retry_db' , $retry_count, true); } } else{ if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_ENCRYPT)) { One_Click_Migration::write_to_log(sprintf('Skipping DB Encrypting')); One_Click_Migration::write_to_log(sprintf('Skipping DB upload')); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step('db', self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, 'db', 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File DB was skipped due to timeout"); } } } if (!self::check_is_complete_backup_step('db', self::STEP_BACKUP_CHILD_ENCRYPT)) { // Reset time limit for DB encryption @set_time_limit(One_Click_Migration::get_timeout()); $return = self::encryptZipFile($filename, $aes_key); if (!$return) { self::fail_backup('encryption of the database archive failed (see previous error line)'); exit; } // No hand-over here: initiate_db_backup() records the finished // archive first, then hands over if the budget is used up. One_Click_Migration::write_to_log('DB Zip file has been encrypted.'); error_reporting($old_error_reporting); return $return; } // Restore original error reporting error_reporting($old_error_reporting); } public static function initiate_folder_backup($folder_name, $presigned_urls, $aes_key) { // Re-create tmp directory if it was deleted (e.g. by Stop & Reset while background process was still running) if (!self::ensure_temporary_directory()) { return OCM_BackgroundBackup::RETURN_TYPE_END_PROCESSS; } $work_path = self::get_backup_work_path(); // Suppress PHP 8+ compatibility warnings from vendor library $old_error_reporting = error_reporting(); error_reporting($old_error_reporting & ~E_DEPRECATED & ~E_NOTICE); // Initialize archive object $zip = new ZipFile(); $filename = sprintf('%s%s.zip', $work_path, $folder_name); $folder_path = sprintf('%s/%s', WP_CONTENT_DIR, $folder_name); $skipped_restore_files = get_option('ocm_skipped_folders', []); // compress $retry_count = get_option('ocm_backup_compress_retry_' . $folder_name, 0); if(!$retry_count){ update_option('ocm_backup_compress_retry_' . $folder_name, 1, true); } if($retry_count){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS)) { One_Click_Migration::write_to_log(sprintf('Compressing %s Retry %d', $folder_name, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_backup_compress_retry_' . $folder_name, $retry_count, true); } } else{ if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS)) { One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Compressing')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Encypting')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Upload')); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, $folder_name, 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File $folder_name was skipped due to timeout"); } } } if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS)) { //log folder sizes $folder_size_bytes = self::folderSize($folder_path); $folder_size = self::convertToReadableSize($folder_size_bytes); One_Click_Migration::write_to_log(sprintf('%s size: %s', $folder_name, $folder_size)); // Pre-flight disk space check: need ~1.5x folder size for ZIP + encrypted copy $free_bytes = self::get_free_disk_space($work_path); if ($free_bytes > 0 && $folder_size_bytes > 0) { $required_bytes = $folder_size_bytes * 1.5; if ($free_bytes < $required_bytes) { One_Click_Migration::write_to_log(sprintf( 'Error: Not enough disk space to backup %s. Need %s, only %s available.', $folder_name, self::convertToReadableSize($required_bytes), self::convertToReadableSize($free_bytes) )); self::capture_failed_folder($skipped_restore_files, $folder_name, 'ocm_skipped_folders'); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD); error_reporting($old_error_reporting); return false; } } // Reset time limit for folder archiving @set_time_limit(One_Click_Migration::get_timeout()); try { One_Click_Migration::write_to_log(sprintf('Archiving %s to %s', $folder_name, $filename)); if (!self::create_native_zip_from_directory($folder_path, $filename)) { if (self::$archive_backend_failed) { error_reporting($old_error_reporting); self::fail_backup(sprintf('archive creation failed for "%s" (see previous error line)', $folder_name)); return OCM_BackgroundBackup::RETURN_TYPE_END_PROCESSS; } /** @var SplFileInfo[] $files */ $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($folder_path, \FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ($files as $name => $file) { if (!$file->isDir()) { $filePath = $file->getRealPath(); if (!$filePath || !is_readable($filePath)) { continue; } $relativePath = str_replace('\\', '/', substr($filePath, strlen($folder_path) + 1)); $zip->addFile($filePath, $relativePath, ZipCompressionMethod::STORED); } } $zip->saveAsFile($filename); $zip->close(); } } catch (ZipException $e) { One_Click_Migration::write_to_log(sprintf('Error: %s', $e->getMessage())); $next_retry_count = get_option('ocm_backup_compress_retry_' . $folder_name, 1); if($next_retry_count && $next_retry_count <= self::MAX_STEP_RETRIES){ One_Click_Migration::write_to_log("Process is Restarting"); update_option('ocm_backup_compress_retry_' . $folder_name, $next_retry_count, true); One_Click_Migration::$process_backup_single->restart_task(); } else{ One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Compressing')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Encypting')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Upload')); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, $folder_name, 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File $folder_name was skipped due to timeout"); } } // The archive is complete: record it before handing over, or the next // worker would zip the folder again and, once its retries ran out, // skip the part. (A ZipException that ran out of retries already // marked the part skipped.) if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS)) { self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_COMPRESS); One_Click_Migration::write_to_log(sprintf('%s ZIP has been created.', $folder_name)); } self::hand_over_if_backup_time_exceeded(); } // encrypt $retry_count = get_option('ocm_backup_encrypt_retry_' . $folder_name, 0); if(!$retry_count){ update_option('ocm_backup_encrypt_retry_' . $folder_name, 1, true); } if($retry_count){ if($retry_count <= self::MAX_STEP_RETRIES){ if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT)) { One_Click_Migration::write_to_log(sprintf('Encrypting %s Retry %d', $folder_name, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_backup_encrypt_retry_' . $folder_name, $retry_count, true); } }else{ if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT)) { One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Encypting')); One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Upload')); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, $folder_name, 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File $folder_name was skipped due to timeout"); } } } if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT)) { // Reset time limit for folder encryption @set_time_limit(One_Click_Migration::get_timeout()); $zip_filepath = self::encryptZipFile($filename, $aes_key); if (!$zip_filepath) { self::fail_backup(sprintf('encryption of the "%s" archive failed (see previous error line)', $folder_name)); exit; } // Encrypted: record it (and the path the upload reads) before handing // over, as for the archive above. self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ZIP_FILE_PATH, $zip_filepath); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ENCRYPT); One_Click_Migration::write_to_log(sprintf('%s ZIP has been encrypted.', $folder_name)); // The upload only needs the encrypted copy. @unlink($filename); self::hand_over_if_backup_time_exceeded(); } // upload $retry_count = get_option('ocm_backup_upload_retry_' . $folder_name, 0); if(!$retry_count){ update_option('ocm_backup_upload_retry_' . $folder_name, 1, true); } if($retry_count){ if($retry_count <= self::MAX_TRANSFER_RETRIES){ if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD)) { One_Click_Migration::write_to_log(sprintf('Uploading %s Retry %d', $folder_name, $retry_count)); $retry_count = $retry_count + 1; update_option('ocm_backup_upload_retry_' . $folder_name, $retry_count, true); } }else{ if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD)) { One_Click_Migration::write_to_log(sprintf('Skipping ' .$folder_name. ' Uploading')); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD); self::capture_failed_folder($skipped_restore_files, $folder_name, 'ocm_skipped_folders'); One_Click_Migration::write_to_log("Notice: File $folder_name was skipped due to timeout"); } } } if (!self::check_is_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD)) { $zip_filepath = self::get_backup_step_value($folder_name, self::STEP_BACKUP_CHILD_ZIP_FILE_PATH); if (empty($zip_filepath)) { // Construct the expected path as fallback $expected_path = sprintf('%s%s.zip.crypt', $work_path, $folder_name); if (file_exists($expected_path)) { One_Click_Migration::write_to_log(sprintf(' SYSLOG: %s zipFilePath was empty but found encrypted file at expected path', $folder_name)); $zip_filepath = $expected_path; self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_ZIP_FILE_PATH, $zip_filepath); } else { One_Click_Migration::write_to_log(sprintf(' SYSLOG: %s upload skipped - encrypted zip file not found at %s', $folder_name, $expected_path)); error_reporting($old_error_reporting); return self::fail_part_upload($folder_name, 'encrypted archive is missing'); } } One_Click_Migration::write_to_log(sprintf('%s folder upload has been started.', $folder_name)); if (!is_string($zip_filepath) || !is_file($zip_filepath) || !is_readable($zip_filepath)) { error_reporting($old_error_reporting); return self::fail_part_upload($folder_name, 'encrypted archive is missing or unreadable'); } try { $return = OCM_S3::upload_zip($zip_filepath); } catch (\Throwable $e) { error_reporting($old_error_reporting); return self::fail_part_upload($folder_name, $e->getMessage()); } if ($return !== true) { error_reporting($old_error_reporting); return self::fail_part_upload($folder_name, 'storage did not confirm the encrypted archive upload'); } // Upload confirmation wins over the worker time budget. Recording it // immediately prevents a retry from converting success into skipped. self::mark_backup_part_uploaded($folder_name); self::set_complete_backup_step($folder_name, self::STEP_BACKUP_CHILD_UPLOAD); if (is_string($zip_filepath)) { @unlink($zip_filepath); } error_reporting($old_error_reporting); return false; } // Restore original error reporting error_reporting($old_error_reporting); } public static function deleteDir($dir, $action_log, $presigned_urls, $keep_root = false) { if (is_dir($dir)) { $objects = @scandir($dir); if (!$objects) { One_Click_Migration::write_to_log(sprintf( '%s. Error: "%s" directory could not be deleted. Please check the write permission for this directory or the parent directory.', $action_log, $dir )); update_option('ocm_is_stopped', true, true); exit; } foreach ($objects as $object) { if ($object !== '.' && $object !== '..') { if (filetype($dir . '/' . $object) === 'dir') { self::deleteDir($dir . '/' . $object, $action_log, $presigned_urls); } else if (!unlink($dir . '/' . $object)) { One_Click_Migration::write_to_log(sprintf( '%s. Error: "%s" file could not be deleted. Please check the write permission for the parent directory.', $action_log, $dir )); update_option('ocm_is_stopped', true, true); exit; } } } if (!$keep_root) { reset($objects); if (!rmdir($dir)) { One_Click_Migration::write_to_log(sprintf( '%s. Error: "%s" directory could not be deleted. Please check the write permission for this directory or the parent directory.', $action_log, $dir )); update_option('ocm_is_stopped', true, true); exit; } } } } private static function force_only_ocm_plugin_active($context) { wp_cache_delete('active_plugins', 'options'); wp_cache_delete('alloptions', 'options'); $active_plugins = get_option('active_plugins', array()); if (!is_array($active_plugins)) { return; } $this_plugin_basename = plugin_basename(OCM_MAIN_FILE); $ocm_only = array($this_plugin_basename); $other_active_count = 0; foreach ($active_plugins as $plugin) { if ($plugin !== $this_plugin_basename) { $other_active_count++; } } if ($active_plugins === $ocm_only) { return; } if (!get_option('ocm_force_deactivated_plugins')) { update_option('ocm_force_deactivated_plugins', $active_plugins, true); } update_option('active_plugins', $ocm_only, true); One_Click_Migration::write_to_log(sprintf( 'Temporarily deactivated %d plugins during restore (%s).', $other_active_count, $context )); } private static function replaceDirectoryFromExtract($extract_path, $directory, $key) { if (!is_dir($extract_path)) { return false; } $bak = rtrim($directory, '/\\') . '.ocm_bak'; // Recover a previous interrupted swap: dest missing, bak still present. if (!is_dir($directory) && is_dir($bak)) { @rename($bak, $directory); } $had_dest = is_dir($directory); if ($had_dest) { if (is_dir($bak)) { self::deleteDirectoryBestEffort($bak); } if (!@rename($directory, $bak)) { One_Click_Migration::write_to_log(sprintf( 'Notice: Could not rename "%s" aside; merging restored files over the existing directory.', $directory )); if (!self::copyDirectory($extract_path, $directory)) { return false; } self::deleteDirectoryBestEffort($extract_path); return true; } } if (@rename($extract_path, $directory)) { if ($had_dest && is_dir($bak)) { self::deleteDirectoryBestEffort($bak); } return true; } // New tree failed to take dest's place — put the old tree back first. if ($had_dest && is_dir($bak) && !is_dir($directory)) { @rename($bak, $directory); } One_Click_Migration::write_to_log(sprintf( 'Notice: Could not rename extract into "%s"; copying instead.', $directory )); if (!is_dir($directory) && !@mkdir($directory, 0755, true) && !is_dir($directory)) { return false; } if (!self::copyDirectory($extract_path, $directory)) { return false; } self::deleteDirectoryBestEffort($extract_path); if ($had_dest && is_dir($bak)) { self::deleteDirectoryBestEffort($bak); } return true; } private static function deleteDirectoryBestEffort($dir, $keep_root = false) { if (!file_exists($dir)) { return true; } if (is_link($dir) || is_file($dir)) { @chmod($dir, 0644); return @unlink($dir); } if (!is_dir($dir)) { return false; } $objects = @scandir($dir); if ($objects === false) { @chmod($dir, 0755); $objects = @scandir($dir); } if ($objects === false) { return false; } $success = true; foreach ($objects as $object) { if ($object === '.' || $object === '..') { continue; } $path = $dir . '/' . $object; if (is_dir($path) && !is_link($path)) { $success = self::deleteDirectoryBestEffort($path) && $success; } else { @chmod($path, 0644); $success = @unlink($path) && $success; } } if (!$keep_root) { @chmod($dir, 0755); $success = @rmdir($dir) && $success; } return $success; } /** * Start every restore with one empty workspace: remove all old ones (they * may hold a plaintext SQL dump), then create it in the private work dir, * or under wp-content with an unguessable name and deny files when that * isn't possible. * * @param string $attempted Set to the directory that could not be cleared or created. * @return string The workspace, or '' on failure. */ private static function prepare_restore_workspace(&$attempted = '') { foreach (self::existing_restore_work_paths() as $old) { if (!self::deleteDirectoryBestEffort($old)) { $attempted = $old; return ''; } } $candidates = array(); if (self::private_work_dir_usable(true)) { $candidates[] = self::get_private_work_dir() . '/restore'; } $candidates[] = WP_CONTENT_DIR . '/' . self::RESTORE_WORKSPACE_FALLBACK_PREFIX . self::random_dir_token(); foreach ($candidates as $dir) { $attempted = $dir; if ((@mkdir($dir, 0700, true) || is_dir($dir)) && is_writable($dir)) { self::write_deny_files($dir); return $dir; } @rmdir($dir); } return ''; } private static function copyDirectory($src, $dst) { if (!is_dir($src)) { return false; } if (!is_dir($dst)) { if (!@mkdir($dst, 0755, true) && !is_dir($dst)) { return false; } } $objects = @scandir($src); if (!$objects) { return false; } foreach ($objects as $object) { if ($object !== '.' && $object !== '..') { $srcPath = $src . '/' . $object; $dstPath = $dst . '/' . $object; if (is_link($srcPath)) { if (file_exists($dstPath) || is_link($dstPath)) { self::deleteDirectoryBestEffort($dstPath); } $link_target = readlink($srcPath); if ($link_target === false || !@symlink($link_target, $dstPath)) { return false; } } else if (is_dir($srcPath)) { if (!self::copyDirectory($srcPath, $dstPath)) { return false; } } else { if (file_exists($dstPath)) { @chmod($dstPath, 0644); } if (!@copy($srcPath, $dstPath)) { return false; } } } } return true; } private static function create_native_zip_from_directory($folder_path, $filename) { self::$archive_backend_failed = false; $close_marker = 'ocm_zip_close_in_progress_' . md5($filename); // Empty uploads/themes/plugins must not fail the backup. ZipArchive::close() // with zero queued files returns true but writes no archive on several // PHP/libzip builds (field backup 48b6b15f). An empty DB dump must still // fail — never write a valid empty db.zip. if (!is_dir($folder_path) || !self::directory_contains_files($folder_path)) { delete_option($close_marker); if (self::archive_allows_empty($filename)) { One_Click_Migration::write_to_log(sprintf( ' SYSLOG: "%s" has no files; writing a valid empty archive.', basename($filename) )); return self::write_empty_zip_archive($filename); } One_Click_Migration::write_to_log(sprintf( 'Error: "%s" source folder is empty; refusing to write an empty database archive.', basename($filename) )); return false; } // Normal sites always use ZipArchive. This marker survives a PHP // timeout/worker death inside close(). The close may nevertheless have // committed a valid central directory before PHP was terminated, so // accept that completed primary result before attempting failover. if (get_option($close_marker, false)) { if (self::is_valid_zip_archive($filename)) { delete_option($close_marker); One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Previous ZipArchive::close() completed "%s" before the worker exited; using the finished archive.', basename($filename) )); return true; } One_Click_Migration::write_to_log(sprintf( 'Notice: Previous ZipArchive::close() did not finish for "%s"; switching to slower monitored zip failover.', basename($filename) )); return self::run_zip_close_failover($folder_path, $filename, $close_marker); } if (!class_exists('ZipArchive')) { return false; } $zip = new ZipArchive(); $opened = $zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE); if ($opened !== true) { return false; } $base_length = strlen(rtrim($folder_path, '/\\')) + 1; $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($folder_path, \FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY ); $queued_files = 0; $queued_bytes = 0; $last_tick = time(); foreach ($files as $file) { if ($file->isDir()) { continue; } $file_path = $file->getRealPath(); if (!$file_path || !is_readable($file_path)) { continue; } $relative_path = str_replace('\\', '/', substr($file_path, $base_length)); if (!$zip->addFile($file_path, $relative_path)) { $zip->close(); @unlink($filename); return false; } if (defined('ZipArchive::CM_STORE')) { $zip->setCompressionName($relative_path, ZipArchive::CM_STORE); } $queued_files++; $size = @filesize($file_path); $queued_bytes += $size ? $size : 0; // Enumerating a huge uploads dir can take minutes on its own, and // nothing else in this loop resets the PHP timer or tells the // stall watchdog the worker is alive. if (0 === ($queued_files % 500) || (time() - $last_tick) >= 5) { @set_time_limit(One_Click_Migration::get_timeout()); One_Click_Migration::worker_heartbeat(); $last_tick = time(); } } if ($queued_files === 0) { @$zip->close(); delete_option($close_marker); if (self::archive_allows_empty($filename)) { One_Click_Migration::write_to_log(sprintf( ' SYSLOG: "%s" queued 0 readable files; writing a valid empty archive.', basename($filename) )); return self::write_empty_zip_archive($filename); } One_Click_Migration::write_to_log(sprintf( 'Error: "%s" queued 0 readable files; refusing to write an empty database archive.', basename($filename) )); return false; } // addFile() only queues entries; close() is the single blocking call // that reads and writes every byte, so on multi-GB archives it can // exceed max_execution_time by itself. Lift the limit entirely where // the host allows it, otherwise fall back to the usual budget. One_Click_Migration::worker_heartbeat(); if (!@set_time_limit(0)) { @set_time_limit(One_Click_Migration::get_timeout()); } One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Zip close starting for "%s": %d files, %d bytes queued. No further log output is expected until close finishes.', basename($filename), $queued_files, $queued_bytes )); update_option($close_marker, time(), false); if (function_exists('error_clear_last')) { error_clear_last(); } $closed = @$zip->close(); if ($closed || self::is_valid_zip_archive($filename)) { delete_option($close_marker); if (!$closed) { One_Click_Migration::write_to_log(sprintf( ' SYSLOG: ZipArchive::close() reported failure for "%s" but the archive is already valid; keeping the primary result.', basename($filename) )); } return true; } // close() returned false: libzip discarded the archive. Record why // (status/warning, bytes left on disk, free space) so support can tell // a disk/quota failure from a libzip one, then fail over. $close_warning = error_get_last(); try { $status = @$zip->getStatusString(); } catch (\Throwable $e) { $status = ''; } One_Click_Migration::write_to_log(sprintf( 'Notice: ZipArchive::close() failed for "%s" (%d files, %d bytes queued; status: %s; warning: %s; on disk: %d bytes; free: %d bytes); switching to slower monitored zip failover.', basename($filename), $queued_files, $queued_bytes, is_string($status) && '' !== $status ? $status : 'n/a', is_array($close_warning) && isset($close_warning['message']) ? $close_warning['message'] : 'n/a', is_file($filename) ? (int) @filesize($filename) : 0, (int) self::get_free_disk_space(dirname($filename)) )); return self::run_zip_close_failover($folder_path, $filename, $close_marker); } /** * Shared failover after ZipArchive::close() failed or hung. Tries the * monitored system zip first. When that is unavailable on the host (no * proc_open or zip binary), returns false with $archive_backend_failed * cleared so callers use their in-process PhpZip streaming fallback * instead of dead-ending. A system zip that ran and failed sets * $archive_backend_failed. */ private static function run_zip_close_failover($folder_path, $filename, $close_marker) { // Never let a half-written primary archive be mistaken for a result. @unlink($filename); $failover_result = self::create_cli_zip_from_directory($folder_path, $filename); if ($failover_result === true) { delete_option($close_marker); return true; } @unlink($filename); if ($failover_result === null) { self::$archive_backend_failed = false; One_Click_Migration::write_to_log(sprintf( 'Notice: Monitored system zip is unavailable on this host for "%s"; switching to in-process streaming zip failover.', basename($filename) )); return false; } self::$archive_backend_failed = true; One_Click_Migration::write_to_log(sprintf( 'Notice: Slower zip failover failed for "%s".', basename($filename) )); return false; } private static function is_valid_zip_archive($filename) { if (!class_exists('ZipArchive') || !is_file($filename) || !is_readable($filename)) { return false; } $zip = new ZipArchive(); $flags = defined('ZipArchive::CHECKCONS') ? ZipArchive::CHECKCONS : 0; $opened = $zip->open($filename, $flags); if (true !== $opened) { return false; } return $zip->close(); } /** * Slower failover used only after ZipArchive::close() failed or a persisted * marker proves it hung/timed out. It streams outside PHP and keeps worker * heartbeats moving instead of entering another uninterruptible close(). */ private static function create_cli_zip_from_directory($folder_path, $filename) { if (!is_dir($folder_path)) { return null; } if (!self::directory_contains_files($folder_path)) { if (self::archive_allows_empty($filename)) { return self::write_empty_zip_archive($filename); } return false; } if (DIRECTORY_SEPARATOR !== '/' || !function_exists('proc_open')) { return null; } $probe = @proc_open( '/usr/bin/env zip -v', array( 1 => array('file', '/dev/null', 'w'), 2 => array('file', '/dev/null', 'w'), ), $probe_pipes ); if (!is_resource($probe) || proc_close($probe) !== 0) { return null; } if (self::is_valid_zip_archive($filename)) { return true; } @unlink($filename); $process = @proc_open( '/usr/bin/env zip -0 -q -r ' . escapeshellarg($filename) . ' .', array( 1 => array('file', '/dev/null', 'w'), 2 => array('pipe', 'w'), ), $pipes, $folder_path ); if (!is_resource($process)) { return null; } stream_set_blocking($pipes[2], false); $error = ''; do { $status = proc_get_status($process); $error .= (string) stream_get_contents($pipes[2]); if (strlen($error) > 2000) { $error = substr($error, -2000); } One_Click_Migration::worker_heartbeat(); @set_time_limit(One_Click_Migration::get_timeout()); if (!empty($status['running'])) { sleep(1); } } while (!empty($status['running'])); $error .= (string) stream_get_contents($pipes[2]); fclose($pipes[2]); $exit_code = proc_close($process); if ($exit_code === -1 && isset($status['exitcode'])) { $exit_code = (int) $status['exitcode']; } if ($exit_code !== 0 || !is_file($filename) || filesize($filename) === 0) { @unlink($filename); One_Click_Migration::write_to_log(sprintf( 'Error: Monitored system zip failed for "%s" (exit=%d): %s', basename($filename), $exit_code, substr(trim($error), 0, 500) )); return false; } One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Created "%s" with monitored system zip.', basename($filename) )); return true; } private static function directory_contains_files($folder_path) { if (!is_dir($folder_path)) { return false; } $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($folder_path, \FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ($files as $file) { if ($file->isFile()) { return true; } } return false; } /** * Empty archives are allowed for uploads/themes/plugins only — never for db.zip. * * @param string $filename * @return bool */ private static function archive_allows_empty($filename) { $base = strtolower(basename((string) $filename)); if ($base === 'db.zip') { return false; } return in_array($base, array('uploads.zip', 'themes.zip', 'plugins.zip'), true) || preg_match('/^(uploads|themes|plugins)\.zip$/i', $base); } /** * Write a minimal valid empty ZIP (EOCD only). Used for empty uploads/themes/ * plugins parts so encryption and restore still have a real archive. * * @param string $filename * @return bool */ public static function write_empty_zip_archive($filename) { @unlink($filename); $empty_zip = hex2bin('504b0506000000000000000000000000000000000000'); if (!is_string($empty_zip)) { return false; } return file_put_contents($filename, $empty_zip) === strlen($empty_zip) && is_file($filename) && is_readable($filename); } private static function post_restore_cleanup() { global $wp_rewrite; $wp_rewrite->init(); if (function_exists('save_mod_rewrite_rules')) { save_mod_rewrite_rules(); } if (function_exists('iis7_save_url_rewrite_rules')) { iis7_save_url_rewrite_rules(); } } /** * Short, log-safe summary of the last PHP filesystem error. Never includes * file contents; distinguishes hosting quota (EDQUOT, errno 122) from a * full disk (ENOSPC, errno 28) because Support triages them differently. */ public static function last_fs_error_summary() { $err = error_get_last(); $message = ($err && isset($err['message'])) ? (string) $err['message'] : ''; $errno = 0; if (preg_match('/errno=(\d+)/', $message, $m)) { $errno = (int) $m[1]; } elseif (false !== stripos($message, 'Disk quota exceeded')) { $errno = 122; } elseif (false !== stripos($message, 'No space left on device')) { $errno = 28; } if (122 === $errno) { return 'Reason: errno=122 EDQUOT (hosting disk quota exceeded — not the same as disk full/ENOSPC).'; } if (28 === $errno) { return 'Reason: errno=28 ENOSPC (no space left on device).'; } if ('' === $message) { return 'Reason: unknown I/O error.'; } // Keep the reason short and printable so nothing binary can reach the log. $message = preg_replace('/[^\x20-\x7E]/', '?', $message); return 'Reason: ' . substr($message, 0, 200); } public static function encryptZipFile($filename, $key) { // open file to read if (false === ($file_handle = fopen($filename, 'rb'))) { One_Click_Migration::write_to_log(sprintf('Error: Failed to open file "%s" for encryption. %s', $filename, self::last_fs_error_summary())); return false; } $encrypted_path = sprintf('%s/encrypted_%s', dirname($filename), basename($filename)); $data_encrypted = 0; $buffer_size = 2097152; $file_size = filesize($filename); $rijndael = new Rijndael(); $rijndael->setKey($key); $rijndael->disablePadding(); $rijndael->enableContinuousBuffer(); // open new file from new path if (false === ($encrypted_handle = fopen($encrypted_path, 'wb+'))) { One_Click_Migration::write_to_log(sprintf('Error: Failed to open file "%s" for writing. %s', $encrypted_path, self::last_fs_error_summary())); fclose($file_handle); return false; } $chunk_index = 0; $last_progress_mark = 0; // loop around the file while ($data_encrypted < $file_size) { // A 41.5GB archive is ~20k 2MB chunks — far beyond one // max_execution_time budget. Refresh the timer and the stall // watchdog every 8 chunks (16MB) to keep the heartbeat cheap. if (0 === ($chunk_index % 8)) { @set_time_limit(One_Click_Migration::get_timeout()); One_Click_Migration::worker_heartbeat(); } $chunk_index++; // read buffer-sized amount from file if (false === ($file_part = fread($file_handle, $buffer_size))) { One_Click_Migration::write_to_log(sprintf( 'Error: Failed to read from file "%s" at offset %d of %d bytes. %s', $filename, $data_encrypted, $file_size, self::last_fs_error_summary() )); fclose($encrypted_handle); fclose($file_handle); return false; } // check to ensure padding is needed before encryption $length = strlen($file_part); if (0 !== $length % 16) { $pad = 16 - ($length % 16); $file_part = str_pad($file_part, $length + $pad, chr($pad)); } $encrypted_data = $rijndael->encrypt($file_part); // A short write (quota/disk full) corrupts the archive the same way a // failed write does, so treat both as fatal. Never log the payload — // binary ciphertext in the log breaks the JSON log consumers. $written = fwrite($encrypted_handle, $encrypted_data); if (false === $written || $written < strlen($encrypted_data)) { One_Click_Migration::write_to_log(sprintf( 'Error: Failed to write encrypted data to "%s" (wrote %s of %d bytes for this chunk, %d of %d source bytes done). %s', $encrypted_path, false === $written ? 'none' : (string) $written, strlen($encrypted_data), min($data_encrypted, $file_size), $file_size, self::last_fs_error_summary() )); fclose($encrypted_handle); fclose($file_handle); return false; } $data_encrypted += $buffer_size; // Progress marker for Support (offsets only, never payload) so a // long-running encrypt is distinguishable from a hang. if (($data_encrypted - $last_progress_mark) >= 536870912) { $last_progress_mark = $data_encrypted; One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Encrypting "%s": %d MB of %d MB done.', basename($filename), (int) floor(min($data_encrypted, $file_size) / 1048576), (int) floor($file_size / 1048576) )); } } // close the main file handle fclose($encrypted_handle); fclose($file_handle); // encrypted path $result_path = $filename . '.crypt'; // need to replace original file with tmp file if (false === @rename($encrypted_path, $result_path)) { One_Click_Migration::write_to_log(sprintf('Error: Failed rename failed: "%s" -> "%s". %s', $encrypted_path, $result_path, self::last_fs_error_summary())); return false; } return $result_path; } public static function decryptZipFile($filename, $key) { // open file to read if (false === ($file_handle = fopen($filename, 'rb'))) { One_Click_Migration::write_to_log(sprintf('Error: Failed to open file "%s" for decryption. %s', $filename, self::last_fs_error_summary())); return false; } $decrypted_path = dirname($filename) . '/decrypt_' . basename($filename); // open new file from new path if (false === ($decrypted_handle = fopen($decrypted_path, 'wb+'))) { One_Click_Migration::write_to_log(sprintf('Error: Failed to open file "%s" for writing. %s', $decrypted_path, self::last_fs_error_summary())); fclose($file_handle); return false; } // setup encryption $rijndael = new Rijndael(); $rijndael->setKey($key); $rijndael->disablePadding(); $rijndael->enableContinuousBuffer(); $file_size = filesize($filename); $bytes_decrypted = 0; $buffer_size = 2097152; // loop around the file while ($bytes_decrypted < $file_size) { // read buffer sized amount from file if (false === ($file_part = fread($file_handle, $buffer_size))) { One_Click_Migration::write_to_log(sprintf( 'Error: Failed to read from file "%s" at offset %d of %d bytes. %s', $filename, $bytes_decrypted, $file_size, self::last_fs_error_summary() )); fclose($decrypted_handle); fclose($file_handle); return false; } // check to ensure padding is needed before decryption $length = strlen($file_part); if (0 !== $length % 16) { $pad = 16 - ($length % 16); $file_part = str_pad($file_part, $length + $pad, chr($pad)); } $decrypted_data = $rijndael->decrypt($file_part); $is_last_block = ($bytes_decrypted + strlen($decrypted_data) >= $file_size); $write_bytes = min($file_size - $bytes_decrypted, strlen($decrypted_data)); if ($is_last_block) { $is_padding = false; $last_byte = ord($decrypted_data[strlen($decrypted_data) - 1]); if ($last_byte < 16) { $is_padding = true; for ($j = 1; $j <= $last_byte; $j++) { if ($decrypted_data[strlen($decrypted_data) - $j] !== chr($last_byte)) { $is_padding = false; } } } if ($is_padding) { $write_bytes -= $last_byte; } } // Never log the decrypted payload here — path, sizes and errno only. // A full disk or quota can return a short count rather than false. $written = fwrite($decrypted_handle, $decrypted_data, $write_bytes); if (false === $written || $written < $write_bytes) { One_Click_Migration::write_to_log(sprintf( 'Error: Failed to write decrypted data to "%s" (%d of %d source bytes done). %s', $decrypted_path, min($bytes_decrypted, $file_size), $file_size, self::last_fs_error_summary() )); fclose($decrypted_handle); fclose($file_handle); return false; } $bytes_decrypted += $buffer_size; One_Click_Migration::transfer_heartbeat(One_Click_Migration::$process_restore_single); if (self::restore_run_ended_throttled()) { // The run ended while this worker was decrypting (#96). fclose($decrypted_handle); fclose($file_handle); @unlink($decrypted_path); return false; } } // close the main file handle fclose($decrypted_handle); // close original file fclose($file_handle); // remove the crypt extension from the end as this causes issues when opening $filename_new = preg_replace('/\.crypt$/', '', $filename, 1); // //need to replace original file with tmp file $filename_basename = basename($filename_new); if (false === @rename($decrypted_path, $filename_new)) { One_Click_Migration::write_to_log(sprintf('Error: Failed rename failed: "%s" -> "%s". %s', $decrypted_path, $filename_new, self::last_fs_error_summary())); return false; } // need to send back the new decrypted path return array( 'filename' => $filename_new, 'basename' => $filename_basename ); } /** * Revive a stalled background worker from the progress poller. The poller is * the only request guaranteed to keep running during a migration on hosts * that block loopback requests, so it doubles as the watchdog: when a run is * in flight but the worker heartbeat has gone stale, resume it inline. * * Only runs in REST/ajax context — trigger_processing() ends its request with * wp_die(), which is fine for a poll but would blank an admin page render. */ private static function maybe_revive_stalled_worker($is_stopped) { if ($is_stopped) { return; } if (!(defined('REST_REQUEST') && REST_REQUEST) && !wp_doing_ajax()) { return; } $backup_active = (bool) get_option('ocm_action_start_backup'); $restore_active = (bool) get_option('ocm_action_start_restore'); if (!$backup_active && !$restore_active) { return; } $process = $restore_active ? One_Click_Migration::$process_restore_single : One_Click_Migration::$process_backup_single; if (!is_object($process) || !method_exists($process, 'trigger_processing')) { return; } $now = time(); // On hosts where the loopback gate already failed there is no healthy // dispatch chain to wait for, so stale means stalled much sooner. $threshold = get_option('ocm_dispatch_blocked') ? 25 : 45; $heartbeat = (int) get_option('ocm_worker_heartbeat', 0); if (0 === $heartbeat) { // First sighting of this run: start the clock instead of assuming a stall. One_Click_Migration::worker_heartbeat(); return; } if (($now - $heartbeat) <= $threshold) { return; } // A restore worker in a long step without heartbeats (e.g. extracting a // large zip) still holds its process lock. It isn't stalled; a second // worker would race it on the same files (#79). if ($restore_active && OCM_BackgroundRestore::is_worker_file_lock_held()) { return; } // Throttle so a burst of concurrent polls produces a single pump. $last_pump = (int) get_option('ocm_worker_watchdog_last', 0); if (($now - $last_pump) <= 20) { return; } update_option('ocm_worker_watchdog_last', $now, false); $revivals = (int) get_option('ocm_worker_revival_count', 0) + 1; update_option('ocm_worker_revival_count', $revivals, false); One_Click_Migration::write_to_log(sprintf( ' SYSLOG: Worker heartbeat is %d seconds old; resuming via direct processing (revival #%d)', $now - $heartbeat, $revivals )); if (3 === $revivals) { One_Click_Migration::write_to_log('Notice: Background dispatch appears blocked on this host; the process continues in direct mode and may take longer than usual.'); } // trigger_processing() no-ops behind the process-lock transient and the // MySQL advisory lock, so a false-positive pump cannot double-process. $process->trigger_processing(); } public static function get_progress() { $log_url = OCM_DEBUG_LOG_FILE_URL; $lastLog = self::get_last_log_message(); $lastLotNoSkip = self::get_last_log_message(false); $uploadFileData = get_option('ocm_upload_file'); $isStopped = get_option('ocm_is_stopped'); // A poll must not tear anything down. It used to run cancel_all_process() // whenever ocm_is_stopped was set, which a new restore only clears once its // worker starts: a poll in that window deleted the new run's queue row and // restore_steps behind the worker's option cache, and dispatched both // cancelled processes. Terminal paths drop their own queue rows, and stale // items are refused by run ownership. self::maybe_revive_stalled_worker($isStopped); if ($lastLog && $lastLotNoSkip === self::LOG_MESSAGE_BG_PROCESS_RESTARTING_LOG) { return [ 'text' => $lastLog, 'value' => self::get_previous_log_percentage(), 'customNotice' => self::LOG_MESSAGE_BG_PROCESS_RESTARTING_LOG, 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ]; } if ($lastLog && $lastLotNoSkip === self::LOG_MESSAGE_BG_PROCESS_RESTARTING) { return [ 'text' => $lastLog, 'value' => self::get_previous_log_percentage(), 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ]; } if ($lastLog && $lastLotNoSkip === "Process is Restarting") { return [ 'text' => 'Process is Restarting', 'value' => self::get_previous_log_percentage(), 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ]; } if ($lastLog && strpos($lastLog, 'was skipped due to timeout') !== false) { return [ 'text' => $lastLog, 'value' => self::get_previous_log_percentage(), 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ]; } $action_in_flight = get_option('ocm_action_start_backup') || get_option('ocm_action_start_restore'); if ($action_in_flight) { $action_label = get_option('ocm_action_start_restore') ? 'restore' : 'backup'; $default_text_value = sprintf('Starting %s…', $action_label); } else { $default_text_value = 'Start a backup or a restore to see current progress here.

Entire process runs in the background, independent of your browser activity.

If you get logged out during restore, log back in using your backup old WordPress credentials and refresh this page for progress.'; } $default_text = array( 'text' => $default_text_value, 'value' => '1%', // Changed from 0% to 1% to prevent initial progress bar jump from 1% → 0% → 1% 'debugLogLine' => $lastLog, // Add the unmapped log line for debugging 'wpDebug' => defined('WP_DEBUG') && WP_DEBUG, // Add WP_DEBUG flag for conditional console logging 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ); $multipart_pct = self::get_multipart_upload_percentage($lastLog); if ($multipart_pct !== false) { $progress_item_text = array( 'uploadFileData' => $uploadFileData, 'text' => $lastLog, 'value' => $multipart_pct, 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ); if ($isStopped) { $progress_item_text['isStopped'] = true; } return $progress_item_text; } foreach (self::$progress_data as $item_key => $progress_item) { if (isset($progress_item[0]) && strpos($lastLog, $progress_item[0]) !== false) { if (preg_match('/Notice:/', $lastLog)) { $progress_item_text = array( 'uploadFileData' => $uploadFileData, 'text' => $lastLog, 'value' => $progress_item[1], 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ); } else { $progress_item_text = array( 'uploadFileData' => $uploadFileData, 'text' => $lastLog, 'value' => $progress_item[1], 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ); } $restore_terminal = ($lastLog === 'Restore completed.' || strpos($lastLog, self::LOG_RESTORE_COMPLETED_WARNINGS_PREFIX) === 0); if ($isStopped && !$restore_terminal) { $progress_item_text = array( 'isStopped' => true, 'text' => $lastLog, 'value' => $progress_item[1], 'maintenanceMode' => (bool) get_option('ocm_maintenance_mode', false) ); } if ($lastLog === 'Downloading restore files. Please wait.') { $progress_item_text['text'] .= '

This step could take a while depending on your backup size'; } elseif ($lastLog === 'Plugins have been restored') { $progress_item_text['text'] .= '

Around this step you might get logged out. Log back in using your old WordPress credentials and refresh this page for progress updates.'; } elseif ($restore_terminal) { if (strpos($lastLog, self::LOG_RESTORE_COMPLETED_WARNINGS_PREFIX) === 0) { $progress_item_text['text'] .= '

Restore finished, but some parts were skipped or missing. Check the migration log, then verify themes, plugins, and media on your site.' . ' See our FAQ page or Contact Form if you need help.'; } else { $progress_item_text['text'] .= '

Please check your website. If you have any issues please check our FAQ page or contact us via this Contact Form.'; } $progress_item_text['restoreCompleted'] = true; } elseif ($lastLog === 'Backup completed.') { $bucket_id = (string) get_option('ocm_bucket_id', ''); $bucket_line = $bucket_id !== '' ? '
Backup ID: ' . esc_html($bucket_id) . '' : ''; $progress_item_text['topBanner'] = 'Check your email for the restore code from pr@1clickmigration.com. If you don\'t see it within a few minutes, check your spam/junk folder and mark messages from pr@1clickmigration.com as not spam so future restore codes arrive in your inbox.' . $bucket_line; } elseif (strpos($lastLog, 'Backup completed with missing parts') === 0) { $bucket_id = (string) get_option('ocm_bucket_id', ''); $bucket_line = $bucket_id !== '' ? '
Backup ID: ' . esc_html($bucket_id) . '' : ''; $missing_label = esc_html(trim((string) substr($lastLog, strlen('Backup completed with missing parts:')))); $progress_item_text['topBanner'] = 'Backup finished, but it is INCOMPLETE. These parts could not be backed up and will NOT be restored: ' . $missing_label . '.' . '
A restore code was still emailed from pr@1clickmigration.com in case you want to migrate the rest.' . '
To get a complete backup, ask your host to raise the PHP max_execution_time, then run the backup again.' . $bucket_line; } $recovery_hint = self::get_recovery_hint_for_log($lastLog); if ($recovery_hint !== '') { $progress_item_text['text'] .= '

How to fix: ' . $recovery_hint; } return $progress_item_text; } } return $default_text; } /** * @return string " and …" naming the private folder that * holds the restore workspace, or '' without one. */ private static function restore_workspace_hint_folder() { $dir = self::get_private_work_dir(); return '' === $dir ? '' : ' and ' . esc_html(dirname($dir)) . ''; } private static function get_recovery_hint_for_log($log) { if (!is_string($log) || $log === '') { return ''; } if (strpos($log, 'disk quota') !== false || strpos($log, 'Not enough disk space') !== false || strpos($log, 'Restore workspace is not writable') !== false) { return 'Free space or increase the hosting account disk quota, then run the restore again. If the hosting panel shows enough space, ask the host to check account quota or inode limits because PHP may report filesystem space instead of account quota.'; } if (strpos($log, 'write permissions') !== false || strpos($log, 'could not be moved or copied') !== false || strpos($log, 'could not be moved into place') !== false || strpos($log, 'could not be extracted') !== false) { return 'Make sure WordPress can write to wp-content, wp-content/plugins, wp-content/themes, wp-content/uploads' . self::restore_workspace_hint_folder() . ', then retry the restore.'; } if (strpos($log, 'Restore failed while downloading') !== false) { return 'Create a fresh backup and restore with the new code. Backups expire after the retention window, and some hosts or firewalls can interrupt large downloads.'; } if (strpos($log, 'Restore failed while decrypting') !== false || strpos($log, 'could not be opened for extraction') !== false) { return 'Retry after freeing space. If it fails again, create a fresh backup because the downloaded archive may be incomplete.'; } if (strpos($log, 'cannot be restored safely (not hex-encoded)') !== false || strpos($log, 'binary/BLOB column values that cannot be restored safely') !== false ) { return 'Update 1-Click Migration on the source site to 3.1.19 or later, take a new backup, then restore using the new backup\'s restore code. Nothing on this site was changed.'; } if (strpos($log, 'Restore stopped before import: backup needs a fresh 3.1.19 backup') !== false) { return 'Update 1-Click Migration on the source site to 3.1.19 or later, take a new backup, then restore using the new backup\'s restore code. Nothing on this site was changed.'; } if (strpos($log, 'Restore stopped: database part could not be downloaded or unpacked') !== false) { return 'The database archive could not be downloaded or unpacked, so nothing on this site was changed. Free disk space or check write permissions under wp-content' . self::restore_workspace_hint_folder() . ', then retry. If it fails again, create a fresh backup and restore with the new code.'; } if (strpos($log, "Restore stopped: the backup's main options table didn't pass the check") !== false || strpos($log, 'incomplete or wrong-prefix') !== false ) { return 'The backup\'s main options table did not pass the check, so nothing on this site was changed. No files, tables or plugins were touched. Take a fresh backup from the source (or fix the source options table) and restore again with the new backup\'s restore code.'; } if (strpos($log, 'Restore stopped: the database could not be imported') !== false) { return 'The database could not be imported, so nothing on this site was changed. No files, tables or plugins were touched. Take a fresh backup from the source and restore again with the new backup\'s restore code. If the log mentions a table name that is too long, shorten that table name or the source table prefix before taking the new backup. If the log says a table has no primary or unique key, ask your host or developer to add one to that table on the source site, then take a new backup. If the log says a table has a row too large, ask your host to raise the PHP memory limit on this site, or ask your developer to shrink the large rows in that table on the source site, then take a new backup. If the log says a table stopped making progress, contact our support with your restore code, and don\'t run the restore again until we\'ve checked it.'; } if (strpos($log, 'Database restore failed while importing') !== false || strpos($log, 'DB Restore') !== false) { return 'The database import failed on a SQL file. If proc_open is disabled, ask the host to enable it so the restore can use the MySQL CLI path. If that is not possible, export a new backup after cleaning invalid or binary data stored in options by third-party plugins.'; } if (strpos($log, 'Backup worker dispatch failed') !== false || strpos($log, 'admin-ajax loopback check failed') !== false || strpos($log, 'blocks loopback requests') !== false) { return 'Ask the host to allow WordPress loopback requests to wp-admin/admin-ajax.php. Security plugins, WAF rules, basic auth, or firewall settings can block the background worker.'; } if (strpos($log, 'was skipped due to timeout') !== false || strpos($log, 'Backup completed with missing parts') !== false) { return 'Parts get skipped when the host repeatedly hits its PHP execution time limit. Ask the host to raise max_execution_time (or temporarily reduce the folder size), then run the backup again so every part is included.'; } if (strpos($log, 'Could not register backup') !== false || strpos($log, 'endpoint') !== false) { return 'Check outbound HTTPS access from the server and try again. If outbound requests are blocked, ask the host to allow connections to 1Click Migration endpoints.'; } return ''; } private static function get_multipart_upload_percentage($log) { if ($log && preg_match('/Uploaded\s+(db|themes|plugins|uploads)\s+part\s+(\d+)\/(\d+)/i', $log, $matches)) { $folder_type = strtolower($matches[1]); $part = (int) $matches[2]; $total = (int) $matches[3]; $ranges = [ 'db' => [43, 44], 'themes' => [47, 48], 'plugins' => [52, 53], 'uploads' => [63, 65], ]; if (isset($ranges[$folder_type]) && $total > 0) { $start = $ranges[$folder_type][0]; $end = $ranges[$folder_type][1]; $pct_val = $start + ($part / $total) * ($end - $start); $pct = (int) round($pct_val); $pct = min($end, max($start, $pct)); return $pct . '%'; } } return false; } private static function get_percent_by_log_message($log) { $multipart_pct = self::get_multipart_upload_percentage($log); if ($multipart_pct !== false) { return $multipart_pct; } $return = false; foreach (self::$progress_data as $item) { list($template, $percent) = $item; if (strpos($log, $template) !== false) { $return = $percent; break; } } return $return; } private static function folderSize($dir) { $size = 0; foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) { $size += is_file($each) ? filesize($each) : self::folderSize($each); } return $size; } public static function convertToReadableSize($size) { $decimals = 1; $sizes = array('B','kB','MB','GB','TB','PB','EB','ZB','YB'); $factor = floor((strlen($size) - 1) / 3); return sprintf("%.{$decimals}f", $size / (1024 ** $factor)) . @$sizes[$factor]; } private static function DBSize () { global $wpdb; $db_size_obj = $wpdb->get_results (' SELECT ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "db_size" FROM information_schema.tables WHERE table_schema = "'.DB_NAME.'" GROUP BY table_schema '); foreach ($db_size_obj as $db_size) { return $db_size->db_size; } } /** * Check whether the local disk has enough free space to run a backup. * Backup processes parts sequentially and clears the temp dir between * uploads, so peak disk overhead is the size of the *largest* part: * - DB: ~2x DB engine size (SQL dumps + zip), matches the in-flow check. * - Each folder: ~1.5x folder size (zip + encrypted copy). * * Also returns the raw per-part sizes so the caller can publish them (via the * log that gets uploaded to S3) for the restore-side disk preflight. * * @return array{ok:bool, required:int, free:int, peak_label:string, sizes:array} */ private static function check_backup_disk_space($excluded_folders) { $excluded = is_array($excluded_folders) ? $excluded_folders : []; $peak_bytes = 0; $peak_label = ''; $sizes = []; if (!in_array('db', $excluded, true)) { $db_bytes = (int) ((float) self::DBSize() * 1024 * 1024); $sizes['db'] = $db_bytes; $need = (int) ($db_bytes * 2); if ($need > $peak_bytes) { $peak_bytes = $need; $peak_label = 'db'; } } foreach (['plugins', 'themes', 'uploads'] as $folder) { if (in_array($folder, $excluded, true)) continue; $path = WP_CONTENT_DIR . '/' . $folder; if (!is_dir($path)) continue; $folder_bytes = (int) self::folderSize($path); $sizes[$folder] = $folder_bytes; $need = (int) ($folder_bytes * 1.5); if ($need > $peak_bytes) { $peak_bytes = $need; $peak_label = $folder; } } $free = self::get_free_disk_space(self::get_backup_work_path()); return [ 'ok' => $free === 0 ? true : ($free >= $peak_bytes), 'required' => $peak_bytes, 'free' => $free, 'peak_label' => $peak_label, 'sizes' => $sizes, ]; } /** * Check disk space for restore. Each part is downloaded (encrypted zip), * decrypted (crypt deleted right after), extracted into the workspace (zip * deleted right after), and db/plugins content stays staged until the final * phases, so peak ≈ sum(unzipped_sizes) + one part's zip in flight. * * When the backup published its uncompressed part sizes (BACKUP_UNCOMPRESSED_SIZES * line in the merged S3 log), use those: sum(uncompressed) + largest zip, +10%. * Otherwise fall back to 1.6x sum(zip_sizes) — typical mixed content (DB SQL * compresses well, uploads barely compress at all) lands around there. The * in-flow download/extract checks catch any overshoot. * * @return array{ok:bool, required:int, free:int, total:int, reason:string} */ private static function check_restore_disk_space($presigned_urls, $excluded_folders) { $excluded = is_array($excluded_folders) ? $excluded_folders : []; $total_bytes = 0; $largest_zip = 0; $write_probe_ok = self::probe_restore_workspace_write(); if (is_array($presigned_urls) || is_object($presigned_urls)) { foreach ((array) $presigned_urls as $key => $url) { if (in_array($key, $excluded, true)) continue; if (in_array($key, array('log', 'log_download'), true)) continue; if (!is_string($url) || $url === '') continue; $resp = wp_remote_head($url, ['timeout' => 8, 'redirection' => 3]); if (is_wp_error($resp)) continue; $len = wp_remote_retrieve_header($resp, 'content-length'); if ($len !== '' && is_numeric($len)) { $total_bytes += (int) $len; $largest_zip = max($largest_zip, (int) $len); } } } $manifest = self::get_backup_size_manifest(); $uncompressed_total = 0; foreach ($manifest as $part => $bytes) { if (in_array($part, $excluded, true)) continue; if (!array_key_exists($part, (array) $presigned_urls)) continue; $uncompressed_total += $bytes; } if ($uncompressed_total > 0) { $required = (int) (($uncompressed_total + $largest_zip) * 1.1); } else { $required = (int) ($total_bytes * 1.6); } $free = self::get_free_disk_space(self::get_restore_work_path()); $space_ok = ($free === 0 || $required === 0) ? true : ($free >= $required); return [ 'ok' => $write_probe_ok && $space_ok, 'required' => $required, 'free' => $free, 'total' => $total_bytes, 'reason' => $write_probe_ok ? ($space_ok ? '' : 'insufficient_space') : 'write_probe_failed', ]; } /** * Parse the uncompressed part sizes the backup published into its log * (merged from S3 before the restore disk check). Matched by bucket_id so a * stale line from an earlier migration on this site cannot be picked up. * * @return array part => bytes, empty when unavailable. */ private static function get_backup_size_manifest() { $bucket_id = (string) get_option('ocm_bucket_id', ''); if ($bucket_id === '' || !defined('OCM_DEBUG_LOG_FILE') || !file_exists(OCM_DEBUG_LOG_FILE)) { return []; } $contents = (string) @file_get_contents(OCM_DEBUG_LOG_FILE); if ($contents === '') { return []; } $pattern = '/BACKUP_UNCOMPRESSED_SIZES bucket=' . preg_quote($bucket_id, '/') . ' ([a-z_]+=\d+(?: [a-z_]+=\d+)*)/'; if (!preg_match_all($pattern, $contents, $matches) || empty($matches[1])) { return []; } $sizes = []; foreach (explode(' ', end($matches[1])) as $pair) { list($part, $bytes) = explode('=', $pair, 2); $sizes[$part] = (int) $bytes; } return $sizes; } private static function get_free_disk_space($preferred_path) { $paths = array($preferred_path, WP_CONTENT_DIR, ABSPATH, '.'); foreach ($paths as $path) { if (!is_string($path) || '' === $path) { continue; } $candidate = $path; if (!is_dir($candidate)) { $candidate = dirname($candidate); } if (!is_dir($candidate)) { continue; } $free = @disk_free_space($candidate); if (false !== $free) { return (int) $free; } } return 0; } /** * Atomically reserve the single backup slot. * * add_option() is a unique-key insert, unlike a transient get/set pair, so * two admin requests cannot both become the owner. * * @return string|false */ public static function claim_backup_run() { $run_id = function_exists('wp_generate_uuid4') ? wp_generate_uuid4() : wp_generate_password(32, false, false); $run_id = strtolower(preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $run_id)); if ('' === $run_id) { return false; } $run = array( 'id' => $run_id, 'bucket_id' => '', 'started_at' => time(), ); if (!add_option(self::BACKUP_RUN_OPTION, $run, '', false)) { return false; } self::$backup_run_id = $run_id; return $run_id; } public static function set_backup_run_context($run_id) { if (!self::is_current_backup_run($run_id)) { return false; } self::$backup_run_id = $run_id; return true; } public static function is_current_backup_run($run_id) { $run = get_option(self::BACKUP_RUN_OPTION, array()); return is_array($run) && isset($run['id']) && is_string($run_id) && hash_equals((string) $run['id'], $run_id); } private static function backup_context_owns_current_run() { return '' !== self::$backup_run_id && self::is_current_backup_run(self::$backup_run_id); } public static function get_stored_backup_run_id() { if ('' !== self::$backup_run_id) { return self::$backup_run_id; } $run = get_option(self::BACKUP_RUN_OPTION, array()); return is_array($run) && isset($run['id']) ? (string) $run['id'] : ''; } private static function set_backup_run_bucket($run_id, $bucket_id) { $run = get_option(self::BACKUP_RUN_OPTION, array()); if (!self::is_current_backup_run($run_id) || !is_array($run)) { return false; } $run['bucket_id'] = (string) $bucket_id; return update_option(self::BACKUP_RUN_OPTION, $run, false); } public static function release_backup_run($run_id = null) { if (null === $run_id) { $run_id = self::$backup_run_id; } if (!is_string($run_id) || '' === $run_id) { return false; } $run = get_option(self::BACKUP_RUN_OPTION, array()); if (!is_array($run) || empty($run['id']) || !hash_equals((string) $run['id'], $run_id)) { return false; } // Compare-and-delete so a delayed releaser cannot drop a newly claimed run. $serialized = maybe_serialize($run); $released = self::delete_option_if_value(self::BACKUP_RUN_OPTION, $serialized); if ($released) { delete_option(self::BACKUP_UPLOADED_PARTS_OPTION); if (hash_equals(self::$backup_run_id, $run_id)) { self::$backup_run_id = ''; } } return $released; } /** * Start a new restore run. Its id travels with the queue item, so a worker * left over from a failed or replaced run can tell it no longer owns the * site and stop without logging or touching the restore workspace (#96). * * @return string */ public static function claim_restore_run() { $run_id = function_exists('wp_generate_uuid4') ? wp_generate_uuid4() : wp_generate_password(32, false, false); $run_id = strtolower(preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $run_id)); update_option(self::RESTORE_RUN_OPTION, $run_id, false); self::set_restore_run_context($run_id); return $run_id; } public static function set_restore_run_context($run_id) { self::$restore_run_id = is_string($run_id) ? $run_id : ''; self::$restore_run_ended = false; self::$restore_run_checked_at = 0; } public static function is_current_restore_run($run_id) { if (!is_string($run_id) || '' === $run_id) { return false; } // Another request ends or replaces the run, so never trust this // request's cached copy of the option. if (function_exists('wp_cache_delete')) { wp_cache_delete(self::RESTORE_RUN_OPTION, 'options'); } $current = get_option(self::RESTORE_RUN_OPTION, ''); return is_string($current) && '' !== $current && hash_equals($current, $run_id); } /** * True when this worker serves a restore run that has since failed, * stopped, completed or been replaced by a newer one. * * @return bool */ public static function restore_run_ended() { if ('' === self::$restore_run_id) { return false; } if (!self::$restore_run_ended && !self::is_current_restore_run(self::$restore_run_id)) { self::$restore_run_ended = true; } return self::$restore_run_ended; } /** * restore_run_ended() for tight transfer loops: re-reads the option at most * every 5 seconds. * * @return bool */ public static function restore_run_ended_throttled() { if ('' === self::$restore_run_id || self::$restore_run_ended) { return self::$restore_run_ended; } $now = time(); if ($now - self::$restore_run_checked_at < 5) { return false; } self::$restore_run_checked_at = $now; return self::restore_run_ended(); } /** * Terminal bookkeeping for a restore run: drop its queue rows so neither * the cron healthcheck nor the watchdog can start it again, and release * the run so any worker still busy with it stops at its next check. * * @return void */ private static function end_restore_run() { if ('' !== self::$restore_run_id && !self::is_current_restore_run(self::$restore_run_id)) { // A newer run owns the slot and its queue. return; } self::clear_restore_batch_option_rows(); delete_option(self::RESTORE_RUN_OPTION); if (function_exists('wp_cache_delete')) { wp_cache_delete(self::RESTORE_RUN_OPTION, 'options'); } if ('' !== self::$restore_run_id) { self::$restore_run_ended = true; } } private static function delete_option_if_value($name, $expected_serialized) { global $wpdb; if (isset($wpdb) && is_object($wpdb) && isset($wpdb->options) && method_exists($wpdb, 'delete')) { $deleted = $wpdb->delete( $wpdb->options, array( 'option_name' => $name, 'option_value' => $expected_serialized, ), array('%s', '%s') ); if ($deleted) { wp_cache_delete($name, 'options'); return true; } return false; } $current = get_option($name, null); if (null === $current || maybe_serialize($current) !== $expected_serialized) { return false; } return delete_option($name); } public static function get_backup_work_path($run_id = null) { if (!is_string($run_id) || '' === $run_id) { $run_id = self::$backup_run_id; } if ('' === $run_id) { $run = get_option(self::BACKUP_RUN_OPTION, array()); $run_id = is_array($run) && isset($run['id']) ? (string) $run['id'] : ''; } $run_id = strtolower(preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $run_id)); $name = 'backup-' . ($run_id !== '' ? $run_id : 'unowned') . '/'; // The SQL dump and zips live in the private work dir, not under the // web root. A run that had to fall back to wp-content/tmp (see // ensure_temporary_directory) keeps using it. $legacy = OCM_PLUGIN_WRITABLE_PATH . $name; $private = self::get_private_work_dir(); if ('' === $private || (!is_dir($private . '/' . $name) && is_dir($legacy))) { return $legacy; } if (!is_dir($private . '/' . $name) && !self::private_work_dir_usable(false)) { return $legacy; } return $private . '/' . $name; } /** * This site's directory for backup and restore work files, beside the * private debug log and outside the web root. OCM_DEBUG_LOG_DIR is already * per site (OCM_Private_Dir), so sibling installs never share it. * * @return string Directory without trailing slash; '' when there is no private dir. */ public static function get_private_work_dir() { if (!defined('OCM_DEBUG_LOG_DIR') || !is_string(OCM_DEBUG_LOG_DIR)) { return ''; } $base = rtrim(OCM_DEBUG_LOG_DIR, '/\\'); if ('' === $base) { return ''; } return $base . '/work'; } /** * Whether new work dirs may go in get_private_work_dir(). It must not be * inside the web root (a WordPress install in a subdirectory of the * document root puts the private dir there). The restore also needs it on * wp-content's filesystem, so the deferred file swap stays a rename. * Writability is checked when the directory is created. * * @param bool $same_filesystem_as_content * @return bool */ private static function private_work_dir_usable($same_filesystem_as_content) { $dir = self::get_private_work_dir(); if ('' === $dir || self::is_inside_web_root($dir)) { return false; } if ($same_filesystem_as_content) { $dir_device = self::filesystem_device($dir); $content_device = self::filesystem_device(WP_CONTENT_DIR); if (null !== $dir_device && null !== $content_device && $dir_device !== $content_device) { return false; } } return true; } /** * @param string $path * @return bool True when $path is ABSPATH, WP_CONTENT_DIR or the document * root, or inside one of them. */ private static function is_inside_web_root($path) { $roots = array(ABSPATH, WP_CONTENT_DIR); if (isset($_SERVER['DOCUMENT_ROOT']) && is_string($_SERVER['DOCUMENT_ROOT']) && '' !== $_SERVER['DOCUMENT_ROOT']) { $roots[] = $_SERVER['DOCUMENT_ROOT']; } $path = self::resolve_path($path) . '/'; foreach ($roots as $root) { if (!is_string($root) || '' === $root) { continue; } if (0 === strpos($path, self::resolve_path($root) . '/')) { return true; } } return false; } /** * realpath() for a path that may not exist yet: resolve its nearest * existing ancestor and append the rest. * * @param string $path * @return string Forward slashes, no trailing slash ('' for the filesystem root). */ private static function resolve_path($path) { $path = rtrim(str_replace('\\', '/', (string) $path), '/'); $rest = ''; while ('' !== $path) { $real = @realpath($path); if (false !== $real) { return rtrim(str_replace('\\', '/', $real), '/') . $rest; } $parent = rtrim(str_replace('\\', '/', dirname($path)), '/'); if ($parent === $path) { break; } $rest = '/' . basename($path) . $rest; $path = $parent; } return $path . $rest; } /** * @param string $path * @return int|null Device id of the nearest existing ancestor; null when unknown. */ private static function filesystem_device($path) { $path = (string) $path; while ('' !== $path) { if (file_exists($path)) { $stat = @stat($path); return is_array($stat) && isset($stat['dev']) ? (int) $stat['dev'] : null; } $parent = dirname($path); if ($parent === $path) { break; } $path = $parent; } return null; } /** * Keep web servers that honour these files (Apache, IIS) from serving or * listing a work dir that had to go under wp-content. nginx ignores them, * which is why such a dir also gets an unguessable name. * * @param string $dir * @return void */ private static function write_deny_files($dir) { $files = array( '.htaccess' => "Deny from all\nRequire all denied\n", 'web.config' => '', 'index.php' => " $contents) { if (!is_file($dir . '/' . $name)) { @file_put_contents($dir . '/' . $name, $contents); } } } /** * @return string 24 lowercase hex characters. */ private static function random_dir_token() { try { return bin2hex(random_bytes(12)); } catch (\Throwable $e) { return strtolower(substr(preg_replace('/[^a-zA-Z0-9]/', '', wp_generate_password(32, false, false)), 0, 24)); } } /** * Where restore parts are downloaded, decrypted and extracted: the private * work dir, or wp-content/ocm_restore- when * prepare_restore_workspace() could not use that. Found from what is on * disk, so every worker of a run, before and after the database swap, * resolves the same directory. * * @return string Directory without trailing slash. Never '' (callers append "/db/"). */ public static function get_restore_work_path() { $private = self::get_private_work_dir(); if ('' !== $private && is_dir($private . '/restore')) { return $private . '/restore'; } $fallbacks = self::find_fallback_restore_work_paths(); if ($fallbacks) { return $fallbacks[0]; } if ('' !== $private && self::private_work_dir_usable(true)) { return $private . '/restore'; } // Nothing prepared yet: a path that doesn't exist. return WP_CONTENT_DIR . '/' . self::RESTORE_WORKSPACE_FALLBACK_PREFIX . 'none'; } /** * @return string[] Existing wp-content/ocm_restore- dirs, newest first. */ private static function find_fallback_restore_work_paths() { $entries = @scandir(WP_CONTENT_DIR); if (!is_array($entries)) { return array(); } $found = array(); foreach ($entries as $entry) { if (!preg_match('/^' . preg_quote(self::RESTORE_WORKSPACE_FALLBACK_PREFIX, '/') . '[a-z0-9]+$/', $entry)) { continue; } $path = WP_CONTENT_DIR . '/' . $entry; if (is_dir($path) && !is_link($path)) { $found[$path] = (int) @filemtime($path); } } arsort($found); return array_keys($found); } /** * Every restore workspace this site may have on disk: the private one, * wp-content fallbacks, and wp-content/ocm_restore from 3.1.20 and earlier. * * @return string[] */ private static function existing_restore_work_paths() { $paths = array(); $private = self::get_private_work_dir(); if ('' !== $private && is_dir($private . '/restore')) { $paths[] = $private . '/restore'; } $paths = array_merge($paths, self::find_fallback_restore_work_paths()); if (is_dir(WP_CONTENT_DIR . '/' . self::LEGACY_RESTORE_WORKSPACE)) { $paths[] = WP_CONTENT_DIR . '/' . self::LEGACY_RESTORE_WORKSPACE; } return $paths; } /** * 3.1.20 and earlier extracted restores into wp-content/ocm_restore, and a * failed run could leave its SQL dump there for nginx to serve. Plugin * updates don't run the activation hook, so remove it on the next admin * page load while no restore is running. * * @return void */ public static function remove_legacy_restore_workspace() { $legacy = WP_CONTENT_DIR . '/' . self::LEGACY_RESTORE_WORKSPACE; if (!is_dir($legacy) || get_option('ocm_action_start_restore')) { return; } self::deleteDirectoryBestEffort($legacy); } /** * Delete every restore workspace, stopping the run on the first failure * (deleteDir() logs it). For Stop & Reset and a completed restore. * * @param string $action_log * @param mixed $presigned_urls * @return void */ public static function delete_restore_workspaces($action_log, $presigned_urls = null) { foreach (self::existing_restore_work_paths() as $dir) { self::deleteDir($dir, $action_log, $presigned_urls); } } /** * Delete this site's backup work dirs in the private work dir (Stop & Reset; * fallbacks under wp-content/tmp go with that folder). * * @param string $action_log * @return void */ public static function delete_private_backup_work_dirs($action_log) { $private = self::get_private_work_dir(); if ('' === $private || !is_dir($private)) { return; } foreach ((array) @scandir($private) as $entry) { if (is_string($entry) && 0 === strpos($entry, 'backup-') && is_dir($private . '/' . $entry)) { self::deleteDir($private . '/' . $entry, $action_log, null); } } } /** * Legacy shared dump directory used by overlapping 3.1.15 runs. * Kept only so tests can prove the isolation contract against that path. */ public static function get_shared_tmp_db_path() { return OCM_PLUGIN_WRITABLE_PATH . 'db/'; } private static function ensure_temporary_directory() { $work_path = self::get_backup_work_path(); if (is_dir($work_path)) { if (is_writable($work_path)) { return true; } One_Click_Migration::write_to_log(sprintf( 'Error: Temporary directory "%s" is not writable.', $work_path )); return false; } if (@mkdir($work_path, 0700, true) || is_dir($work_path)) { return true; } // The private dir isn't writable on this host: fall back to // wp-content/tmp. The run id makes the name unguessable; the deny // files cover servers that would list or serve it. $legacy = OCM_PLUGIN_WRITABLE_PATH . basename(rtrim($work_path, '/\\')) . '/'; if ($legacy !== $work_path && (@mkdir($legacy, 0700, true) || is_dir($legacy))) { // Only an index.php in the shared tmp folder: other plugins may // serve files from it. if (!is_file(OCM_PLUGIN_WRITABLE_PATH . 'index.php')) { @file_put_contents(OCM_PLUGIN_WRITABLE_PATH . 'index.php', " 15, 'redirection' => 3)); if (is_wp_error($response)) { return 0; } $length = wp_remote_retrieve_header($response, 'content-length'); return ($length !== '' && is_numeric($length)) ? (int) $length : 0; } private static function get_restore_transfer_timeout() { $timeout = max(20, One_Click_Migration::get_timeout()); if (is_object(One_Click_Migration::$process_restore_single) && method_exists(One_Click_Migration::$process_restore_single, 'remaining_time')) { $remaining = (int) One_Click_Migration::$process_restore_single->remaining_time(); if ($remaining > 10) { $timeout = max(10, min($timeout, $remaining - 5)); } } return $timeout; } private static function download_restore_file($url, $target_path, $key) { $target_dir = dirname($target_path); if (!is_dir($target_dir) && !@mkdir($target_dir, 0700, true) && !is_dir($target_dir)) { return new WP_Error('ocm_restore_workspace_missing', sprintf( 'Restore workspace "%s" could not be created.', $target_dir )); } $expected_size = self::get_remote_file_size($url); if ($expected_size > 0 && file_exists($target_path) && filesize($target_path) >= $expected_size) { return $target_path; } if (file_exists($target_path)) { @unlink($target_path); } if (!function_exists('curl_init')) { $download_tmp_path = download_url($url, One_Click_Migration::get_timeout()); if (is_wp_error($download_tmp_path)) { return $download_tmp_path; } // copy('') throws a ValueError on PHP 8 instead of returning false. if (!is_string($download_tmp_path) || '' === $download_tmp_path || !is_file($download_tmp_path)) { return new WP_Error('ocm_download_missing', sprintf( 'Download for "%s" returned no file.', $key )); } if (!@rename($download_tmp_path, $target_path)) { if (!@copy($download_tmp_path, $target_path)) { return new WP_Error('ocm_download_move_failed', sprintf( 'Downloaded file for "%s" could not be moved into the restore workspace.', $key )); } @unlink($download_tmp_path); } return $target_path; } $partial_path = $target_path . '.part'; $offset = file_exists($partial_path) ? (int) filesize($partial_path) : 0; if ($expected_size > 0 && $offset >= $expected_size) { if (@rename($partial_path, $target_path)) { return $target_path; } clearstatcache(true, $target_path); if (file_exists($target_path) && filesize($target_path) >= $expected_size) { return $target_path; } return new WP_Error('ocm_download_move_failed', sprintf( 'Downloaded file for "%s" could not be finalized.', $key )); } $handle = @fopen($partial_path, 'ab'); if (!$handle) { return new WP_Error('ocm_download_open_failed', sprintf( 'Could not open partial download file for "%s".', $key )); } $before = $offset; $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $handle); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_TIMEOUT, self::get_restore_transfer_timeout()); curl_setopt($ch, CURLOPT_FAILONERROR, false); if ($offset > 0) { curl_setopt($ch, CURLOPT_RANGE, $offset . '-'); } // A single curl_exec() can run for minutes; keep the heartbeat and // process lock fresh so the watchdog doesn't start a second worker // on the same .part file (#79). curl_setopt($ch, CURLOPT_NOPROGRESS, false); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, static function () { One_Click_Migration::transfer_heartbeat(One_Click_Migration::$process_restore_single); // Abort the transfer once its restore run has ended (#96). return OCM_Backup::restore_run_ended_throttled() ? 1 : 0; }); $ok = curl_exec($ch); $errno = curl_errno($ch); $error = curl_error($ch); $http_code = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); // Release the handle (and its hold on the .part file) before fclose(); // curl_close() is a no-op since 8.0 and deprecated in 8.5. unset($ch); fclose($handle); clearstatcache(true, $partial_path); if ($http_code === 404) { return new WP_Error('http_404', 'Not Found'); } if ($errno === 23) { // CURLE_WRITE_ERROR: writing the downloaded bytes to disk failed, which // means a full disk or quota — retrying cannot succeed until space is freed. return new WP_Error('ocm_disk_full', sprintf( 'Not enough disk space while downloading "%s.zip.crypt" (%s free). Free space or increase the hosting account quota, then run the restore again.', $key, self::convertToReadableSize(self::get_free_disk_space(dirname($target_path))) )); } $downloaded = file_exists($partial_path) ? (int) filesize($partial_path) : 0; if ($offset > 0 && $http_code === 200) { @unlink($partial_path); return new WP_Error('ocm_partial_download', sprintf( 'Server restarted the download for "%s.zip.crypt"; restarting cleanly.', $key )); } if ($expected_size > 0 && $downloaded > $expected_size) { @unlink($partial_path); return new WP_Error('ocm_partial_download', sprintf( 'Partial download for "%s.zip.crypt" exceeded the expected size; restarting cleanly.', $key )); } if (!$ok || $errno !== 0) { if ($downloaded > $before) { return new WP_Error('ocm_partial_download', sprintf( 'Partial download saved for "%s.zip.crypt" (%s downloaded%s).', $key, self::convertToReadableSize($downloaded), $expected_size > 0 ? ' of ' . self::convertToReadableSize($expected_size) : '' )); } return new WP_Error('http_request_failed', $error ? $error : sprintf('HTTP %d', $http_code)); } if ($http_code < 200 || $http_code >= 300) { return new WP_Error('http_request_failed', sprintf('HTTP %d', $http_code)); } if ($expected_size > 0 && $downloaded < $expected_size) { return new WP_Error('ocm_partial_download', sprintf( 'Partial download saved for "%s.zip.crypt" (%s downloaded of %s).', $key, self::convertToReadableSize($downloaded), self::convertToReadableSize($expected_size) )); } if (!@rename($partial_path, $target_path)) { clearstatcache(true, $target_path); if ($expected_size > 0 && file_exists($target_path) && filesize($target_path) >= $expected_size) { // Already finalized (e.g. by a worker that resumed this download). return $target_path; } return new WP_Error('ocm_download_move_failed', sprintf( 'Downloaded file for "%s" could not be finalized.', $key )); } return $target_path; } private static function probe_restore_workspace_write() { if (!is_dir(WP_CONTENT_DIR) || !is_writable(WP_CONTENT_DIR)) { return false; } $probe_file = WP_CONTENT_DIR . '/.ocm-write-probe-' . wp_generate_password(8, false, false) . '.tmp'; $probe_bytes = str_repeat('0', 1048576); $written = @file_put_contents($probe_file, $probe_bytes, LOCK_EX); if (file_exists($probe_file)) { @unlink($probe_file); } return $written === strlen($probe_bytes); } private static function save_system_data_to_log_file($username, $bucket_id) { global $wp_version; One_Click_Migration::write_to_log("Email address: $username"); One_Click_Migration::write_to_log('Site URL: ' . get_site_url()); One_Click_Migration::write_to_log("WP Version: $wp_version"); One_Click_Migration::write_to_log('PHP Version: ' . PHP_VERSION); One_Click_Migration::write_to_log('OCM Plugin Version: ' . One_Click_Migration::get_version()); One_Click_Migration::write_to_log('default max_execution_time: ' . One_Click_Migration::get_default_max_execution_time()); One_Click_Migration::write_to_log('Available Space: ' . self::convertToReadableSize(self::get_free_disk_space("."))); One_Click_Migration::write_to_log('max_execution_time: ' . One_Click_Migration::get_current_max_execution_time()); One_Click_Migration::write_to_log('timeout set: ' . One_Click_Migration::get_timeout()); One_Click_Migration::write_to_log(sprintf('proc_open / proc_close: %s', OCM_SQL_Utils::check_proc_available(true) ? 'enabled' : 'disabled')); One_Click_Migration::write_to_log("Backup ID: $bucket_id"); $host = gethostname(); $ip = gethostbyname($host); One_Click_Migration::write_to_log("Current IP Address: $ip"); // Log initial memory status $memory_limit = ini_get('memory_limit'); $memory_usage = memory_get_usage(true); $memory_peak = memory_get_peak_usage(true); One_Click_Migration::write_to_log(" SYSLOG: Initial Memory - Limit: " . self::format_bytes(self::convert_to_bytes($memory_limit)) . ", Usage: " . self::format_bytes($memory_usage) . ", Peak: " . self::format_bytes($memory_peak)); self::set_server_settings(); } public static function send_restore_code_email($missing_parts = array()) { $email = get_option('ocm_user_email'); $bucket_id = get_option('ocm_bucket_id'); $restore_code = get_option('ocm_restore_code_plain'); if (!$email || !$bucket_id || !$restore_code) { One_Click_Migration::write_to_log('Error: Restore-code email could not be sent because backup registration data is missing'); return false; } // The Lambda marks the backup ready: from then on a restore precheck can // read the URL inventory and BACKUP_PARTS lines from the S3 log. One_Click_Migration::flush_log_upload(); for ($attempt = 1; $attempt <= 3; $attempt++) { if (OCM_S3::send_restore_code_email($email, $bucket_id, $restore_code, get_site_url(), $missing_parts)) { delete_option('ocm_restore_code_plain'); return true; } if ($attempt < 3) { One_Click_Migration::write_to_log("Retrying restore-code email (attempt $attempt/3)..."); sleep(2); } } One_Click_Migration::write_to_log('Error: Backup finished but the restore-code email could not be sent. Please run the backup again.'); return false; } private static function set_server_settings(){ if (!defined('MB_IN_BYTES')) { define('MB_IN_BYTES', 1024 * KB_IN_BYTES); } if (!defined('OCM_PHP_MAX_MEMORY')) { define('OCM_PHP_MAX_MEMORY', 4096 * MB_IN_BYTES); } if (!function_exists('wp_is_ini_value_changeable')) { /** * Determines whether a PHP ini value is changeable at runtime. * * @staticvar array $ini_all * * @link https://secure.php.net/manual/en/function.ini-get-all.php * * @param string $setting The name of the ini setting to check. * @return bool True if the value is changeable at runtime. False otherwise. */ function wp_is_ini_value_changeable( $setting ) { static $ini_all; if ( ! isset( $ini_all ) ) { $ini_all = false; // Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes". if ( function_exists( 'ini_get_all' ) ) { $ini_all = ini_get_all(); } } // Bit operator to workaround https://bugs.php.net/bug.php?id=44936 which changes access level to 63 in PHP 5.2.6 - 5.2.17. if ( isset( $ini_all[ $setting ]['access'] ) && ( INI_ALL === ( $ini_all[ $setting ]['access'] & 7 ) || INI_USER === ( $ini_all[ $setting ]['access'] & 7 ) ) ) { return true; } // If we were unable to retrieve the details, fail gracefully to assume it's changeable. if ( ! is_array( $ini_all ) ) { return true; } return false; } } if (wp_is_ini_value_changeable('memory_limit')) @ini_set('memory_limit', OCM_PHP_MAX_MEMORY); if (wp_is_ini_value_changeable('pcre.backtrack_limit')) @ini_set('pcre.backtrack_limit', PHP_INT_MAX); if (wp_is_ini_value_changeable('default_socket_timeout')) @ini_set('default_socket_timeout', 3600); } private static function get_excluded_folders() { $excluded_folders = ''; if(isset($_GET['selected'])){ $excluded = sanitize_text_field($_GET['selected']);; $excluded_folders = explode(",", $excluded); update_option( 'ocm_excluded_folders', $excluded_folders); } else $excluded_folders = []; return $excluded_folders; } private static function get_username() { $username = ''; if(isset($_GET['username'])){ $username = sanitize_email($_GET['username']); return $username; }else{ // Log the error instead of redirecting immediately One_Click_Migration::write_to_log('Error: Username parameter is missing'); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=missing_username')); exit; } } private static function get_restore_code() { if (!isset($_GET['restore_code']) || $_GET['restore_code'] === '') { One_Click_Migration::write_to_log('Error: Restore code parameter is missing'); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=missing_restore_code')); exit; } // Strip dashes/spaces, uppercase, keep only Crockford-safe chars (the Lambda re-normalizes anyway). $raw = preg_replace('/[\s\-]+/', '', (string) $_GET['restore_code']); $raw = strtoupper(preg_replace('/[^A-Za-z0-9]/', '', $raw)); if (strlen($raw) < 6 || strlen($raw) > 32) { One_Click_Migration::write_to_log('Error: Restore code length out of range'); wp_safe_redirect(admin_url('tools.php?page=one-click-migration&message=invalid_restore_code')); exit; } return $raw; } private static function get_restore_steps_template() { $child_steps = array( self::STEP_RESTORE_CHILD_INITIATE => false, self::STEP_RESTORE_CHILD_DOWNLOAD => false, self::STEP_RESTORE_CHILD_DOWNLOAD_TMP_PATH => null, self::STEP_RESTORE_CHILD_PRESIGNED_URLS => null, self::STEP_RESTORE_CHILD_DECRYPT => false, self::STEP_RESTORE_CHILD_EXTRACT => false, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW => false, self::STEP_RESTORE_CHILD_THEMES_UPLOADS_RESTORED => false, self::STEP_RESTORE_CHILD_RESTORE => false, self::STEP_RESTORE_CHILD_THEMES_RESTORED => false, self::STEP_RESTORE_CHILD_UPLOADS_RESTORED => false, self::STEP_RESTORE_CHILD_DB_IMPORT => null, self::STEP_RESTORE_CHILD_DB_IMPORTED => false, self::STEP_RESTORE_CHILD_DB_URLS => false, self::STEP_RESTORE_CHILD_DB_PROMOTED => false, ); return array( 'init' => $child_steps, 'db' => $child_steps, 'uploads' => $child_steps, 'themes' => $child_steps, 'plugins' => $child_steps, 'themesUploads' => $child_steps ); } public static function get_current_restore_steps() { $restore_steps = get_option('restore_steps', null); if (null === $restore_steps) { $restore_steps = self::get_restore_steps_template(); } return $restore_steps; } public static function reset_current_restore_steps() { $restore_steps = self::get_restore_steps_template(); update_option('restore_steps', $restore_steps, true); } public static function check_is_complete_restore_step($step, $child) { $restore_steps = self::get_current_restore_steps(); $step_key = strtolower($step); return isset($restore_steps[$step_key]) && is_array($restore_steps[$step_key]) && isset($restore_steps[$step_key][$child]) && $restore_steps[$step_key][$child] === true; } private static function get_restore_step_value($step, $child) { $restore_steps = self::get_current_restore_steps(); $step_key = strtolower($step); return (isset($restore_steps[$step_key]) && is_array($restore_steps[$step_key]) && array_key_exists($child, $restore_steps[$step_key])) ? $restore_steps[$step_key][$child] : null; } private static function set_complete_restore_step($step, $child, $value = null) { $restore_steps = self::get_current_restore_steps(); $step_key = strtolower($step); if (!isset($restore_steps[$step_key]) || !is_array($restore_steps[$step_key])) { $template = self::get_restore_steps_template(); $restore_steps[$step_key] = isset($template[$step_key]) ? $template[$step_key] : array(); } $restore_steps[$step_key][$child] = (null !== $value ? $value : true); update_option('restore_steps', $restore_steps, true); } private static function get_backup_steps_template() { $child_steps = array( self::STEP_BACKUP_CHILD_URL_GENERATED => false, self::STEP_BACKUP_CHILD_INITIATE => false, self::STEP_BACKUP_CHILD_INITIATE_DB_BACKUP => false, self::STEP_BACKUP_CHILD_INITIATE_BACKUP_THEMES => false, self::STEP_BACKUP_CHILD_INITIATE_BACKUP_PLUGINS => false, self::STEP_BACKUP_CHILD_INITIATE_BACKUP_UPLOADS => false, self::STEP_BACKUP_CHILD_COMPRESS => false, self::STEP_BACKUP_CHILD_ENCRYPT => false, self::STEP_BACKUP_CHILD_UPLOAD => false, self::STEP_BACKUP_CHILD_ZIP_FILE_PATH => null, ); return array( 'init' => $child_steps, 'url' => $child_steps, 'db' => $child_steps, 'themes' => $child_steps, 'plugins' => $child_steps, 'uploads' => $child_steps, ); } public static function get_current_backup_steps() { $backup_steps = get_option('backup_steps', null); if (null === $backup_steps) { $backup_steps = self::get_backup_steps_template(); } return $backup_steps; } public static function reset_current_backup_steps() { $backup_steps = self::get_backup_steps_template(); update_option('backup_steps', $backup_steps, true); } private static function check_is_complete_backup_step($step, $child) { $backup_steps = self::get_current_backup_steps(); $step_key = strtolower($step); return isset($backup_steps[$step_key]) && is_array($backup_steps[$step_key]) && isset($backup_steps[$step_key][$child]) && $backup_steps[$step_key][$child] === true; } private static function get_backup_step_value($step, $child) { $backup_steps = self::get_current_backup_steps(); $step_key = strtolower($step); return (isset($backup_steps[$step_key]) && is_array($backup_steps[$step_key]) && array_key_exists($child, $backup_steps[$step_key])) ? $backup_steps[$step_key][$child] : null; } public static function set_complete_backup_step($step, $child, $value = null) { if (!self::backup_context_owns_current_run()) { return false; } $backup_steps = self::get_current_backup_steps(); $step_key = strtolower($step); if (!isset($backup_steps[$step_key]) || !is_array($backup_steps[$step_key])) { $template = self::get_backup_steps_template(); $backup_steps[$step_key] = isset($template[$step_key]) ? $template[$step_key] : array(); } $backup_steps[$step_key][$child] = (null !== $value ? $value : true); update_option('backup_steps', $backup_steps, true); } public static function unset_empty_log_line($lines){ foreach ($lines as $key => $line) { if(empty($line)){ unset($lines[$key]); } $line_arr = explode("-", $line); $date_string = $line_arr[0]; $is_date = OCM_Backup::validate_date($date_string); if(!$is_date){ unset($lines[$key]); } } return $lines; } public static function validate_date($date_string){ $timestamp = strtotime($date_string); return $timestamp ? true : false; } /** * Check if a SYSLOG message should be allowed for progress tracking * @param string $line The log line to check * @return bool True if the SYSLOG message should be shown in progress */ private static function is_syslog_allowed_for_progress($line) { // Always allow these specific SYSLOG messages $allowed_syslog_patterns = [ // Only allow these essential SYSLOG messages to prevent progress bar jumping 'SYSLOG: Table being backed up', 'SYSLOG: "[PHP ERR][FATAL]', 'SYSLOG: Number of updates made from replace', 'SYSLOG: Finished updating URLs in the database', 'SYSLOG: No table structure for table', // Removed: 'SYSLOG: Inserting table data into table', - too frequent, causes jumping // Removed: 'SYSLOG: End of data contents of table', - too frequent, causes jumping // Removed: 'SYSLOG: There is no mysqldump support', - appears for every table, causes jumping 'SYSLOG: Column statistics are supported', 'SYSLOG: Table retrieved', 'SYSLOG: Column statistics are not supported', // Allow only critical memory issues, not routine operations 'SYSLOG: Low memory available', 'SYSLOG: Low memory - Page Size reduced', 'SYSLOG: DB memory preflight', 'SYSLOG: Using row increment of', 'Calculated increment', 'SYSLOG: Error calculating dynamic increment', 'SYSLOG: Using direct processing', 'SYSLOG: Dispatching backup worker', 'SYSLOG: Server IP:', 'SYSLOG: WP_HTTP_BLOCK_EXTERNAL:', 'SYSLOG: WP_ACCESSIBLE_HOSTS:', 'SYSLOG: Admin URL:' ]; foreach ($allowed_syslog_patterns as $pattern) { if (strpos($line, $pattern) !== false) { return true; } } // Block all other SYSLOG messages from affecting progress bar return false; } /** * The message of a log line: what follows the " - " prefix that * write_to_log() adds. Only the first separator counts; splitting on the * last one cut messages such as "SYSLOG: Queue is empty - nothing to * process" down to a tail without "SYSLOG", which then showed as the * current step and sent the progress bar back to 1%. * * @param string $line * @return string */ public static function log_line_message($line) { $line = (string) $line; $sep = strpos($line, ' - '); return $sep === false ? $line : (string) substr($line, $sep + 3); } public static function get_last_log_message($skip = true) { $file = OCM_DEBUG_LOG_FILE; $contents = null; if (file_exists($file)) { $contents = file_get_contents($file); } else { return false; } $lastLine = ''; $lines = array_reverse(explode(PHP_EOL, $contents)); $lines = OCM_Backup::unset_empty_log_line($lines); foreach ($lines as $key => $line) { $line = self::log_line_message($line); if (empty(trim($line))) { continue; } if ($skip && in_array($line, self::$skipMessages, true)) { continue; } // Skip SYSLOG messages unless they're allowed for progress tracking if (strpos($line,'SYSLOG') !== false) { if (!self::is_syslog_allowed_for_progress($line)) { continue; } } $lastLine = $line; break; } return $lastLine; } public static function get_previous_log_percentage(){ $file = OCM_DEBUG_LOG_FILE; $contents = null; if (file_exists($file)) { $contents = file_get_contents($file); } else { return false; } $lastLine = ''; $lines = array_reverse(explode(PHP_EOL, $contents)); foreach ($lines as $key => $line) { $line = self::log_line_message($line); if (empty(trim($line))) { continue; } if ($line === self::LOG_MESSAGE_BG_PROCESS_RESTARTING) { $line = $lines[$key+2]; } if ($line === self::LOG_MESSAGE_BG_PROCESS_RESTARTING_LOG) { $line = $lines[$key+3]; } if($line === "Process is Restarting"){ $line = $lines[$key+1]; } $lastLine = $line; break; } $percentage = self::get_percent_by_log_message($lastLine); return $percentage; } /** * Put db ahead of themes/plugins/uploads so extract+preflight can gate file replace. * * @param array|object $presigned_urls * @return array */ private static function presigned_urls_db_first($presigned_urls) { $as_array = array(); foreach ($presigned_urls as $key => $url) { $as_array[(string) $key] = $url; } if (!array_key_exists('db', $as_array)) { return $as_array; } $ordered = array('db' => $as_array['db']); foreach ($as_array as $key => $url) { if ($key === 'db') { continue; } $ordered[$key] = $url; } return $ordered; } /** * Mid-restore skip of a part after max retries. User-excluded parts never * call this. A skipped database part sets OPTION_RESTORE_DB_PART_FAILED and * stops before any site files are replaced / plugins deactivated. * * @param array $excluded_restore_files * @param array $skipped_restore_files * @param string $key */ private static function capture_skipped_restore_part($excluded_restore_files, $skipped_restore_files, $key) { self::capture_failed_folder($excluded_restore_files, $key, 'ocm_excluded_folders'); self::capture_failed_folder($skipped_restore_files, $key, 'ocm_skipped_folders'); if ($key !== 'db') { $warnings = get_option(self::OPTION_RESTORE_PART_WARNINGS, array()); if (!is_array($warnings) || !isset($warnings[$key])) { self::record_restore_part_warning($key, 'timeout'); } } if ($key === 'db') { update_option(self::OPTION_RESTORE_DB_PART_FAILED, 1, true); self::stop_restore_after_db_part_failure(); } } /** * Stop when the database was meant to be included but is unavailable. * Does not stop for db excluded for THIS restore (selective UI exclude or * backup-log db=excluded files-only — those live on ocm_excluded_folders). * Backup-side db=skipped (ocm_backup_missing_parts) still stops. * * @param array $user_excluded * @return void */ public static function stop_if_db_unavailable_for_restore($user_excluded = null) { if ($user_excluded === null) { $user_excluded = get_option('ocm_excluded_folders', array()); } $user_excluded = is_array($user_excluded) ? $user_excluded : array(); if (in_array('db', $user_excluded, true)) { return; } if (get_option(self::OPTION_RESTORE_DB_PART_FAILED)) { self::stop_restore_after_db_part_failure(); } $missing = get_option('ocm_backup_missing_parts', array()); $missing = is_array($missing) ? $missing : array(); // Only backup-side skipped db (meant to be included, failed at backup). // db=excluded is intentional files-only and must not stop here. if (in_array('db', $missing, true)) { update_option(self::OPTION_RESTORE_DB_PART_FAILED, 1, true); self::stop_restore_after_db_part_failure(); } } /** * Shared cleanup for terminal stops that leave the live site unchanged * (apart from this plugin's progress options). The caller writes its one * stop line first; this adds "review_ask: none (stopped)", clears the * review flags, queue rows and staging tables, then the extracted SQL. */ private static function cleanup_after_restore_no_change_stop() { self::fail_restore_before_file_swap(); self::remove_extracted_restore_sql(); } /** * Remove the restore workspace (extracted SQL dump and part archives), * including a 3.1.20 wp-content/ocm_restore, after a no-change stop or a * Restore failed. */ private static function remove_extracted_restore_sql() { foreach (self::existing_restore_work_paths() as $dir) { self::deleteDirectoryBestEffort($dir); } } /** * Terminal stop when the database archive could not be downloaded, * decrypted, or extracted. No file replace, DB import, or plugin * deactivation has run. * * @return never */ private static function stop_restore_after_db_part_failure() { One_Click_Migration::write_to_log(self::LOG_RESTORE_STOPPED_DB_PART); self::cleanup_after_restore_no_change_stop(); exit; } /** * Deactivate other plugins only after DB import + options validation succeed * (or on files-only restores), immediately before file swap. */ private static function ensure_plugins_deactivated_for_restore() { if (get_option(self::OPTION_RESTORE_PLUGINS_ISOLATED)) { return; } self::force_only_ocm_plugin_active('before file replace'); update_option(self::OPTION_RESTORE_PLUGINS_ISOLATED, 1, true); } /** * Reinstate the active_plugins list saved at isolation time. * On a DB restore that list is the backup's own plugins (imported before * deferral). On failure before isolation this is a no-op. * * @return void */ private static function restore_active_plugins_after_failed_restore() { $saved = get_option('ocm_force_deactivated_plugins', null); if (!is_array($saved) || !$saved) { return; } update_option('active_plugins', $saved, true); delete_option('ocm_force_deactivated_plugins'); delete_option(self::OPTION_RESTORE_PLUGINS_ISOLATED); wp_cache_delete('active_plugins', 'options'); wp_cache_delete('alloptions', 'options'); } /** * Shared bookkeeping when restore fails before any file swap: clear review * flags, restore active_plugins if needed, drop staging tables and queue * rows, log "review_ask: none (stopped)", stop cleanly. * * @return void */ private static function fail_restore_before_file_swap() { self::restore_active_plugins_after_failed_restore(); self::drop_leftover_staging_tables_after_stop(); // No review CTA; do not consume the first-clean restore flag. update_option('ocm_show_restore_success', 0, false); update_option('ocm_show_review_cta', 0, false); update_option(self::OPTION_RESTORE_WAS_WARNED, 0, false); delete_option(OCM_Extra_Domains::OPTION); One_Click_Migration::write_to_log('SYSLOG: review_ask: none (stopped)'); One_Click_Migration::disable_maintenance_mode(); OCM_Debug_Mode::disable(); One_Click_Migration::reset_actions_start_mark(); update_option('ocm_is_stopped', true, true); // Drops the queue rows so no worker restarts the stopped restore. self::end_restore_run(); } /** * Remove leftover background-restore batch rows so a stopped restore cannot * be restarted by a queued worker item. * * @return void */ private static function clear_restore_batch_option_rows() { global $wpdb; $prefix = 'wp_ocm_background_restore_batch_'; if (is_object($wpdb) && method_exists($wpdb, 'query') && !empty($wpdb->options)) { $table = $wpdb->options; $column = 'option_name'; if (function_exists('is_multisite') && is_multisite() && !empty($wpdb->sitemeta)) { $table = $wpdb->sitemeta; $column = 'meta_key'; } if (method_exists($wpdb, 'get_col')) { $names = $wpdb->get_col( "SELECT {$column} FROM {$table} WHERE {$column} LIKE '{$prefix}%'" ); if (is_array($names)) { foreach ($names as $name) { if (is_string($name) && $name !== '') { delete_option($name); } } } } $wpdb->query("DELETE FROM {$table} WHERE {$column} LIKE '{$prefix}%'"); } // Test harnesses (and any in-request option bag) store rows via update_option. if (isset($GLOBALS['ocm_options']) && is_array($GLOBALS['ocm_options'])) { foreach (array_keys($GLOBALS['ocm_options']) as $name) { if (is_string($name) && strpos($name, $prefix) === 0) { delete_option($name); } } } } /** * Drop any ocmXXXX_ staging tables left after an import-stage stop. * * @return void */ private static function drop_leftover_staging_tables_after_stop() { global $wpdb; if (!is_object($wpdb)) { return; } OCM_SQL_Utils::drop_stale_temp_tables( $wpdb, null, isset($wpdb->base_prefix) ? $wpdb->base_prefix : null ); delete_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX); self::clear_db_import_state(); } /** * Arm a shutdown guard for the staging-import + URL-rewrite window. * A PHP fatal (e.g. memory exhausted) must not leave staging tables or * restart the worker mid-URL-step. * * @return void */ public static function register_import_oom_guard() { if (self::$import_oom_guard_active) { return; } self::$import_oom_guard_active = true; self::$import_oom_stop_written = false; register_shutdown_function(array(__CLASS__, 'handle_import_oom_shutdown')); } /** * Disarm the import OOM guard after a normal (non-fatal) exit from the stage. * * @return void */ public static function clear_import_oom_guard() { self::$import_oom_guard_active = false; } /** * Shutdown callback: on fatal during import/URL stage, stop with * LOG_RESTORE_STOPPED_IMPORT and run the shared stop bookkeeping * (fail_restore_before_file_swap: staging tables, restore batches, flags). * * @param array|null $error Optional error array (tests); defaults to error_get_last(). * @return void */ public static function handle_import_oom_shutdown($error = null) { if (!self::$import_oom_guard_active || self::$import_oom_stop_written) { return; } if ($error === null) { $error = error_get_last(); } if (!is_array($error) || empty($error['type'])) { return; } $fatal_types = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR); if (defined('E_RECOVERABLE_ERROR')) { $fatal_types[] = E_RECOVERABLE_ERROR; } if (!in_array((int) $error['type'], $fatal_types, true)) { return; } self::$import_oom_stop_written = true; self::$import_oom_guard_active = false; One_Click_Migration::write_to_log(self::LOG_RESTORE_STOPPED_IMPORT); self::fail_restore_before_file_swap(); } /** * Block themes/plugins/uploads replacement when db download/unpack failed * or when binary preflight refuses the dump. Does not deactivate plugins — * that waits until apply_deferred_file_swaps() after a successful DB step. * * @return void */ private static function gate_restore_before_file_replace() { if (get_option(self::OPTION_RESTORE_DB_PART_FAILED)) { self::stop_restore_after_db_part_failure(); } self::stop_if_db_unavailable_for_restore(); $preflight = self::ensure_binary_preflight_before_file_replace(); if ($preflight === 'continue') { One_Click_Migration::write_to_log('SYSLOG: Binary preflight scan still running; restarting worker.'); One_Click_Migration::write_to_log('Process is Restarting'); if (isset(One_Click_Migration::$process_restore_single) && is_object(One_Click_Migration::$process_restore_single)) { One_Click_Migration::$process_restore_single->restart_task(); } exit; } if ($preflight === false) { self::stop_restore_after_binary_preflight_refusal(); } } /** * Swap extracted themes/uploads/plugins into place only after DB import and * options/prefix validation have succeeded (or on an intentional files-only * restore where db is excluded). A failed DB step must never reach here. * * @param array $presigned_urls * @return void */ private static function apply_deferred_file_swaps($presigned_urls) { $excluded_restore_files = get_option('ocm_excluded_folders', array()); if (!is_array($excluded_restore_files)) { $excluded_restore_files = array(); } $skipped_restore_files = get_option('ocm_skipped_folders', array()); if (!is_array($skipped_restore_files)) { $skipped_restore_files = array(); } $content_dir = self::get_restore_work_path(); self::gate_restore_before_file_replace(); // Plugins stay active through DB staging/validation; isolate only now. self::ensure_plugins_deactivated_for_restore(); foreach (array('themes', 'uploads') as $key) { if (in_array($key, $excluded_restore_files, true)) { continue; } if (self::check_is_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW)) { continue; } $extract_path = $content_dir . '/' . $key; if (!is_dir($extract_path)) { self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); continue; } $directory = WP_CONTENT_DIR . '/' . $key; // An empty uploads archive must not wipe the destination's // existing media (mirror of an empty tree). if ($key === 'uploads' && !self::directory_contains_files($extract_path)) { One_Click_Migration::write_to_log( 'Notice: Backup uploads archive is empty; leaving the destination uploads folder unchanged.' ); self::deleteDirectoryBestEffort($extract_path); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); continue; } One_Click_Migration::write_to_log(sprintf('Restoring "%s" to directory "%s"', ucfirst($key), $directory)); if (!self::replaceDirectoryFromExtract($extract_path, $directory, $key)) { One_Click_Migration::write_to_log(sprintf( 'Downloading restore files. Error: "%s" directory could not be moved or copied to "%s" directory. Please check the write permission for these directories.', $extract_path, $directory )); One_Click_Migration::write_to_log(sprintf( 'Restore failed: restored "%s" files could not be moved into place (write permission).', $key )); update_option('ocm_is_stopped', true, true); die(); } if (isset(One_Click_Migration::$process_restore_single) && is_object(One_Click_Migration::$process_restore_single) && One_Click_Migration::$process_restore_single->time_exceeded() ) { $next_retry_count = get_option('ocm_restore_child_delete_retry_' . $key, 1); if ($next_retry_count && $next_retry_count <= self::MAX_STEP_RETRIES) { One_Click_Migration::write_to_log('Process is Restarting'); update_option('ocm_restore_child_delete_retry_' . $key, $next_retry_count, true); One_Click_Migration::$process_restore_single->restart_task(); } One_Click_Migration::write_to_log(sprintf( 'Skipping %s old-folder cleanup after repeated timeouts (new copy is in place).', $key )); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); self::record_restore_part_warning($key, 'copied, old copy not removed'); self::capture_skipped_restore_part($excluded_restore_files, $skipped_restore_files, $key); continue; } self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); } if (!in_array('themes', $excluded_restore_files, true)) { if (!self::check_is_complete_restore_step('themes', self::STEP_RESTORE_CHILD_THEMES_RESTORED)) { One_Click_Migration::write_to_log('Themes have been restored'); self::set_complete_restore_step('themes', self::STEP_RESTORE_CHILD_THEMES_RESTORED); } } if (!in_array('uploads', $excluded_restore_files, true)) { if (!self::check_is_complete_restore_step('uploads', self::STEP_RESTORE_CHILD_UPLOADS_RESTORED)) { One_Click_Migration::write_to_log('Uploads have been restored'); self::set_complete_restore_step('uploads', self::STEP_RESTORE_CHILD_UPLOADS_RESTORED); } } if (!in_array('plugins', $excluded_restore_files, true)) { if (!self::check_is_complete_restore_step('plugins', self::STEP_RESTORE_CHILD_RESTORE)) { self::restore_plugins($presigned_urls); } } // File swap done: put back the plugins list saved at isolation. After a // DB import that list is the source dump's active_plugins (master behaviour). self::restore_active_plugins_after_failed_restore(); } /** * Fingerprint for the extracted dump set + backup id. A stored verdict * whose key does not match must be ignored (rescan, never pass). * * @return string */ public static function binary_preflight_fingerprint() { $bucket = (string) get_option('ocm_bucket_id', ''); $dbfiles = self::get_restore_work_path() . '/db/'; $parts = array('bucket=' . $bucket); $files = OCM_SQL_Utils::list_restore_sql_dump_files($dbfiles); foreach ($files as $filepath) { $parts[] = basename($filepath) . ':' . (int) @filesize($filepath) . ':' . (int) @filemtime($filepath); } return sha1(implode('|', $parts)); } /** * After prefix remap rewrites dumps in place (size/mtime change), re-key a * stored pass verdict so a resumed import still sees a matching pass and * does not rescan / refuse. Only rekeys when the stored key still equals * the fingerprint captured before this rewrite; otherwise leave it so the * scan reruns. * * @param string $pre_rewrite_key Fingerprint from before swap_table_prefix. * @return void */ public static function rekey_binary_preflight_after_prefix_rewrite($pre_rewrite_key) { $pre_rewrite_key = (string) $pre_rewrite_key; if ($pre_rewrite_key === '') { return; } $stored = get_option(self::OPTION_BINARY_PREFLIGHT, null); if (!is_array($stored) || !isset($stored['status'], $stored['key']) || $stored['status'] !== 'pass') { return; } if ((string) $stored['key'] !== $pre_rewrite_key) { return; } $stored['key'] = self::binary_preflight_fingerprint(); update_option(self::OPTION_BINARY_PREFLIGHT, $stored, false); } /** * Scan already-extracted dumps under the workspace's db/ before replacing files. * Verdicts are persisted against binary_preflight_fingerprint(); a mismatched * key is treated as no verdict (rescan). Large dumps resume across requests. * * @return bool|string True when restore may continue; false when refused; * 'continue' when the scan needs another worker pass. */ public static function ensure_binary_preflight_before_file_replace() { if (self::$db_restore_refused_binary) { return false; } if (get_option(self::OPTION_RESTORE_DB_PART_FAILED)) { return false; } if (self::$binary_preflight_passed) { return true; } $excluded = get_option('ocm_excluded_folders', array()); // User-excluded db (selective restore) skips binary preflight. if (is_array($excluded) && in_array('db', $excluded, true)) { self::$binary_preflight_passed = true; return true; } $dbfiles = self::get_restore_work_path() . '/db/'; if (!is_dir($dbfiles)) { // DB not extracted yet. Callers with db-first ordering only replace // files after the db iteration has finished extract+preflight. return true; } $sql_files = OCM_SQL_Utils::list_restore_sql_dump_files($dbfiles); if ($sql_files === array()) { // Db part extracted but no SQL dumps — fail closed. One_Click_Migration::write_to_log( 'Error: Database restore folder has no .sql dump files to import.' ); One_Click_Migration::write_to_log( 'Restore stopped before import: backup needs a fresh 3.1.19 backup. No changes were made.' ); self::$db_restore_refused_binary = true; update_option(self::OPTION_BINARY_PREFLIGHT, array( 'key' => self::binary_preflight_fingerprint(), 'status' => 'refuse', ), false); return false; } $key = self::binary_preflight_fingerprint(); $stored = get_option(self::OPTION_BINARY_PREFLIGHT, null); if (is_array($stored) && isset($stored['key'], $stored['status']) && $stored['key'] === $key) { if ($stored['status'] === 'pass') { self::$binary_preflight_passed = true; return true; } if ($stored['status'] === 'refuse') { self::$db_restore_refused_binary = true; return false; } } else { // Key mismatch or absent → treat as no verdict (never pass on stale). $stored = array('key' => $key, 'status' => 'pending', 'file_index' => 0, 'byte_offset' => 0); delete_option(self::OPTION_BINARY_PREFLIGHT); } $deadline = microtime(true) + 8.0; $result = OCM_SQL_Utils::preflight_reject_unsafe_binary_dumps_resumable($sql_files, $stored, $deadline); $status = isset($result['status']) ? $result['status'] : 'refuse'; $state = isset($result['state']) && is_array($result['state']) ? $result['state'] : array(); $state['key'] = $key; $state['status'] = $status; update_option(self::OPTION_BINARY_PREFLIGHT, $state, false); if ($status === 'continue') { return 'continue'; } if ($status === 'refuse') { self::$db_restore_refused_binary = true; return false; } self::$binary_preflight_passed = true; return true; } /** * Terminal stop after binary preflight refusal (marker already in the log). * * @return never */ private static function stop_restore_after_binary_preflight_refusal() { self::cleanup_after_restore_no_change_stop(); exit; } /** * Terminal stop when binary preflight refuses (or cannot finish) at the * import step. Deferred file swap + staging import mean live files/tables * are untouched — LOG_RESTORE_STOPPED_IMPORT states that honestly. * * @return never */ public static function stop_restore_during_db_import() { self::$db_restore_refused_binary = true; self::clear_import_oom_guard(); One_Click_Migration::write_to_log(self::LOG_RESTORE_STOPPED_IMPORT); self::cleanup_after_restore_no_change_stop(); exit; } private static function restore_db($presigned_urls, wpdb $wpdb) { OCM_SQL_Utils::check_proc_available(false, $presigned_urls); // Get current site URL here $current_url = site_url(); $dbfiles = self::get_restore_work_path() . '/db/'; $excluded_folders = get_option( 'ocm_excluded_folders', []); //cater for the database restore. if(!in_array('db', $excluded_folders)){ if (!is_dir($dbfiles)) { One_Click_Migration::write_to_log(sprintf('Error: DB directory "%s" does not exist in unzipped files.', $dbfiles)); return false; } $sql_bytes = 0; foreach (OCM_SQL_Utils::list_restore_sql_dump_files($dbfiles) as $sql_file) { $sql_bytes += (int) filesize($sql_file); } OCM_SQL_Utils::preflight_db_memory($sql_bytes, 'import'); } // Progress of an import an earlier worker started. Its dumps on disk // are already rewritten for its staging prefix. $state = self::db_import_state(); // Detect source table prefix from backup SQL filenames (via usermeta.sql). $source_prefix = is_dir($dbfiles) ? OCM_SQL_Utils::get_table_prefix($dbfiles . 'dummy.sql') : false; if ($source_prefix === false || $source_prefix === '') { One_Click_Migration::write_to_log('Error: Database restore could not determine one unambiguous source table prefix.'); return false; } if ($state === null) { One_Click_Migration::write_to_log(sprintf( 'Detected source_prefix="%s" dest_base_prefix="%s" dest_prefix="%s"', $source_prefix, $wpdb->base_prefix, $wpdb->prefix )); } $found_files = OCM_SQL_Utils::list_restore_sql_dump_files($dbfiles); if (is_dir($dbfiles)) { $dir = new DirectoryIterator($dbfiles); foreach ($dir as $fileinfo) { if ($fileinfo->isDot() || !$fileinfo->isFile()) { continue; } if (strtolower(substr($fileinfo->getFilename(), -4)) === '.sql') { continue; } // Untouched copy of a dump whose prefix rewrite was cut off; // prepare_dump_for_staging_once() takes it back. if (substr($fileinfo->getFilename(), -strlen(self::PRISTINE_DUMP_SUFFIX)) === self::PRISTINE_DUMP_SUFFIX) { continue; } // Leftover work files from an interrupted run (e.g. *.ocm-stream.tmp) // must never be imported: they can carry a stale or partial prefix // state and would DROP + re-create tables that were just restored. One_Click_Migration::write_to_log(sprintf('Notice: Skipping non-SQL file "%s" in the DB restore directory.', $fileinfo->getFilename())); } } // Give priority to WP tables so they're imported first $priority = array( 'options.sql', 'users.sql', 'links.sql', 'commentmeta.sql', 'term_relationships.sql', 'postmeta.sql', 'posts.sql', 'term_taxonomy.sql', 'usermeta.sql', 'terms.sql', 'comments.sql', 'termmeta.sql', ); $result = array(); foreach ($priority as $p) { $result = array_unique(array_merge($result, preg_grep('/' . $p . '/i', $found_files))); } $sorted_table_list = array_unique(array_merge($result, $found_files)); sleep(1); // Preflight: refuse dumps whose declared binary/BLOB INSERT values cannot // round-trip, before allocating staging or touching live tables. Usually // already ran after DB extract (before file replace); memoized on pass so // the happy path does not scan twice in one request. $preflight = self::ensure_binary_preflight_before_file_replace(); if ($preflight === 'continue') { // Never treat an incomplete scan as refusal at the import step — // requeue so the resumable scan can finish. One_Click_Migration::write_to_log('SYSLOG: Binary preflight scan still running at import; restarting worker.'); One_Click_Migration::write_to_log('Process is Restarting'); if (isset(One_Click_Migration::$process_restore_single) && is_object(One_Click_Migration::$process_restore_single)) { One_Click_Migration::$process_restore_single->restart_task(); } exit; } if ($preflight !== true) { // Files and live tables are still untouched (staging import + deferred // file swap). Same no-change stop path as the early gate. self::stop_restore_during_db_import(); } $dump_names = array_values(array_map('basename', $sorted_table_list)); if ($state !== null && !self::db_import_state_matches($state, $source_prefix, $dump_names)) { if (!empty($state['prepared'])) { // Some dumps were rewritten for a prefix this worker can't confirm. One_Click_Migration::write_to_log('Error: Database restore could not resume: the saved staging import no longer matches the extracted database dumps.'); return false; } // Nothing was rewritten yet: start over. self::clear_db_import_state(); $state = null; } if ($state === null && (string) get_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX, '') !== '') { // An earlier attempt rewrote the dumps for that prefix without // recording how far it got. One_Click_Migration::write_to_log('Error: Database restore could not resume: an earlier attempt left a staging import without its progress.'); return false; } if ($state === null) { // Drop leftover staging tables from restores killed mid-import. Trash // (ocd…/ocmd…) is kept when live counterparts are missing. OCM_SQL_Utils::drop_stale_temp_tables($wpdb, null, $wpdb->base_prefix); // Import into a temporary staging prefix first. Live destination tables // and site files stay intact until every dump imports cleanly and the // options/prefix gate passes; then we promote tables and swap files. $staging_prefix = OCM_SQL_Utils::allocate_staging_prefix($wpdb); if ($staging_prefix === false || $staging_prefix === '') { One_Click_Migration::write_to_log('Error: Database restore could not allocate a staging table prefix.'); return false; } if (!OCM_SQL_Utils::assert_staging_identifier_headroom($sorted_table_list, $staging_prefix, $source_prefix)) { return false; } // A worker cut off from here on resumes with this prefix: the dumps // are rewritten for it in place. $state = array( 'prefix' => $staging_prefix, 'source_prefix' => $source_prefix, 'files' => $dump_names, 'prepared' => array(), 'imported' => array(), 'site_url' => '', 'unit' => '', 'attempts' => 0, ); self::save_db_import_state($state); update_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX, $staging_prefix, false); One_Click_Migration::write_to_log(sprintf( 'SYSLOG: Importing database into staging prefix "%s"; live tables with prefix "%s" stay intact until import succeeds.', $staging_prefix, $wpdb->base_prefix )); $leftover_foreign_options = 0; $tables_before_import = $wpdb->get_results('SHOW TABLES'); foreach ($tables_before_import as $table) { foreach ($table as $table_name) { if (substr($table_name, -8) === '_options' && strpos($table_name, $wpdb->base_prefix) !== 0 && strpos($table_name, $staging_prefix) !== 0 ) { $leftover_foreign_options++; } } } if ($leftover_foreign_options > 0) { One_Click_Migration::write_to_log(sprintf( 'Notice: Found %d leftover foreign-prefix *_options table(s) on destination; siteurl/home gate will use staging "%soptions" matching source_prefix="%s" only.', $leftover_foreign_options, $staging_prefix, $source_prefix )); } } else { $staging_prefix = $state['prefix']; // Keep this import's tables; anything else under ocmXXXX_ is stale. OCM_SQL_Utils::drop_stale_temp_tables($wpdb, $staging_prefix, $wpdb->base_prefix); One_Click_Migration::write_to_log(sprintf( 'SYSLOG: Resuming database import into staging prefix "%s" (%d of %d dumps rewritten, %d imported).', $staging_prefix, count($state['prepared']), count($dump_names), count($state['imported']) )); } // Fatals (including memory exhausted) during staging import / URL rewrite // must stop with LOG_RESTORE_STOPPED_IMPORT and never retry the URL step. self::register_import_oom_guard(); $source_suffixes = OCM_SQL_Utils::source_table_suffixes_from_dumps($sorted_table_list, $source_prefix); if (!self::check_is_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_IMPORTED)) { // Data swap source → live dest; remap only statement table names to staging // so row data keeps {dest}capabilities / {dest}user_roles. Foreign-prefix // dumps (ktv_options, nst_options) are also pulled under staging. foreach ($sorted_table_list as $filepath) { $dump = basename($filepath); if (!empty($state['prepared'][$dump])) { // Recorded already; drop the untouched copy a cut-off worker left. @unlink($filepath . self::PRISTINE_DUMP_SUFFIX); continue; } if (!self::begin_db_unit($state, sprintf('rewriting the table prefix in "%s"', $dump))) { self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } // Prefix rewrite changes dump size/mtime; keep a stored pass valid only // when it still matches the fingerprint from before this rewrite. $pre_rewrite_key = self::binary_preflight_fingerprint(); if (!self::prepare_dump_for_staging_once($filepath, $wpdb->base_prefix, $staging_prefix, $source_prefix)) { One_Click_Migration::write_to_log(sprintf('Error: Database restore failed while remapping the table prefix in "%s".', $dump)); self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } if (!is_file($filepath) || !is_readable($filepath) || filesize($filepath) === 0) { One_Click_Migration::write_to_log(sprintf('Error: Database restore rejected invalid SQL dump "%s".', $dump)); self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } $state['prepared'][$dump] = true; self::end_db_unit($state); @unlink($filepath . self::PRISTINE_DUMP_SUFFIX); self::rekey_binary_preflight_after_prefix_rewrite($pre_rewrite_key); self::maybe_yield_db_phase('table prefix rewrite'); } foreach ($sorted_table_list as $filepath) { $dump = basename($filepath); if (!empty($state['imported'][$dump])) { continue; } if (!self::begin_db_unit($state, sprintf('importing "%s"', $dump))) { self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } // Import remaps statement heads from source_prefix → staging for any // remaining source-prefixed names; foreign tables already use staging. if (!OCM_SQL_Utils::import($filepath, $source_prefix, $staging_prefix)) { One_Click_Migration::write_to_log(sprintf('Error: Database restore failed while importing "%s".', $dump)); self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } // Gate siteurl/home only after the source-prefix options dump imports. // Stray *options.sql files (ktv_options, nst_options, multisite // {base}{id}_options) must import without this check. if ($dump === $source_prefix . 'options.sql') { if (!self::verify_staged_main_options($wpdb, $source_prefix, $staging_prefix, $current_url)) { return self::refuse_options_prefix_gate($staging_prefix, $source_prefix, $wpdb); } $state['site_url'] = (string) self::$restore_site_url; } $state['imported'][$dump] = true; self::end_db_unit($state); self::maybe_yield_db_phase('import'); } if (!in_array($source_prefix . 'options.sql', $dump_names, true)) { // No source-prefix options dump — still require a resolvable options // table with siteurl/home under staging before promoting. $table_name = OCM_SQL_Utils::resolve_main_options_table($wpdb, $source_prefix, $staging_prefix); if ($table_name === false) { return self::refuse_options_prefix_gate($staging_prefix, $source_prefix, $wpdb); } $db_url = $wpdb->get_results("SELECT option_value FROM `{$table_name}` WHERE option_name = 'siteurl'"); if (!empty($db_url) && !empty($db_url[0]->option_value)) { $state['site_url'] = (string) $db_url[0]->option_value; self::save_db_import_state($state); } } self::set_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_IMPORTED); } self::$restore_site_url = isset($state['site_url']) ? (string) $state['site_url'] : ''; if (!self::check_is_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_URLS)) { // URL rewrite on temp-prefixed staging tables only — before atomic RENAME // and before any file swap. Failure / OOM leaves the live site untouched. if (!self::rewrite_urls_on_staging_tables($staging_prefix)) { self::clear_import_oom_guard(); if (self::$url_rewrite_unpageable_table) { // Drop staging quietly so the dedicated Error stays immediately // before LOG_RESTORE_STOPPED_IMPORT (no discard Error in between). OCM_SQL_Utils::drop_tables_with_prefix($staging_prefix); delete_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX); self::clear_db_import_state(); } else { self::discard_staging_import($staging_prefix, $wpdb->base_prefix); } return false; } self::set_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_URLS); // Promote and the file swaps after it start with a full time budget. self::maybe_yield_db_phase('URL update'); } if (!self::check_is_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_PROMOTED)) { $state = self::db_import_state(); $was_promoting = !empty($state['promoting']); if ($was_promoting && !self::staging_tables_exist($wpdb, $staging_prefix)) { // The RENAME went through and the worker was cut off after it. One_Click_Migration::write_to_log(sprintf( 'SYSLOG: Staging prefix "%s" was already promoted by an earlier worker.', $staging_prefix )); } else { // Saved before the RENAME, so the promoted options table (which // keeps this site's restore_steps) carries the marker. $state['promoting'] = true; if (!self::begin_db_unit($state, 'promoting the staging tables')) { self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } $review_options = self::snapshot_destination_review_options($wpdb); if (!OCM_SQL_Utils::promote_staging_tables($staging_prefix, $wpdb->base_prefix, $source_suffixes)) { One_Click_Migration::write_to_log(sprintf( 'Error: Database import failed while promoting staging prefix "%s" to live prefix "%s"; discarding staging tables and leaving any recoverable live tables in place.', $staging_prefix, $wpdb->base_prefix )); self::clear_import_oom_guard(); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); return false; } self::clear_import_oom_guard(); // Destination review prefs were snapshotted from the live options table // before promote; write them onto the newly promoted options table. self::restore_destination_review_options($wpdb, $review_options); } self::set_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_PROMOTED); } self::clear_import_oom_guard(); // The source's queue rows and step checkpoints never reach the live // table: promote swaps them for this site's own (keep_destination_plugin_state). // Deleting queue rows here would drop this restore's own queue item and // restore_steps, and a worker restarted after promote could not resume. One_Click_Migration::write_to_log('Database has been restored'); return true; } /** * In-flight database import of this restore (restore_steps db/dbImport): * staging prefix, dumps rewritten and imported so far, the source siteurl * read at the options gate and the URL-rewrite position. Null when no * import has started. * * @return array|null */ private static function db_import_state() { $state = self::get_restore_step_value('db', self::STEP_RESTORE_CHILD_DB_IMPORT); if (!is_array($state) || empty($state['prefix']) || !is_string($state['prefix'])) { return null; } return $state + array( 'source_prefix' => '', 'files' => array(), 'prepared' => array(), 'imported' => array(), 'site_url' => '', 'unit' => '', 'attempts' => 0, ); } private static function save_db_import_state(array $state) { self::set_complete_restore_step('db', self::STEP_RESTORE_CHILD_DB_IMPORT, $state); } /** * Forget the in-flight import: its staging tables were dropped. * * @return void */ private static function clear_db_import_state() { $steps = get_option('restore_steps', null); if (is_array($steps) && isset($steps['db']) && is_array($steps['db']) && (!empty($steps['db'][self::STEP_RESTORE_CHILD_DB_IMPORT]) || !empty($steps['db'][self::STEP_RESTORE_CHILD_DB_IMPORTED]) || !empty($steps['db'][self::STEP_RESTORE_CHILD_DB_URLS]) || !empty($steps['db'][self::STEP_RESTORE_CHILD_DB_PROMOTED])) ) { $steps['db'][self::STEP_RESTORE_CHILD_DB_IMPORT] = null; $steps['db'][self::STEP_RESTORE_CHILD_DB_IMPORTED] = false; $steps['db'][self::STEP_RESTORE_CHILD_DB_URLS] = false; $steps['db'][self::STEP_RESTORE_CHILD_DB_PROMOTED] = false; update_option('restore_steps', $steps, true); } delete_option(self::OPTION_RESTORE_URL_HOSTS); } /** * May this worker continue the saved import? The dumps, the source prefix * and (until promote) the stored staging prefix must all still match. * * @param array $state * @param string $source_prefix * @param string[] $dump_names * @return bool */ private static function db_import_state_matches(array $state, $source_prefix, array $dump_names) { if ($state['source_prefix'] !== $source_prefix || array_values((array) $state['files']) !== $dump_names) { return false; } return !empty($state['promoting']) || (string) get_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX, '') === $state['prefix']; } /** * Record that this worker starts $unit of the import. Starting the same * unit again without having finished it means the worker before was cut * off in it (PHP-FPM request_terminate_timeout sends SIGTERM, which skips * shutdown functions). After MAX_DB_UNIT_ATTEMPTS of those the restore * stops instead of looping. * * @param array $state * @param string $unit Human-readable step, e.g. 'importing "wp_posts.sql"'. * @return bool False when the unit was cut off too often. */ private static function begin_db_unit(array &$state, $unit) { $attempts = (isset($state['unit']) && $state['unit'] === $unit) ? (int) $state['attempts'] + 1 : 1; if ($attempts > self::MAX_DB_UNIT_ATTEMPTS) { One_Click_Migration::write_to_log(sprintf( 'Error: Database restore was cut off %d times in a row while %s. The server stops PHP before this step can finish; raise its PHP time limit (max_execution_time, PHP-FPM request_terminate_timeout) and run the restore again.', $attempts - 1, $unit )); return false; } $state['unit'] = $unit; $state['attempts'] = $attempts; self::save_db_import_state($state); return true; } /** * The unit begin_db_unit() recorded has finished; save it with $state. * * @param array $state * @return void */ private static function end_db_unit(array &$state) { $state['unit'] = ''; $state['attempts'] = 0; self::save_db_import_state($state); self::$db_phase_progress = true; } /** * True once this restore worker has used up its time budget. * * @return bool */ private static function restore_worker_out_of_time() { $process = One_Click_Migration::$process_restore_single; return is_object($process) && method_exists($process, 'time_exceeded') && $process->time_exceeded(); } /** * After a finished unit: when the worker is out of time, hand the rest of * the DB phase to a new worker. Progress is saved, and the queue item stays * queued, so the next worker re-enters restore_db() at the checkpoint. * * @param string $stage For the log. * @return void */ private static function maybe_yield_db_phase($stage) { if (self::$db_phase_progress && self::restore_worker_out_of_time()) { self::yield_db_phase($stage); } } /** * @param string $stage For the log. * @return never */ private static function yield_db_phase($stage) { self::clear_import_oom_guard(); One_Click_Migration::write_to_log(sprintf('SYSLOG: Database restore: %s continues in a new worker.', $stage)); One_Click_Migration::write_to_log('Process is Restarting'); One_Click_Migration::$process_restore_single->restart_task(); exit; } /** * Rewrite one extracted dump for the staging prefix. The untouched dump is * kept (hard link, else a copy) until the caller records the rewrite: every * rewrite pass replaces the file via a temp file and rename(), so the link * still points at the original bytes. A worker cut off halfway leaves that * copy behind, and the next one starts again from it instead of rewriting a * half-rewritten dump a second time. * * @param string $filepath * @param string $dest_prefix * @param string $staging_prefix * @param string $source_prefix * @return bool */ private static function prepare_dump_for_staging_once($filepath, $dest_prefix, $staging_prefix, $source_prefix) { $pristine = $filepath . self::PRISTINE_DUMP_SUFFIX; @unlink($pristine . '.tmp'); if (is_file($pristine)) { if (!@rename($pristine, $filepath)) { One_Click_Migration::write_to_log(sprintf( 'Error: Could not put back the untouched copy of "%s" after an interrupted table prefix rewrite.', basename($filepath) )); return false; } One_Click_Migration::write_to_log(sprintf( 'SYSLOG: Restarting the table prefix rewrite of "%s" from its untouched copy.', basename($filepath) )); } $kept = function_exists('link') && @link($filepath, $pristine); if (!$kept) { $kept = @copy($filepath, $pristine . '.tmp') && @rename($pristine . '.tmp', $pristine); } if (!$kept) { @unlink($pristine . '.tmp'); One_Click_Migration::write_to_log(sprintf( 'Error: Could not keep an untouched copy of "%s" while rewriting its table prefix. Please check disk quota and write permissions.', basename($filepath) )); return false; } return OCM_SQL_Utils::prepare_sql_dump_for_staging($filepath, $dest_prefix, $staging_prefix, $source_prefix); } /** * Staging table a prepared dump creates: source- or destination-prefixed * names keep their suffix under the staging prefix, foreign tables are * nested under it (see OCM_SQL_Utils::prepare_sql_dump_for_staging()). * * @param string $filepath * @param string $source_prefix * @param string $dest_prefix * @param string $staging_prefix * @return string */ private static function staged_table_for_dump($filepath, $source_prefix, $dest_prefix, $staging_prefix) { $table = preg_replace('/\.sql$/i', '', basename((string) $filepath)); foreach (array($source_prefix, $dest_prefix) as $prefix) { if (is_string($prefix) && $prefix !== '' && strpos($table, $prefix) === 0) { return $staging_prefix . substr($table, strlen($prefix)); } } return $staging_prefix . $table; } /** * @param wpdb $wpdb * @param string $prefix * @return bool */ private static function staging_tables_exist(wpdb $wpdb, $prefix) { $tables = $wpdb->get_col('SHOW TABLES'); foreach (is_array($tables) ? $tables : array() as $table) { if (strpos((string) $table, $prefix) === 0) { return true; } } return false; } /** * Options gate on the imported main options table: siteurl and home must be * there. Remembers the source siteurl for the URL rewrite and points * siteurl/home at this site. * * @return bool False when the gate refuses. */ private static function verify_staged_main_options(wpdb $wpdb, $source_prefix, $staging_prefix, $current_url) { $table_name = OCM_SQL_Utils::resolve_main_options_table($wpdb, $source_prefix, $staging_prefix); if ($table_name === false) { return false; } $db_url = $wpdb->get_results("SELECT option_value FROM `{$table_name}` WHERE option_name = 'siteurl'"); self::$restore_site_url = $db_url[0]->option_value; One_Click_Migration::write_to_log(sprintf( 'Main options verified on staging table "%s" (source_prefix="%s" staging_prefix="%s" dest_base_prefix="%s" dest_prefix="%s").', $table_name, $source_prefix, $staging_prefix, $wpdb->base_prefix, $wpdb->prefix )); self::point_staged_options_at_destination($wpdb, $table_name, $current_url); return true; } private static function point_staged_options_at_destination(wpdb $wpdb, $table_name, $current_url) { if ($current_url === self::$restore_site_url) { return; } $wpdb->update( $table_name, array('option_value' => $current_url), array('option_name' => 'siteurl'), array('%s'), array('%s') ); $wpdb->update( $table_name, array('option_value' => $current_url), array('option_name' => 'home'), array('%s'), array('%s') ); } /** * Re-import the dump behind one staging table. A worker cut off while * rewriting that table's URLs leaves rows after its last checkpoint * possibly rewritten, and a second pass is not always safe (a new address * can contain the old one), so the table starts over from its dump. * * @param string $table * @param array $state * @return bool */ private static function reimport_staging_table($table, array $state) { global $wpdb; $dbfiles = self::get_restore_work_path() . '/db/'; foreach (OCM_SQL_Utils::list_restore_sql_dump_files($dbfiles) as $filepath) { if (self::staged_table_for_dump($filepath, $state['source_prefix'], $wpdb->base_prefix, $state['prefix']) !== $table) { continue; } $wpdb->query('DROP TABLE IF EXISTS `' . str_replace('`', '``', $table) . '`'); if (!OCM_SQL_Utils::import($filepath, $state['source_prefix'], $state['prefix'])) { One_Click_Migration::write_to_log(sprintf( 'Error: Database restore failed while importing "%s" again after an interrupted URL update.', basename($filepath) )); return false; } if (basename($filepath) === $state['source_prefix'] . 'options.sql') { self::point_staged_options_at_destination($wpdb, $table, site_url()); } One_Click_Migration::write_to_log(sprintf( 'SYSLOG: Imported "%s" again: a worker was cut off while updating its URLs.', basename($filepath) )); return true; } One_Click_Migration::write_to_log(sprintf( 'Error: Database restore could not find the dump behind staging table "%s" to import it again after an interrupted URL update.', $table )); return false; } /** * Run URL search-replace against staging-prefixed tables only. Must complete * before promote_staging_tables() / file swap. Returns false on rewrite errors * so the caller can discard staging and stop with LOG_RESTORE_STOPPED_IMPORT. * * Inside restore_db() the rewrite checkpoints after every table and when * the worker runs out of time mid-table (keyset position), then hands over * to a new worker. A table a worker was cut off in is imported again. * * @param string $staging_prefix * @return bool */ private static function rewrite_urls_on_staging_tables($staging_prefix) { self::$url_rewrite_unpageable_table = null; $old_url = self::$restore_site_url; if (empty($old_url) || !is_string($staging_prefix) || $staging_prefix === '') { return true; } $current_url = site_url(); $url_plan = get_option(OCM_Restore_Precheck::OPTION, array()); if (!is_array($url_plan)) { $url_plan = array(); } $extra_arguments = OCM_Restore_Precheck::replacement_args($url_plan, $old_url, $current_url); // Checkpoints exist only for restore_db()'s own staging import. $state = self::db_import_state(); if ($state !== null && $state['prefix'] !== $staging_prefix) { $state = null; } $progress = ($state !== null && isset($state['url']) && is_array($state['url'])) ? $state['url'] : array(); $progress += array( 'done' => array(), 'table' => null, 'last_key' => null, 'clean' => true, 'updates' => 0, 'errors' => array(), 'error_count' => 0, 'failed_pairs' => array(), ); $started = $progress['done'] || $progress['table'] !== null; $db_details = new OCM_DB(); $collect = empty($url_plan['reviewed']); if ($collect) { $found = $started ? get_option(self::OPTION_RESTORE_URL_HOSTS, array()) : array(); $db_details->enable_url_collection(is_array($found) ? $found : array()); } $all_tables = OCM_DB::get_tables(); if (!is_array($all_tables)) { $all_tables = array(); } $staging_tables = array(); foreach ($all_tables as $table) { if (is_string($table) && strpos($table, $staging_prefix) === 0) { $staging_tables[] = $table; } } if (!$staging_tables) { return true; } // Finish the table the last worker was in before any other: its // checkpoint (or the mark that it was cut off) is only kept until the // next table starts. SHOW TABLES order can change when it is re-imported. $in_progress = array_search($progress['table'], $staging_tables, true); if ($in_progress !== false) { unset($staging_tables[$in_progress]); array_unshift($staging_tables, $progress['table']); } elseif ($progress['table'] !== null) { One_Click_Migration::write_to_log(sprintf( 'Error: Database restore could not resume the URL update: staging table "%s" is gone.', $progress['table'] )); return false; } $save_progress = static function (array $progress) use (&$state, $collect, $db_details) { $state['url'] = $progress; if ($collect) { update_option(self::OPTION_RESTORE_URL_HOSTS, $db_details->collected_urls(), false); } }; $should_yield = $state !== null ? static function () { return self::restore_worker_out_of_time(); } : null; $remaining = count($staging_tables) - count(array_intersect_key(array_flip($staging_tables), $progress['done'])); foreach ($staging_tables as $table) { if (!empty($progress['done'][$table])) { continue; } $resume_key = null; if ($progress['table'] === $table) { if (empty($progress['clean'])) { if (!self::reimport_staging_table($table, $state)) { return false; } } else { $resume_key = is_array($progress['last_key']) ? $progress['last_key'] : null; } } if ($state !== null) { $progress['table'] = $table; $progress['last_key'] = $resume_key; $progress['clean'] = false; $state['url'] = $progress; if (!self::begin_db_unit($state, sprintf('updating URLs in "%s"', $table))) { return false; } } $table_result = $db_details->search_replace_db(array($table), $extra_arguments + array( 'continuing' => $started, 'log_summary' => false, 'resume_key' => $resume_key, 'should_yield' => $should_yield, )); $started = true; // Dedicated stop (keyless unpageable or oversized row): Error was already // logged; return before Finished/Number SYSLOG so that Error sits // immediately before LOG_RESTORE_STOPPED_IMPORT. if (!empty($table_result['unpageable_table'])) { self::$url_rewrite_unpageable_table = (string) $table_result['unpageable_table']; return false; } if (!empty($table_result['oversized_table'])) { self::$url_rewrite_unpageable_table = (string) $table_result['oversized_table']; return false; } if (!empty($table_result['paging_stuck_table'])) { self::$url_rewrite_unpageable_table = (string) $table_result['paging_stuck_table']; return false; } $progress['updates'] += isset($table_result['updates']) ? (int) $table_result['updates'] : 0; $table_errors = isset($table_result['errors']) && is_array($table_result['errors']) ? $table_result['errors'] : array(); $progress['error_count'] += count($table_errors); // Only the count is logged; keep a few messages, not one per row. $progress['errors'] = array_slice(array_merge($progress['errors'], $table_errors), 0, 20); if (isset($table_result['failed_pairs']) && is_array($table_result['failed_pairs'])) { foreach ($table_result['failed_pairs'] as $pair) { if (is_array($pair) && isset($pair['search']) && !isset($progress['failed_pairs'][$pair['search']])) { $progress['failed_pairs'][$pair['search']] = $pair; } } } if (!empty($table_result['yielded']) && $state !== null) { $progress['last_key'] = isset($table_result['cursor']['last_key']) ? $table_result['cursor']['last_key'] : null; $progress['clean'] = true; $save_progress($progress); self::end_db_unit($state); self::yield_db_phase('URL update'); } $progress['done'][$table] = true; $progress['table'] = null; $progress['last_key'] = null; $progress['clean'] = true; $remaining--; if ($state !== null) { $save_progress($progress); self::end_db_unit($state); if ($remaining > 0) { self::maybe_yield_db_phase('URL update'); } } } // Logging the summary and the extra-domain candidates can take a while // (every log line is uploaded); count cut-offs here too. if ($state !== null && !self::begin_db_unit($state, 'finishing the URL update')) { return false; } $result = array( 'updates' => $progress['updates'], 'errors' => $progress['errors'], 'failed_pairs' => array_values($progress['failed_pairs']), ); One_Click_Migration::write_to_log(sprintf( 'SYSLOG: URL update: peak_bytes=%d memory_limit=%s', (int) memory_get_peak_usage(true), (string) ini_get('memory_limit') )); $report_update = isset($result['updates']) ? $result['updates'] : 0; One_Click_Migration::write_to_log(sprintf('SYSLOG: Number of updates made from replace "%s"', $report_update)); One_Click_Migration::write_to_log('SYSLOG: Finished updating URLs in the database'); if (!empty($result['errors'])) { One_Click_Migration::write_to_log(sprintf( 'Error: Database restore failed while updating URLs on staging tables (%d).', (int) $progress['error_count'] )); $failed_pairs = isset($result['failed_pairs']) && is_array($result['failed_pairs']) ? $result['failed_pairs'] : array(); self::log_failed_address_pairs($failed_pairs); return false; } // Only after a clean rewrite: candidates make the admin page open the // restore-success modal, which must never follow a stopped restore. if ($collect) { self::store_extra_domain_candidates($db_details->collected_urls(), $old_url, $current_url); delete_option(self::OPTION_RESTORE_URL_HOSTS); } if (!empty($url_plan['reviewed'])) { delete_option(OCM_Extra_Domains::OPTION); delete_option('ocm_restore_partial_address_updates'); delete_option('ocm_restore_failed_address_pairs'); } return true; } /** * Options/prefix gate failure: write the dedicated stop line, discard staging, * leave live tables/files/plugins unchanged. * * @param string $staging_prefix * @param string $source_prefix * @param wpdb $wpdb * @return false */ private static function refuse_options_prefix_gate($staging_prefix, $source_prefix, wpdb $wpdb) { self::$db_restore_refused_options = true; self::clear_import_oom_guard(); One_Click_Migration::write_to_log(sprintf( 'Error: Database restore failed: main options table "%soptions" is incomplete or wrong-prefix (missing siteurl and/or home). source_prefix="%s" staging_prefix="%s" dest_base_prefix="%s" dest_prefix="%s".', $staging_prefix, $source_prefix, $staging_prefix, $wpdb->base_prefix, $wpdb->prefix )); self::discard_staging_import($staging_prefix, $wpdb->base_prefix); One_Click_Migration::write_to_log(self::LOG_RESTORE_STOPPED_OPTIONS); return false; } /** * Drop an in-flight staging import and leave live destination tables alone. * * @param string $staging_prefix * @param string $dest_prefix * @return void */ private static function discard_staging_import($staging_prefix, $dest_prefix) { One_Click_Migration::write_to_log(sprintf( 'Error: Database import failed; discarding staging prefix "%s" and leaving live tables with prefix "%s" unchanged.', $staging_prefix, $dest_prefix )); OCM_SQL_Utils::drop_tables_with_prefix($staging_prefix); delete_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX); self::clear_db_import_state(); } private static function snapshot_destination_review_options(wpdb $wpdb) { $table = $wpdb->base_prefix . 'options'; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT option_name, option_value, autoload FROM {$table} WHERE option_name IN (%s, %s)", 'ocm_review_ask_dismissed', 'ocm_clean_restore_count' ), ARRAY_A ); return is_array($rows) ? $rows : array(); } private static function restore_destination_review_options(wpdb $wpdb, $rows) { if (!is_array($rows)) { return; } $table = $wpdb->base_prefix . 'options'; foreach ($rows as $row) { if (!is_array($row) || !isset($row['option_name'], $row['option_value'], $row['autoload'])) { continue; } $wpdb->replace( $table, array( 'option_name' => $row['option_name'], 'option_value' => $row['option_value'], 'autoload' => $row['autoload'], ), array('%s', '%s', '%s') ); wp_cache_delete($row['option_name'], 'options'); } } /** * @param $presigned_urls */ private static function restore_plugins($presigned_urls) { // Handle plugin extraction. GLOB_ONLYDIR so loose files like the WP placeholder // index.php aren't treated as plugin directories. $plugins = glob(WP_CONTENT_DIR . '/plugins/*', GLOB_ONLYDIR) ?: array(); // Loop through the list and delete every plugin directory except 1-click-migration foreach ($plugins as $plugin) { if ($plugin === WP_CONTENT_DIR . '/plugins/1-click-migration') { continue; } if (!self::deleteDirectoryBestEffort($plugin) && is_dir($plugin)) { One_Click_Migration::write_to_log(sprintf( 'Notice: Could not fully remove plugin directory "%s"; restored plugin files will be merged over it.', $plugin )); } } // Move every plugin from the restore directory one by one in to the existing plugins directory. // GLOB_ONLYDIR so the WP-generated index.php placeholder isn't treated as a plugin directory. $restore_plugins_dir = self::get_restore_work_path() . '/plugins'; $restore_plugins = glob($restore_plugins_dir . '/*', GLOB_ONLYDIR) ?: array(); foreach ($restore_plugins as $plugin) { if ($plugin === $restore_plugins_dir . '/1-click-migration') { continue; } $directory_path_split = explode('/', $plugin); $plugin_name = $directory_path_split[count($directory_path_split) - 1]; $pluginDirectoryName = WP_CONTENT_DIR . '/plugins/' . $plugin_name; if (!self::replaceDirectoryFromExtract($plugin, $pluginDirectoryName, 'plugin ' . $plugin_name)) { One_Click_Migration::write_to_log(sprintf( 'Restore plugins. Error: "%s" directory could not be moved to "%s" directory. Please check the write permission for these directories.', $plugin, $pluginDirectoryName )); die(); } } One_Click_Migration::write_to_log('Plugins have been restored'); } public static function add_file_not_found_notice(){ $file = OCM_DEBUG_LOG_FILE; $lines = []; } private static function substring_in_array($needle, array $haystack) { $filtered = array_filter($haystack, function ($item) use ($needle) { return false !== strpos($item, $needle); }); return !empty($filtered); } public static function restart_failed_process(){ // Check user capabilities if (!current_user_can('manage_options')) { wp_die('You do not have sufficient permissions to perform this action.'); } // Verify nonce if (!wp_verify_nonce($_POST['_wpnonce'] ?? '', 'ocm_restart_failed_process')) { wp_die('Security check failed. Please try again.'); } if(isset($_POST['process'])){ $process = sanitize_text_field($_POST['process']); One_Click_Migration::write_to_log("Process is Restarting"); if($process == 'backup'){ if (One_Click_Migration::get_backup_plugin_isolation_token() === '') { One_Click_Migration::start_backup_plugin_isolation(); } One_Click_Migration::$process_backup_single->restart_task(); } if($process == 'restore'){ One_Click_Migration::$process_restore_single->restart_task(); } } wp_send_json_success("success"); } /** * Rewrite only the extra hosts an administrator checked on the restore-complete screen. * An empty selection records the decision and rewrites nothing. */ public static function apply_extra_domains() { if (!current_user_can('manage_options')) { wp_send_json_error(array('message' => 'Forbidden'), 403); } check_ajax_referer('ocm_apply_extra_domains'); $stored = get_option(OCM_Extra_Domains::OPTION, array()); if (!is_array($stored) || !$stored) { wp_send_json_success(array('updates' => 0)); } $raw_ids = array(); if (isset($_POST['ids'])) { $raw_ids = wp_unslash($_POST['ids']); if (!is_array($raw_ids)) { $raw_ids = explode(',', (string) $raw_ids); } } $wanted = array(); foreach ($raw_ids as $id) { $id = sanitize_text_field($id); if ('' !== $id) { $wanted[$id] = true; } } $selected = array(); foreach ($stored as $row) { if (is_array($row) && isset($row['id']) && isset($wanted[$row['id']])) { $selected[] = $row; } } $updates = 0; if ($selected) { $pairs = array(); foreach ($selected as $row) { $pairs = array_merge($pairs, OCM_Extra_Domains::replacement_pairs($row, site_url())); } if ($pairs) { One_Click_Migration::enable_maintenance_mode(); $maintenance_on = true; register_shutdown_function(function () use (&$maintenance_on) { if ($maintenance_on) { One_Click_Migration::disable_maintenance_mode(); $maintenance_on = false; } }); try { @set_time_limit(One_Click_Migration::get_timeout()); $db = new OCM_DB(); $result = $db->search_replace_db($db->get_tables(), array('pairs' => $pairs)); $updates = isset($result['updates']) ? (int) $result['updates'] : 0; } finally { One_Click_Migration::disable_maintenance_mode(); $maintenance_on = false; } if (!empty($result['errors'])) { One_Click_Migration::write_to_log(sprintf('Error: Extra-domain rewrite failed for %d rows.', count($result['errors']))); wp_send_json_error(array( 'message' => 'Some addresses could not be updated. Please retry.', 'updates' => $updates, ), 500); return; } } } delete_option(OCM_Extra_Domains::OPTION); One_Click_Migration::write_to_log(sprintf('SYSLOG: Extra-domain rewrite updates "%d"', $updates)); wp_send_json_success(array('updates' => $updates)); } /** * Hosts were collected during the primary rewrite, so this does not scan the database again. * * @param array $found * @param string $source_siteurl * @param string $dest_siteurl */ private static function store_extra_domain_candidates($found, $source_siteurl, $dest_siteurl) { $report = OCM_Extra_Domains::finalize($found, $source_siteurl, $dest_siteurl); $candidates = isset($report['candidates']) && is_array($report['candidates']) ? $report['candidates'] : array(); if (!$candidates) { delete_option(OCM_Extra_Domains::OPTION); One_Click_Migration::write_to_log('SYSLOG: Extra domains awaiting confirmation "0"'); return; } update_option(OCM_Extra_Domains::OPTION, $candidates, false); One_Click_Migration::write_to_log(sprintf('SYSLOG: Extra domains awaiting confirmation "%d"', count($candidates))); if (!empty($report['omitted'])) { One_Click_Migration::write_to_log(sprintf('SYSLOG: Extra domains omitted from the checklist "%d"', (int) $report['omitted'])); } foreach ($candidates as $candidate) { One_Click_Migration::write_to_log(sprintf( 'SYSLOG: Extra domain host "%s" class "%s" count "%d" example "%s"', $candidate['host'], $candidate['class'], $candidate['count'], $candidate['example'] )); } } public static function delete_restore_options(){ delete_option('ocm_force_deactivated_plugins'); delete_option('ocm_restore_download_retry_db'); delete_option('ocm_restore_download_retry_themes'); delete_option('ocm_restore_download_retry_plugins'); delete_option('ocm_restore_download_retry_uploads'); delete_option('ocm_restore_decrypt_retry_db'); delete_option('ocm_restore_decrypt_retry_themes'); delete_option('ocm_restore_decrypt_retry_plugins'); delete_option('ocm_restore_decrypt_retry_uploads'); delete_option('ocm_restore_extract_retry_db'); delete_option('ocm_restore_extract_retry_themes'); delete_option('ocm_restore_extract_retry_plugins'); delete_option('ocm_restore_extract_retry_uploads'); delete_option('ocm_restore_child_delete_retry_db'); delete_option('ocm_restore_child_delete_retry_themes'); delete_option('ocm_restore_child_delete_retry_plugins'); delete_option('ocm_restore_child_delete_retry_uploads'); delete_option('ocm_excluded_folders'); delete_option('ocm_skipped_folders'); delete_option('ocm_eexcluded_folders'); delete_option('ocm_backup_missing_parts'); delete_option('ocm_backup_excluded_parts'); delete_option('ocm_backup_parts_known'); delete_option(self::OPTION_RESTORE_DB_PART_FAILED); delete_option(self::OPTION_BINARY_PREFLIGHT); delete_option(self::OPTION_RESTORE_PART_WARNINGS); delete_option(self::OPTION_RESTORE_PLUGINS_ISOLATED); delete_option(self::OPTION_RESTORE_WAS_WARNED); delete_option(self::OPTION_RESTORE_URL_HOSTS); // A run abandoned mid-import left its prefix; this run's dumps are new. delete_option(OCM_SQL_Utils::OPTION_STAGING_PREFIX); delete_option('ocm_restore_was_partial'); // A new run's log must show its own PHP errors, not skip ones the // previous run already logged. delete_option('ocm_error_log_dedupe'); delete_option('ocm_restore_partial_address_updates'); delete_option('ocm_restore_failed_address_pairs'); delete_option(OCM_SQL_Utils::OPTION_BACKUP_PLUGIN_VERSION); delete_option(OCM_Extra_Domains::OPTION); delete_option(OCM_Restore_Precheck::OPTION); delete_option(self::RESTORE_RUN_OPTION); } public static function delete_backup_options(){ // Clean up transient backup state from previous runs. delete_option('ocm_force_deactivated_plugins'); delete_option('ocm_error_log_dedupe'); delete_option('ocm_backup_compress_retry_db'); delete_option('ocm_backup_compress_retry_themes'); delete_option('ocm_backup_compress_retry_plugins'); delete_option('ocm_backup_compress_retry_uploads'); delete_option('ocm_backup_encrypt_retry_db'); delete_option('ocm_backup_encrypt_retry_themes'); delete_option('ocm_backup_encrypt_retry_plugins'); delete_option('ocm_backup_encrypt_retry_uploads'); delete_option('ocm_backup_upload_retry_db'); delete_option('ocm_backup_upload_retry_themes'); delete_option('ocm_backup_upload_retry_plugins'); delete_option('ocm_backup_upload_retry_uploads'); delete_option('ocm_multipart_upload_db'); delete_option('ocm_multipart_upload_themes'); delete_option('ocm_multipart_upload_plugins'); delete_option('ocm_multipart_upload_uploads'); delete_option('ocm_excluded_folders'); delete_option('ocm_skipped_folders'); delete_option('ocm_eexcluded_folders'); delete_option('ocm_restore_code_plain'); delete_option(self::BACKUP_UPLOADED_PARTS_OPTION); } public static function capture_failed_folder($excluded_restore_files, $key, $option){ $backup_active = is_array(get_option(self::BACKUP_RUN_OPTION, array())) && !empty(get_option(self::BACKUP_RUN_OPTION, array())['id']); if ('ocm_skipped_folders' === $option && $backup_active && !self::backup_context_owns_current_run()) { return; } if ('ocm_skipped_folders' === $option && self::was_backup_part_uploaded($key)) { return; } // Always merge with current storage. Callers often retain an array across // long compression/upload operations; writing that stale snapshot can // erase another part's status. $current = get_option($option, array()); $current = is_array($current) ? $current : array(); $excluded_restore_files = is_array($excluded_restore_files) ? $excluded_restore_files : array(); $excluded_restore_files = array_merge($current, $excluded_restore_files, array($key)); update_option($option, array_values(array_unique($excluded_restore_files))); } /** @var array Parts for which download_restore_file was invoked (tests). */ public static $restore_download_requests = array(); /** * Merge backup-log =excluded parts into ocm_excluded_folders so downloads, * DB import, and URL rewrite follow the same path as a selective exclude * in 3.1.18 (files-only: complete_restore, no search-replace). * * @return array Updated ocm_excluded_folders */ public static function apply_backup_excluded_parts_to_restore_exclusions() { $backup_excluded = get_option('ocm_backup_excluded_parts', array()); $backup_excluded = is_array($backup_excluded) ? $backup_excluded : array(); if ($backup_excluded === array()) { $current = get_option('ocm_excluded_folders', array()); return is_array($current) ? $current : array(); } $current = get_option('ocm_excluded_folders', array()); $current = is_array($current) ? $current : array(); $merged = array_values(array_unique(array_merge($current, $backup_excluded))); update_option('ocm_excluded_folders', $merged, true); return $merged; } /** * If the backup log marked this part =excluded, never request/download it. * Records it on ocm_excluded_folders and marks restore steps complete. * Includes db=excluded (files-only restore → complete_restore, no import). * * @param string $key * @param array $excluded_restore_files * @return bool True when the caller must continue (skip download). */ public static function maybe_skip_backup_excluded_restore_part($key, array &$excluded_restore_files) { $key = strtolower((string) $key); if ($key === '' || $key === 'log' || $key === 'log_download') { return false; } $backup_excluded = get_option('ocm_backup_excluded_parts', array()); $backup_excluded = is_array($backup_excluded) ? $backup_excluded : array(); if (!in_array($key, $backup_excluded, true)) { return false; } One_Click_Migration::write_to_log(sprintf( 'Notice: %s was excluded from this backup by the user; not downloading.', $key )); $excluded_restore_files[] = $key; $excluded_restore_files = array_values(array_unique($excluded_restore_files)); update_option('ocm_excluded_folders', $excluded_restore_files, true); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); return true; } /** * Real download-path entry used by the restore loop (and tests): skip * backup-excluded parts without requesting; otherwise record a request and * optionally run the Not Found handler. * * @param string $key * @param bool $simulate_404 * @return string 'skipped_excluded'|'continue'|'fail_restore'|'requested' */ public static function process_restore_part_download_decision($key, $simulate_404 = false) { $key = strtolower((string) $key); $excluded_restore_files = get_option('ocm_excluded_folders', array()); $excluded_restore_files = is_array($excluded_restore_files) ? $excluded_restore_files : array(); if (in_array($key, $excluded_restore_files, true)) { return 'skipped_excluded'; } if (self::maybe_skip_backup_excluded_restore_part($key, $excluded_restore_files)) { return 'skipped_excluded'; } self::$restore_download_requests[] = $key; if (!$simulate_404) { return 'requested'; } $eexcluded_restore_files = get_option('ocm_eexcluded_folders', array()); $eexcluded_restore_files = is_array($eexcluded_restore_files) ? $eexcluded_restore_files : array(); return self::handle_restore_part_download_not_found( $key, $key . '.zip.crypt', $excluded_restore_files, $eexcluded_restore_files ); } /** * Handle a Not Found download for one restore part. * - Backup =excluded (incl. db=excluded): no warning, treat as selective exclude. * - Included / skipped db, or pre-manifest missing db: DB-part stop. * - Included non-db 404 / pre-manifest missing non-db: warn skipped (not found). * * @param string $key * @param string $keyZipCrypt * @param array $excluded_restore_files * @param array $eexcluded_restore_files * @return string 'continue' or 'fail_restore' (db stop never returns) */ public static function handle_restore_part_download_not_found( $key, $keyZipCrypt, array &$excluded_restore_files, array &$eexcluded_restore_files ) { $key = (string) $key; $backup_missing_parts = get_option('ocm_backup_missing_parts', []); $backup_missing_parts = is_array($backup_missing_parts) ? $backup_missing_parts : []; $backup_excluded_parts = get_option('ocm_backup_excluded_parts', []); $backup_excluded_parts = is_array($backup_excluded_parts) ? $backup_excluded_parts : []; $excluded_at_backup = in_array($key, $backup_excluded_parts, true); $skipped_at_backup = in_array($key, $backup_missing_parts, true); // Intentional backup exclusion (files-only db, or non-db): never warn. if ($excluded_at_backup) { One_Click_Migration::write_to_log("Notice: $key was excluded from this backup by the user; continuing without it."); $excluded_restore_files[] = $key; $excluded_restore_files = array_values(array_unique($excluded_restore_files)); update_option('ocm_excluded_folders', $excluded_restore_files, true); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); return 'continue'; } // Meant-to-be-included db missing (skipped-at-backup, included 404, or // pre-manifest with no BACKUP_PARTS list): stop before file replace. if ($key === 'db') { update_option(self::OPTION_RESTORE_DB_PART_FAILED, 1, true); self::stop_restore_after_db_part_failure(); } if ($skipped_at_backup) { One_Click_Migration::write_to_log("Notice: $key was not included in this backup (skipped while the backup was created); continuing without it."); self::record_restore_part_warning($key, 'not in backup'); } elseif (get_option('ocm_backup_parts_known')) { return 'fail_restore'; } else { // No BACKUP_PARTS line: cannot tell excluded from missing → warn. One_Click_Migration::write_to_log("Notice: file $keyZipCrypt not found"); self::record_restore_part_warning($key, 'not found'); } $excluded_restore_files[] = $key; $excluded_restore_files = array_values(array_unique($excluded_restore_files)); update_option('ocm_excluded_folders', $excluded_restore_files, true); self::capture_failed_folder($eexcluded_restore_files, $key, 'ocm_eexcluded_folders'); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DOWNLOAD); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DECRYPT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_EXTRACT); self::set_complete_restore_step($key, self::STEP_RESTORE_CHILD_DELETE_OLD_AND_MOVE_NEW); return 'continue'; } public static function mark_backup_part_uploaded($part) { if (!self::backup_context_owns_current_run()) { return false; } $run = get_option(self::BACKUP_RUN_OPTION, array()); if (!is_array($run) || empty($run['id']) || empty($run['bucket_id'])) { return false; } $state = get_option(self::BACKUP_UPLOADED_PARTS_OPTION, array()); if (!is_array($state) || !isset($state['run_id'], $state['bucket_id']) || !hash_equals((string) $run['id'], (string) $state['run_id']) || !hash_equals((string) $run['bucket_id'], (string) $state['bucket_id']) ) { $state = array( 'run_id' => (string) $run['id'], 'bucket_id' => (string) $run['bucket_id'], 'parts' => array(), ); } $parts = isset($state['parts']) && is_array($state['parts']) ? $state['parts'] : array(); $parts[] = strtolower((string) $part); $state['parts'] = array_values(array_unique($parts)); return update_option(self::BACKUP_UPLOADED_PARTS_OPTION, $state, false); } public static function was_backup_part_uploaded($part) { $run = get_option(self::BACKUP_RUN_OPTION, array()); $state = get_option(self::BACKUP_UPLOADED_PARTS_OPTION, array()); if (!is_array($run) || !is_array($state) || empty($run['id']) || empty($run['bucket_id']) || empty($state['run_id']) || empty($state['bucket_id']) || !hash_equals((string) $run['id'], (string) $state['run_id']) || !hash_equals((string) $run['bucket_id'], (string) $state['bucket_id']) ) { return false; } $parts = isset($state['parts']) && is_array($state['parts']) ? $state['parts'] : array(); return in_array(strtolower((string) $part), $parts, true); } public static function print_folders_not_found(){ $not_found_folders = get_option("ocm_eexcluded_folders", []); foreach ($not_found_folders as $key => $not_found_folder) { // One_Click_Migration::write_to_log("Notice: File " . $not_found_folder. ".zip.crypt was not found on the remote server. Please try to back it up again."); } } public static function print_folders_skipped(){ $skipped_folders = get_option("ocm_skipped_folders", []); foreach ($skipped_folders as $key => $skipped_folder) { One_Click_Migration::write_to_log("File $skipped_folder was skipped due to timeout"); } return true; } /** * Parts the just-finished backup does not contain (skipped after retry * exhaustion). Normalized, deduped, sorted. * * @return string[] */ public static function get_backup_missing_parts() { $skipped = get_option('ocm_skipped_folders', []); $excluded = get_option('ocm_excluded_folders', array()); $excluded = is_array($excluded) ? $excluded : array(); $missing = array(); foreach ((is_array($skipped) ? $skipped : array()) as $part) { if (is_string($part) && $part !== '' && !self::was_backup_part_uploaded($part)) { $missing[] = strtolower($part); } } foreach (array('db', 'themes', 'plugins', 'uploads') as $part) { if (!in_array($part, $excluded, true) && !self::was_backup_part_uploaded($part) && !self::check_is_complete_backup_step($part, self::STEP_BACKUP_CHILD_UPLOAD) ) { $missing[] = $part; } } $missing = array_values(array_unique($missing)); sort($missing); return $missing; } /** Append bounded manifest records once; the final log write mirrors them to S3. */ private static function publish_url_manifest($found) { if (!self::backup_context_owns_current_run() || !One_Click_Migration::ensure_private_log_directory()) { return; } $run = get_option(self::BACKUP_RUN_OPTION, array()); $bucket = $run['bucket_id'] ?? ''; if (!$bucket) { return; } $lines = OCM_URL_Manifest::lines($found, site_url(), $bucket); if (!$lines) { One_Click_Migration::write_to_log('Notice: Backup address inventory unavailable; addresses can be reviewed after restore.'); return; } $text = ''; foreach ($lines as $line) { $text .= date('m/d/YTH:i:sa') . ' - ' . $line . PHP_EOL; } if (@file_put_contents(OCM_DEBUG_LOG_FILE, $text, FILE_APPEND | LOCK_EX) !== strlen($text)) { One_Click_Migration::write_to_log('Notice: Backup address inventory could not be saved; addresses can be reviewed after restore.'); return; } One_Click_Migration::write_to_log('SYSLOG: Backup address inventory saved.'); } /** * Write the per-part status marker into the log. write_to_log mirrors the * whole log to S3, so the restore side can read this back before it starts * downloading (see One_Click_Migration::extract_missing_parts_from_backup_log). */ public static function log_backup_parts_manifest($missing_parts) { if (!self::backup_context_owns_current_run()) { return; } $run = get_option(self::BACKUP_RUN_OPTION, array()); $bucket_id = is_array($run) && isset($run['bucket_id']) ? (string) $run['bucket_id'] : ''; if ('' === $bucket_id) { return; } $excluded = get_option('ocm_excluded_folders', []); $excluded = is_array($excluded) ? $excluded : array(); $missing_parts = is_array($missing_parts) ? $missing_parts : array(); $pairs = array(); foreach (array('db', 'themes', 'plugins', 'uploads') as $part) { if (in_array($part, $excluded, true)) { $pairs[] = $part . '=excluded'; } elseif (in_array($part, $missing_parts, true)) { $pairs[] = $part . '=skipped'; } else { $pairs[] = $part . '=ok'; } } One_Click_Migration::write_to_log(' SYSLOG: BACKUP_PARTS bucket=' . $bucket_id . ' ' . implode(' ', $pairs)); } /** * Convert memory limit string to bytes. * @param string $val Memory limit string (e.g., "512M") * @return int */ private static function convert_to_bytes($val) { $val = trim($val); $last = strtolower($val[strlen($val)-1]); $val = (int) $val; switch($last) { case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } return $val; } /** * Format bytes to human readable format. * @param int $bytes * @return string */ private static function format_bytes($bytes) { $units = ['B', 'KB', 'MB', 'GB']; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= (1 << (10 * $pow)); return round($bytes, 2) . ' ' . $units[$pow]; } }