$cursor Cursor. * @param array $components Selected components. * @param float $deadline Soft deadline. * @return array{done:bool,cursor:array,error?:string} */ public function tick( int $job_id, string $job_dir, array $cursor, array $components, float $deadline ): array { $snap = self::dir( $job_dir ); if ( empty( $cursor['snapshot_dir'] ) ) { if ( ! wp_mkdir_p( $snap ) ) { return array( 'done' => false, 'cursor' => $cursor, 'error' => __( 'Unable to create snapshot directory.', '007aj-backup-restore-verifier' ), ); } Working_Dir::harden( $snap ); $cursor['snapshot_dir'] = $snap; $cursor['snapshot_phase'] = 'files'; Logger::info( $job_id, 'Snapshot: directory ready.' ); } $phase = (string) ( $cursor['snapshot_phase'] ?? 'files' ); if ( 'files' === $phase ) { $result = $this->snapshot_files( $job_id, $cursor, $components, $deadline ); if ( ! empty( $result['error'] ) ) { return $result; } $cursor = $result['cursor']; if ( empty( $result['done'] ) ) { return array( 'done' => false, 'cursor' => $cursor, ); } $cursor['snapshot_phase'] = 'database'; } if ( 'database' === (string) ( $cursor['snapshot_phase'] ?? '' ) ) { if ( empty( $components['database'] ) ) { $cursor['snapshot_phase'] = 'done'; Logger::info( $job_id, 'Snapshot: skipped database (not selected).' ); return array( 'done' => true, 'cursor' => $cursor, ); } $result = $this->snapshot_database( $job_id, $cursor, $deadline ); if ( ! empty( $result['error'] ) ) { return $result; } $cursor = $result['cursor']; if ( empty( $result['done'] ) ) { return array( 'done' => false, 'cursor' => $cursor, ); } $cursor['snapshot_phase'] = 'done'; } Logger::info( $job_id, 'Snapshot completed.' ); return array( 'done' => true, 'cursor' => $cursor, ); } /** * Copy selected live files into snapshot/files/ (discovery + copy, both chunked). * * Large state: state/snap-dirs.ndjson and state/snap-files.ndjson. Cursor key * "sn" holds the walker state, the list offset and the byte offset inside a * large file being copied. * * @param int $job_id Job id. * @param array $cursor Cursor. * @param array $components Components. * @param float $deadline Deadline. * @return array{done:bool,cursor:array,error?:string} */ private function snapshot_files( int $job_id, array $cursor, array $components, float $deadline ): array { $snap = (string) $cursor['snapshot_dir']; $file_root = trailingslashit( $snap ) . 'files'; if ( ! is_dir( $file_root ) ) { wp_mkdir_p( $file_root ); } $state_dir = State_Files::dir( (string) ( $cursor['job_dir'] ?? dirname( $snap ) ) ); $sn = isset( $cursor['sn'] ) && is_array( $cursor['sn'] ) ? $cursor['sn'] : array(); unset( $cursor['snapshot_file_list'] ); if ( empty( $sn['roots'] ) ) { $sn = array( 'roots' => File_Walker::component_roots( $components ), 'w' => array(), 'o' => 0, 'b' => 0, 'n' => 0, ); } if ( empty( $sn['w']['done'] ) ) { $walker = new File_Walker( $sn['roots'], $state_dir . '/snap-dirs.ndjson', $state_dir . '/snap-files.ndjson', Working_Dir::default_exclusions() ); $sn['w'] = $walker->step( (array) $sn['w'], $deadline ); if ( empty( $sn['w']['done'] ) ) { $cursor['sn'] = $sn; return array( 'done' => false, 'cursor' => $cursor, ); } Logger::info( $job_id, sprintf( 'Snapshot: queuing %d live file(s) (%s).', (int) $sn['w']['files'], size_format( (int) $sn['w']['bytes'] ) ) ); } $done = false; while ( microtime( true ) < $deadline ) { $batch = State_Files::read_rows( $state_dir . '/snap-files.ndjson', (int) $sn['o'], 200 ); if ( empty( $batch['rows'] ) ) { $done = true; break; } foreach ( $batch['rows'] as $k => $row ) { $root = $sn['roots'][ (int) ( $row['r'] ?? -1 ) ] ?? null; $p = (string) ( $row['p'] ?? '' ); if ( null !== $root && '' !== $p && Path_Guard::is_safe_relative( $p ) ) { $src = (string) $root['abs'] . '/' . $p; $dst = $file_root . '/' . (string) $root['rel'] . '/' . $p; if ( is_file( $src ) && ! is_link( $src ) ) { $copied = self::copy_slice( $src, $dst, (int) $sn['b'], $deadline ); if ( $copied >= 0 ) { $sn['b'] = $copied; $cursor['sn'] = $sn; return array( 'done' => false, 'cursor' => $cursor, ); } ++$sn['n']; } } $sn['o'] = (int) $batch['ends'][ $k ]; $sn['b'] = 0; if ( microtime( true ) >= $deadline ) { break 2; } } if ( $batch['eof'] ) { $done = true; break; } } $cursor['sn'] = $sn; return array( 'done' => $done, 'cursor' => $cursor, ); } /** * Copy a file in slices. Returns -1 when finished, or the byte offset to resume from. * * @param string $src Source. * @param string $dst Destination. * @param int $from Resume offset. * @param float $deadline Deadline. * @throws State_Exception On I/O failure. */ public static function copy_slice( string $src, string $dst, int $from, float $deadline ): int { $dir = dirname( $dst ); if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) { throw new State_Exception( esc_html( State_Files::io_message( __( 'Unable to create directory', '007aj-backup-restore-verifier' ), $dir ) ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $in = @fopen( $src, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( false === $in ) { throw new State_Exception( esc_html( State_Files::io_message( __( 'Unable to read file', '007aj-backup-restore-verifier' ), $src ) ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $out = @fopen( $dst, $from > 0 ? 'cb' : 'wb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_system_operations_fopen if ( false === $out ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose throw new State_Exception( esc_html( State_Files::io_message( __( 'Unable to write file', '007aj-backup-restore-verifier' ), $dst ) ) ); } if ( $from > 0 ) { ftruncate( $out, $from ); fseek( $out, $from ); fseek( $in, $from ); } $pos = $from; while ( ! feof( $in ) ) { $chunk = fread( $in, 8388608 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread if ( false === $chunk ) { break; } if ( '' === $chunk ) { continue; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite if ( strlen( $chunk ) !== fwrite( $out, $chunk ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose throw new State_Exception( esc_html( State_Files::io_message( __( 'Short write (disk full?)', '007aj-backup-restore-verifier' ), $dst ) ) ); } $pos += strlen( $chunk ); if ( microtime( true ) >= $deadline && ! feof( $in ) ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return $pos; } } fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return -1; } /** * Dump current prefix tables into snapshot/database/dump.sql (chunked via Db_Dumper). * * @param int $job_id Job id. * @param array $cursor Cursor. * @param float $deadline Deadline. * @return array{done:bool,cursor:array,error?:string} */ private function snapshot_database( int $job_id, array $cursor, float $deadline ): array { $snap = (string) $cursor['snapshot_dir']; $db_dir = trailingslashit( $snap ) . 'database'; if ( ! is_dir( $db_dir ) ) { wp_mkdir_p( $db_dir ); } // Reuse Db_Dumper against the snapshot directory as if it were a job dir. // Db_Dumper writes to {job_dir}/database/dump.sql — pass $snap as job_dir. $dumper = new Db_Dumper(); $sub = isset( $cursor['snapshot_db_cursor'] ) && is_array( $cursor['snapshot_db_cursor'] ) ? $cursor['snapshot_db_cursor'] : array(); $result = $dumper->tick( $job_id, $snap, $sub, $deadline ); $cursor['snapshot_db_cursor'] = $result['cursor']; if ( ! empty( $result['error'] ) ) { return array( 'done' => false, 'cursor' => $cursor, 'error' => (string) $result['error'], ); } if ( ! empty( $result['done'] ) ) { $cursor['snapshot_db_path'] = trailingslashit( $db_dir ) . 'dump.sql'; } return array( 'done' => ! empty( $result['done'] ), 'cursor' => $cursor, ); } /** * Roll files back from the snapshot onto live targets (chunked). * * Cursor key "rf": list offset + byte offset of a large file in progress. * * @param int $job_id Job id. * @param array $cursor Cursor (by reference). * @param float $deadline Deadline. * @return array{done:bool,restored:int} */ public function rollback_files_step( int $job_id, array &$cursor, float $deadline ): array { $snap = (string) ( $cursor['snapshot_dir'] ?? '' ); $sn = isset( $cursor['sn'] ) && is_array( $cursor['sn'] ) ? $cursor['sn'] : array(); $rf = isset( $cursor['rf'] ) && is_array( $cursor['rf'] ) ? $cursor['rf'] : array( 'o' => 0, 'b' => 0, 'n' => 0, ); if ( '' === $snap || ! is_dir( $snap ) || empty( $sn['roots'] ) ) { return array( 'done' => true, 'restored' => 0, ); } $state_dir = State_Files::dir( (string) ( $cursor['job_dir'] ?? dirname( $snap ) ) ); $file_root = trailingslashit( $snap ) . 'files'; $limit = (int) ( $sn['o'] ?? 0 ); // Only files that were actually copied. $done = false; while ( microtime( true ) < $deadline ) { if ( (int) $rf['o'] >= $limit && empty( $rf['b'] ) ) { $done = true; break; } $batch = State_Files::read_rows( $state_dir . '/snap-files.ndjson', (int) $rf['o'], 200 ); if ( empty( $batch['rows'] ) ) { $done = true; break; } foreach ( $batch['rows'] as $k => $row ) { if ( (int) $rf['o'] >= $limit ) { break; } $root = $sn['roots'][ (int) ( $row['r'] ?? -1 ) ] ?? null; $p = (string) ( $row['p'] ?? '' ); if ( null !== $root && '' !== $p && Path_Guard::is_safe_relative( $p ) ) { $src = $file_root . '/' . (string) $root['rel'] . '/' . $p; $dst = (string) $root['abs'] . '/' . $p; if ( is_file( $src ) ) { $res = self::copy_slice( $src, $dst, (int) $rf['b'], $deadline ); if ( $res >= 0 ) { $rf['b'] = $res; $cursor['rf'] = $rf; return array( 'done' => false, 'restored' => (int) $rf['n'], ); } ++$rf['n']; } } $rf['o'] = (int) $batch['ends'][ $k ]; $rf['b'] = 0; if ( microtime( true ) >= $deadline ) { break 2; } } if ( $batch['eof'] ) { $done = true; break; } } $cursor['rf'] = $rf; if ( $done ) { Logger::info( $job_id, sprintf( 'Rollback files: restored %d file(s).', (int) $rf['n'] ) ); } return array( 'done' => $done, 'restored' => (int) $rf['n'], ); } /** * Roll files back in one call (compatibility wrapper; prefer rollback_files_step()). * * @param int $job_id Job id. * @param array $cursor Cursor with snapshot_dir. * @param array $components Components (unused; recorded at snapshot time). * @return array{ok:bool,error?:string,restored?:int} */ public function rollback_files( int $job_id, array $cursor, array $components ): array { unset( $components ); do { $r = $this->rollback_files_step( $job_id, $cursor, microtime( true ) + 3600 ); } while ( empty( $r['done'] ) ); return array( 'ok' => true, 'restored' => (int) $r['restored'], ); } /** * Map selected components to absolute live roots. * * @param array $components Components. * @return array rel_prefix => abs path */ public function component_targets( array $components ): array { $targets = array(); if ( ! empty( $components['plugins'] ) ) { $targets['wp-content/plugins'] = \BRVerify\Support\Working_Dir::plugins_root(); } if ( ! empty( $components['themes'] ) ) { $targets['wp-content/themes'] = get_theme_root(); } if ( ! empty( $components['uploads'] ) ) { $upload = wp_upload_dir( null, false ); if ( ! empty( $upload['basedir'] ) ) { $targets['wp-content/uploads'] = (string) $upload['basedir']; } } return $targets; } }