$cursor Cursor. * @param float $deadline Deadline. * @return array{done:bool,cursor:array,error?:string} */ public function tick( int $job_id, string $sql_path, array $cursor, float $deadline ): array { global $wpdb; if ( ! is_readable( $sql_path ) ) { return array( 'done' => false, 'cursor' => $cursor, 'error' => sprintf( /* translators: %s: path */ __( 'Database dump file is not readable: %s', '007aj-backup-restore-verifier' ), $sql_path ), ); } $cursor['db_apply_path'] = $sql_path; $offset = (int) ( $cursor['db_apply_offset'] ?? 0 ); $stmts = (int) ( $cursor['db_apply_stmts'] ?? 0 ); // Old cursors could carry buffered text; it is re-read from the boundary instead. if ( isset( $cursor['db_apply_buffer'] ) && '' !== (string) $cursor['db_apply_buffer'] ) { $offset = max( 0, $offset - strlen( (string) $cursor['db_apply_buffer'] ) ); } unset( $cursor['db_apply_buffer'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $fh = fopen( $sql_path, 'rb' ); if ( false === $fh ) { return array( 'done' => false, 'cursor' => $cursor, 'error' => __( 'Unable to open database dump for apply.', '007aj-backup-restore-verifier' ), ); } $size = (int) fstat( $fh )['size']; fseek( $fh, $offset ); $buffer = ''; $buf_off = $offset; // File offset of $buffer[0]. $eof = false; $error = ''; while ( microtime( true ) < $deadline ) { $end = $this->statement_end( $buffer ); if ( null === $end ) { if ( $eof ) { break; } if ( strlen( $buffer ) > self::MAX_STATEMENT ) { $error = __( 'A single SQL statement in the dump is larger than 256 MB.', '007aj-backup-restore-verifier' ); break; } $chunk = fread( $fh, self::WINDOW ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread if ( false === $chunk || '' === $chunk ) { $eof = true; continue; } $buffer .= $chunk; continue; } $statement = trim( substr( $buffer, 0, $end ) ); $buffer = (string) substr( $buffer, $end + 1 ); $buf_off += $end + 1; $run = $this->run( $statement ); if ( false === $run ) { $err = $wpdb->last_error ? $wpdb->last_error : __( 'Unknown DB error.', '007aj-backup-restore-verifier' ); $error = sprintf( /* translators: 1: DB error, 2: byte offset, 3: statement start */ __( 'Database apply failed: %1$s (dump offset %2$d, statement: %3$s)', '007aj-backup-restore-verifier' ), $err, $buf_off - $end - 1, substr( preg_replace( '/\s+/', ' ', $statement ), 0, 120 ) ); Logger::error( $job_id, $error ); // Keep the offset at the failed statement. $buf_off -= $end + 1; break; } if ( true === $run ) { ++$stmts; } $offset = $buf_off; } // Unterminated tail at end of file. if ( '' === $error && $eof && null === $this->statement_end( $buffer ) ) { $tail = trim( $buffer ); if ( '' !== $tail ) { $run = $this->run( $tail ); if ( false === $run ) { $err = $wpdb->last_error ? $wpdb->last_error : __( 'Unknown DB error.', '007aj-backup-restore-verifier' ); $error = sprintf( /* translators: %s: DB error */ __( 'Database apply failed: %s', '007aj-backup-restore-verifier' ), $err ); } elseif ( true === $run ) { ++$stmts; } } if ( '' === $error ) { $offset = $size; } } fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose $cursor['db_apply_offset'] = $offset; $cursor['db_apply_stmts'] = $stmts; if ( '' !== $error ) { return array( 'done' => false, 'cursor' => $cursor, 'error' => $error, ); } $done = $offset >= $size; if ( $done ) { Logger::info( $job_id, sprintf( 'DB apply completed (%d statement(s)).', $stmts ) ); } return array( 'done' => $done, 'cursor' => $cursor, ); } /** * Run one statement. Returns true (ran), null (skipped) or false (error). * * @param string $statement Statement. * @return bool|null */ private function run( string $statement ) { global $wpdb; if ( '' === $statement || $this->is_ignorable( $statement ) ) { return null; } $statement = $this->prepare_statement( $statement ); if ( null === $statement ) { return null; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $result = $wpdb->query( $statement ); return false === $result ? false : true; } /** * Index of the terminating semicolon of the first statement in $buf, or null. * * Skips quoted strings and identifiers (with backslash escapes) using strcspn, * so long INSERT values are scanned quickly. * * @param string $buf Buffer starting at a statement boundary. */ private function statement_end( string $buf ): ?int { $len = strlen( $buf ); $i = 0; while ( $i < $len ) { $i += strcspn( $buf, ";'\"`", $i ); if ( $i >= $len ) { return null; } $ch = $buf[ $i ]; if ( ';' === $ch ) { return $i; } // Quoted: find the matching close quote, honouring backslash escapes (not in identifiers). ++$i; while ( true ) { $stop = '`' === $ch ? '`' : $ch . '\\'; $i += strcspn( $buf, $stop, $i ); if ( $i >= $len ) { return null; } if ( '\\' === $buf[ $i ] ) { $i += 2; if ( $i > $len ) { return null; } continue; } ++$i; // Past the closing quote. break; } } return null; } /** * Prepare one statement for execution. * * 1. Leading `-- comment` lines are stripped. MySQL ignores them, but the SQLite * integration treats a comment-prefixed DROP/CREATE as a silent no-op, which made a * full-database restore fail with duplicate keys. * 2. Statements that target 007AJ Backup Restore Verifier's own job/log/artifact tables are skipped: * replaying them mid-restore would wipe the very job that is running. * 3. A stale `brverify_global_lock` option row (dumps before 0.5.4) is skipped. * * @param string $statement SQL statement. * @return string|null Statement to run, or null to skip. */ private function prepare_statement( string $statement ): ?string { global $wpdb; $statement = $this->strip_leading_comments( $statement ); if ( '' === $statement ) { return null; } if ( preg_match( '/^(?:INSERT(?:\s+IGNORE)?\s+INTO|REPLACE\s+INTO|DROP\s+TABLE(?:\s+IF\s+EXISTS)?|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?|ALTER\s+TABLE|LOCK\s+TABLES|TRUNCATE(?:\s+TABLE)?)\s+`([^`]+)`/i', $statement, $m ) ) { // The pre-rename table names (restoreproof_*) can still be inside older backups: never recreate them as junk. foreach ( array( 'brverify_jobs', 'brverify_logs', 'brverify_artifacts', 'restoreproof_jobs', 'restoreproof_logs', 'restoreproof_artifacts' ) as $own ) { if ( $m[1] === $wpdb->prefix . $own || $m[1] === $own || 1 === preg_match( '/^[A-Za-z0-9_]*_' . $own . '$/', $m[1] ) ) { return null; } } } // A dump made before 0.5.4 may carry the transient job-lock option; replaying it would // make the running restore lose its own lock ("Global lock held by another worker"). if ( false !== strpos( $statement, 'brverify_global_lock' ) && preg_match( '/^INSERT\s+INTO\s+`[^`]*options`[^(]*\([^)]*\)\s*VALUES\s*\(\s*\'\d*\'\s*,\s*\'brverify_global_lock\'/i', $statement ) ) { return null; } // Replay safety: a request can be killed after a statement ran but before the dump offset // was saved, so the next request runs that statement again. Row inserts are applied as // REPLACE so a repeated row overwrites itself with identical data instead of failing with // a duplicate key. Every table is dropped and recreated from the dump first, so this // never changes the restored data. if ( 1 === preg_match( '/^INSERT\s+INTO\s+`/i', $statement ) ) { $statement = (string) preg_replace( '/^INSERT\s+INTO\s+/i', 'REPLACE INTO ', $statement, 1 ); } return $statement; } /** * Strip leading `--` comment lines and blank lines. * * @param string $sql Statement. */ private function strip_leading_comments( string $sql ): string { $sql = ltrim( $sql ); while ( \brverify_str_starts_with( $sql, '--' ) ) { $nl = strpos( $sql, "\n" ); $sql = false === $nl ? '' : ltrim( substr( $sql, $nl + 1 ) ); } return $sql; } /** * Skip empty / comment-only statements. * * @param string $sql Statement. */ private function is_ignorable( string $sql ): bool { $sql = trim( $sql ); if ( '' === $sql ) { return true; } $lines = preg_split( '/\r\n|\r|\n/', $sql ); $kept = array(); if ( is_array( $lines ) ) { foreach ( $lines as $line ) { $t = ltrim( $line ); if ( '' === $t || \brverify_str_starts_with( $t, '--' ) ) { continue; } $kept[] = $line; } } $body = trim( implode( "\n", $kept ) ); return '' === $body; } }