store = new JobStore(); } /** * Start a new backup job. * * @param string $name Backup name; empty for the default site name and date. * @param array $options See sanitize_options(). * @return int|\WP_Error Job ID. */ public function start_backup( $name = '', array $options = array() ) { $busy = $this->ensure_idle(); if ( is_wp_error( $busy ) ) { return $busy; } $dir = PathGuard::backups_dir(); if ( is_wp_error( $dir ) ) { return $dir; } $file = BackupStore::unique_filename( $name ); if ( is_wp_error( $file ) ) { return $file; } $opts = self::sanitize_options( $options ); if ( ! $opts['expect_files'] ) { // The page had no sizes yet: measure here (cached for 15 minutes). $opts['expect_files'] = SiteInfo::expected_files( $opts ); } $tmp = PathGuard::temp_dir( 'backup' ); if ( is_wp_error( $tmp ) ) { return $tmp; } $job_id = $this->store->create( 'backup', array( 'file' => $file, 'part' => $dir . $file . '.part', 'dir' => $tmp, 'stage' => 'database', 'progress' => 1, 'zip' => array(), 'options' => $opts, ) ); if ( ! $job_id ) { PathGuard::delete_dir( untrailingslashit( $tmp ) ); return new \WP_Error( 'sitezip_job', __( 'Could not create the backup job.', 'sitezip' ) ); } $this->store->log( $job_id, sprintf( /* translators: %s: backup file name. */ __( 'Backup started: %s', 'sitezip' ), $file ) ); $this->pick_driver( $job_id ); $this->kick( $job_id ); return $job_id; } /** * Boolean backup options, all off by default (everything included). */ const OPTION_KEYS = array( 'skip_media', 'skip_themes', 'active_theme_only', 'skip_plugins', 'active_plugins_only', 'skip_mu_plugins', 'skip_other', 'skip_revisions', 'skip_spam', 'skip_transients' ); /** * Keep only known options. * * @param array $options Raw options. * @return array */ public static function sanitize_options( array $options ) { $clean = array(); foreach ( self::OPTION_KEYS as $key ) { $clean[ $key ] = ! empty( $options[ $key ] ); } // Uncompressed bytes of files the UI expects, for a real percentage. $clean['expect_files'] = isset( $options['expect_files'] ) ? max( 0, (int) $options['expect_files'] ) : 0; return $clean; } /** * Start a restore job. * * @param string $file Backup file name in the backups folder. * @return int|\WP_Error Job ID. */ public function start_restore( $file, &$resume_key = '' ) { if ( ! class_exists( '\ZipArchive' ) ) { return new \WP_Error( 'sitezip_no_zip', __( 'The ZipArchive PHP extension is required to restore backups.', 'sitezip' ) ); } $busy = $this->ensure_idle(); if ( is_wp_error( $busy ) ) { return $busy; } $path = BackupStore::path( $file ); if ( is_wp_error( $path ) ) { return $path; } // A restore replaces the users and sessions tables, which logs the // administrator out. This one-time key lets the page that started // the restore sign them back in afterwards (see ResumeLogin). $resume_key = bin2hex( random_bytes( 32 ) ); $user = wp_get_current_user(); $state = RestoreRunner::initial_state( $file ); $state['resume'] = array( 'hash' => hash( 'sha256', $resume_key ), 'login' => $user->user_login, 'email' => $user->user_email, 'expires' => time() + 2 * HOUR_IN_SECONDS, ); $job_id = $this->store->create( 'restore', $state ); if ( ! $job_id ) { return new \WP_Error( 'sitezip_job', __( 'Could not create the restore job.', 'sitezip' ) ); } $this->store->log( $job_id, sprintf( /* translators: %s: backup file name. */ __( 'Restore started from %s', 'sitezip' ), $file ) ); $this->pick_driver( $job_id ); $this->kick( $job_id ); return $job_id; } /** * Cancel the running job. * * @return bool */ public function cancel() { $job = $this->store->get_running(); if ( ! $job ) { return false; } $this->store->update( (int) $job['id'], array( 'status' => 'cancelled' ) ); $this->cleanup( $job ); $this->store->log( (int) $job['id'], __( 'Cancelled.', 'sitezip' ) ); return true; } /** * Run one slice of a job. * * @param int $job_id Job ID. * @return void */ public function run( $job_id ) { $job = $this->store->get( $job_id ); if ( ! $job || 'running' !== $job['status'] ) { return; } $lock = 'sitezip_lock_' . (int) $job_id; if ( ! $this->acquire( $lock ) ) { return; } // If PHP dies mid-slice (fatal error, memory), the finally block below // never runs: record why, and free the lock so the job can resume. self::$current = (int) $job_id; $finished = false; register_shutdown_function( function () use ( $job_id, $lock, &$finished ) { if ( $finished ) { return; } delete_option( $lock ); $error = error_get_last(); $trace = get_option( 'sitezip_trace' ); $where = is_array( $trace ) && isset( $trace['msg'] ) ? ' [' . $trace['msg'] . ']' : ''; if ( $error && in_array( $error['type'], array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR ), true ) ) { $this->fail( $job_id, sprintf( 'PHP error: %s (%s:%d)%s', $error['message'], wp_basename( $error['file'] ), $error['line'], $where ) ); } else { $this->store->log( $job_id, 'A step stopped unexpectedly' . $where ); } } ); self::trace( 'slice started' ); // Read the host's limit before lifting it, or deadline() sees 0. if ( null === self::$time_limit ) { self::$time_limit = (int) ini_get( 'max_execution_time' ); } // A slice may still be killed mid-flight; the cursor in the job row // is always the truth, so the next request simply resumes. @set_time_limit( 0 ); // phpcs:ignore Squiz.PHP.DiscouragedFunctions.Discouraged -- long backups must not stop at the default 30 s limit. @ignore_user_abort( true ); $more = false; try { $result = ( 'restore' === $job['type'] ) ? ( new RestoreRunner( $this->store ) )->advance( $job, $this->deadline() ) : $this->advance( $job ); if ( is_wp_error( $result ) ) { $this->fail( $job_id, $result->get_error_message() ); } elseif ( true === $result ) { $this->finish( $job_id ); } else { $more = true; } } catch ( \Throwable $e ) { $this->fail( $job_id, $e->getMessage() ); } finally { $finished = true; delete_option( $lock ); } // Release the lock first: the next slice starts within milliseconds // and would otherwise find it still held and give up. if ( $more ) { $this->kick( $job_id ); } } /** * Keep a running job moving from the progress poll. * * When the chain of slices has stalled, the site cannot reach itself: * staging sites behind a password prompt (BlogVault, WP Remote, many * hosts' staging), firewalls and some containers all reject loopbacks, * and WP-Cron is triggered by a loopback too. The poll comes from the * administrator's browser, which gets through, so from then on the poll * runs each slice itself while the page is open. * * @param array $job Job row. * @return bool Whether a slice ran in this request. */ public function nudge( array $job ) { $job_id = (int) $job['id']; $held = (int) get_option( 'sitezip_lock_' . $job_id, 0 ); if ( 'running' !== $job['status'] || ( $held && ( time() - $held ) <= self::LOCK_STALE ) ) { return false; } if ( ! self::is_browser_driven( $job_id ) ) { $updated = strtotime( $job['updated_at'] . ' UTC' ); if ( ! $updated || ( time() - $updated ) <= self::STALL_SECONDS ) { return false; } set_transient( 'sitezip_inline_' . $job_id, 1, DAY_IN_SECONDS ); $this->store->log( $job_id, __( 'The server could not reach itself in the background, so this page is moving the job along. Keep it open until it finishes.', 'sitezip' ) ); } $this->run( $job_id ); return true; } /** * Whether the progress poll is running this job's slices. * * @param int $job_id Job ID. * @return bool */ public static function is_browser_driven( $job_id ) { return (bool) get_transient( 'sitezip_inline_' . (int) $job_id ); } /** * Remember the last thing the running slice did, so a slice that dies * without an error still shows where it stopped (see diagnostics). * * @param string $msg What is happening now. * @return void */ public static function trace( $msg ) { update_option( 'sitezip_trace', array( 'job' => self::$current, 'time' => gmdate( 'H:i:s' ), 'msg' => $msg, 'memory' => size_format( memory_get_usage( true ) ), ), false ); } /** * Advance a backup job. * * @param array $job Job row. * @return bool|\WP_Error True when the whole job is finished. */ private function advance( array $job ) { $state = $job['state']; $job_id = (int) $job['id']; $deadline = $this->deadline(); $dir = PathGuard::backups_dir(); if ( is_wp_error( $dir ) ) { return $dir; } // The archive was already finalised and renamed by a request that // died before it could record that. if ( 'finalize' === $state['stage'] && ! file_exists( $state['part'] ) && file_exists( $dir . $state['file'] ) ) { $state['stage'] = 'checksum'; } if ( 'checksum' === $state['stage'] ) { return $this->checksum( $job_id, $state, $dir, $deadline ); } self::trace( 'opening archive' ); $writer = new ZipWriter( $state['part'], $state['dir'] . 'central-directory.bin' ); $opened = $writer->open( $state['zip'] ); if ( is_wp_error( $opened ) ) { return $opened; } while ( microtime( true ) < $deadline ) { switch ( $state['stage'] ) { case 'database': $done = ( new DatabaseSource( $state['dir'], isset( $state['options'] ) ? $state['options'] : array() ) )->step( $state, $writer, $deadline ); if ( is_wp_error( $done ) ) { $writer->close(); return $done; } if ( isset( $state['db'] ) ) { $state['progress'] = 1 + (int) ( 19 * $state['db']['ti'] / max( 1, count( $state['db']['tables'] ) ) ); } if ( true === $done ) { $state['stage'] = 'files'; $state['progress'] = 20; $this->store->log( $job_id, sprintf( /* translators: %s: number of database rows. */ __( 'Database exported (%s rows).', 'sitezip' ), number_format_i18n( (int) $state['db_rows'] ) ) ); } break; case 'files': self::trace( 'files: ' . ( isset( $state['files']['count'] ) ? (int) $state['files']['count'] : 0 ) . ' done' ); // Short steps, so progress is saved every couple of seconds. $done = ( new FilesSource( $dir, isset( $state['options'] ) ? $state['options'] : array() ) )->step( $state, $writer, min( $deadline, microtime( true ) + 2 ) ); if ( is_wp_error( $done ) ) { $writer->close(); return $done; } $expect = isset( $state['options']['expect_files'] ) ? (int) $state['options']['expect_files'] : 0; if ( $expect > 0 && isset( $state['files']['bytes'] ) ) { $state['progress'] = 20 + (int) floor( 75 * min( 1, $state['files']['bytes'] / $expect ) ); } if ( true === $done ) { $state['stage'] = 'finalize'; $state['progress'] = 95; $this->store->log( $job_id, sprintf( /* translators: 1: number of files, 2: human-readable size. */ __( 'Files archived: %1$s files, %2$s.', 'sitezip' ), number_format_i18n( (int) $state['file_count'] ), size_format( (int) $state['file_bytes'] ) ) ); $labels = array( 'skip_media' => __( 'media library', 'sitezip' ), 'skip_themes' => __( 'themes', 'sitezip' ), 'active_theme_only' => __( 'inactive themes', 'sitezip' ), 'skip_plugins' => __( 'plugins', 'sitezip' ), 'active_plugins_only' => __( 'inactive plugins', 'sitezip' ), 'skip_mu_plugins' => __( 'must-use plugins', 'sitezip' ), 'skip_other' => __( 'other wp-content files', 'sitezip' ), 'skip_revisions' => __( 'post revisions', 'sitezip' ), 'skip_spam' => __( 'spam and trashed comments', 'sitezip' ), 'skip_transients' => __( 'transients', 'sitezip' ), ); $left = array(); foreach ( $labels as $key => $label ) { if ( ! empty( $state['options'][ $key ] ) ) { $left[] = $label; } } if ( $left ) { /* translators: %s: comma-separated list of what was left out. */ $this->store->log( $job_id, sprintf( __( 'Left out, as requested: %s.', 'sitezip' ), implode( ', ', $left ) ) ); } } break; case 'finalize': self::trace( 'finalizing archive' ); $added = $writer->add_string( BackupStore::MANIFEST, wp_json_encode( $this->manifest( $state ), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) ); if ( is_wp_error( $added ) ) { $writer->close(); return $added; } $finished = $writer->finish(); if ( is_wp_error( $finished ) ) { $writer->close(); return $finished; } if ( ! @rename( $state['part'], $dir . $state['file'] ) ) { return new \WP_Error( 'sitezip_rename', __( 'Could not save the finished backup file.', 'sitezip' ) ); } $state['stage'] = 'checksum'; $state['progress'] = 97; $this->save( $job_id, $state ); return $this->checksum( $job_id, $state, $dir, $deadline ); } $state['zip'] = $writer->cursor(); $this->save( $job_id, $state ); } $state['zip'] = $writer->cursor(); $writer->close(); $this->save( $job_id, $state ); return false; } /** * Fingerprint the finished archive so a restore can refuse a file that * was damaged in transfer or changed since. * * The archive is already complete, so a failure here only means the * backup has no fingerprint; it never fails the backup. * * @param int $job_id Job ID. * @param array $state Job state. * @param string $dir Backups folder. * @param float $deadline Stop-by timestamp. * @return bool True when finished. */ private function checksum( $job_id, array $state, $dir, $deadline ) { self::trace( 'fingerprinting archive' ); $path = $dir . $state['file']; $sum = isset( $state['sum'] ) ? $state['sum'] : array(); $done = BackupStore::hash_step( $path, $sum, $deadline ); if ( is_wp_error( $done ) ) { $this->store->log( $job_id, __( 'Could not fingerprint the backup: ', 'sitezip' ) . $done->get_error_message() ); return true; } $state['sum'] = $sum; $this->save( $job_id, $state ); if ( true !== $done ) { return false; } if ( false === @file_put_contents( BackupStore::sum_path( $path ), $sum['chain'] ) ) { $this->store->log( $job_id, __( 'Could not save the backup fingerprint.', 'sitezip' ) ); } else { $this->store->log( $job_id, __( 'Fingerprint saved; a restore will check the file against it.', 'sitezip' ) ); } return true; } /** * The manifest stored inside every archive. * * @param array $state Job state. * @return array */ private function manifest( array $state ) { global $wp_version, $wpdb; return array( 'format' => BackupStore::FORMAT, 'plugin' => SITEZIP_VERSION, 'name' => preg_replace( '/\.zip$/i', '', $state['file'] ), 'created' => gmdate( 'c' ), 'site_url' => site_url(), 'home_url' => home_url(), 'content_dir' => WP_CONTENT_DIR, 'wp_version' => $wp_version, 'php_version' => PHP_VERSION, 'prefix' => $wpdb->base_prefix, 'charset' => $wpdb->charset, 'multisite' => is_multisite(), 'db_rows' => isset( $state['db_rows'] ) ? (int) $state['db_rows'] : 0, 'file_count' => isset( $state['file_count'] ) ? (int) $state['file_count'] : 0, 'file_bytes' => isset( $state['file_bytes'] ) ? (int) $state['file_bytes'] : 0, 'options' => isset( $state['options'] ) ? $state['options'] : array(), ); } /** * Mark a job complete and clean up after it. * * @param int $job_id Job ID. * @return void */ private function finish( $job_id ) { $job = $this->store->get( $job_id ); if ( ! $job ) { return; } PathGuard::delete_dir( untrailingslashit( (string) $job['state']['dir'] ) ); wp_clear_scheduled_hook( 'sitezip_run_job', array( (int) $job_id ) ); delete_transient( 'sitezip_inline_' . (int) $job_id ); if ( 'restore' === $job['type'] ) { $this->check_site( $job_id, isset( $job['state']['dropins_skipped'] ) ? (array) $job['state']['dropins_skipped'] : array() ); } $this->store->update( $job_id, array( 'status' => 'completed', 'stage' => 'done', 'progress' => 100, ) ); $this->store->log( $job_id, 'restore' === $job['type'] ? __( 'Restore complete.', 'sitezip' ) : sprintf( /* translators: %s: backup file name. */ __( 'Backup complete: %s', 'sitezip' ), $job['state']['file'] ) ); $this->store->prune(); } /** * After a restore, make sure the site loads and log what was done. * * @param int $job_id Job ID. * @param array $skipped Drop-ins the restore did not copy. * @return void */ private function check_site( $job_id, array $skipped ) { $report = \SiteZip\Restore\HealthCheck::run( $skipped ); foreach ( $skipped as $name ) { /* translators: %s: file name. */ $this->store->log( $job_id, sprintf( __( 'Not copied (belongs to the old server): %s', 'sitezip' ), $name ) ); } foreach ( $report['disabled'] as $name ) { /* translators: %s: file name. */ $this->store->log( $job_id, sprintf( __( 'Switched off because the site failed to load: %s', 'sitezip' ), $name ) ); } $lines = array( 'ok' => __( 'Site check: the home page loads normally.', 'sitezip' ), 'fixed' => __( 'Site check: the home page loads normally now.', 'sitezip' ), 'broken' => __( 'Site check: the home page still shows an error: ', 'sitezip' ) . $report['problem'], 'unknown' => __( 'Site check: could not load the home page to check it.', 'sitezip' ), ); $this->store->log( $job_id, $lines[ $report['status'] ] ); } /** * Record a failure and release everything the job was holding. * * @param int $job_id Job ID. * @param string $message Error message. * @return void */ private function fail( $job_id, $message ) { $job = $this->store->get( $job_id ); // A cancelled job's last slice may still error out; keep "cancelled". if ( ! $job || 'running' !== $job['status'] ) { return; } $this->store->update( $job_id, array( 'status' => 'failed', 'error' => wp_strip_all_tags( $message ), ) ); $this->cleanup( $job ); $this->store->log( $job_id, __( 'Failed: ', 'sitezip' ) . wp_strip_all_tags( $message ) ); } /** * Delete a stopped job's temporary files, partial archive and tables. * * @param array $job Job row. * @return void */ private function cleanup( array $job ) { $state = $job['state']; wp_clear_scheduled_hook( 'sitezip_run_job', array( (int) $job['id'] ) ); delete_transient( 'sitezip_inline_' . (int) $job['id'] ); if ( ! empty( $state['dir'] ) ) { PathGuard::delete_dir( untrailingslashit( $state['dir'] ) ); } if ( 'restore' === $job['type'] ) { RestoreRunner::drop_temp_tables(); return; } $dir = PathGuard::backups_dir(); if ( ! is_wp_error( $dir ) && ! empty( $state['part'] ) && dirname( $state['part'] ) . '/' === $dir && '.part' === substr( $state['part'], -5 ) ) { @unlink( $state['part'] ); } } /** * Refuse to start while another job is running. * * @return true|\WP_Error */ private function ensure_idle() { $this->store->reap(); if ( $this->store->get_running() ) { return new \WP_Error( 'sitezip_busy', __( 'A backup or restore is already running.', 'sitezip' ), array( 'status' => 409 ) ); } return true; } /** * Persist state and progress. * * @param int $job_id Job ID. * @param array $state Job state. * @return void */ private function save( $job_id, array $state ) { $this->store->update( $job_id, array( 'stage' => $state['stage'], 'progress' => isset( $state['progress'] ) ? min( 99, (int) $state['progress'] ) : 0, 'state' => $state, ) ); } /** * When this slice must stop. * * @return float */ private function deadline() { $limit = null === self::$time_limit ? (int) ini_get( 'max_execution_time' ) : self::$time_limit; $budget = $limit <= 0 ? self::MAX_SLICE : max( 5, min( self::MAX_SLICE, (int) floor( $limit * self::TIME_FRACTION ) ) ); return microtime( true ) + $budget; } /** * Take an exclusive lock for a job. * * add_option() is atomic thanks to the unique index on option_name, so * a loopback and a cron firing together cannot both enter a slice and * corrupt the shared cursor. * * @param string $lock Option name. * @return bool */ private function acquire( $lock ) { if ( add_option( $lock, time(), '', false ) ) { return true; } // Break a lock left behind by a killed process. $held = (int) get_option( $lock, 0 ); if ( $held && ( time() - $held ) > self::LOCK_STALE ) { delete_option( $lock ); return (bool) add_option( $lock, time(), '', false ); } return false; } /** * Whether this site can send itself a background request. * * Checked once when a job starts (cached for an hour), so a server that * blocks loopbacks hands the job to the open page at once instead of * after a stall. * * @return bool */ public static function loopback_ok() { $cached = get_transient( 'sitezip_loopback' ); if ( 'yes' === $cached || 'no' === $cached ) { return 'yes' === $cached; } $response = wp_remote_post( admin_url( 'admin-ajax.php' ), array( 'timeout' => 5, 'sslverify' => false, 'headers' => self::loopback_headers(), 'body' => array( 'action' => 'sitezip_ping' ), ) ); $ok = ! is_wp_error( $response ) && 200 === (int) wp_remote_retrieve_response_code( $response ) && 'sitezip-pong' === trim( (string) wp_remote_retrieve_body( $response ) ); set_transient( 'sitezip_loopback', $ok ? 'yes' : 'no', $ok ? HOUR_IN_SECONDS : 10 * MINUTE_IN_SECONDS ); return $ok; } /** * Hand a new job to the open page when background requests cannot work. * * @param int $job_id Job ID. * @return void */ private function pick_driver( $job_id ) { if ( self::loopback_ok() ) { return; } set_transient( 'sitezip_inline_' . (int) $job_id, 1, DAY_IN_SECONDS ); $this->store->log( $job_id, __( 'This server blocks background requests, so this page will run the job. Keep it open until it finishes.', 'sitezip' ) ); } /** * Headers that let a loopback through a password-protected staging site. * * The browser sends the staging site's HTTP auth with every request, so * the request that starts or continues a job can pass it on, the same * way core's Site Health loopback test does. * * @return array */ public static function loopback_headers() { $headers = array( 'Cache-Control' => 'no-cache' ); // phpcs:disable WordPress.Security.ValidatedSanitizedInput -- credentials are forwarded verbatim to this same site, never output. if ( isset( $_SERVER['PHP_AUTH_USER'] ) ) { $pass = isset( $_SERVER['PHP_AUTH_PW'] ) ? wp_unslash( $_SERVER['PHP_AUTH_PW'] ) : ''; $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . $pass ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- HTTP Basic auth. } elseif ( ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) { $headers['Authorization'] = wp_unslash( $_SERVER['HTTP_AUTHORIZATION'] ); } elseif ( ! empty( $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ) ) { $headers['Authorization'] = wp_unslash( $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] ); } // phpcs:enable WordPress.Security.ValidatedSanitizedInput return $headers; } /** * Trigger the next slice. * * @param int $job_id Job ID. * @return void */ private function kick( $job_id ) { $job = $this->store->get( $job_id ); if ( ! $job ) { return; } // Cron is the safety net for hosts where loopbacks are blocked. if ( ! wp_next_scheduled( 'sitezip_run_job', array( (int) $job_id ) ) ) { wp_schedule_single_event( time() + 120, 'sitezip_run_job', array( (int) $job_id ) ); } wp_remote_post( admin_url( 'admin-ajax.php' ), array( 'timeout' => 0.01, 'blocking' => false, // Matches core's own loopback behaviour; the request never // leaves this host, and it carries only a job token. 'sslverify' => false, 'headers' => self::loopback_headers(), 'body' => array( 'action' => 'sitezip_run', 'job' => (int) $job_id, 'token' => $job['runner_token'], ), ) ); } }